blob: c31bf8d751fd549958cd002840c81d4a04884333 [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010015#include <common/h1.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020016#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020017#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020018#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020019#include <common/hpack-tbl.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010020#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010021#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020022#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/connection.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010024#include <proto/http_htx.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026#include <proto/stream.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010027#include <proto/stream_interface.h>
Willy Tarreauea392822017-10-31 10:02:25 +010028#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020029#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020030
31
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010032/* dummy streams returned for closed, error, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020033static const struct h2s *h2_closed_stream;
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010034static const struct h2s *h2_error_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010035static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020036static const struct h2s *h2_idle_stream;
37
Willy Tarreau5ab6b572017-09-22 08:05:00 +020038/* Connection flags (32 bit), in h2c->flags */
39#define H2_CF_NONE 0x00000000
40
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020041/* Flags indicating why writing to the mux is blocked. */
42#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
43#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
44#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
45
Willy Tarreau315d8072017-12-10 22:17:57 +010046/* Flags indicating why writing to the demux is blocked.
47 * The first two ones directly affect the ability for the mux to receive data
48 * from the connection. The other ones affect the mux's ability to demux
49 * received data.
50 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020051#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
52#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010053
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020054#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
55#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
56#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
57#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
59#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020060
Willy Tarreau081d4722017-05-16 21:51:05 +020061/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020062#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
63#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
64#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreaub3fb56d2018-10-03 13:56:38 +020065#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau97aaa672018-12-23 09:49:04 +010066#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020067
Willy Tarreau5ab6b572017-09-22 08:05:00 +020068/* H2 connection state, in h2c->st0 */
69enum h2_cs {
70 H2_CS_PREFACE, // init done, waiting for connection preface
71 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
72 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
73 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010074 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
75 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020076 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
77 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
78 H2_CS_ENTRIES // must be last
79} __attribute__((packed));
80
81/* H2 connection descriptor */
82struct h2c {
83 struct connection *conn;
84
85 enum h2_cs st0; /* mux state */
86 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
87
88 /* 16 bit hole here */
89 uint32_t flags; /* connection flags: H2_CF_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010090 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020091 int32_t max_id; /* highest ID known on this connection, <0 before preface */
92 uint32_t rcvd_c; /* newly received data to ACK for the connection */
93 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
94
95 /* states for the demux direction */
96 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020097 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020098
99 int32_t dsi; /* demux stream ID (<0 = idle) */
100 int32_t dfl; /* demux frame length (if dsi >= 0) */
101 int8_t dft; /* demux frame type (if dsi >= 0) */
102 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100103 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
104 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
106
107 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200108 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200109 int32_t msi; /* mux stream ID (<0 = idle) */
110 int32_t mfl; /* mux frame length (if dsi >= 0) */
111 int8_t mft; /* mux frame type (if dsi >= 0) */
112 int8_t mff; /* mux frame flags (if dsi >= 0) */
113 /* 16 bit hole here */
114 int32_t miw; /* mux initial window size for all new streams */
115 int32_t mws; /* mux window size. Can be negative. */
116 int32_t mfs; /* mux's max frame size */
117
Willy Tarreauea392822017-10-31 10:02:25 +0100118 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100119 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100120 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200121 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100122 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100123 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200124 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100125 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200126 struct eb_root streams_by_id; /* all active streams by their ID */
127 struct list send_list; /* list of blocked streams requesting to send */
128 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200129 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100130 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200131 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200132};
133
Willy Tarreau18312642017-10-11 07:57:07 +0200134/* H2 stream state, in h2s->st */
135enum h2_ss {
136 H2_SS_IDLE = 0, // idle
137 H2_SS_RLOC, // reserved(local)
138 H2_SS_RREM, // reserved(remote)
139 H2_SS_OPEN, // open
140 H2_SS_HREM, // half-closed(remote)
141 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200142 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200143 H2_SS_CLOSED, // closed
144 H2_SS_ENTRIES // must be last
145} __attribute__((packed));
146
147/* HTTP/2 stream flags (32 bit), in h2s->flags */
148#define H2_SF_NONE 0x00000000
149#define H2_SF_ES_RCVD 0x00000001
150#define H2_SF_ES_SENT 0x00000002
151
152#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
153#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
154
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200155/* stream flags indicating the reason the stream is blocked */
156#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
157#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
158#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
159#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
160#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
161
Willy Tarreau454f9052017-10-26 19:40:35 +0200162/* stream flags indicating how data is supposed to be sent */
163#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
164#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
165
166/* step we're currently in when sending chunks. This is needed because we may
167 * have to transfer chunks as large as a full buffer so there's no room left
168 * for size nor crlf around.
169 */
170#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
171#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
172#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
173
174#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
175
Willy Tarreau67434202017-11-06 20:20:51 +0100176#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100177#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100178
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100179#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
180
Willy Tarreau18312642017-10-11 07:57:07 +0200181/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
182 * it is being processed in the internal HTTP representation (H1 for now).
183 */
184struct h2s {
185 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100186 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200187 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200188 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200189 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200190 int32_t id; /* stream ID */
191 uint32_t flags; /* H2_SF_* */
192 int mws; /* mux window size for this stream */
193 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
194 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200195 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100196 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200197 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200198 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
Willy Tarreau749f5ca2019-03-21 19:19:36 +0100199 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
200 struct wait_event *send_wait; /* send wait_event the conn_stream associated is waiting on (via h2_subscribe) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200201 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100202 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200203};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200204
Willy Tarreauc6405142017-09-21 20:23:50 +0200205/* descriptor for an h2 frame header */
206struct h2_fh {
207 uint32_t len; /* length, host order, 24 bits */
208 uint32_t sid; /* stream id, host order, 31 bits */
209 uint8_t ft; /* frame type */
210 uint8_t ff; /* frame flags */
211};
212
Willy Tarreau8ceae722018-11-26 11:58:30 +0100213/* the h2c connection pool */
214DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
215
216/* the h2s stream pool */
217DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
218
Willy Tarreaudc572362018-12-12 08:08:05 +0100219/* The default connection window size is 65535, it may only be enlarged using
220 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
221 * we'll pretend we already received the difference between the two to send
222 * an equivalent window update to enlarge it to 2G-1.
223 */
224#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
225
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200226/* a few settings from the global section */
227static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200228static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100229static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100230static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200231
Willy Tarreau2a856182017-05-16 15:20:39 +0200232/* a dmumy closed stream */
233static const struct h2s *h2_closed_stream = &(const struct h2s){
234 .cs = NULL,
235 .h2c = NULL,
236 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100237 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100238 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200239 .id = 0,
240};
241
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100242/* a dmumy closed stream returning a PROTOCOL_ERROR error */
243static const struct h2s *h2_error_stream = &(const struct h2s){
244 .cs = NULL,
245 .h2c = NULL,
246 .st = H2_SS_CLOSED,
247 .errcode = H2_ERR_PROTOCOL_ERROR,
248 .flags = 0,
249 .id = 0,
250};
251
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100252/* a dmumy closed stream returning a REFUSED_STREAM error */
253static const struct h2s *h2_refused_stream = &(const struct h2s){
254 .cs = NULL,
255 .h2c = NULL,
256 .st = H2_SS_CLOSED,
257 .errcode = H2_ERR_REFUSED_STREAM,
258 .flags = 0,
259 .id = 0,
260};
261
Willy Tarreau2a856182017-05-16 15:20:39 +0200262/* and a dummy idle stream for use with any unannounced stream */
263static const struct h2s *h2_idle_stream = &(const struct h2s){
264 .cs = NULL,
265 .h2c = NULL,
266 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100267 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200268 .id = 0,
269};
270
Olivier Houchard9f6af332018-05-25 14:04:04 +0200271static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200272static int h2_send(struct h2c *h2c);
273static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200274static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200275static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100276static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100277static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100278static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200279static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100280static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100281static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200282
Olivier Houchard7a977432019-03-21 15:47:13 +0100283static __inline int
284h2c_is_dead(struct h2c *h2c)
285{
286 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
287 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
288 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
289 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
290 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
291 (conn_xprt_read0_pending(h2c->conn) ||
292 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
293 return 1;
294
295 return 0;
296
297}
298
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200299/*****************************************************/
300/* functions below are for dynamic buffer management */
301/*****************************************************/
302
Willy Tarreau315d8072017-12-10 22:17:57 +0100303/* indicates whether or not the we may call the h2_recv() function to attempt
304 * to receive data into the buffer and/or demux pending data. The condition is
305 * a bit complex due to some API limits for now. The rules are the following :
306 * - if an error or a shutdown was detected on the connection and the buffer
307 * is empty, we must not attempt to receive
308 * - if the demux buf failed to be allocated, we must not try to receive and
309 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100310 * - if no flag indicates a blocking condition, we may attempt to receive,
311 * regardless of whether the demux buffer is full or not, so that only
312 * de demux part decides whether or not to block. This is needed because
313 * the connection API indeed prevents us from re-enabling receipt that is
314 * already enabled in a polled state, so we must always immediately stop
315 * as soon as the demux can't proceed so as never to hit an end of read
316 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100317 * - otherwise must may not attempt
318 */
319static inline int h2_recv_allowed(const struct h2c *h2c)
320{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200321 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100322 (h2c->st0 >= H2_CS_ERROR ||
323 h2c->conn->flags & CO_FL_ERROR ||
324 conn_xprt_read0_pending(h2c->conn)))
325 return 0;
326
327 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100328 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100329 return 1;
330
331 return 0;
332}
333
Willy Tarreau47b515a2018-12-21 16:09:41 +0100334/* restarts reading on the connection if it was not enabled */
335static inline void h2c_restart_reading(const struct h2c *h2c)
336{
337 if (!h2_recv_allowed(h2c))
338 return;
Willy Tarreau872e2fa2019-01-03 08:27:41 +0100339 if (!b_data(&h2c->dbuf) && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100340 return;
341 tasklet_wakeup(h2c->wait_event.task);
342}
343
344
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100345/* returns true if the front connection has too many conn_streams attached */
346static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200347{
Willy Tarreaua8754662018-12-23 20:43:58 +0100348 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200349}
350
Willy Tarreau44e973f2018-03-01 17:49:30 +0100351/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
352 * flags are used to figure what buffer was requested. It returns 1 if the
353 * allocation succeeds, in which case the connection is woken up, or 0 if it's
354 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200355 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100356static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200357{
358 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100359 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200360
Willy Tarreau44e973f2018-03-01 17:49:30 +0100361 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200362 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100363 h2c_restart_reading(h2c);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200364 return 1;
365 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200366
Willy Tarreau44e973f2018-03-01 17:49:30 +0100367 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
368 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200369
370 if (h2c->flags & H2_CF_DEM_MROOM) {
371 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100372 h2c_restart_reading(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200373 }
Willy Tarreau14398122017-09-22 14:26:04 +0200374 return 1;
375 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100376
377 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
378 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200379 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100380 h2c->flags &= ~H2_CF_DEM_SALLOC;
Willy Tarreau47b515a2018-12-21 16:09:41 +0100381 h2c_restart_reading(h2c);
Willy Tarreau0b559072018-02-26 15:22:17 +0100382 return 1;
383 }
384
Willy Tarreau14398122017-09-22 14:26:04 +0200385 return 0;
386}
387
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200388static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200389{
390 struct buffer *buf = NULL;
391
Willy Tarreau44e973f2018-03-01 17:49:30 +0100392 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
393 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
394 h2c->buf_wait.target = h2c;
395 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100396 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100397 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100398 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200399 __conn_xprt_stop_recv(h2c->conn);
400 }
401 return buf;
402}
403
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200404static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200405{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200406 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100407 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200408 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200409 }
410}
411
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100412/* returns the number of allocatable outgoing streams for the connection taking
413 * the last_sid and the reserved ones into account.
414 */
415static inline int h2_streams_left(const struct h2c *h2c)
416{
417 int ret;
418
419 /* consider the number of outgoing streams we're allowed to create before
420 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
421 * nb_reserved is the number of streams which don't yet have an ID.
422 */
423 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
424 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
425 if (ret < 0)
426 ret = 0;
427 return ret;
428}
429
Willy Tarreau00f18a32019-01-26 12:19:01 +0100430/* returns the number of streams in use on a connection to figure if it's
431 * idle or not. We check nb_cs and not nb_streams as the caller will want
432 * to know if it was the last one after a detach().
433 */
434static int h2_used_streams(struct connection *conn)
435{
436 struct h2c *h2c = conn->ctx;
437
438 return h2c->nb_cs;
439}
440
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100441/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100442static int h2_avail_streams(struct connection *conn)
443{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100444 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100445 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100446 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100447
Willy Tarreau6afec462019-01-28 06:40:19 +0100448 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
449 * streams on the connection.
450 */
451 if (h2c->last_sid >= 0)
452 return 0;
453
Willy Tarreau86949782019-01-31 10:42:05 +0100454 /* note: may be negative if a SETTINGS frame changes the limit */
455 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100456
457 /* we must also consider the limit imposed by stream IDs */
458 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100459 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100460 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100461 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
462 ret1 = MIN(ret1, ret2);
463 }
464 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100465}
466
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200467
Willy Tarreau62f52692017-10-08 23:01:42 +0200468/*****************************************************************/
469/* functions below are dedicated to the mux setup and management */
470/*****************************************************************/
471
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200472/* Initialize the mux once it's attached. For outgoing connections, the context
473 * is already initialized before installing the mux, so we detect incoming
474 * connections from the fact that the context is still NULL. Returns < 0 on
475 * error.
476 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100477static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200478{
479 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100480 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200481
Willy Tarreaubafbe012017-11-24 17:34:44 +0100482 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200483 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200484 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200485
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100486 if (conn->ctx) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200487 h2c->flags = H2_CF_IS_BACK;
488 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
489 if (tick_isset(prx->timeout.serverfin))
490 h2c->shut_timeout = prx->timeout.serverfin;
491 } else {
492 h2c->flags = H2_CF_NONE;
493 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
494 if (tick_isset(prx->timeout.clientfin))
495 h2c->shut_timeout = prx->timeout.clientfin;
496 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100497
Willy Tarreau0b37d652018-10-03 10:33:02 +0200498 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100499 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100500 if (tick_isset(h2c->timeout)) {
501 t = task_new(tid_bit);
502 if (!t)
503 goto fail;
504
505 h2c->task = t;
506 t->process = h2_timeout_task;
507 t->context = h2c;
508 t->expire = tick_add(now_ms, h2c->timeout);
509 }
Willy Tarreauea392822017-10-31 10:02:25 +0100510
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200511 h2c->wait_event.task = tasklet_new();
512 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200513 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200514 h2c->wait_event.task->process = h2_io_cb;
515 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100516 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200517
Willy Tarreau32218eb2017-09-22 08:07:25 +0200518 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
519 if (!h2c->ddht)
520 goto fail;
521
522 /* Initialise the context. */
523 h2c->st0 = H2_CS_PREFACE;
524 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100525 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200526 h2c->max_id = -1;
527 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100528 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200529 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100530 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200531 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100532 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100533 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200534
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200535 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200536 h2c->dsi = -1;
537 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100538
Willy Tarreau32218eb2017-09-22 08:07:25 +0200539 h2c->last_sid = -1;
540
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200541 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200542 h2c->miw = 65535; /* mux initial window size */
543 h2c->mws = 65535; /* mux window size */
544 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200545 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200546 LIST_INIT(&h2c->send_list);
547 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200548 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100549 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200550
Willy Tarreau3f133572017-10-31 19:21:06 +0100551 if (t)
552 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100553
Willy Tarreau01b44822018-10-03 14:26:37 +0200554 if (h2c->flags & H2_CF_IS_BACK) {
555 /* FIXME: this is temporary, for outgoing connections we need
556 * to immediately allocate a stream until the code is modified
557 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100558 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200559 */
560 struct h2s *h2s;
561
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100562 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200563 if (!h2s)
564 goto fail_stream;
565 }
566
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100567 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200568
Willy Tarreau0f383582018-10-03 14:22:21 +0200569 /* prepare to read something */
Willy Tarreau47b515a2018-12-21 16:09:41 +0100570 h2c_restart_reading(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200571 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200572 fail_stream:
573 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200574 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100575 if (t)
576 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200577 if (h2c->wait_event.task)
578 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100579 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200580 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200581 return -1;
582}
583
Willy Tarreau751f2d02018-10-05 09:35:00 +0200584/* returns the next allocatable outgoing stream ID for the H2 connection, or
585 * -1 if no more is allocatable.
586 */
587static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
588{
589 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100590
591 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200592 id = -1;
593 return id;
594}
595
Willy Tarreau2373acc2017-10-12 17:35:14 +0200596/* returns the stream associated with id <id> or NULL if not found */
597static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
598{
599 struct eb32_node *node;
600
Willy Tarreau751f2d02018-10-05 09:35:00 +0200601 if (id == 0)
602 return (struct h2s *)h2_closed_stream;
603
Willy Tarreau2a856182017-05-16 15:20:39 +0200604 if (id > h2c->max_id)
605 return (struct h2s *)h2_idle_stream;
606
Willy Tarreau2373acc2017-10-12 17:35:14 +0200607 node = eb32_lookup(&h2c->streams_by_id, id);
608 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200609 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200610
611 return container_of(node, struct h2s, by_id);
612}
613
Willy Tarreau62f52692017-10-08 23:01:42 +0200614/* release function for a connection. This one should be called to free all
615 * resources allocated to the mux.
616 */
617static void h2_release(struct connection *conn)
618{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100619 struct h2c *h2c = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200620
Willy Tarreau32218eb2017-09-22 08:07:25 +0200621 if (h2c) {
622 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200623
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100624 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100625 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100626 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200627
Willy Tarreau44e973f2018-03-01 17:49:30 +0100628 h2_release_buf(h2c, &h2c->dbuf);
629 h2_release_buf(h2c, &h2c->mbuf);
630
Willy Tarreauea392822017-10-31 10:02:25 +0100631 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200632 h2c->task->context = NULL;
633 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100634 h2c->task = NULL;
635 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200636 if (h2c->wait_event.task)
637 tasklet_free(h2c->wait_event.task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100638 if (h2c->wait_event.events != 0)
639 conn->xprt->unsubscribe(conn, h2c->wait_event.events,
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200640 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100641
Willy Tarreaubafbe012017-11-24 17:34:44 +0100642 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200643 }
644
645 conn->mux = NULL;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100646 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200647
648 conn_stop_tracking(conn);
649 conn_full_close(conn);
650 if (conn->destroy_cb)
651 conn->destroy_cb(conn);
652 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200653}
654
655
Willy Tarreau71681172017-10-23 14:39:06 +0200656/******************************************************/
657/* functions below are for the H2 protocol processing */
658/******************************************************/
659
660/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100661static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200662{
663 return h2s ? h2s->id : 0;
664}
665
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200666/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100667static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200668{
669 if (h2c->msi < 0)
670 return 0;
671
672 if (h2c->msi == h2s_id(h2s))
673 return 0;
674
675 return 1;
676}
677
Willy Tarreau741d6df2017-10-17 08:00:59 +0200678/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100679static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200680{
681 h2c->errcode = err;
682 h2c->st0 = H2_CS_ERROR;
683}
684
Willy Tarreau175cebb2019-01-24 10:02:24 +0100685/* marks an error on the stream. It may also update an already closed stream
686 * (e.g. to report an error after an RST was received).
687 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100688static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200689{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100690 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200691 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100692 if (h2s->st < H2_SS_ERROR)
693 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100694 if (h2s->cs)
695 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200696 }
697}
698
Willy Tarreau7e094452018-12-19 18:08:52 +0100699/* attempt to notify the data layer of recv availability */
700static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
701{
702 struct wait_event *sw;
703
704 if (h2s->recv_wait) {
705 sw = h2s->recv_wait;
706 sw->events &= ~SUB_RETRY_RECV;
707 tasklet_wakeup(sw->task);
708 h2s->recv_wait = NULL;
709 }
710}
711
712/* attempt to notify the data layer of send availability */
713static void __maybe_unused h2s_notify_send(struct h2s *h2s)
714{
715 struct wait_event *sw;
716
717 if (h2s->send_wait) {
718 sw = h2s->send_wait;
719 sw->events &= ~SUB_RETRY_SEND;
720 tasklet_wakeup(sw->task);
721 h2s->send_wait = NULL;
Willy Tarreau645b33d2018-12-20 15:35:57 +0100722 LIST_DEL(&h2s->list);
723 LIST_INIT(&h2s->list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100724 }
725}
726
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100727/* alerts the data layer, trying to wake it up by all means, following
728 * this sequence :
729 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
730 * - if its subscribed to send, then it's woken up for send
731 * - if it was subscribed to neither, its ->wake() callback is called
732 * It is safe to call this function with a closed stream which doesn't have a
733 * conn_stream anymore.
734 */
735static void __maybe_unused h2s_alert(struct h2s *h2s)
736{
737 if (h2s->recv_wait || h2s->send_wait) {
738 h2s_notify_recv(h2s);
739 h2s_notify_send(h2s);
740 }
741 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
742 h2s->cs->data_cb->wake(h2s->cs);
743}
744
Willy Tarreaue4820742017-07-27 13:37:23 +0200745/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100746static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200747{
748 uint8_t *out = frame;
749
750 *out = len >> 16;
751 write_n16(out + 1, len);
752}
753
Willy Tarreau54c15062017-10-10 17:10:03 +0200754/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
755 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
756 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200757 * available in the buffer's input prior to calling this function. The buffer
758 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200759 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100760static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200761 const struct buffer *b, int o)
762{
Willy Tarreau591d4452018-06-15 17:21:00 +0200763 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 +0200764}
765
Willy Tarreau1f094672017-11-20 21:27:45 +0100766static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200767{
Willy Tarreau591d4452018-06-15 17:21:00 +0200768 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200769}
770
Willy Tarreau1f094672017-11-20 21:27:45 +0100771static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200772{
Willy Tarreau591d4452018-06-15 17:21:00 +0200773 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200774}
775
Willy Tarreau1f094672017-11-20 21:27:45 +0100776static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200777{
Willy Tarreau591d4452018-06-15 17:21:00 +0200778 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200779}
780
781
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100782/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
783 * The algorithm is not obvious. It turns out that H2 headers are neither
784 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
785 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200786 *
787 * b0 b1 b2 b3 b4 b5..b8
788 * +----------+---------+--------+----+----+----------------------+
789 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
790 * +----------+---------+--------+----+----+----------------------+
791 *
792 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
793 * we get the sid properly aligned and ordered, and 16 bits of len properly
794 * ordered as well. The type and flags can be extracted using bit shifts from
795 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200796 * Returns zero if some bytes are missing, otherwise non-zero on success. The
797 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200798 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100799static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200800{
801 uint64_t w;
802
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100803 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200804 return 0;
805
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100806 w = h2_get_n64(b, o + 1);
807 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200808 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
809 h->ff = w >> 32;
810 h->ft = w >> 40;
811 h->len += w >> 48;
812 return 1;
813}
814
815/* skip the next 9 bytes corresponding to the frame header possibly parsed by
816 * h2_peek_frame_hdr() above.
817 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100818static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200819{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200820 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200821}
822
823/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100824static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200825{
826 int ret;
827
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100828 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200829 if (ret > 0)
830 h2_skip_frame_hdr(b);
831 return ret;
832}
833
Willy Tarreau00dd0782018-03-01 16:31:34 +0100834/* marks stream <h2s> as CLOSED and decrement the number of active streams for
835 * its connection if the stream was not yet closed. Please use this exclusively
836 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100837 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100838static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100839{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100840 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100841 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100842 if (!h2s->id)
843 h2s->h2c->nb_reserved--;
844 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100845 h2s->st = H2_SS_CLOSED;
846}
847
Willy Tarreau71049cc2018-03-28 13:56:39 +0200848/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
849static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100850{
851 h2s_close(h2s);
852 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200853 if (b_size(&h2s->rxbuf)) {
854 b_free(&h2s->rxbuf);
855 offer_buffers(NULL, tasks_run_queue);
856 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200857 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100858 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200859 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100860 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800861 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200862 * reference left would be in the h2c send_list/fctl_list, and if
863 * we're in it, we're getting out anyway
864 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100865 LIST_DEL_INIT(&h2s->list);
866 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200867 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100868 pool_free(pool_head_h2s, h2s);
869}
870
Willy Tarreaua8e49542018-10-03 18:53:55 +0200871/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
872 * stream tree. In case of error, nothing is added and NULL is returned. The
873 * causes of errors can be any failed memory allocation. The caller is
874 * responsible for checking if the connection may support an extra stream
875 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200876 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200877static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200878{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200879 struct h2s *h2s;
880
Willy Tarreaubafbe012017-11-24 17:34:44 +0100881 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200882 if (!h2s)
883 goto out;
884
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200885 h2s->wait_event.task = tasklet_new();
886 if (!h2s->wait_event.task) {
887 pool_free(pool_head_h2s, h2s);
888 goto out;
889 }
890 h2s->send_wait = NULL;
891 h2s->recv_wait = NULL;
892 h2s->wait_event.task->process = h2_deferred_shut;
893 h2s->wait_event.task->context = h2s;
894 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100895 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200896 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100897 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200898 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200899 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200900 h2s->mws = h2c->miw;
901 h2s->flags = H2_SF_NONE;
902 h2s->errcode = H2_ERR_NO_ERROR;
903 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200904 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100905 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200906 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200907
908 if (h2c->flags & H2_CF_IS_BACK) {
909 h1m_init_req(&h2s->h1m);
910 h2s->h1m.err_pos = -1; // don't care about errors on the request path
911 h2s->h1m.flags |= H1_MF_TOLOWER;
912 } else {
913 h1m_init_res(&h2s->h1m);
914 h2s->h1m.err_pos = -1; // don't care about errors on the response path
915 h2s->h1m.flags |= H1_MF_TOLOWER;
916 }
917
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200918 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200919 if (id > 0)
920 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100921 else
922 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200923
924 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100925 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100926 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200927
928 return h2s;
929
930 out_free_h2s:
931 pool_free(pool_head_h2s, h2s);
932 out:
933 return NULL;
934}
935
936/* creates a new stream <id> on the h2c connection and returns it, or NULL in
937 * case of memory allocation error.
938 */
939static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
940{
941 struct session *sess = h2c->conn->owner;
942 struct conn_stream *cs;
943 struct h2s *h2s;
944
945 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
946 goto out;
947
948 h2s = h2s_new(h2c, id);
949 if (!h2s)
950 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200951
952 cs = cs_new(h2c->conn);
953 if (!cs)
954 goto out_close;
955
Olivier Houchard746fb772018-12-15 19:42:00 +0100956 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200957 h2s->cs = cs;
958 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200959 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200960
961 if (stream_create_from_cs(cs) < 0)
962 goto out_free_cs;
963
Willy Tarreau590a0512018-09-05 11:56:48 +0200964 /* We want the accept date presented to the next stream to be the one
965 * we have now, the handshake time to be null (since the next stream
966 * is not delayed by a handshake), and the idle time to count since
967 * right now.
968 */
969 sess->accept_date = date;
970 sess->tv_accept = now;
971 sess->t_handshake = 0;
972
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200973 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100974 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200975 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200976 return h2s;
977
978 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200979 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200980 cs_free(cs);
981 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200982 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200983 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200984 sess_log(sess);
985 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200986}
987
Willy Tarreau751f2d02018-10-05 09:35:00 +0200988/* allocates a new stream associated to conn_stream <cs> on the h2c connection
989 * and returns it, or NULL in case of memory allocation error or if the highest
990 * possible stream ID was reached.
991 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100992static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200993{
994 struct h2s *h2s = NULL;
995
Willy Tarreau86949782019-01-31 10:42:05 +0100996 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200997 goto out;
998
Willy Tarreaua80dca82019-01-24 17:08:28 +0100999 if (h2_streams_left(h2c) < 1)
1000 goto out;
1001
Willy Tarreau751f2d02018-10-05 09:35:00 +02001002 /* Defer choosing the ID until we send the first message to create the stream */
1003 h2s = h2s_new(h2c, 0);
1004 if (!h2s)
1005 goto out;
1006
1007 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001008 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001009 cs->ctx = h2s;
1010 h2c->nb_cs++;
1011
Willy Tarreau751f2d02018-10-05 09:35:00 +02001012 out:
1013 return h2s;
1014}
1015
Willy Tarreaube5b7152017-09-25 16:25:39 +02001016/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1017 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1018 * the various settings codes.
1019 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001020static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001021{
1022 struct buffer *res;
1023 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001024 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001025 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001026 int ret;
1027
1028 if (h2c_mux_busy(h2c, NULL)) {
1029 h2c->flags |= H2_CF_DEM_MBUSY;
1030 return 0;
1031 }
1032
Willy Tarreau44e973f2018-03-01 17:49:30 +01001033 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001034 if (!res) {
1035 h2c->flags |= H2_CF_MUX_MALLOC;
1036 h2c->flags |= H2_CF_DEM_MROOM;
1037 return 0;
1038 }
1039
1040 chunk_init(&buf, buf_data, sizeof(buf_data));
1041 chunk_memcpy(&buf,
1042 "\x00\x00\x00" /* length : 0 for now */
1043 "\x04\x00" /* type : 4 (settings), flags : 0 */
1044 "\x00\x00\x00\x00", /* stream ID : 0 */
1045 9);
1046
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001047 if (h2c->flags & H2_CF_IS_BACK) {
1048 /* send settings_enable_push=0 */
1049 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1050 }
1051
Willy Tarreaube5b7152017-09-25 16:25:39 +02001052 if (h2_settings_header_table_size != 4096) {
1053 char str[6] = "\x00\x01"; /* header_table_size */
1054
1055 write_n32(str + 2, h2_settings_header_table_size);
1056 chunk_memcat(&buf, str, 6);
1057 }
1058
1059 if (h2_settings_initial_window_size != 65535) {
1060 char str[6] = "\x00\x04"; /* initial_window_size */
1061
1062 write_n32(str + 2, h2_settings_initial_window_size);
1063 chunk_memcat(&buf, str, 6);
1064 }
1065
1066 if (h2_settings_max_concurrent_streams != 0) {
1067 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1068
1069 /* Note: 0 means "unlimited" for haproxy's config but not for
1070 * the protocol, so never send this value!
1071 */
1072 write_n32(str + 2, h2_settings_max_concurrent_streams);
1073 chunk_memcat(&buf, str, 6);
1074 }
1075
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001076 mfs = h2_settings_max_frame_size;
1077 if (mfs > global.tune.bufsize)
1078 mfs = global.tune.bufsize;
1079
1080 if (!mfs)
1081 mfs = global.tune.bufsize;
1082
1083 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001084 char str[6] = "\x00\x05"; /* max_frame_size */
1085
1086 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1087 * match bufsize - rewrite size, but at the moment it seems
1088 * that clients don't take care of it.
1089 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001090 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001091 chunk_memcat(&buf, str, 6);
1092 }
1093
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001094 h2_set_frame_size(buf.area, buf.data - 9);
1095 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001096 if (unlikely(ret <= 0)) {
1097 if (!ret) {
1098 h2c->flags |= H2_CF_MUX_MFULL;
1099 h2c->flags |= H2_CF_DEM_MROOM;
1100 return 0;
1101 }
1102 else {
1103 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1104 return 0;
1105 }
1106 }
1107 return ret;
1108}
1109
Willy Tarreau52eed752017-09-22 15:05:09 +02001110/* Try to receive a connection preface, then upon success try to send our
1111 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1112 * missing data. It may return an error in h2c.
1113 */
1114static int h2c_frt_recv_preface(struct h2c *h2c)
1115{
1116 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001117 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001118
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001119 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001120
1121 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001122 if (ret1 < 0)
1123 sess_log(h2c->conn->owner);
1124
Willy Tarreau52eed752017-09-22 15:05:09 +02001125 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1126 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1127 return 0;
1128 }
1129
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001130 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001131 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001132 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001133
Willy Tarreaube5b7152017-09-25 16:25:39 +02001134 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001135}
1136
Willy Tarreau01b44822018-10-03 14:26:37 +02001137/* Try to send a connection preface, then upon success try to send our
1138 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1139 * missing data. It may return an error in h2c.
1140 */
1141static int h2c_bck_send_preface(struct h2c *h2c)
1142{
1143 struct buffer *res;
1144
1145 if (h2c_mux_busy(h2c, NULL)) {
1146 h2c->flags |= H2_CF_DEM_MBUSY;
1147 return 0;
1148 }
1149
1150 res = h2_get_buf(h2c, &h2c->mbuf);
1151 if (!res) {
1152 h2c->flags |= H2_CF_MUX_MALLOC;
1153 h2c->flags |= H2_CF_DEM_MROOM;
1154 return 0;
1155 }
1156
1157 if (!b_data(res)) {
1158 /* preface not yet sent */
1159 b_istput(res, ist(H2_CONN_PREFACE));
1160 }
1161
1162 return h2c_send_settings(h2c);
1163}
1164
Willy Tarreau081d4722017-05-16 21:51:05 +02001165/* try to send a GOAWAY frame on the connection to report an error or a graceful
1166 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1167 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1168 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1169 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1170 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1171 * on unrecoverable failure. It will not attempt to send one again in this last
1172 * case so that it is safe to use h2c_error() to report such errors.
1173 */
1174static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1175{
1176 struct buffer *res;
1177 char str[17];
1178 int ret;
1179
1180 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1181 return 1; // claim that it worked
1182
1183 if (h2c_mux_busy(h2c, h2s)) {
1184 if (h2s)
1185 h2s->flags |= H2_SF_BLK_MBUSY;
1186 else
1187 h2c->flags |= H2_CF_DEM_MBUSY;
1188 return 0;
1189 }
1190
Willy Tarreau44e973f2018-03-01 17:49:30 +01001191 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001192 if (!res) {
1193 h2c->flags |= H2_CF_MUX_MALLOC;
1194 if (h2s)
1195 h2s->flags |= H2_SF_BLK_MROOM;
1196 else
1197 h2c->flags |= H2_CF_DEM_MROOM;
1198 return 0;
1199 }
1200
1201 /* len: 8, type: 7, flags: none, sid: 0 */
1202 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1203
1204 if (h2c->last_sid < 0)
1205 h2c->last_sid = h2c->max_id;
1206
1207 write_n32(str + 9, h2c->last_sid);
1208 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001209 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001210 if (unlikely(ret <= 0)) {
1211 if (!ret) {
1212 h2c->flags |= H2_CF_MUX_MFULL;
1213 if (h2s)
1214 h2s->flags |= H2_SF_BLK_MROOM;
1215 else
1216 h2c->flags |= H2_CF_DEM_MROOM;
1217 return 0;
1218 }
1219 else {
1220 /* we cannot report this error using GOAWAY, so we mark
1221 * it and claim a success.
1222 */
1223 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1224 h2c->flags |= H2_CF_GOAWAY_FAILED;
1225 return 1;
1226 }
1227 }
1228 h2c->flags |= H2_CF_GOAWAY_SENT;
1229 return ret;
1230}
1231
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001232/* Try to send an RST_STREAM frame on the connection for the indicated stream
1233 * during mux operations. This stream must be valid and cannot be closed
1234 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1235 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1236 * not yet.
1237 *
1238 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1239 * to write the message, it subscribes the stream to future notifications.
1240 */
1241static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1242{
1243 struct buffer *res;
1244 char str[13];
1245 int ret;
1246
1247 if (!h2s || h2s->st == H2_SS_CLOSED)
1248 return 1;
1249
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001250 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1251 * RST_STREAM in response to a RST_STREAM frame.
1252 */
1253 if (h2c->dft == H2_FT_RST_STREAM) {
1254 ret = 1;
1255 goto ignore;
1256 }
1257
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001258 if (h2c_mux_busy(h2c, h2s)) {
1259 h2s->flags |= H2_SF_BLK_MBUSY;
1260 return 0;
1261 }
1262
Willy Tarreau44e973f2018-03-01 17:49:30 +01001263 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001264 if (!res) {
1265 h2c->flags |= H2_CF_MUX_MALLOC;
1266 h2s->flags |= H2_SF_BLK_MROOM;
1267 return 0;
1268 }
1269
1270 /* len: 4, type: 3, flags: none */
1271 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1272 write_n32(str + 5, h2s->id);
1273 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001274 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001275
1276 if (unlikely(ret <= 0)) {
1277 if (!ret) {
1278 h2c->flags |= H2_CF_MUX_MFULL;
1279 h2s->flags |= H2_SF_BLK_MROOM;
1280 return 0;
1281 }
1282 else {
1283 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1284 return 0;
1285 }
1286 }
1287
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001288 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001289 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001290 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001291 return ret;
1292}
1293
1294/* Try to send an RST_STREAM frame on the connection for the stream being
1295 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001296 * error code, even if the stream is one of the dummy ones, and will update
1297 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001298 *
1299 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1300 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001301 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001302 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001303 */
1304static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1305{
1306 struct buffer *res;
1307 char str[13];
1308 int ret;
1309
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001310 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1311 * RST_STREAM in response to a RST_STREAM frame.
1312 */
1313 if (h2c->dft == H2_FT_RST_STREAM) {
1314 ret = 1;
1315 goto ignore;
1316 }
1317
Willy Tarreau27a84c92017-10-17 08:10:17 +02001318 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001319 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001320 return 0;
1321 }
1322
Willy Tarreau44e973f2018-03-01 17:49:30 +01001323 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001324 if (!res) {
1325 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001326 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001327 return 0;
1328 }
1329
1330 /* len: 4, type: 3, flags: none */
1331 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001332
Willy Tarreau27a84c92017-10-17 08:10:17 +02001333 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001334 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001335 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001336
Willy Tarreau27a84c92017-10-17 08:10:17 +02001337 if (unlikely(ret <= 0)) {
1338 if (!ret) {
1339 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001340 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001341 return 0;
1342 }
1343 else {
1344 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1345 return 0;
1346 }
1347 }
1348
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001349 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001350 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001351 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001352 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001353 }
1354
Willy Tarreau27a84c92017-10-17 08:10:17 +02001355 return ret;
1356}
1357
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001358/* try to send an empty DATA frame with the ES flag set to notify about the
1359 * end of stream and match a shutdown(write). If an ES was already sent as
1360 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1361 * on success or zero if nothing was done. In case of lack of room to write the
1362 * message, it subscribes the requesting stream to future notifications.
1363 */
1364static int h2_send_empty_data_es(struct h2s *h2s)
1365{
1366 struct h2c *h2c = h2s->h2c;
1367 struct buffer *res;
1368 char str[9];
1369 int ret;
1370
Willy Tarreau721c9742017-11-07 11:05:42 +01001371 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001372 return 1;
1373
1374 if (h2c_mux_busy(h2c, h2s)) {
1375 h2s->flags |= H2_SF_BLK_MBUSY;
1376 return 0;
1377 }
1378
Willy Tarreau44e973f2018-03-01 17:49:30 +01001379 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001380 if (!res) {
1381 h2c->flags |= H2_CF_MUX_MALLOC;
1382 h2s->flags |= H2_SF_BLK_MROOM;
1383 return 0;
1384 }
1385
1386 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1387 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1388 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001389 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001390 if (likely(ret > 0)) {
1391 h2s->flags |= H2_SF_ES_SENT;
1392 }
1393 else if (!ret) {
1394 h2c->flags |= H2_CF_MUX_MFULL;
1395 h2s->flags |= H2_SF_BLK_MROOM;
1396 return 0;
1397 }
1398 else {
1399 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1400 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001401 }
1402 return ret;
1403}
1404
Christopher Fauletf02ca002019-03-07 16:21:34 +01001405/* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags>
1406 * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close
1407 * connection. The stream's state is automatically updated accordingly. If the
1408 * stream is orphaned, it is destroyed.
1409 */
1410static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags)
1411{
1412 if (!h2s->cs) {
1413 /* this stream was already orphaned */
1414 h2s_destroy(h2s);
1415 return;
1416 }
1417
1418 h2s->cs->flags |= flags;
1419 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1420 h2s->cs->flags |= CS_FL_ERROR;
1421
1422 h2s_alert(h2s);
1423
1424 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
1425 h2s->st = H2_SS_ERROR;
1426 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
1427 h2s->st = H2_SS_HREM;
1428 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
1429 h2s_close(h2s);
1430}
1431
1432/* wake the streams attached to the connection, whose id is greater than <last>
1433 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001434 */
1435static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1436{
1437 struct eb32_node *node;
1438 struct h2s *h2s;
1439
1440 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001441 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001442
1443 if (conn_xprt_read0_pending(h2c->conn))
Christopher Faulet203b2b02019-03-08 09:23:46 +01001444 flags |= (CS_FL_REOS|CS_FL_READ_NULL);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001445
Christopher Fauletf02ca002019-03-07 16:21:34 +01001446 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001447 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1448 while (node) {
1449 h2s = container_of(node, struct h2s, by_id);
1450 if (h2s->id <= last)
1451 break;
1452 node = eb32_next(node);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001453 h2s_wake_one_stream(h2s, flags);
1454 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001455
Christopher Fauletf02ca002019-03-07 16:21:34 +01001456 /* Wake all streams with unassigned ID (ID == 0) */
1457 node = eb32_lookup(&h2c->streams_by_id, 0);
1458 while (node) {
1459 h2s = container_of(node, struct h2s, by_id);
1460 if (h2s->id > 0)
1461 break;
1462 node = eb32_next(node);
1463 h2s_wake_one_stream(h2s, flags);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001464 }
1465}
1466
Willy Tarreau3421aba2017-07-27 15:41:03 +02001467/* Increase all streams' outgoing window size by the difference passed in
1468 * argument. This is needed upon receipt of the settings frame if the initial
1469 * window size is different. The difference may be negative and the resulting
1470 * window size as well, for the time it takes to receive some window updates.
1471 */
1472static void h2c_update_all_ws(struct h2c *h2c, int diff)
1473{
1474 struct h2s *h2s;
1475 struct eb32_node *node;
1476
1477 if (!diff)
1478 return;
1479
1480 node = eb32_first(&h2c->streams_by_id);
1481 while (node) {
1482 h2s = container_of(node, struct h2s, by_id);
1483 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001484
1485 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1486 h2s->flags &= ~H2_SF_BLK_SFCTL;
1487 if (h2s->send_wait)
1488 LIST_ADDQ(&h2c->send_list, &h2s->list);
1489
1490 }
1491
Willy Tarreau3421aba2017-07-27 15:41:03 +02001492 node = eb32_next(node);
1493 }
1494}
1495
1496/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1497 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001498 * return an error in h2c. The caller must have already verified frame length
1499 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001500 */
1501static int h2c_handle_settings(struct h2c *h2c)
1502{
1503 unsigned int offset;
1504 int error;
1505
1506 if (h2c->dff & H2_F_SETTINGS_ACK) {
1507 if (h2c->dfl) {
1508 error = H2_ERR_FRAME_SIZE_ERROR;
1509 goto fail;
1510 }
1511 return 1;
1512 }
1513
Willy Tarreau3421aba2017-07-27 15:41:03 +02001514 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001515 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001516 return 0;
1517
1518 /* parse the frame */
1519 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001520 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1521 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001522
1523 switch (type) {
1524 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1525 /* we need to update all existing streams with the
1526 * difference from the previous iws.
1527 */
1528 if (arg < 0) { // RFC7540#6.5.2
1529 error = H2_ERR_FLOW_CONTROL_ERROR;
1530 goto fail;
1531 }
1532 h2c_update_all_ws(h2c, arg - h2c->miw);
1533 h2c->miw = arg;
1534 break;
1535 case H2_SETTINGS_MAX_FRAME_SIZE:
1536 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1537 error = H2_ERR_PROTOCOL_ERROR;
1538 goto fail;
1539 }
1540 h2c->mfs = arg;
1541 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001542 case H2_SETTINGS_ENABLE_PUSH:
1543 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1544 error = H2_ERR_PROTOCOL_ERROR;
1545 goto fail;
1546 }
1547 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001548 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1549 if (h2c->flags & H2_CF_IS_BACK) {
1550 /* the limit is only for the backend; for the frontend it is our limit */
1551 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1552 arg = h2_settings_max_concurrent_streams;
1553 h2c->streams_limit = arg;
1554 }
1555 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001556 }
1557 }
1558
1559 /* need to ACK this frame now */
1560 h2c->st0 = H2_CS_FRAME_A;
1561 return 1;
1562 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001563 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001564 h2c_error(h2c, error);
1565 return 0;
1566}
1567
1568/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1569 * success or one of the h2_status values.
1570 */
1571static int h2c_ack_settings(struct h2c *h2c)
1572{
1573 struct buffer *res;
1574 char str[9];
1575 int ret = -1;
1576
1577 if (h2c_mux_busy(h2c, NULL)) {
1578 h2c->flags |= H2_CF_DEM_MBUSY;
1579 return 0;
1580 }
1581
Willy Tarreau44e973f2018-03-01 17:49:30 +01001582 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001583 if (!res) {
1584 h2c->flags |= H2_CF_MUX_MALLOC;
1585 h2c->flags |= H2_CF_DEM_MROOM;
1586 return 0;
1587 }
1588
1589 memcpy(str,
1590 "\x00\x00\x00" /* length : 0 (no data) */
1591 "\x04" "\x01" /* type : 4, flags : ACK */
1592 "\x00\x00\x00\x00" /* stream ID */, 9);
1593
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001594 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001595 if (unlikely(ret <= 0)) {
1596 if (!ret) {
1597 h2c->flags |= H2_CF_MUX_MFULL;
1598 h2c->flags |= H2_CF_DEM_MROOM;
1599 return 0;
1600 }
1601 else {
1602 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1603 return 0;
1604 }
1605 }
1606 return ret;
1607}
1608
Willy Tarreaucf68c782017-10-10 17:11:41 +02001609/* processes a PING frame and schedules an ACK if needed. The caller must pass
1610 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001611 * missing data. The caller must have already verified frame length
1612 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001613 */
1614static int h2c_handle_ping(struct h2c *h2c)
1615{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001616 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001617 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001618 h2c->st0 = H2_CS_FRAME_A;
1619 return 1;
1620}
1621
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001622/* Try to send a window update for stream id <sid> and value <increment>.
1623 * Returns > 0 on success or zero on missing room or failure. It may return an
1624 * error in h2c.
1625 */
1626static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1627{
1628 struct buffer *res;
1629 char str[13];
1630 int ret = -1;
1631
1632 if (h2c_mux_busy(h2c, NULL)) {
1633 h2c->flags |= H2_CF_DEM_MBUSY;
1634 return 0;
1635 }
1636
Willy Tarreau44e973f2018-03-01 17:49:30 +01001637 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001638 if (!res) {
1639 h2c->flags |= H2_CF_MUX_MALLOC;
1640 h2c->flags |= H2_CF_DEM_MROOM;
1641 return 0;
1642 }
1643
1644 /* length: 4, type: 8, flags: none */
1645 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1646 write_n32(str + 5, sid);
1647 write_n32(str + 9, increment);
1648
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001649 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001650
1651 if (unlikely(ret <= 0)) {
1652 if (!ret) {
1653 h2c->flags |= H2_CF_MUX_MFULL;
1654 h2c->flags |= H2_CF_DEM_MROOM;
1655 return 0;
1656 }
1657 else {
1658 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1659 return 0;
1660 }
1661 }
1662 return ret;
1663}
1664
1665/* try to send pending window update for the connection. It's safe to call it
1666 * with no pending updates. Returns > 0 on success or zero on missing room or
1667 * failure. It may return an error in h2c.
1668 */
1669static int h2c_send_conn_wu(struct h2c *h2c)
1670{
1671 int ret = 1;
1672
1673 if (h2c->rcvd_c <= 0)
1674 return 1;
1675
Willy Tarreau97aaa672018-12-23 09:49:04 +01001676 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1677 /* increase the advertised connection window to 2G on
1678 * first update.
1679 */
1680 h2c->flags |= H2_CF_WINDOW_OPENED;
1681 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1682 }
1683
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001684 /* send WU for the connection */
1685 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1686 if (ret > 0)
1687 h2c->rcvd_c = 0;
1688
1689 return ret;
1690}
1691
1692/* try to send pending window update for the current dmux stream. It's safe to
1693 * call it with no pending updates. Returns > 0 on success or zero on missing
1694 * room or failure. It may return an error in h2c.
1695 */
1696static int h2c_send_strm_wu(struct h2c *h2c)
1697{
1698 int ret = 1;
1699
1700 if (h2c->rcvd_s <= 0)
1701 return 1;
1702
1703 /* send WU for the stream */
1704 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1705 if (ret > 0)
1706 h2c->rcvd_s = 0;
1707
1708 return ret;
1709}
1710
Willy Tarreaucf68c782017-10-10 17:11:41 +02001711/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1712 * success, 0 on missing data or one of the h2_status values.
1713 */
1714static int h2c_ack_ping(struct h2c *h2c)
1715{
1716 struct buffer *res;
1717 char str[17];
1718 int ret = -1;
1719
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001720 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001721 return 0;
1722
1723 if (h2c_mux_busy(h2c, NULL)) {
1724 h2c->flags |= H2_CF_DEM_MBUSY;
1725 return 0;
1726 }
1727
Willy Tarreau44e973f2018-03-01 17:49:30 +01001728 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001729 if (!res) {
1730 h2c->flags |= H2_CF_MUX_MALLOC;
1731 h2c->flags |= H2_CF_DEM_MROOM;
1732 return 0;
1733 }
1734
1735 memcpy(str,
1736 "\x00\x00\x08" /* length : 8 (same payload) */
1737 "\x06" "\x01" /* type : 6, flags : ACK */
1738 "\x00\x00\x00\x00" /* stream ID */, 9);
1739
1740 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001741 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001742
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001743 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001744 if (unlikely(ret <= 0)) {
1745 if (!ret) {
1746 h2c->flags |= H2_CF_MUX_MFULL;
1747 h2c->flags |= H2_CF_DEM_MROOM;
1748 return 0;
1749 }
1750 else {
1751 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1752 return 0;
1753 }
1754 }
1755 return ret;
1756}
1757
Willy Tarreau26f95952017-07-27 17:18:30 +02001758/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1759 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001760 * h2c or h2s. The caller must have already verified frame length and stream ID
1761 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001762 */
1763static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1764{
1765 int32_t inc;
1766 int error;
1767
Willy Tarreau26f95952017-07-27 17:18:30 +02001768 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001769 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001770 return 0;
1771
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001772 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001773
1774 if (h2c->dsi != 0) {
1775 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001776
1777 /* it's not an error to receive WU on a closed stream */
1778 if (h2s->st == H2_SS_CLOSED)
1779 return 1;
1780
1781 if (!inc) {
1782 error = H2_ERR_PROTOCOL_ERROR;
1783 goto strm_err;
1784 }
1785
1786 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1787 error = H2_ERR_FLOW_CONTROL_ERROR;
1788 goto strm_err;
1789 }
1790
1791 h2s->mws += inc;
1792 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1793 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001794 if (h2s->send_wait)
1795 LIST_ADDQ(&h2c->send_list, &h2s->list);
1796
Willy Tarreau26f95952017-07-27 17:18:30 +02001797 }
1798 }
1799 else {
1800 /* connection window update */
1801 if (!inc) {
1802 error = H2_ERR_PROTOCOL_ERROR;
1803 goto conn_err;
1804 }
1805
1806 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1807 error = H2_ERR_FLOW_CONTROL_ERROR;
1808 goto conn_err;
1809 }
1810
1811 h2c->mws += inc;
1812 }
1813
1814 return 1;
1815
1816 conn_err:
1817 h2c_error(h2c, error);
1818 return 0;
1819
1820 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001821 h2s_error(h2s, error);
1822 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001823 return 0;
1824}
1825
Willy Tarreaue96b0922017-10-30 00:28:29 +01001826/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001827 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1828 * have already verified frame length and stream ID validity. Described in
1829 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001830 */
1831static int h2c_handle_goaway(struct h2c *h2c)
1832{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001833 int last;
1834
Willy Tarreaue96b0922017-10-30 00:28:29 +01001835 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001836 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001837 return 0;
1838
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001839 last = h2_get_n32(&h2c->dbuf, 0);
1840 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001841 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001842 if (h2c->last_sid < 0)
1843 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001844 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001845}
1846
Willy Tarreau92153fc2017-12-03 19:46:19 +01001847/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001848 * invalid. Returns > 0 on success or zero on missing data. It may return an
1849 * error in h2c. The caller must have already verified frame length and stream
1850 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001851 */
1852static int h2c_handle_priority(struct h2c *h2c)
1853{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001854 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001855 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001856 return 0;
1857
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001858 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001859 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001860 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1861 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001862 }
1863 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001864}
1865
Willy Tarreaucd234e92017-08-18 10:59:39 +02001866/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001867 * Returns > 0 on success or zero on missing data. The caller must have already
1868 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001869 */
1870static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1871{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001872 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001873 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001874 return 0;
1875
1876 /* late RST, already handled */
1877 if (h2s->st == H2_SS_CLOSED)
1878 return 1;
1879
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001880 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001881 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001882
1883 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001884 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001885 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001886 }
1887
1888 h2s->flags |= H2_SF_RST_RCVD;
1889 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001890}
1891
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001892/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1893 * It may return an error in h2c or h2s. The caller must consider that the
1894 * return value is the new h2s in case one was allocated (most common case).
1895 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001896 * errors here are reported as connection errors since it's impossible to
1897 * recover from such errors after the compression context has been altered.
1898 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001899static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001900{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001901 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001902 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001903 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001904 int error;
1905
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001906 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001907 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001908
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001909 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001910 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001911
1912 /* now either the frame is complete or the buffer is complete */
1913 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001914 /* The stream exists/existed, this must be a trailers frame */
1915 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001916 if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0)
Willy Tarreau88d138e2019-01-02 19:38:14 +01001917 goto out;
1918 goto done;
1919 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001920 /* the connection was already killed by an RST, let's consume
1921 * the data and send another RST.
1922 */
1923 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1924 h2s = (struct h2s*)h2_error_stream;
1925 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001926 }
1927 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1928 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1929 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001930 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001931 goto conn_err;
1932 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001933 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1934 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001935
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001936 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001937
Willy Tarreau25919232019-01-03 14:48:18 +01001938 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001939 if (h2c->st0 >= H2_CS_ERROR)
1940 goto out;
1941
Willy Tarreau25919232019-01-03 14:48:18 +01001942 if (error <= 0) {
1943 if (error == 0)
1944 goto out; // missing data
1945
1946 /* Failed to decode this stream (e.g. too large request)
1947 * but the HPACK decompressor is still synchronized.
1948 */
1949 h2s = (struct h2s*)h2_error_stream;
1950 goto send_rst;
1951 }
1952
Willy Tarreau22de8d32018-09-05 19:55:58 +02001953 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001954 * positively from h2c_frt_stream_new(), the stream will report the error,
1955 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001956 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001957 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001958 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001959 h2s = (struct h2s*)h2_refused_stream;
1960 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001961 }
1962
1963 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001964 h2s->rxbuf = rxbuf;
1965 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001966 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001967
Willy Tarreau88d138e2019-01-02 19:38:14 +01001968 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001969 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001970 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001971
1972 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01001973 if (h2s->st == H2_SS_OPEN)
1974 h2s->st = H2_SS_HREM;
1975 else
1976 h2s_close(h2s);
Willy Tarreau927b88b2019-03-04 08:03:25 +01001977 if (h2s->cs)
1978 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001979 }
1980
Willy Tarreau3a429f02019-01-03 11:41:50 +01001981 /* update the max stream ID if the request is being processed */
1982 if (h2s->id > h2c->max_id)
1983 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02001984
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001985 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001986
1987 conn_err:
1988 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001989 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02001990
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001991 out:
1992 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001993 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01001994
1995 send_rst:
1996 /* make the demux send an RST for the current stream. We may only
1997 * do this if we're certain that the HEADERS frame was properly
1998 * decompressed so that the HPACK decoder is still kept up to date.
1999 */
2000 h2_release_buf(h2c, &rxbuf);
2001 h2c->st0 = H2_CS_FRAME_E;
2002 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002003}
2004
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002005/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2006 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2007 * errors here are reported as connection errors since it's impossible to
2008 * recover from such errors after the compression context has been altered.
2009 */
2010static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2011{
2012 int error;
2013
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002014 if (!b_size(&h2c->dbuf))
2015 return NULL; // empty buffer
2016
2017 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2018 return NULL; // incomplete frame
2019
Willy Tarreau1915ca22019-01-24 11:49:37 +01002020 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002021
Willy Tarreau25919232019-01-03 14:48:18 +01002022 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002023 if (h2c->st0 >= H2_CS_ERROR)
2024 return NULL;
2025
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002026 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2027 /* RFC7540#5.1 */
2028 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2029 h2c->st0 = H2_CS_FRAME_E;
2030 return NULL;
2031 }
2032
Willy Tarreau25919232019-01-03 14:48:18 +01002033 if (error <= 0) {
2034 if (error == 0)
2035 return NULL; // missing data
2036
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002037 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002038 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002039 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002040 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002041 }
2042
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002043 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2044 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002045 if (h2s->cs)
2046 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002047 }
2048
Willy Tarreau927b88b2019-03-04 08:03:25 +01002049 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002050 h2s->st = H2_SS_ERROR;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002051 else if (h2s->cs && h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002052 h2s->st = H2_SS_HREM;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002053 else if ((!h2s || h2s->cs->flags & CS_FL_REOS) && h2s->st == H2_SS_HLOC)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002054 h2s_close(h2s);
2055
2056 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002057}
2058
Willy Tarreau454f9052017-10-26 19:40:35 +02002059/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2060 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2061 */
2062static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2063{
2064 int error;
2065
2066 /* note that empty DATA frames are perfectly valid and sometimes used
2067 * to signal an end of stream (with the ES flag).
2068 */
2069
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002070 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002071 return 0; // empty buffer
2072
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002073 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002074 return 0; // incomplete frame
2075
2076 /* now either the frame is complete or the buffer is complete */
2077
Willy Tarreau454f9052017-10-26 19:40:35 +02002078 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2079 /* RFC7540#6.1 */
2080 error = H2_ERR_STREAM_CLOSED;
2081 goto strm_err;
2082 }
2083
Willy Tarreau1915ca22019-01-24 11:49:37 +01002084 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2085 /* RFC7540#8.1.2 */
2086 error = H2_ERR_PROTOCOL_ERROR;
2087 goto strm_err;
2088 }
2089
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002090 if (!h2_frt_transfer_data(h2s))
2091 return 0;
2092
Willy Tarreau454f9052017-10-26 19:40:35 +02002093 /* call the upper layers to process the frame, then let the upper layer
2094 * notify the stream about any change.
2095 */
2096 if (!h2s->cs) {
2097 error = H2_ERR_STREAM_CLOSED;
2098 goto strm_err;
2099 }
2100
Willy Tarreau8f650c32017-11-21 19:36:21 +01002101 if (h2c->st0 >= H2_CS_ERROR)
2102 return 0;
2103
Willy Tarreau721c9742017-11-07 11:05:42 +01002104 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002105 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002106 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002107 }
2108
2109 /* check for completion : the callee will change this to FRAME_A or
2110 * FRAME_H once done.
2111 */
2112 if (h2c->st0 == H2_CS_FRAME_P)
2113 return 0;
2114
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002115 /* last frame */
2116 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002117 if (h2s->st == H2_SS_OPEN)
2118 h2s->st = H2_SS_HREM;
2119 else
2120 h2s_close(h2s);
2121
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002122 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002123 if (h2s->cs)
2124 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002125
2126 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2127 /* RFC7540#8.1.2 */
2128 error = H2_ERR_PROTOCOL_ERROR;
2129 goto strm_err;
2130 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002131 }
2132
Willy Tarreau454f9052017-10-26 19:40:35 +02002133 return 1;
2134
Willy Tarreau454f9052017-10-26 19:40:35 +02002135 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002136 h2s_error(h2s, error);
2137 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002138 return 0;
2139}
2140
Willy Tarreaubc933932017-10-09 16:21:43 +02002141/* process Rx frames to be demultiplexed */
2142static void h2_process_demux(struct h2c *h2c)
2143{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002144 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002145 struct h2_fh hdr;
2146 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002147
Willy Tarreau081d4722017-05-16 21:51:05 +02002148 if (h2c->st0 >= H2_CS_ERROR)
2149 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002150
2151 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2152 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002153 if (h2c->flags & H2_CF_IS_BACK)
2154 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002155 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2156 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002157 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002158 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002159 sess_log(h2c->conn->owner);
2160 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002161 goto fail;
2162 }
2163
2164 h2c->max_id = 0;
2165 h2c->st0 = H2_CS_SETTINGS1;
2166 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002167
2168 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002169 /* ensure that what is pending is a valid SETTINGS frame
2170 * without an ACK.
2171 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002172 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002173 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002174 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002175 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002176 sess_log(h2c->conn->owner);
2177 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002178 goto fail;
2179 }
2180
2181 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2182 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2183 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2184 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002185 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002186 goto fail;
2187 }
2188
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002189 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002190 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2191 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2192 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002193 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002194 goto fail;
2195 }
2196
Willy Tarreau3bf69182018-12-21 15:34:50 +01002197 /* that's OK, switch to FRAME_P to process it. This is
2198 * a SETTINGS frame whose header has already been
2199 * deleted above.
2200 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002201 padlen = 0;
2202 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002203 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002204 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002205
2206 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002207 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002208 int ret = 0;
2209
2210 if (h2c->st0 >= H2_CS_ERROR)
2211 break;
2212
2213 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002214 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002215 break;
2216
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002217 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002218 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002219 if (!h2c->nb_streams) {
2220 /* only log if no other stream can report the error */
2221 sess_log(h2c->conn->owner);
2222 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002223 break;
2224 }
2225
Willy Tarreau3bf69182018-12-21 15:34:50 +01002226 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2227 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2228 * we read the pad length and drop it from the remaining
2229 * payload (one byte + the 9 remaining ones = 10 total
2230 * removed), so we have a frame payload starting after the
2231 * pad len. Flow controlled frames (DATA) also count the
2232 * padlen in the flow control, so it must be adjusted.
2233 */
2234 if (hdr.len < 1) {
2235 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2236 sess_log(h2c->conn->owner);
2237 goto fail;
2238 }
2239 hdr.len--;
2240
2241 if (b_data(&h2c->dbuf) < 10)
2242 break; // missing padlen
2243
2244 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2245
2246 if (padlen > hdr.len) {
2247 /* RFC7540#6.1 : pad length = length of
2248 * frame payload or greater => error.
2249 */
2250 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2251 sess_log(h2c->conn->owner);
2252 goto fail;
2253 }
2254
2255 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2256 h2c->rcvd_c++;
2257 h2c->rcvd_s++;
2258 }
2259 b_del(&h2c->dbuf, 1);
2260 }
2261 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002262
2263 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002264 h2c->dfl = hdr.len;
2265 h2c->dsi = hdr.sid;
2266 h2c->dft = hdr.ft;
2267 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002268 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002269 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002270
2271 /* check for minimum basic frame format validity */
2272 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2273 if (ret != H2_ERR_NO_ERROR) {
2274 h2c_error(h2c, ret);
2275 sess_log(h2c->conn->owner);
2276 goto fail;
2277 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002278 }
2279
2280 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002281 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2282
Willy Tarreau567beb82018-12-18 16:52:44 +01002283 if (tmp_h2s != h2s && h2s && h2s->cs &&
2284 (b_data(&h2s->rxbuf) ||
2285 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002286 /* we may have to signal the upper layers */
2287 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002288 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002289 }
2290 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002291
Willy Tarreaud7901432017-12-29 11:34:40 +01002292 if (h2c->st0 == H2_CS_FRAME_E)
2293 goto strm_err;
2294
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002295 if (h2s->st == H2_SS_IDLE &&
2296 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2297 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2298 * this state MUST be treated as a connection error
2299 */
2300 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002301 if (!h2c->nb_streams) {
2302 /* only log if no other stream can report the error */
2303 sess_log(h2c->conn->owner);
2304 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002305 break;
2306 }
2307
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002308 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2309 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2310 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002311 * this state MUST be treated as a stream error.
2312 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2313 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002314 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002315 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2316 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2317 else
2318 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002319 goto strm_err;
2320 }
2321
Willy Tarreauab837502017-12-27 15:07:30 +01002322 /* Below the management of frames received in closed state is a
2323 * bit hackish because the spec makes strong differences between
2324 * streams closed by receiving RST, sending RST, and seeing ES
2325 * in both directions. In addition to this, the creation of a
2326 * new stream reusing the identifier of a closed one will be
2327 * detected here. Given that we cannot keep track of all closed
2328 * streams forever, we consider that unknown closed streams were
2329 * closed on RST received, which allows us to respond with an
2330 * RST without breaking the connection (eg: to abort a transfer).
2331 * Some frames have to be silently ignored as well.
2332 */
2333 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002334 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002335 /* #5.1.1: The identifier of a newly
2336 * established stream MUST be numerically
2337 * greater than all streams that the initiating
2338 * endpoint has opened or reserved. This
2339 * governs streams that are opened using a
2340 * HEADERS frame and streams that are reserved
2341 * using PUSH_PROMISE. An endpoint that
2342 * receives an unexpected stream identifier
2343 * MUST respond with a connection error.
2344 */
2345 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2346 goto strm_err;
2347 }
2348
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002349 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002350 /* RFC7540#5.1:closed: an endpoint that
2351 * receives any frame other than PRIORITY after
2352 * receiving a RST_STREAM MUST treat that as a
2353 * stream error of type STREAM_CLOSED.
2354 *
2355 * Note that old streams fall into this category
2356 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002357 *
2358 * However, we cannot generalize this to all frame types. Those
2359 * carrying compression state must still be processed before
2360 * being dropped or we'll desynchronize the decoder. This can
2361 * happen with request trailers received after sending an
2362 * RST_STREAM, or with header/trailers responses received after
2363 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002364 */
2365 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2366 h2c->st0 = H2_CS_FRAME_E;
2367 goto strm_err;
2368 }
2369
2370 /* RFC7540#5.1:closed: if this state is reached as a
2371 * result of sending a RST_STREAM frame, the peer that
2372 * receives the RST_STREAM might have already sent
2373 * frames on the stream that cannot be withdrawn. An
2374 * endpoint MUST ignore frames that it receives on
2375 * closed streams after it has sent a RST_STREAM
2376 * frame. An endpoint MAY choose to limit the period
2377 * over which it ignores frames and treat frames that
2378 * arrive after this time as being in error.
2379 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002380 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002381 /* RFC7540#5.1:closed: any frame other than
2382 * PRIO/WU/RST in this state MUST be treated as
2383 * a connection error
2384 */
2385 if (h2c->dft != H2_FT_RST_STREAM &&
2386 h2c->dft != H2_FT_PRIORITY &&
2387 h2c->dft != H2_FT_WINDOW_UPDATE) {
2388 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2389 goto strm_err;
2390 }
2391 }
2392 }
2393
Willy Tarreauc0da1962017-10-30 18:38:00 +01002394#if 0
2395 // problem below: it is not possible to completely ignore such
2396 // streams as we need to maintain the compression state as well
2397 // and for this we need to completely process these frames (eg:
2398 // HEADERS frames) as well as counting DATA frames to emit
2399 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2400 // This is a typical case of layer violation where the
2401 // transported contents are critical to the connection's
2402 // validity and must be ignored at the same time :-(
2403
2404 /* graceful shutdown, ignore streams whose ID is higher than
2405 * the one advertised in GOAWAY. RFC7540#6.8.
2406 */
2407 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002408 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2409 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002410 h2c->dfl -= ret;
2411 ret = h2c->dfl == 0;
2412 goto strm_err;
2413 }
2414#endif
2415
Willy Tarreau7e98c052017-10-10 15:56:59 +02002416 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002417 case H2_FT_SETTINGS:
2418 if (h2c->st0 == H2_CS_FRAME_P)
2419 ret = h2c_handle_settings(h2c);
2420
2421 if (h2c->st0 == H2_CS_FRAME_A)
2422 ret = h2c_ack_settings(h2c);
2423 break;
2424
Willy Tarreaucf68c782017-10-10 17:11:41 +02002425 case H2_FT_PING:
2426 if (h2c->st0 == H2_CS_FRAME_P)
2427 ret = h2c_handle_ping(h2c);
2428
2429 if (h2c->st0 == H2_CS_FRAME_A)
2430 ret = h2c_ack_ping(h2c);
2431 break;
2432
Willy Tarreau26f95952017-07-27 17:18:30 +02002433 case H2_FT_WINDOW_UPDATE:
2434 if (h2c->st0 == H2_CS_FRAME_P)
2435 ret = h2c_handle_window_update(h2c, h2s);
2436 break;
2437
Willy Tarreau61290ec2017-10-17 08:19:21 +02002438 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002439 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2440 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2441 * frames' parsers consume all following CONTINUATION
2442 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002443 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002444 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2445 sess_log(h2c->conn->owner);
2446 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002447
Willy Tarreau13278b42017-10-13 19:23:14 +02002448 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002449 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002450 if (h2c->flags & H2_CF_IS_BACK)
2451 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2452 else
2453 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002454 if (tmp_h2s) {
2455 h2s = tmp_h2s;
2456 ret = 1;
2457 }
2458 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002459 break;
2460
Willy Tarreau454f9052017-10-26 19:40:35 +02002461 case H2_FT_DATA:
2462 if (h2c->st0 == H2_CS_FRAME_P)
2463 ret = h2c_frt_handle_data(h2c, h2s);
2464
2465 if (h2c->st0 == H2_CS_FRAME_A)
2466 ret = h2c_send_strm_wu(h2c);
2467 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002468
Willy Tarreau92153fc2017-12-03 19:46:19 +01002469 case H2_FT_PRIORITY:
2470 if (h2c->st0 == H2_CS_FRAME_P)
2471 ret = h2c_handle_priority(h2c);
2472 break;
2473
Willy Tarreaucd234e92017-08-18 10:59:39 +02002474 case H2_FT_RST_STREAM:
2475 if (h2c->st0 == H2_CS_FRAME_P)
2476 ret = h2c_handle_rst_stream(h2c, h2s);
2477 break;
2478
Willy Tarreaue96b0922017-10-30 00:28:29 +01002479 case H2_FT_GOAWAY:
2480 if (h2c->st0 == H2_CS_FRAME_P)
2481 ret = h2c_handle_goaway(h2c);
2482 break;
2483
Willy Tarreau1c661982017-10-30 13:52:01 +01002484 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002485 default:
2486 /* drop frames that we ignore. They may be larger than
2487 * the buffer so we drain all of their contents until
2488 * we reach the end.
2489 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002490 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2491 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002492 h2c->dfl -= ret;
2493 ret = h2c->dfl == 0;
2494 }
2495
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002496 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002497 /* We may have to send an RST if not done yet */
2498 if (h2s->st == H2_SS_ERROR)
2499 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002500
Willy Tarreaua20a5192017-12-27 11:02:06 +01002501 if (h2c->st0 == H2_CS_FRAME_E)
2502 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002503
Willy Tarreau7e98c052017-10-10 15:56:59 +02002504 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002505 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002506 break;
2507
2508 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002509 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002510 h2c->st0 = H2_CS_FRAME_H;
2511 }
2512 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002513
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002514 if (h2c->rcvd_c > 0 &&
2515 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2516 h2c_send_conn_wu(h2c);
2517
Willy Tarreau52eed752017-09-22 15:05:09 +02002518 fail:
2519 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002520 if (h2s && h2s->cs &&
2521 (b_data(&h2s->rxbuf) ||
2522 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002523 /* we may have to signal the upper layers */
2524 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002525 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002526 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002527
Willy Tarreau47b515a2018-12-21 16:09:41 +01002528 h2c_restart_reading(h2c);
Willy Tarreaubc933932017-10-09 16:21:43 +02002529}
2530
2531/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2532 * the end.
2533 */
2534static int h2_process_mux(struct h2c *h2c)
2535{
Olivier Houchardd360ac62019-03-22 17:37:16 +01002536 struct h2s *h2s;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002537
Willy Tarreau01b44822018-10-03 14:26:37 +02002538 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2539 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2540 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2541 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2542 if (h2c->st0 == H2_CS_ERROR) {
2543 h2c->st0 = H2_CS_ERROR2;
2544 sess_log(h2c->conn->owner);
2545 }
2546 goto fail;
2547 }
2548 h2c->st0 = H2_CS_SETTINGS1;
2549 }
2550 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002551 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002552 return 1;
2553 }
2554
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002555 /* start by sending possibly pending window updates */
2556 if (h2c->rcvd_c > 0 &&
2557 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2558 h2c_send_conn_wu(h2c) < 0)
2559 goto fail;
2560
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002561 /* First we always process the flow control list because the streams
2562 * waiting there were already elected for immediate emission but were
2563 * blocked just on this.
2564 */
2565
Olivier Houchardd360ac62019-03-22 17:37:16 +01002566 list_for_each_entry(h2s, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002567 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2568 h2c->st0 >= H2_CS_ERROR)
2569 break;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002570 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2571 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002572
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002573 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002574 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2575 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002576 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002577 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002578 }
2579
Olivier Houchardd360ac62019-03-22 17:37:16 +01002580 list_for_each_entry(h2s, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002581 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2582 break;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002583 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2584 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002585
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002586 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002587 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2588 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002589 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002590 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002591 }
2592
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002593 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002594 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002595 if (h2c->st0 == H2_CS_ERROR) {
2596 if (h2c->max_id >= 0) {
2597 h2c_send_goaway_error(h2c, NULL);
2598 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2599 return 0;
2600 }
2601
2602 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2603 }
2604 return 1;
2605 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002606 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002607}
2608
Willy Tarreau62f52692017-10-08 23:01:42 +02002609
Willy Tarreau479998a2018-11-18 06:30:59 +01002610/* Attempt to read data, and subscribe if none available.
2611 * The function returns 1 if data has been received, otherwise zero.
2612 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002613static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002614{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002615 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002616 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002617 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002618 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002619
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002620 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002621 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002622
Willy Tarreau315d8072017-12-10 22:17:57 +01002623 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002624 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002625
Willy Tarreau44e973f2018-03-01 17:49:30 +01002626 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002627 if (!buf) {
2628 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002629 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002630 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002631
Olivier Houchard7505f942018-08-21 18:10:44 +02002632 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002633 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002634 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2635 /* HTX in use : try to pre-align the buffer like the
2636 * rxbufs will be to optimize memory copies. We'll make
2637 * sure that the frame header lands at the end of the
2638 * HTX block to alias it upon recv. We cannot use the
2639 * head because rcv_buf() will realign the buffer if
2640 * it's empty. Thus we cheat and pretend we already
2641 * have a few bytes there.
2642 */
2643 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002644 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002645 }
2646 else
2647 max = b_room(buf);
2648
Olivier Houchard7505f942018-08-21 18:10:44 +02002649 if (max)
2650 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2651 else
2652 ret = 0;
2653 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002654
Olivier Houchard53216e72018-10-10 15:46:36 +02002655 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002656 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002657
Olivier Houcharda1411e62018-08-17 18:42:48 +02002658 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002659 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002660 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002661 }
2662
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002663 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002664 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002665 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002666}
2667
Willy Tarreau479998a2018-11-18 06:30:59 +01002668/* Try to send data if possible.
2669 * The function returns 1 if data have been sent, otherwise zero.
2670 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002671static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002672{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002673 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002674 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002675 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002676
2677 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002678 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002679
Olivier Houchard7505f942018-08-21 18:10:44 +02002680
Willy Tarreaua2af5122017-10-09 11:56:46 +02002681 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2682 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002683 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002684 }
2685
Willy Tarreaubc933932017-10-09 16:21:43 +02002686 /* This loop is quite simple : it tries to fill as much as it can from
2687 * pending streams into the existing buffer until it's reportedly full
2688 * or the end of send requests is reached. Then it tries to send this
2689 * buffer's contents out, marks it not full if at least one byte could
2690 * be sent, and tries again.
2691 *
2692 * The snd_buf() function normally takes a "flags" argument which may
2693 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2694 * data immediately comes and CO_SFL_STREAMER to indicate that the
2695 * connection is streaming lots of data (used to increase TLS record
2696 * size at the expense of latency). The former can be sent any time
2697 * there's a buffer full flag, as it indicates at least one stream
2698 * attempted to send and failed so there are pending data. An
2699 * alternative would be to set it as long as there's an active stream
2700 * but that would be problematic for ACKs until we have an absolute
2701 * guarantee that all waiters have at least one byte to send. The
2702 * latter should possibly not be set for now.
2703 */
2704
2705 done = 0;
2706 while (!done) {
2707 unsigned int flags = 0;
2708
2709 /* fill as much as we can into the current buffer */
2710 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2711 done = h2_process_mux(h2c);
2712
Olivier Houchard2b094432019-01-29 18:28:36 +01002713 if (h2c->flags & H2_CF_MUX_MALLOC)
2714 break;
2715
Willy Tarreaubc933932017-10-09 16:21:43 +02002716 if (conn->flags & CO_FL_ERROR)
2717 break;
2718
2719 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2720 flags |= CO_SFL_MSG_MORE;
2721
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002722 if (b_data(&h2c->mbuf)) {
2723 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002724 if (!ret)
2725 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002726 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002727 b_del(&h2c->mbuf, ret);
2728 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002729 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002730
2731 /* wrote at least one byte, the buffer is not full anymore */
2732 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2733 }
2734
Willy Tarreaua2af5122017-10-09 11:56:46 +02002735 if (conn->flags & CO_FL_SOCK_WR_SH) {
2736 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002737 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002738 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002739 /* We're not full anymore, so we can wake any task that are waiting
2740 * for us.
2741 */
2742 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002743 struct h2s *h2s;
2744
2745 list_for_each_entry(h2s, &h2c->send_list, list) {
2746 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2747 break;
2748 if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)
2749 continue;
2750
2751 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002752 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2753 h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002754 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002755 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002756 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002757 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002758 /* We're done, no more to send */
2759 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002760 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002761schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002762 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
2763 conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002764 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002765}
2766
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002767/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002768static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2769{
2770 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002771 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002772
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002773 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002774 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002775 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002776 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002777 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002778 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002779 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002780}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002781
Willy Tarreau62f52692017-10-08 23:01:42 +02002782/* callback called on any event by the connection handler.
2783 * It applies changes and returns zero, or < 0 if it wants immediate
2784 * destruction of the connection (which normally doesn not happen in h2).
2785 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002786static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002787{
Olivier Houchard7505f942018-08-21 18:10:44 +02002788 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002789
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002790 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002791 h2_process_demux(h2c);
2792
2793 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002794 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002795
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002796 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002797 h2c->flags &= ~H2_CF_DEM_DFULL;
2798 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002799 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002800
Willy Tarreau0b37d652018-10-03 10:33:02 +02002801 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002802 /* frontend is stopping, reload likely in progress, let's try
2803 * to announce a graceful shutdown if not yet done. We don't
2804 * care if it fails, it will be tried again later.
2805 */
2806 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2807 if (h2c->last_sid < 0)
2808 h2c->last_sid = (1U << 31) - 1;
2809 h2c_send_goaway_error(h2c, NULL);
2810 }
2811 }
2812
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002813 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002814 * If we received early data, and the handshake is done, wake
2815 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002816 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002817 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2818 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2819 struct eb32_node *node;
2820 struct h2s *h2s;
2821
2822 h2c->flags |= H2_CF_WAIT_FOR_HS;
2823 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2824
2825 while (node) {
2826 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002827 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002828 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002829 node = eb32_next(node);
2830 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002831 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002832
Willy Tarreau26bd7612017-10-09 16:47:04 +02002833 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002834 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2835 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2836 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002837 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002838
2839 if (eb_is_empty(&h2c->streams_by_id)) {
2840 /* no more stream, kill the connection now */
2841 h2_release(conn);
2842 return -1;
2843 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002844 }
2845
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002846 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002847 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002848
Olivier Houchard53216e72018-10-10 15:46:36 +02002849 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2850 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2851 (h2c->st0 != H2_CS_ERROR &&
2852 !b_data(&h2c->mbuf) &&
2853 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2854 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002855 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002856
Willy Tarreau3f133572017-10-31 19:21:06 +01002857 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002858 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002859 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002860 task_queue(h2c->task);
2861 }
2862 else
2863 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002864 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002865
Olivier Houchard7505f942018-08-21 18:10:44 +02002866 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002867 return 0;
2868}
2869
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002870/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002871static int h2_wake(struct connection *conn)
2872{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002873 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002874
2875 return (h2_process(h2c));
2876}
2877
Willy Tarreauea392822017-10-31 10:02:25 +01002878/* Connection timeout management. The principle is that if there's no receipt
2879 * nor sending for a certain amount of time, the connection is closed. If the
2880 * MUX buffer still has lying data or is not allocatable, the connection is
2881 * immediately killed. If it's allocatable and empty, we attempt to send a
2882 * GOAWAY frame.
2883 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002884static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002885{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002886 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002887 int expired = tick_is_expired(t->expire, now_ms);
2888
Willy Tarreau0975f112018-03-29 15:22:59 +02002889 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002890 return t;
2891
Willy Tarreau0975f112018-03-29 15:22:59 +02002892 task_delete(t);
2893 task_free(t);
2894
2895 if (!h2c) {
2896 /* resources were already deleted */
2897 return NULL;
2898 }
2899
2900 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002901 h2c_error(h2c, H2_ERR_NO_ERROR);
2902 h2_wake_some_streams(h2c, 0, 0);
2903
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002904 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002905 /* don't even try to send a GOAWAY, the buffer is stuck */
2906 h2c->flags |= H2_CF_GOAWAY_FAILED;
2907 }
2908
2909 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002910 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002911 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2912 h2c->flags |= H2_CF_GOAWAY_FAILED;
2913
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002914 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2915 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002916 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002917 b_del(&h2c->mbuf, ret);
2918 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002919 }
2920 }
Willy Tarreauea392822017-10-31 10:02:25 +01002921
Willy Tarreau0975f112018-03-29 15:22:59 +02002922 /* either we can release everything now or it will be done later once
2923 * the last stream closes.
2924 */
2925 if (eb_is_empty(&h2c->streams_by_id))
2926 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002927
Willy Tarreauea392822017-10-31 10:02:25 +01002928 return NULL;
2929}
2930
2931
Willy Tarreau62f52692017-10-08 23:01:42 +02002932/*******************************************/
2933/* functions below are used by the streams */
2934/*******************************************/
2935
2936/*
2937 * Attach a new stream to a connection
2938 * (Used for outgoing connections)
2939 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002940static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002941{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002942 struct conn_stream *cs;
2943 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002944 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002945
2946 cs = cs_new(conn);
2947 if (!cs)
2948 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002949 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002950 if (!h2s) {
2951 cs_free(cs);
2952 return NULL;
2953 }
2954 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002955}
2956
Willy Tarreaufafd3982018-11-18 21:29:20 +01002957/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2958 * We have to scan because we may have some orphan streams. It might be
2959 * beneficial to scan backwards from the end to reduce the likeliness to find
2960 * orphans.
2961 */
2962static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2963{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002964 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01002965 struct h2s *h2s;
2966 struct eb32_node *node;
2967
2968 node = eb32_first(&h2c->streams_by_id);
2969 while (node) {
2970 h2s = container_of(node, struct h2s, by_id);
2971 if (h2s->cs)
2972 return h2s->cs;
2973 node = eb32_next(node);
2974 }
2975 return NULL;
2976}
2977
Willy Tarreau62f52692017-10-08 23:01:42 +02002978/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002979 * Destroy the mux and the associated connection, if it is no longer used
2980 */
2981static void h2_destroy(struct connection *conn)
2982{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002983 struct h2c *h2c = conn->ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01002984
2985 if (eb_is_empty(&h2c->streams_by_id))
2986 h2_release(h2c->conn);
2987}
2988
2989/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002990 * Detach the stream from the connection and possibly release the connection.
2991 */
2992static void h2_detach(struct conn_stream *cs)
2993{
Willy Tarreau60935142017-10-16 18:11:19 +02002994 struct h2s *h2s = cs->ctx;
2995 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002996 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002997
2998 cs->ctx = NULL;
2999 if (!h2s)
3000 return;
3001
Olivier Houchardf502aca2018-12-14 19:42:40 +01003002 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003003 h2c = h2s->h2c;
3004 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003005 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003006 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3007 !h2_frt_has_too_many_cs(h2c)) {
3008 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003009 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01003010 h2c_restart_reading(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02003011 }
Willy Tarreau60935142017-10-16 18:11:19 +02003012
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003013 /* this stream may be blocked waiting for some data to leave (possibly
3014 * an ES or RST frame), so orphan it in this case.
3015 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003016 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003017 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003018 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) && (h2s->send_wait || h2s->recv_wait))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003019 return;
3020
Willy Tarreau45f752e2017-10-30 15:44:59 +01003021 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3022 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3023 /* unblock the connection if it was blocked on this
3024 * stream.
3025 */
3026 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3027 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Willy Tarreau47b515a2018-12-21 16:09:41 +01003028 h2c_restart_reading(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003029 }
3030
Willy Tarreau71049cc2018-03-28 13:56:39 +02003031 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003032
Olivier Houchard8a786902018-12-15 16:05:40 +01003033 if (h2c->flags & H2_CF_IS_BACK &&
3034 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003035 if (!(h2c->conn->flags &
3036 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3037 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003038 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003039 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3040 h2c->conn->owner = NULL;
3041 if (eb_is_empty(&h2c->streams_by_id)) {
3042 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3043 /* The server doesn't want it, let's kill the connection right away */
3044 h2c->conn->mux->destroy(h2c->conn);
3045 return;
3046 }
3047 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003048 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003049 if (eb_is_empty(&h2c->streams_by_id)) {
3050 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3051 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3052 return;
3053 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003054 /* Never ever allow to reuse a connection from a non-reuse backend */
3055 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3056 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003057 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003058 struct server *srv = objt_server(h2c->conn->target);
3059
3060 if (srv) {
3061 if (h2c->conn->flags & CO_FL_PRIVATE)
3062 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3063 else
3064 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3065 }
3066
3067 }
3068 }
3069 }
3070
Willy Tarreaue323f342018-03-28 13:51:45 +02003071 /* We don't want to close right now unless we're removing the
3072 * last stream, and either the connection is in error, or it
3073 * reached the ID already specified in a GOAWAY frame received
3074 * or sent (as seen by last_sid >= 0).
3075 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003076 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003077 /* no more stream will come, kill it now */
3078 h2_release(h2c->conn);
3079 }
3080 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003081 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003082 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3083 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003084 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003085 else
3086 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003087 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003088}
3089
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003090/* Performs a synchronous or asynchronous shutr().
3091 * FIXME: guess what the return code tries to indicate!
3092 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003093static int h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003094{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003095 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003096 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003097
Willy Tarreau721c9742017-11-07 11:05:42 +01003098 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003099 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003100
Willy Tarreau18059042019-01-31 19:12:48 +01003101 /* a connstream may require us to immediately kill the whole connection
3102 * for example because of a "tcp-request content reject" rule that is
3103 * normally used to limit abuse. In this case we schedule a goaway to
3104 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003105 */
Willy Tarreau18059042019-01-31 19:12:48 +01003106 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3107 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3108 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3109 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3110 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003111 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3112 /* Nothing was never sent for this stream, so reset with
3113 * REFUSED_STREAM error to let the client retry the
3114 * request.
3115 */
3116 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3117 }
Willy Tarreau18059042019-01-31 19:12:48 +01003118
Willy Tarreau90c32322017-11-24 08:00:30 +01003119 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003120 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003121 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003122
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003123 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003124 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003125 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003126
Olivier Houchard7a977432019-03-21 15:47:13 +01003127 return 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003128add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003129 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003130 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003131 if (h2s->flags & H2_SF_BLK_MFCTL) {
3132 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3133 h2s->send_wait = sw;
3134 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3135 h2s->send_wait = sw;
3136 LIST_ADDQ(&h2c->send_list, &h2s->list);
3137 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003138 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003139 /* Let the handler know we want shutr */
3140 sw->handle = (void *)((long)sw->handle | 1);
Olivier Houchard7a977432019-03-21 15:47:13 +01003141 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02003142}
3143
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003144/* Performs a synchronous or asynchronous shutw().
3145 * FIXME: guess what the return code tries to indicate!
3146 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003147static int h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003148{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003149 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003150 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003151
Willy Tarreau721c9742017-11-07 11:05:42 +01003152 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003153 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003154
Willy Tarreau67434202017-11-06 20:20:51 +01003155 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003156 /* we can cleanly close using an empty data frame only after headers */
3157
3158 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3159 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003160 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003161
3162 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003163 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003164 else
3165 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003166 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003167 /* a connstream may require us to immediately kill the whole connection
3168 * for example because of a "tcp-request content reject" rule that is
3169 * normally used to limit abuse. In this case we schedule a goaway to
3170 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003171 */
Willy Tarreau18059042019-01-31 19:12:48 +01003172 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3173 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3174 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3175 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3176 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003177 else {
3178 /* Nothing was never sent for this stream, so reset with
3179 * REFUSED_STREAM error to let the client retry the
3180 * request.
3181 */
3182 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3183 }
Willy Tarreau18059042019-01-31 19:12:48 +01003184
Willy Tarreau90c32322017-11-24 08:00:30 +01003185 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003186 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003187 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003188
Willy Tarreau00dd0782018-03-01 16:31:34 +01003189 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003190 }
3191
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003192 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003193 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchard7a977432019-03-21 15:47:13 +01003194 return 0;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003195
3196 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003197 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003198 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003199 if (h2s->flags & H2_SF_BLK_MFCTL) {
3200 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3201 h2s->send_wait = sw;
3202 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3203 h2s->send_wait = sw;
3204 LIST_ADDQ(&h2c->send_list, &h2s->list);
3205 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003206 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003207 /* let the handler know we want to shutw */
3208 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard7a977432019-03-21 15:47:13 +01003209 return 1;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003210}
3211
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003212/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3213 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3214 * and prevented the last frame from being emitted.
3215 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003216static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3217{
3218 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003219 long reason = (long)h2s->wait_event.handle;
Olivier Houchard7a977432019-03-21 15:47:13 +01003220 int ret = 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003221
Olivier Houchard2c68a462018-12-15 22:42:20 +01003222 if (h2s->send_wait) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003223 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003224 h2s->send_wait = NULL;
3225 LIST_DEL(&h2s->list);
3226 LIST_INIT(&h2s->list);
3227 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003228 if (reason & 2)
Olivier Houchard7a977432019-03-21 15:47:13 +01003229 ret |= h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003230 if (reason & 1)
Olivier Houchard7a977432019-03-21 15:47:13 +01003231 ret |= h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003232
Olivier Houchard7a977432019-03-21 15:47:13 +01003233 /* We're no longer trying to send anything, let's destroy the h2s */
3234 if (!ret) {
3235 struct h2c *h2c = h2s->h2c;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003236 h2s_destroy(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01003237
3238 if (h2c_is_dead(h2c))
3239 h2_release(h2c->conn);
3240 }
3241
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003242 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003243}
3244
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003245/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003246static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3247{
3248 struct h2s *h2s = cs->ctx;
3249
3250 if (!mode)
3251 return;
3252
3253 h2_do_shutr(h2s);
3254}
3255
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003256/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003257static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3258{
3259 struct h2s *h2s = cs->ctx;
3260
3261 h2_do_shutw(h2s);
3262}
3263
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003264/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003265 * HTX request or response depending on the connection's side. Returns a
3266 * positive value on success, a negative value on failure, or 0 if it couldn't
3267 * proceed. May report connection errors in h2c->errcode if the frame is
3268 * non-decodable and the connection unrecoverable. In absence of connection
3269 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003270 *
3271 * The function may fold CONTINUATION frames into the initial HEADERS frame
3272 * by removing padding and next frame header, then moving the CONTINUATION
3273 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3274 * leaving a hole between the main frame and the beginning of the next one.
3275 * The possibly remaining incomplete or next frame at the end may be moved
3276 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3277 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3278 *
3279 * A buffer at the beginning of processing may look like this :
3280 *
3281 * ,---.---------.-----.--------------.--------------.------.---.
3282 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3283 * `---^---------^-----^--------------^--------------^------^---'
3284 * | | <-----> | |
3285 * area | dpl | wrap
3286 * |<--------------> |
3287 * | dfl |
3288 * |<-------------------------------------------------->|
3289 * head data
3290 *
3291 * Padding is automatically overwritten when folding, participating to the
3292 * hole size after dfl :
3293 *
3294 * ,---.------------------------.-----.--------------.------.---.
3295 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3296 * `---^------------------------^-----^--------------^------^---'
3297 * | | <-----> | |
3298 * area | hole | wrap
3299 * |<-----------------------> |
3300 * | dfl |
3301 * |<-------------------------------------------------->|
3302 * head data
3303 *
3304 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3305 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3306 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003307 *
3308 * The <flags> field must point to either the stream's flags or to a copy of it
3309 * so that the function can update the following flags :
3310 * - H2_SF_DATA_CLEN when content-length is seen
3311 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3312 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003313 *
3314 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3315 * decoding, in order to detect if we're dealing with a headers or a trailers
3316 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003317 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003318static 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 +02003319{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003320 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003321 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003322 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003323 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003324 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003325 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003326 int flen; // header frame len
3327 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003328 int ret = 0;
3329 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003330 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003331 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003332
Willy Tarreauea18f862018-12-22 20:19:26 +01003333next_frame:
3334 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3335 goto leave; // incomplete input frame
3336
3337 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3338 * this case, we'll try to paste it immediately after the initial
3339 * HEADERS frame payload and kill any possible padding. The initial
3340 * frame's length will be increased to represent the concatenation
3341 * of the two frames. The next frame is read from position <tlen>
3342 * and written at position <flen> (minus padding if some is present).
3343 */
3344 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3345 struct h2_fh hdr;
3346 int clen; // CONTINUATION frame's payload length
3347
3348 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3349 /* no more data, the buffer may be full, either due to
3350 * too large a frame or because of too large a hole that
3351 * we're going to compact at the end.
3352 */
3353 goto leave;
3354 }
3355
3356 if (hdr.ft != H2_FT_CONTINUATION) {
3357 /* RFC7540#6.10: frame of unexpected type */
3358 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3359 goto fail;
3360 }
3361
3362 if (hdr.sid != h2c->dsi) {
3363 /* RFC7540#6.10: frame of different stream */
3364 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3365 goto fail;
3366 }
3367
3368 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3369 /* RFC7540#4.2: invalid frame length */
3370 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3371 goto fail;
3372 }
3373
3374 /* detect when we must stop aggragating frames */
3375 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3376
3377 /* Take as much as we can of the CONTINUATION frame's payload */
3378 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3379 if (clen > hdr.len)
3380 clen = hdr.len;
3381
3382 /* Move the frame's payload over the padding, hole and frame
3383 * header. At least one of hole or dpl is null (see diagrams
3384 * above). The hole moves after the new aggragated frame.
3385 */
3386 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3387 h2c->dfl += clen - h2c->dpl;
3388 hole += h2c->dpl + 9;
3389 h2c->dpl = 0;
3390 goto next_frame;
3391 }
3392
3393 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003394
Willy Tarreau13278b42017-10-13 19:23:14 +02003395 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003396 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003397 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003398 copy = alloc_trash_chunk();
3399 if (!copy) {
3400 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3401 goto fail;
3402 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003403 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3404 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3405 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003406 }
3407
Willy Tarreau13278b42017-10-13 19:23:14 +02003408 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3409 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003410 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003411 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3412 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003413 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003414 }
3415
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003416 if (flen < 5) {
3417 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3418 goto fail;
3419 }
3420
Willy Tarreau13278b42017-10-13 19:23:14 +02003421 hdrs += 5; // stream dep = 4, weight = 1
3422 flen -= 5;
3423 }
3424
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003425 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003426 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003427 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003428 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003429
Willy Tarreau937f7602018-02-26 15:22:17 +01003430 /* we can't retry a failed decompression operation so we must be very
3431 * careful not to take any risks. In practice the output buffer is
3432 * always empty except maybe for trailers, in which case we simply have
3433 * to wait for the upper layer to finish consuming what is available.
3434 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003435
3436 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003437 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003438 if (!htx_is_empty(htx)) {
3439 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003440 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003441 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003442 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003443 if (b_data(rxbuf)) {
3444 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003445 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003446 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003447
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003448 rxbuf->head = 0;
3449 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003450 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003451
Willy Tarreau25919232019-01-03 14:48:18 +01003452 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003453 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3454 sizeof(list)/sizeof(list[0]), tmp);
3455 if (outlen < 0) {
3456 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3457 goto fail;
3458 }
3459
Willy Tarreau25919232019-01-03 14:48:18 +01003460 /* The PACK decompressor was updated, let's update the input buffer and
3461 * the parser's state to commit these changes and allow us to later
3462 * fail solely on the stream if needed.
3463 */
3464 b_del(&h2c->dbuf, h2c->dfl + hole);
3465 h2c->dfl = hole = 0;
3466 h2c->st0 = H2_CS_FRAME_H;
3467
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003468 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003469 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003470
Willy Tarreau88d138e2019-01-02 19:38:14 +01003471 if (*flags & H2_SF_HEADERS_RCVD)
3472 goto trailers;
3473
3474 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003475 if (htx) {
3476 /* HTX mode */
3477 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003478 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003479 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003480 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003481 } else {
3482 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003483 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003484 if (outlen > 0)
3485 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003486 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003487
3488 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003489 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003490 goto fail;
3491 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003492
Willy Tarreau174b06a2018-04-25 18:13:58 +02003493 if (msgf & H2_MSGF_BODY) {
3494 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003495 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003496 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003497 if (htx)
3498 htx->extra = *body_len;
3499 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003500 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003501 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003502 }
3503
Willy Tarreau88d138e2019-01-02 19:38:14 +01003504 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003505 /* indicate that a HEADERS frame was received for this stream, except
3506 * for 1xx responses. For 1xx responses, another HEADERS frame is
3507 * expected.
3508 */
3509 if (!(msgf & H2_MSGF_RSP_1XX))
3510 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003511
Christopher Faulet0b465482019-02-19 15:14:23 +01003512 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003513 /* Mark the end of message, either using EOM in HTX or with the
3514 * trailing CRLF after the end of trailers. Note that DATA_CHNK
3515 * is not set during headers with END_STREAM.
3516 */
3517 if (htx) {
3518 if (!htx_add_endof(htx, HTX_BLK_EOM))
3519 goto fail;
3520 }
3521 else if (*flags & H2_SF_DATA_CHNK) {
3522 if (!b_putblk(rxbuf, "\r\n", 2))
3523 goto fail;
3524 }
3525 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003526
Willy Tarreau86277d42019-01-02 15:36:11 +01003527 /* success */
3528 ret = 1;
3529
Willy Tarreau68dd9852017-07-03 14:44:26 +02003530 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003531 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003532 * move the remaining data over it.
3533 */
3534 if (hole) {
3535 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3536 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3537 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3538 b_sub(&h2c->dbuf, hole);
3539 }
3540
3541 if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) {
3542 /* too large frames */
3543 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003544 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003545 }
3546
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003547 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003548 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003549 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003550 return ret;
3551
Willy Tarreau68dd9852017-07-03 14:44:26 +02003552 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003553 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003554 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003555
3556 trailers:
3557 /* This is the last HEADERS frame hence a trailer */
3558
3559 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3560 /* It's a trailer but it's missing ES flag */
3561 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3562 goto fail;
3563 }
3564
3565 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3566 * block, and when using chunks we must send the 0 CRLF marker. For
3567 * other modes, the trailers are silently dropped.
3568 */
3569 if (htx) {
3570 if (!htx_add_endof(htx, HTX_BLK_EOD))
3571 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003572 if (h2_make_htx_trailers(list, htx) <= 0)
3573 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003574 }
3575 else if (*flags & H2_SF_DATA_CHNK) {
3576 /* Legacy mode with chunked encoding : we must finalize the
3577 * data block message emit the trailing CRLF */
3578 if (!b_putblk(rxbuf, "0\r\n", 3))
3579 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003580
3581 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3582 if (outlen > 0)
3583 b_add(rxbuf, outlen);
3584 else
3585 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003586 }
3587
3588 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003589}
3590
Willy Tarreau454f9052017-10-26 19:40:35 +02003591/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3592 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3593 * in use, a new chunk is emitted for each frame. This is supposed to fit
3594 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3595 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3596 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003597 * parser state is automatically updated. Returns > 0 if it could completely
3598 * send the current frame, 0 if it couldn't complete, in which case
3599 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3600 * DATA frame can return 0 as a valid result). Stream errors are reported in
3601 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3602 * have checked the frame header and ensured that the frame was complete or the
3603 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003604 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003605static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003606{
3607 struct h2c *h2c = h2s->h2c;
3608 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003609 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003610 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003611 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003612 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003613
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003614 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003615
Olivier Houchard638b7992018-08-16 15:41:52 +02003616 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003617 if (!csbuf) {
3618 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003619 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003620 }
3621
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003622try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003623 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003624 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3625 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003626 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003627 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003628
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003629 if (flen > b_data(&h2c->dbuf)) {
3630 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003631 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003632 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003633 }
3634
Willy Tarreaua9b77962019-01-31 07:23:00 +01003635 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003636 block1 = htx_free_data_space(htx);
3637 if (!block1) {
3638 h2c->flags |= H2_CF_DEM_SFULL;
3639 goto fail;
3640 }
3641 if (flen > block1)
3642 flen = block1;
3643
3644 /* here, flen is the max we can copy into the output buffer */
3645 block1 = b_contig_data(&h2c->dbuf, 0);
3646 if (flen > block1)
3647 flen = block1;
3648
3649 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3650 h2c->flags |= H2_CF_DEM_SFULL;
3651 goto fail;
3652 }
3653
3654 b_del(&h2c->dbuf, flen);
3655 h2c->dfl -= flen;
3656 h2c->rcvd_c += flen;
3657 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003658
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003659 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003660 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003661 htx->extra = h2s->body_len;
3662 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003663 goto try_again;
3664 }
3665 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003666 /* it doesn't fit and the buffer is fragmented,
3667 * so let's defragment it and try again.
3668 */
3669 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003670 }
3671
Willy Tarreaueba10f22018-04-25 20:44:22 +02003672 /* chunked-encoding requires more room */
3673 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003674 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003675 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3676 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3677 (chklen < 1048576) ? 4 : 8;
3678 chklen += 4; // CRLF, CRLF
3679 }
3680
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003681 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003682 if (flen + chklen > b_room(csbuf)) {
3683 if (chklen >= b_room(csbuf)) {
3684 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003685 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003686 }
3687 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003688 }
3689
3690 if (h2s->flags & H2_SF_DATA_CHNK) {
3691 /* emit the chunk size */
3692 unsigned int chksz = flen;
3693 char str[10];
3694 char *beg;
3695
3696 beg = str + sizeof(str);
3697 *--beg = '\n';
3698 *--beg = '\r';
3699 do {
3700 *--beg = hextab[chksz & 0xF];
3701 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003702 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003703 }
3704
Willy Tarreau454f9052017-10-26 19:40:35 +02003705 /* Block1 is the length of the first block before the buffer wraps,
3706 * block2 is the optional second block to reach the end of the frame.
3707 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003708 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003709 if (block1 > flen)
3710 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003711 block2 = flen - block1;
3712
3713 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003714 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003715
3716 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003717 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003718
Willy Tarreaueba10f22018-04-25 20:44:22 +02003719 if (h2s->flags & H2_SF_DATA_CHNK) {
3720 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003721 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003722 }
3723
Willy Tarreau454f9052017-10-26 19:40:35 +02003724 /* now mark the input data as consumed (will be deleted from the buffer
3725 * by the caller when seeing FRAME_A after sending the window update).
3726 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003727 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003728 h2c->dfl -= flen;
3729 h2c->rcvd_c += flen;
3730 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3731
Willy Tarreau1915ca22019-01-24 11:49:37 +01003732 if (h2s->flags & H2_SF_DATA_CLEN)
3733 h2s->body_len -= flen;
3734
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003735 if (h2c->dfl > h2c->dpl) {
3736 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003737 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003738 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003739 }
3740
Willy Tarreau4a28da12018-01-04 14:41:00 +01003741 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003742 /* here we're done with the frame, all the payload (except padding) was
3743 * transferred.
3744 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003745
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003746 if (h2c->dff & H2_F_DATA_END_STREAM) {
3747 if (htx) {
3748 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3749 h2c->flags |= H2_CF_DEM_SFULL;
3750 goto fail;
3751 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003752 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003753 else if (h2s->flags & H2_SF_DATA_CHNK) {
3754 /* emit the trailing 0 CRLF CRLF */
3755 if (b_room(csbuf) < 5) {
3756 h2c->flags |= H2_CF_DEM_SFULL;
3757 goto fail;
3758 }
3759 chklen += 5;
3760 b_putblk(csbuf, "0\r\n\r\n", 5);
3761 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003762 }
3763
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003764 h2c->rcvd_c += h2c->dpl;
3765 h2c->rcvd_s += h2c->dpl;
3766 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003767 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003768 if (htx)
3769 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003770 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003771 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003772 if (htx)
3773 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003774 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003775}
3776
Willy Tarreau5dd17352018-06-14 13:33:30 +02003777/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3778 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3779 * number of bytes sent. The caller must check the stream's status to detect
3780 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003781 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003782static 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 +02003783{
3784 struct http_hdr list[MAX_HTTP_HDR];
3785 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003786 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003787 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003788 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003789 int es_now = 0;
3790 int ret = 0;
3791 int hdr;
3792
3793 if (h2c_mux_busy(h2c, h2s)) {
3794 h2s->flags |= H2_SF_BLK_MBUSY;
3795 return 0;
3796 }
3797
Willy Tarreau44e973f2018-03-01 17:49:30 +01003798 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003799 h2c->flags |= H2_CF_MUX_MALLOC;
3800 h2s->flags |= H2_SF_BLK_MROOM;
3801 return 0;
3802 }
3803
3804 /* First, try to parse the H1 response and index it into <list>.
3805 * NOTE! Since it comes from haproxy, we *know* that a response header
3806 * block does not wrap and we can safely read it this way without
3807 * having to realign the buffer.
3808 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003809 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003810 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003811 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003812 /* incomplete or invalid response, this is abnormal coming from
3813 * haproxy and may only result in a bad errorfile or bad Lua code
3814 * so that won't be fixed, raise an error now.
3815 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003816 * FIXME: we should instead add the ability to only return a
3817 * 502 bad gateway. But in theory this is not supposed to
3818 * happen.
3819 */
3820 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3821 ret = 0;
3822 goto end;
3823 }
3824
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003825 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003826
3827 /* certain statuses have no body or an empty one, regardless of
3828 * what the headers say.
3829 */
3830 if (sl.st.status >= 100 && sl.st.status < 200) {
3831 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3832 h1m->curr_len = h1m->body_len = 0;
3833 }
3834 else if (sl.st.status == 204 || sl.st.status == 304) {
3835 /* no contents, claim c-len is present and set to zero */
3836 h1m->flags &= ~H1_MF_CHNK;
3837 h1m->flags |= H1_MF_CLEN;
3838 h1m->curr_len = h1m->body_len = 0;
3839 }
3840
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003841 chunk_reset(&outbuf);
3842
3843 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003844 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003845 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003846 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003847
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003848 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003849 break;
3850 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003851 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003852 }
3853
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003854 if (outbuf.size < 9)
3855 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003856
3857 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003858 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3859 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3860 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003861
3862 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003863 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003864 /* this is an unparsable response */
3865 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3866 ret = 0;
3867 goto end;
3868 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003869
3870 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003871 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003872 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003873 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003874 }
3875
3876 /* encode all headers, stop at empty name */
3877 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003878 /* these ones do not exist in H2 and must be dropped. */
3879 if (isteq(list[hdr].n, ist("connection")) ||
3880 isteq(list[hdr].n, ist("proxy-connection")) ||
3881 isteq(list[hdr].n, ist("keep-alive")) ||
3882 isteq(list[hdr].n, ist("upgrade")) ||
3883 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003884 continue;
3885
3886 if (isteq(list[hdr].n, ist("")))
3887 break; // end
3888
3889 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3890 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003891 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003892 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003893 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003894 }
3895 }
3896
3897 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003898 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003899 es_now = 1;
3900
3901 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003902 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003903
3904 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003905 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003906
3907 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003908 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003909
3910 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003911 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003912 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003913
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003914 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003915 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003916 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003917
Willy Tarreau801250e2018-09-11 11:45:04 +02003918 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003919 h2s->flags |= H2_SF_ES_SENT;
3920 if (h2s->st == H2_SS_OPEN)
3921 h2s->st = H2_SS_HLOC;
3922 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003923 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003924 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003925 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003926 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003927 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003928 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003929 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003930 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003931 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003932
3933 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003934
3935 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003936 //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 +02003937 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003938 full:
3939 h1m_init_res(h1m);
3940 h1m->err_pos = -1; // don't care about errors on the response path
3941 h2c->flags |= H2_CF_MUX_MFULL;
3942 h2s->flags |= H2_SF_BLK_MROOM;
3943 ret = 0;
3944 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003945}
3946
Willy Tarreau5dd17352018-06-14 13:33:30 +02003947/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3948 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3949 * the number of bytes sent. The caller must check the stream's status to
3950 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003951 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003952static 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 +02003953{
3954 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003955 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003956 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003957 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003958 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003959 int es_now = 0;
3960 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003961 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003962 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003963
3964 if (h2c_mux_busy(h2c, h2s)) {
3965 h2s->flags |= H2_SF_BLK_MBUSY;
3966 goto end;
3967 }
3968
Willy Tarreau44e973f2018-03-01 17:49:30 +01003969 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003970 h2c->flags |= H2_CF_MUX_MALLOC;
3971 h2s->flags |= H2_SF_BLK_MROOM;
3972 goto end;
3973 }
3974
3975 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003976 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003977 goto end;
3978
3979 chunk_reset(&outbuf);
3980
3981 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003982 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003983 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003984 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003985
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003986 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003987 break;
3988 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003989 /* If there are pending data in the output buffer, and we have
3990 * less than 1/4 of the mbuf's size and everything fits, we'll
3991 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3992 * is full and wait, to save some slow realign calls.
3993 */
3994 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3995 h2c->flags |= H2_CF_MUX_MFULL;
3996 h2s->flags |= H2_SF_BLK_MROOM;
3997 goto end;
3998 }
3999
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004000 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004001 }
4002
4003 if (outbuf.size < 9) {
4004 h2c->flags |= H2_CF_MUX_MFULL;
4005 h2s->flags |= H2_SF_BLK_MROOM;
4006 goto end;
4007 }
4008
4009 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004010 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4011 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4012 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004013
4014 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4015 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004016 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004017 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004018 break;
4019 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004020 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004021 if ((long long)size > h1m->curr_len)
4022 size = h1m->curr_len;
4023 break;
4024 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004025 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004026 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004027 if (!ret)
4028 goto end;
4029
4030 if (ret < 0) {
4031 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004032 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004033 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4034 goto end;
4035 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004036 max -= ret;
4037 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004038 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004039 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004040 }
4041
Willy Tarreau801250e2018-09-11 11:45:04 +02004042 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004043 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004044 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004045 if (!ret)
4046 goto end;
4047
4048 if (ret < 0) {
4049 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004050 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004051 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4052 goto end;
4053 }
4054
4055 size = chunk;
4056 h1m->curr_len = chunk;
4057 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004058 max -= ret;
4059 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004060 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004061 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004062 if (!size)
4063 goto send_empty;
4064 }
4065
4066 /* in MSG_DATA state, continue below */
4067 size = h1m->curr_len;
4068 break;
4069 }
4070
4071 /* we have in <size> the exact number of bytes we need to copy from
4072 * the H1 buffer. We need to check this against the connection's and
4073 * the stream's send windows, and to ensure that this fits in the max
4074 * frame size and in the buffer's available space minus 9 bytes (for
4075 * the frame header). The connection's flow control is applied last so
4076 * that we can use a separate list of streams which are immediately
4077 * unblocked on window opening. Note: we don't implement padding.
4078 */
4079
Willy Tarreau5dd17352018-06-14 13:33:30 +02004080 if (size > max)
4081 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004082
4083 if (size > h2s->mws)
4084 size = h2s->mws;
4085
4086 if (size <= 0) {
4087 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02004088 if (h2s->send_wait) {
4089 LIST_DEL(&h2s->list);
4090 LIST_INIT(&h2s->list);
4091 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004092 goto end;
4093 }
4094
4095 if (h2c->mfs && size > h2c->mfs)
4096 size = h2c->mfs;
4097
4098 if (size + 9 > outbuf.size) {
4099 /* we have an opportunity for enlarging the too small
4100 * available space, let's try.
4101 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004102 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004103 goto realign_again;
4104 size = outbuf.size - 9;
4105 }
4106
4107 if (size <= 0) {
4108 h2c->flags |= H2_CF_MUX_MFULL;
4109 h2s->flags |= H2_SF_BLK_MROOM;
4110 goto end;
4111 }
4112
4113 if (size > h2c->mws)
4114 size = h2c->mws;
4115
4116 if (size <= 0) {
4117 h2s->flags |= H2_SF_BLK_MFCTL;
4118 goto end;
4119 }
4120
4121 /* copy whatever we can */
4122 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004123 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004124 if (ret == 1)
4125 len2 = 0;
4126
4127 if (!ret || len1 + len2 < size) {
4128 /* FIXME: must normally never happen */
4129 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4130 goto end;
4131 }
4132
4133 /* limit len1/len2 to size */
4134 if (len1 + len2 > size) {
4135 int sub = len1 + len2 - size;
4136
4137 if (len2 > sub)
4138 len2 -= sub;
4139 else {
4140 sub -= len2;
4141 len2 = 0;
4142 len1 -= sub;
4143 }
4144 }
4145
4146 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004147 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004148 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004149 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004150
4151 send_empty:
4152 /* we may need to add END_STREAM */
4153 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4154 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004155 *
4156 * FIXME: what we do here is not correct because we send end_stream
4157 * before knowing if we'll have to send a HEADERS frame for the
4158 * trailers. More importantly we're not consuming the trailing CRLF
4159 * after the end of trailers, so it will be left to the caller to
4160 * eat it. The right way to do it would be to measure trailers here
4161 * and to send ES only if there are no trailers.
4162 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004163 */
4164 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004165 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004166 es_now = 1;
4167
4168 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004169 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004170
4171 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004172 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004173
4174 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004175 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004176
4177 /* consume incoming H1 response */
4178 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004179 max -= size;
4180 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004181 total += size;
4182 h1m->curr_len -= size;
4183 h2s->mws -= size;
4184 h2c->mws -= size;
4185
4186 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004187 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004188 goto new_frame;
4189 }
4190 }
4191
4192 if (es_now) {
4193 if (h2s->st == H2_SS_OPEN)
4194 h2s->st = H2_SS_HLOC;
4195 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004196 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004197
Willy Tarreau35a62702018-02-27 15:37:25 +01004198 if (!(h1m->flags & H1_MF_CHNK)) {
4199 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004200 total += max;
4201 ofs += max;
4202 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004203
Willy Tarreau801250e2018-09-11 11:45:04 +02004204 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004205 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004206
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004207 h2s->flags |= H2_SF_ES_SENT;
4208 }
4209
4210 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004211 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 +02004212 return total;
4213}
4214
Willy Tarreau115e83b2018-12-01 19:17:53 +01004215/* Try to send a HEADERS frame matching HTX response present in HTX message
4216 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4217 * must check the stream's status to detect any error which might have happened
4218 * subsequently to a successful send. The htx blocks are automatically removed
4219 * from the message. The htx message is assumed to be valid since produced from
4220 * the internal code, hence it contains a start line, an optional series of
4221 * header blocks and an end of header, otherwise an invalid frame could be
4222 * emitted and the resulting htx message could be left in an inconsistent state.
4223 */
4224static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4225{
4226 struct http_hdr list[MAX_HTTP_HDR];
4227 struct h2c *h2c = h2s->h2c;
4228 struct htx_blk *blk;
4229 struct htx_blk *blk_end;
4230 struct buffer outbuf;
4231 struct htx_sl *sl;
4232 enum htx_blk_type type;
4233 int es_now = 0;
4234 int ret = 0;
4235 int hdr;
4236 int idx;
4237
4238 if (h2c_mux_busy(h2c, h2s)) {
4239 h2s->flags |= H2_SF_BLK_MBUSY;
4240 return 0;
4241 }
4242
4243 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4244 h2c->flags |= H2_CF_MUX_MALLOC;
4245 h2s->flags |= H2_SF_BLK_MROOM;
4246 return 0;
4247 }
4248
4249 /* determine the first block which must not be deleted, blk_end may
4250 * be NULL if all blocks have to be deleted.
4251 */
4252 idx = htx_get_head(htx);
4253 blk_end = NULL;
4254 while (idx != -1) {
4255 type = htx_get_blk_type(htx_get_blk(htx, idx));
4256 idx = htx_get_next(htx, idx);
4257 if (type == HTX_BLK_EOH) {
4258 if (idx != -1)
4259 blk_end = htx_get_blk(htx, idx);
4260 break;
4261 }
4262 }
4263
4264 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004265 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004266 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004267 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004268 if (h2s->status < 100 || h2s->status > 999)
4269 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004270
4271 /* and the rest of the headers, that we dump starting at header 0 */
4272 hdr = 0;
4273
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004274 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004275 while ((idx = htx_get_next(htx, idx)) != -1) {
4276 blk = htx_get_blk(htx, idx);
4277 type = htx_get_blk_type(blk);
4278
4279 if (type == HTX_BLK_UNUSED)
4280 continue;
4281
4282 if (type != HTX_BLK_HDR)
4283 break;
4284
4285 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4286 goto fail;
4287
4288 list[hdr].n = htx_get_blk_name(htx, blk);
4289 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004290 hdr++;
4291 }
4292
4293 /* marker for end of headers */
4294 list[hdr].n = ist("");
4295
4296 if (h2s->status == 204 || h2s->status == 304) {
4297 /* no contents, claim c-len is present and set to zero */
4298 es_now = 1;
4299 }
4300
4301 chunk_reset(&outbuf);
4302
4303 while (1) {
4304 outbuf.area = b_tail(&h2c->mbuf);
4305 outbuf.size = b_contig_space(&h2c->mbuf);
4306 outbuf.data = 0;
4307
4308 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4309 break;
4310 realign_again:
4311 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4312 }
4313
4314 if (outbuf.size < 9)
4315 goto full;
4316
4317 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4318 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4319 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4320 outbuf.data = 9;
4321
4322 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004323 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004324 if (b_space_wraps(&h2c->mbuf))
4325 goto realign_again;
4326 goto full;
4327 }
4328
4329 /* encode all headers, stop at empty name */
4330 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4331 /* these ones do not exist in H2 and must be dropped. */
4332 if (isteq(list[hdr].n, ist("connection")) ||
4333 isteq(list[hdr].n, ist("proxy-connection")) ||
4334 isteq(list[hdr].n, ist("keep-alive")) ||
4335 isteq(list[hdr].n, ist("upgrade")) ||
4336 isteq(list[hdr].n, ist("transfer-encoding")))
4337 continue;
4338
4339 if (isteq(list[hdr].n, ist("")))
4340 break; // end
4341
4342 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4343 /* output full */
4344 if (b_space_wraps(&h2c->mbuf))
4345 goto realign_again;
4346 goto full;
4347 }
4348 }
4349
Christopher Faulet0b465482019-02-19 15:14:23 +01004350 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004351 * FIXME: we should also set it when we know for sure that the
4352 * content-length is zero as well as on 204/304
4353 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004354 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4355 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004356 es_now = 1;
4357
Willy Tarreau927b88b2019-03-04 08:03:25 +01004358 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004359 es_now = 1;
4360
4361 /* update the frame's size */
4362 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4363
4364 if (es_now)
4365 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4366
4367 /* commit the H2 response */
4368 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004369
4370 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4371 * 1xx responses, another HEADERS frame is expected.
4372 */
4373 if (h2s->status >= 200 || h2s->status == 101)
4374 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004375
Willy Tarreau115e83b2018-12-01 19:17:53 +01004376 if (es_now) {
4377 h2s->flags |= H2_SF_ES_SENT;
4378 if (h2s->st == H2_SS_OPEN)
4379 h2s->st = H2_SS_HLOC;
4380 else
4381 h2s_close(h2s);
4382 }
4383
4384 /* OK we could properly deliver the response */
4385
4386 /* remove all header blocks including the EOH and compute the
4387 * corresponding size.
4388 *
4389 * FIXME: We should remove everything when es_now is set.
4390 */
4391 ret = 0;
4392 idx = htx_get_head(htx);
4393 blk = htx_get_blk(htx, idx);
4394 while (blk != blk_end) {
4395 ret += htx_get_blksz(blk);
4396 blk = htx_remove_blk(htx, blk);
4397 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004398
4399 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4400 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004401 end:
4402 return ret;
4403 full:
4404 h2c->flags |= H2_CF_MUX_MFULL;
4405 h2s->flags |= H2_SF_BLK_MROOM;
4406 ret = 0;
4407 goto end;
4408 fail:
4409 /* unparsable HTX messages, too large ones to be produced in the local
4410 * list etc go here (unrecoverable errors).
4411 */
4412 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4413 ret = 0;
4414 goto end;
4415}
4416
Willy Tarreau80739692018-10-05 11:35:57 +02004417/* Try to send a HEADERS frame matching HTX request present in HTX message
4418 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4419 * must check the stream's status to detect any error which might have happened
4420 * subsequently to a successful send. The htx blocks are automatically removed
4421 * from the message. The htx message is assumed to be valid since produced from
4422 * the internal code, hence it contains a start line, an optional series of
4423 * header blocks and an end of header, otherwise an invalid frame could be
4424 * emitted and the resulting htx message could be left in an inconsistent state.
4425 */
4426static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4427{
4428 struct http_hdr list[MAX_HTTP_HDR];
4429 struct h2c *h2c = h2s->h2c;
4430 struct htx_blk *blk;
4431 struct htx_blk *blk_end;
4432 struct buffer outbuf;
4433 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004434 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004435 enum htx_blk_type type;
4436 int es_now = 0;
4437 int ret = 0;
4438 int hdr;
4439 int idx;
4440
4441 if (h2c_mux_busy(h2c, h2s)) {
4442 h2s->flags |= H2_SF_BLK_MBUSY;
4443 return 0;
4444 }
4445
4446 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4447 h2c->flags |= H2_CF_MUX_MALLOC;
4448 h2s->flags |= H2_SF_BLK_MROOM;
4449 return 0;
4450 }
4451
4452 /* determine the first block which must not be deleted, blk_end may
4453 * be NULL if all blocks have to be deleted.
4454 */
4455 idx = htx_get_head(htx);
4456 blk_end = NULL;
4457 while (idx != -1) {
4458 type = htx_get_blk_type(htx_get_blk(htx, idx));
4459 idx = htx_get_next(htx, idx);
4460 if (type == HTX_BLK_EOH) {
4461 if (idx != -1)
4462 blk_end = htx_get_blk(htx, idx);
4463 break;
4464 }
4465 }
4466
4467 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004468 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004469 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004470 meth = htx_sl_req_meth(sl);
4471 path = htx_sl_req_uri(sl);
4472
4473 /* and the rest of the headers, that we dump starting at header 0 */
4474 hdr = 0;
4475
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004476 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004477 while ((idx = htx_get_next(htx, idx)) != -1) {
4478 blk = htx_get_blk(htx, idx);
4479 type = htx_get_blk_type(blk);
4480
4481 if (type == HTX_BLK_UNUSED)
4482 continue;
4483
4484 if (type != HTX_BLK_HDR)
4485 break;
4486
4487 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4488 goto fail;
4489
4490 list[hdr].n = htx_get_blk_name(htx, blk);
4491 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004492 hdr++;
4493 }
4494
4495 /* marker for end of headers */
4496 list[hdr].n = ist("");
4497
4498 chunk_reset(&outbuf);
4499
4500 while (1) {
4501 outbuf.area = b_tail(&h2c->mbuf);
4502 outbuf.size = b_contig_space(&h2c->mbuf);
4503 outbuf.data = 0;
4504
4505 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4506 break;
4507 realign_again:
4508 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4509 }
4510
4511 if (outbuf.size < 9)
4512 goto full;
4513
4514 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4515 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4516 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4517 outbuf.data = 9;
4518
4519 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004520 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004521 if (b_space_wraps(&h2c->mbuf))
4522 goto realign_again;
4523 goto full;
4524 }
4525
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004526 /* RFC7540 #8.3: the CONNECT method must have :
4527 * - :authority set to the URI part (host:port)
4528 * - :method set to CONNECT
4529 * - :scheme and :path omitted
4530 */
4531 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4532 /* encode the scheme which is always "https" (or 0x86 for "http") */
4533 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4534 /* output full */
4535 if (b_space_wraps(&h2c->mbuf))
4536 goto realign_again;
4537 goto full;
4538 }
Willy Tarreau80739692018-10-05 11:35:57 +02004539
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004540 /* encode the path, which necessarily is the second one */
4541 if (!hpack_encode_path(&outbuf, path)) {
4542 /* output full */
4543 if (b_space_wraps(&h2c->mbuf))
4544 goto realign_again;
4545 goto full;
4546 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004547
4548 /* look for the Host header and place it in :authority */
4549 auth = ist2(NULL, 0);
4550 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4551 if (isteq(list[hdr].n, ist("")))
4552 break; // end
4553
4554 if (isteq(list[hdr].n, ist("host"))) {
4555 auth = list[hdr].v;
4556 break;
4557 }
4558 }
4559 }
4560 else {
4561 /* for CONNECT, :authority is taken from the path */
4562 auth = path;
4563 }
4564
4565 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4566 /* output full */
4567 if (b_space_wraps(&h2c->mbuf))
4568 goto realign_again;
4569 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004570 }
4571
4572 /* encode all headers, stop at empty name */
4573 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4574 /* these ones do not exist in H2 and must be dropped. */
4575 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004576 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004577 isteq(list[hdr].n, ist("proxy-connection")) ||
4578 isteq(list[hdr].n, ist("keep-alive")) ||
4579 isteq(list[hdr].n, ist("upgrade")) ||
4580 isteq(list[hdr].n, ist("transfer-encoding")))
4581 continue;
4582
4583 if (isteq(list[hdr].n, ist("")))
4584 break; // end
4585
4586 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4587 /* output full */
4588 if (b_space_wraps(&h2c->mbuf))
4589 goto realign_again;
4590 goto full;
4591 }
4592 }
4593
4594 /* we may need to add END_STREAM if we have no body :
4595 * - request already closed, or :
4596 * - no transfer-encoding, and :
4597 * - no content-length or content-length:0
4598 * Fixme: this doesn't take into account CONNECT requests.
4599 */
4600 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4601 es_now = 1;
4602
4603 if (sl->flags & HTX_SL_F_BODYLESS)
4604 es_now = 1;
4605
Willy Tarreau927b88b2019-03-04 08:03:25 +01004606 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004607 es_now = 1;
4608
4609 /* update the frame's size */
4610 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4611
4612 if (es_now)
4613 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4614
4615 /* commit the H2 response */
4616 b_add(&h2c->mbuf, outbuf.data);
4617 h2s->flags |= H2_SF_HEADERS_SENT;
4618 h2s->st = H2_SS_OPEN;
4619
Willy Tarreau80739692018-10-05 11:35:57 +02004620 if (es_now) {
4621 // trim any possibly pending data (eg: inconsistent content-length)
4622 h2s->flags |= H2_SF_ES_SENT;
4623 h2s->st = H2_SS_HLOC;
4624 }
4625
4626 /* remove all header blocks including the EOH and compute the
4627 * corresponding size.
4628 *
4629 * FIXME: We should remove everything when es_now is set.
4630 */
4631 ret = 0;
4632 idx = htx_get_head(htx);
4633 blk = htx_get_blk(htx, idx);
4634 while (blk != blk_end) {
4635 ret += htx_get_blksz(blk);
4636 blk = htx_remove_blk(htx, blk);
4637 }
4638
4639 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4640 htx_remove_blk(htx, blk_end);
4641
4642 end:
4643 return ret;
4644 full:
4645 h2c->flags |= H2_CF_MUX_MFULL;
4646 h2s->flags |= H2_SF_BLK_MROOM;
4647 ret = 0;
4648 goto end;
4649 fail:
4650 /* unparsable HTX messages, too large ones to be produced in the local
4651 * list etc go here (unrecoverable errors).
4652 */
4653 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4654 ret = 0;
4655 goto end;
4656}
4657
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004658/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004659 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4660 * caller must check the stream's status to detect any error which might have
4661 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004662 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4663 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004664static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004665{
4666 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004667 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004668 struct buffer outbuf;
4669 size_t total = 0;
4670 int es_now = 0;
4671 int bsize; /* htx block size */
4672 int fsize; /* h2 frame size */
4673 struct htx_blk *blk;
4674 enum htx_blk_type type;
4675 int idx;
4676
4677 if (h2c_mux_busy(h2c, h2s)) {
4678 h2s->flags |= H2_SF_BLK_MBUSY;
4679 goto end;
4680 }
4681
4682 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4683 h2c->flags |= H2_CF_MUX_MALLOC;
4684 h2s->flags |= H2_SF_BLK_MROOM;
4685 goto end;
4686 }
4687
Willy Tarreau98de12a2018-12-12 07:03:00 +01004688 htx = htx_from_buf(buf);
4689
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004690 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4691 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4692 * the caller to handle.
4693 */
4694
4695 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004696 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004697 goto end;
4698
4699 idx = htx_get_head(htx);
4700 blk = htx_get_blk(htx, idx);
4701 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4702 bsize = htx_get_blksz(blk);
4703 fsize = bsize;
4704
4705 if (type == HTX_BLK_EOD) {
4706 /* if we have an EOD, we're dealing with chunked data. We may
4707 * have a set of trailers after us that the caller will want to
4708 * deal with. Let's simply remove the EOD and return.
4709 */
4710 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004711 total++; // EOD counts as one byte
4712 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004713 goto end;
4714 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004715 else if (type == HTX_BLK_EOM) {
4716 if (h2s->flags & H2_SF_ES_SENT) {
4717 /* ES already sent */
4718 htx_remove_blk(htx, blk);
4719 total++; // EOM counts as one byte
4720 count--;
4721 goto end;
4722 }
4723 }
4724 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004725 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004726
4727 /* Perform some optimizations to reduce the number of buffer copies.
4728 * First, if the mux's buffer is empty and the htx area contains
4729 * exactly one data block of the same size as the requested count, and
4730 * this count fits within the frame size, the stream's window size, and
4731 * the connection's window size, then it's possible to simply swap the
4732 * caller's buffer with the mux's output buffer and adjust offsets and
4733 * length to match the entire DATA HTX block in the middle. In this
4734 * case we perform a true zero-copy operation from end-to-end. This is
4735 * the situation that happens all the time with large files. Second, if
4736 * this is not possible, but the mux's output buffer is empty, we still
4737 * have an opportunity to avoid the copy to the intermediary buffer, by
4738 * making the intermediary buffer's area point to the output buffer's
4739 * area. In this case we want to skip the HTX header to make sure that
4740 * copies remain aligned and that this operation remains possible all
4741 * the time. This goes for headers, data blocks and any data extracted
4742 * from the HTX blocks.
4743 */
4744 if (unlikely(fsize == count &&
4745 htx->used == 1 && type == HTX_BLK_DATA &&
4746 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4747 void *old_area = h2c->mbuf.area;
4748
4749 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004750 /* Too bad there are data left there. We're willing to memcpy/memmove
4751 * up to 1/4 of the buffer, which means that it's OK to copy a large
4752 * frame into a buffer containing few data if it needs to be realigned,
4753 * and that it's also OK to copy few data without realigning. Otherwise
4754 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004755 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004756 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4757 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4758 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004759 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004760
Willy Tarreau98de12a2018-12-12 07:03:00 +01004761 h2c->flags |= H2_CF_MUX_MFULL;
4762 h2s->flags |= H2_SF_BLK_MROOM;
4763 goto end;
4764 }
4765
4766 /* map an H2 frame to the HTX block so that we can put the
4767 * frame header there.
4768 */
4769 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004770 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004771 h2c->mbuf.data = fsize + 9;
4772 outbuf.area = b_head(&h2c->mbuf);
4773
4774 /* prepend an H2 DATA frame header just before the DATA block */
4775 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4776 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4777 h2_set_frame_size(outbuf.area, fsize);
4778
4779 /* update windows */
4780 h2s->mws -= fsize;
4781 h2c->mws -= fsize;
4782
4783 /* and exchange with our old area */
4784 buf->area = old_area;
4785 buf->data = buf->head = 0;
4786 total += fsize;
4787 goto end;
4788 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004789
Willy Tarreau98de12a2018-12-12 07:03:00 +01004790 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004791 /* for DATA and EOM we'll have to emit a frame, even if empty */
4792
4793 while (1) {
4794 outbuf.area = b_tail(&h2c->mbuf);
4795 outbuf.size = b_contig_space(&h2c->mbuf);
4796 outbuf.data = 0;
4797
4798 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4799 break;
4800 realign_again:
4801 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4802 }
4803
4804 if (outbuf.size < 9) {
4805 h2c->flags |= H2_CF_MUX_MFULL;
4806 h2s->flags |= H2_SF_BLK_MROOM;
4807 goto end;
4808 }
4809
4810 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4811 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4812 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4813 outbuf.data = 9;
4814
4815 /* we have in <fsize> the exact number of bytes we need to copy from
4816 * the HTX buffer. We need to check this against the connection's and
4817 * the stream's send windows, and to ensure that this fits in the max
4818 * frame size and in the buffer's available space minus 9 bytes (for
4819 * the frame header). The connection's flow control is applied last so
4820 * that we can use a separate list of streams which are immediately
4821 * unblocked on window opening. Note: we don't implement padding.
4822 */
4823
4824 /* EOM is presented with bsize==1 but would lead to the emission of an
4825 * empty frame, thus we force it to zero here.
4826 */
4827 if (type == HTX_BLK_EOM)
4828 bsize = fsize = 0;
4829
4830 if (!fsize)
4831 goto send_empty;
4832
4833 if (h2s->mws <= 0) {
4834 h2s->flags |= H2_SF_BLK_SFCTL;
4835 if (h2s->send_wait) {
4836 LIST_DEL(&h2s->list);
4837 LIST_INIT(&h2s->list);
4838 }
4839 goto end;
4840 }
4841
Willy Tarreauee573762018-12-04 15:25:57 +01004842 if (fsize > count)
4843 fsize = count;
4844
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004845 if (fsize > h2s->mws)
4846 fsize = h2s->mws; // >0
4847
4848 if (h2c->mfs && fsize > h2c->mfs)
4849 fsize = h2c->mfs; // >0
4850
4851 if (fsize + 9 > outbuf.size) {
4852 /* we have an opportunity for enlarging the too small
4853 * available space, let's try.
4854 * FIXME: is this really interesting to do? Maybe we'll
4855 * spend lots of time realigning instead of using two
4856 * frames.
4857 */
4858 if (b_space_wraps(&h2c->mbuf))
4859 goto realign_again;
4860 fsize = outbuf.size - 9;
4861
4862 if (fsize <= 0) {
4863 /* no need to send an empty frame here */
4864 h2c->flags |= H2_CF_MUX_MFULL;
4865 h2s->flags |= H2_SF_BLK_MROOM;
4866 goto end;
4867 }
4868 }
4869
4870 if (h2c->mws <= 0) {
4871 h2s->flags |= H2_SF_BLK_MFCTL;
4872 goto end;
4873 }
4874
4875 if (fsize > h2c->mws)
4876 fsize = h2c->mws;
4877
4878 /* now let's copy this this into the output buffer */
4879 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004880 h2s->mws -= fsize;
4881 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004882 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004883
4884 send_empty:
4885 /* update the frame's size */
4886 h2_set_frame_size(outbuf.area, fsize);
4887
4888 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4889 * meeting EOM. We should optimize this later.
4890 */
4891 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004892 total++; // EOM counts as one byte
4893 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004894 es_now = 1;
4895 }
4896
4897 if (es_now)
4898 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4899
4900 /* commit the H2 response */
4901 b_add(&h2c->mbuf, fsize + 9);
4902
4903 /* consume incoming HTX block, including EOM */
4904 total += fsize;
4905 if (fsize == bsize) {
4906 htx_remove_blk(htx, blk);
4907 if (fsize)
4908 goto new_frame;
4909 } else {
4910 /* we've truncated this block */
4911 htx_cut_data_blk(htx, blk, fsize);
4912 }
4913
4914 if (es_now) {
4915 if (h2s->st == H2_SS_OPEN)
4916 h2s->st = H2_SS_HLOC;
4917 else
4918 h2s_close(h2s);
4919
4920 h2s->flags |= H2_SF_ES_SENT;
4921 }
4922
4923 end:
4924 return total;
4925}
4926
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004927/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4928 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4929 * processed. The caller must check the stream's status to detect any error
4930 * which might have happened subsequently to a successful send. The htx blocks
4931 * are automatically removed from the message. The htx message is assumed to be
4932 * valid since produced from the internal code. Processing stops when meeting
4933 * the EOM, which is also removed. All trailers are processed at once and sent
4934 * as a single frame. The ES flag is always set.
4935 */
4936static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
4937{
4938 struct http_hdr list[MAX_HTTP_HDR];
4939 struct h2c *h2c = h2s->h2c;
4940 struct htx_blk *blk;
4941 struct htx_blk *blk_end;
4942 struct buffer outbuf;
4943 struct h1m h1m;
4944 enum htx_blk_type type;
4945 uint32_t size;
4946 int ret = 0;
4947 int hdr;
4948 int idx;
4949 void *start;
4950
4951 if (h2c_mux_busy(h2c, h2s)) {
4952 h2s->flags |= H2_SF_BLK_MBUSY;
4953 goto end;
4954 }
4955
4956 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4957 h2c->flags |= H2_CF_MUX_MALLOC;
4958 h2s->flags |= H2_SF_BLK_MROOM;
4959 goto end;
4960 }
4961
4962 /* The principle is that we parse each and every trailers block using
4963 * the H1 headers parser, and append it to the list. We don't proceed
4964 * until EOM is met. blk_end will point to the EOM block.
4965 */
4966 hdr = 0;
4967 memset(list, 0, sizeof(list));
4968 blk_end = NULL;
4969
4970 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
4971 blk = htx_get_blk(htx, idx);
4972 type = htx_get_blk_type(blk);
4973
4974 if (type == HTX_BLK_UNUSED)
4975 continue;
4976
4977 if (type != HTX_BLK_TLR) {
4978 if (type == HTX_BLK_EOM)
4979 blk_end = blk;
4980 break;
4981 }
4982
4983 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4984 goto fail;
4985
4986 size = htx_get_blksz(blk);
4987 start = htx_get_blk_ptr(htx, blk);
4988
4989 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
4990 h1m.err_pos = 0;
4991 ret = h1_headers_to_hdr_list(start, start + size,
4992 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
4993 &h1m, NULL);
4994 if (ret < 0)
4995 goto fail;
4996
4997 /* ret == 0 if an incomplete trailers block was found (missing
4998 * empty line), or > 0 if it was found. We have to continue on
4999 * incomplete messages because the trailers block might be
5000 * incomplete.
5001 */
5002
5003 /* search the new end */
5004 while (hdr <= sizeof(list)/sizeof(list[0])) {
5005 if (!list[hdr].n.len)
5006 break;
5007 hdr++;
5008 }
5009 }
5010
5011 if (!blk_end)
5012 goto end; // end not found yet
5013
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005014 chunk_reset(&outbuf);
5015
5016 while (1) {
5017 outbuf.area = b_tail(&h2c->mbuf);
5018 outbuf.size = b_contig_space(&h2c->mbuf);
5019 outbuf.data = 0;
5020
5021 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5022 break;
5023 realign_again:
5024 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5025 }
5026
5027 if (outbuf.size < 9)
5028 goto full;
5029
5030 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5031 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5032 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5033 outbuf.data = 9;
5034
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005035 /* encode all headers */
5036 for (idx = 0; idx < hdr; idx++) {
5037 /* these ones do not exist in H2 or must not appear in
5038 * trailers and must be dropped.
5039 */
5040 if (isteq(list[idx].n, ist("host")) ||
5041 isteq(list[idx].n, ist("content-length")) ||
5042 isteq(list[idx].n, ist("connection")) ||
5043 isteq(list[idx].n, ist("proxy-connection")) ||
5044 isteq(list[idx].n, ist("keep-alive")) ||
5045 isteq(list[idx].n, ist("upgrade")) ||
5046 isteq(list[idx].n, ist("te")) ||
5047 isteq(list[idx].n, ist("transfer-encoding")))
5048 continue;
5049
5050 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5051 /* output full */
5052 if (b_space_wraps(&h2c->mbuf))
5053 goto realign_again;
5054 goto full;
5055 }
5056 }
5057
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005058 if (!hdr) {
5059 /* here we have a problem, we've received an empty trailers
5060 * block followed by an EOM. Because of this we can't send a
5061 * HEADERS frame, so we have to cheat and instead send an empty
5062 * DATA frame conveying the ES flag.
5063 */
5064 outbuf.area[3] = H2_FT_DATA;
5065 outbuf.area[4] = H2_F_DATA_END_STREAM;
5066 }
5067
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005068 /* update the frame's size */
5069 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5070
5071 /* commit the H2 response */
5072 b_add(&h2c->mbuf, outbuf.data);
5073 h2s->flags |= H2_SF_ES_SENT;
5074
5075 if (h2s->st == H2_SS_OPEN)
5076 h2s->st = H2_SS_HLOC;
5077 else
5078 h2s_close(h2s);
5079
5080 /* OK we could properly deliver the response */
5081 done:
5082 /* remove all header blocks including EOM and compute the corresponding size. */
5083 ret = 0;
5084 idx = htx_get_head(htx);
5085 blk = htx_get_blk(htx, idx);
5086 while (blk != blk_end) {
5087 ret += htx_get_blksz(blk);
5088 blk = htx_remove_blk(htx, blk);
5089 }
5090 blk = htx_remove_blk(htx, blk);
5091 end:
5092 return ret;
5093 full:
5094 h2c->flags |= H2_CF_MUX_MFULL;
5095 h2s->flags |= H2_SF_BLK_MROOM;
5096 ret = 0;
5097 goto end;
5098 fail:
5099 /* unparsable HTX messages, too large ones to be produced in the local
5100 * list etc go here (unrecoverable errors).
5101 */
5102 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5103 ret = 0;
5104 goto end;
5105}
5106
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005107/* Called from the upper layer, to subscribe to events, such as being able to send.
5108 * The <param> argument here is supposed to be a pointer to a wait_event struct
5109 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5110 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5111 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5112 * returns 0 except for the error above.
5113 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005114static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5115{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005116 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005117 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005118 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005119
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005120 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005121 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005122 if (!(sw->events & SUB_RETRY_RECV)) {
5123 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005124 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005125 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005126 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005127 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005128 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005129 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005130 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005131 if (!(sw->events & SUB_RETRY_SEND)) {
5132 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005133 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005134 h2s->send_wait = sw;
5135 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
5136 if (h2s->flags & H2_SF_BLK_MFCTL)
5137 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5138 else
5139 LIST_ADDQ(&h2c->send_list, &h2s->list);
5140 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005141 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005142 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005143 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005144 if (event_type != 0)
5145 return -1;
5146 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005147}
5148
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005149/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5150 * The <param> argument here is supposed to be a pointer to the same wait_event
5151 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5152 * It always returns zero.
5153 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005154static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5155{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005156 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005157 struct h2s *h2s = cs->ctx;
5158
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005159 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005160 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005161 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005162 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005163 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005164 }
5165 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005166 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005167 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005168 if (h2s->send_wait == sw) {
5169 LIST_DEL(&h2s->list);
5170 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005171 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005172 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005173 }
5174 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005175 if (event_type & SUB_CALL_UNSUBSCRIBE) {
5176 sw = param;
5177 if (h2s->send_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005178 sw->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005179 h2s->send_wait = NULL;
Olivier Houchardf29cd5c2018-12-20 11:56:28 +01005180 LIST_DEL(&h2s->list);
5181 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005182 }
5183 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005184 return 0;
5185}
5186
5187
Olivier Houchard511efea2018-08-16 15:30:32 +02005188/* Called from the upper layer, to receive data */
5189static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5190{
Olivier Houchard638b7992018-08-16 15:41:52 +02005191 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005192 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005193 struct htx *h2s_htx = NULL;
5194 struct htx *buf_htx = NULL;
5195 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005196 size_t ret = 0;
5197
5198 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005199 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5200 /* in HTX mode we ignore the count argument */
5201 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005202 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005203 /* Here htx_to_buf() will set buffer data to 0 because
5204 * the HTX is empty.
5205 */
5206 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005207 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005208 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005209
5210 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005211 count = htx_free_data_space(buf_htx);
5212 if (flags & CO_RFL_KEEP_RSV) {
5213 if (count <= global.tune.maxrewrite)
5214 goto end;
5215 count -= global.tune.maxrewrite;
5216 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005217
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005218 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005219
5220 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5221 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5222
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005223 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005224 htx_to_buf(buf_htx, buf);
5225 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005226 ret = htx_ret.ret;
5227 }
5228 else {
5229 ret = b_xfer(buf, &h2s->rxbuf, count);
5230 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005231
Christopher Faulet37070b22019-02-14 15:12:14 +01005232 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005233 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005234 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005235 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005236 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005237 if (cs->flags & CS_FL_REOS)
5238 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005239 if (cs->flags & CS_FL_ERR_PENDING)
5240 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005241 if (b_size(&h2s->rxbuf)) {
5242 b_free(&h2s->rxbuf);
5243 offer_buffers(NULL, tasks_run_queue);
5244 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005245 }
5246
Willy Tarreau082f5592018-11-25 08:03:32 +01005247 if (ret && h2c->dsi == h2s->id) {
5248 /* demux is blocking on this stream's buffer */
5249 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau872e2fa2019-01-03 08:27:41 +01005250 h2c_restart_reading(h2c);
Willy Tarreau082f5592018-11-25 08:03:32 +01005251 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005252
Olivier Houchard511efea2018-08-16 15:30:32 +02005253 return ret;
5254}
5255
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005256/* stops all senders of this connection for example when the mux buffer is full.
5257 * They are moved from the sending_list to either fctl_list or send_list.
5258 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005259static void h2_stop_senders(struct h2c *h2c)
5260{
5261 struct h2s *h2s, *h2s_back;
5262
Olivier Houchardd360ac62019-03-22 17:37:16 +01005263 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5264 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005265 task_remove_from_task_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005266 h2s->send_wait->events |= SUB_RETRY_SEND;
5267 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd846c262018-10-19 17:24:29 +02005268 }
5269}
5270
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005271/* Called from the upper layer, to send data from buffer <buf> for no more than
5272 * <count> bytes. Returns the number of bytes effectively sent. Some status
5273 * flags may be updated on the conn_stream.
5274 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005275static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005276{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005277 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005278 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005279 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005280 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005281 struct htx *htx;
5282 struct htx_blk *blk;
5283 enum htx_blk_type btype;
5284 uint32_t bsize;
5285 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005286
Olivier Houchardd360ac62019-03-22 17:37:16 +01005287 /* If we were not just woken because we wanted to send but couldn't,
5288 * and there's somebody else that is waiting to send, do nothing,
5289 * we will subscribe later and be put at the end of the list
5290 */
5291 LIST_DEL_INIT(&h2s->sending_list);
5292 if ((!(h2s->send_wait) || !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) &&
5293 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5294 return 0;
5295
Olivier Houchardd846c262018-10-19 17:24:29 +02005296 if (h2s->send_wait) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005297 /* We want to stay in the send_list, so prepare ourself to be
5298 * eventually recalled if needed, and only remove ourself from
5299 * the list if we managed to send anything.
5300 */
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005301 h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE;
Olivier Houchardd360ac62019-03-22 17:37:16 +01005302 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005303 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005304 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5305 return 0;
5306
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005307 /* htx will be enough to decide if we're using HTX or legacy */
5308 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5309
Willy Tarreau0bad0432018-06-14 16:54:01 +02005310 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005311 h2s->flags |= H2_SF_OUTGOING_DATA;
5312
Willy Tarreau751f2d02018-10-05 09:35:00 +02005313 if (h2s->id == 0) {
5314 int32_t id = h2c_get_next_sid(h2s->h2c);
5315
5316 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005317 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005318 return 0;
5319 }
5320
5321 eb32_delete(&h2s->by_id);
5322 h2s->by_id.key = h2s->id = id;
5323 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005324 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005325 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5326 }
5327
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005328 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01005329 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
5330 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005331 idx = htx_get_head(htx);
5332 blk = htx_get_blk(htx, idx);
5333 btype = htx_get_blk_type(blk);
5334 bsize = htx_get_blksz(blk);
5335
5336 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005337 case HTX_BLK_REQ_SL:
5338 /* start-line before headers */
5339 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5340 if (ret > 0) {
5341 total += ret;
5342 count -= ret;
5343 if (ret < bsize)
5344 goto done;
5345 }
5346 break;
5347
Willy Tarreau115e83b2018-12-01 19:17:53 +01005348 case HTX_BLK_RES_SL:
5349 /* start-line before headers */
5350 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5351 if (ret > 0) {
5352 total += ret;
5353 count -= ret;
5354 if (ret < bsize)
5355 goto done;
5356 }
5357 break;
5358
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005359 case HTX_BLK_DATA:
5360 case HTX_BLK_EOD:
5361 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005362 /* all these cause the emission of a DATA frame (possibly empty).
5363 * This EOM necessarily is one before trailers, as the EOM following
5364 * trailers would have been consumed by the trailers parser.
5365 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005366 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005367 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005368 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005369 total += ret;
5370 count -= ret;
5371 if (ret < bsize)
5372 goto done;
5373 }
5374 break;
5375
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005376 case HTX_BLK_TLR:
5377 /* This is the first trailers block, all the subsequent ones AND
5378 * the EOM will be swallowed by the parser.
5379 */
5380 ret = h2s_htx_make_trailers(h2s, htx);
5381 if (ret > 0) {
5382 total += ret;
5383 count -= ret;
5384 if (ret < bsize)
5385 goto done;
5386 }
5387 break;
5388
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005389 default:
5390 htx_remove_blk(htx, blk);
5391 total += bsize;
5392 count -= bsize;
5393 break;
5394 }
5395 }
5396 goto done;
5397 }
5398
5399 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005400 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005401 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005402 if (h2s->h2c->flags & H2_CF_IS_BACK)
5403 ret = -1;
5404 else
5405 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005406 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005407 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005408 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005409 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005410 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005411 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005412 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005413
Willy Tarreau5dd17352018-06-14 13:33:30 +02005414 if (unlikely((int)ret <= 0)) {
5415 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005416 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5417 break;
5418 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005419 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005420 total += count;
5421 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005422 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005423 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005424 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005425 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005426 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005427 break;
5428 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005429
5430 total += ret;
5431 count -= ret;
5432
5433 if (h2s->st >= H2_SS_ERROR)
5434 break;
5435
5436 if (h2s->flags & H2_SF_BLK_ANY)
5437 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005438 }
5439
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005440 done:
Willy Tarreau00610962018-07-19 10:58:28 +02005441 if (h2s->st >= H2_SS_ERROR) {
5442 /* trim any possibly pending data after we close (extra CR-LF,
5443 * unprocessed trailers, abnormal extra data, ...)
5444 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005445 total += count;
5446 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005447 }
5448
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005449 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005450 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005451 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005452 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005453 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005454 }
5455
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005456 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005457 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005458 } else {
5459 b_del(buf, total);
5460 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005461
5462 /* The mux is full, cancel the pending tasks */
5463 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5464 (h2s->flags & H2_SF_BLK_MBUSY))
5465 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005466
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005467 /* If we're running HTX, and we read the whole buffer, then pretend
5468 * we read exactly what the caller specified, as with HTX the caller
5469 * will always give the buffer size, instead of the amount of data
5470 * available.
5471 */
5472 if (htx && !b_data(buf))
5473 total = orig_count;
5474
Olivier Houchard7505f942018-08-21 18:10:44 +02005475 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005476 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005477 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005478
Olivier Houchard7505f942018-08-21 18:10:44 +02005479 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005480 /* If we're waiting for flow control, and we got a shutr on the
5481 * connection, we will never be unlocked, so add an error on
5482 * the conn_stream.
5483 */
5484 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5485 !b_data(&h2s->h2c->dbuf) &&
5486 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5487 if (cs->flags & CS_FL_EOS)
5488 cs->flags |= CS_FL_ERROR;
5489 else
5490 cs->flags |= CS_FL_ERR_PENDING;
5491 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01005492 if (total > 0 && h2s->send_wait) {
5493 /* Ok we managed to send something, leave the send_list */
5494 h2s->send_wait->events &= ~SUB_RETRY_SEND;
5495 h2s->send_wait = NULL;
5496 LIST_DEL_INIT(&h2s->list);
5497 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005498 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005499}
5500
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005501/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005502static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005503{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005504 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005505 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005506 struct eb32_node *node;
5507 int fctl_cnt = 0;
5508 int send_cnt = 0;
5509 int tree_cnt = 0;
5510 int orph_cnt = 0;
5511
5512 if (!h2c)
5513 return;
5514
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005515 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005516 fctl_cnt++;
5517
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005518 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005519 send_cnt++;
5520
Willy Tarreau3af37712018-12-18 14:34:41 +01005521 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005522 node = eb32_first(&h2c->streams_by_id);
5523 while (node) {
5524 h2s = container_of(node, struct h2s, by_id);
5525 tree_cnt++;
5526 if (!h2s->cs)
5527 orph_cnt++;
5528 node = eb32_next(node);
5529 }
5530
Willy Tarreau987c0632018-12-18 10:32:05 +01005531 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5532 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5533 " .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 +02005534 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5535 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005536 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005537 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5538 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5539 h2c->msi,
5540 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5541 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5542
5543 if (h2s) {
5544 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5545 h2s, h2s->id, h2s->flags,
5546 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5547 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5548 h2s->cs);
5549 if (h2s->cs)
5550 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5551 h2s->cs->flags, h2s->cs->data);
5552 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005553}
Willy Tarreau62f52692017-10-08 23:01:42 +02005554
5555/*******************************************************/
5556/* functions below are dedicated to the config parsers */
5557/*******************************************************/
5558
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005559/* config parser for global "tune.h2.header-table-size" */
5560static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5561 struct proxy *defpx, const char *file, int line,
5562 char **err)
5563{
5564 if (too_many_args(1, args, err, NULL))
5565 return -1;
5566
5567 h2_settings_header_table_size = atoi(args[1]);
5568 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5569 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5570 return -1;
5571 }
5572 return 0;
5573}
Willy Tarreau62f52692017-10-08 23:01:42 +02005574
Willy Tarreaue6baec02017-07-27 11:45:11 +02005575/* config parser for global "tune.h2.initial-window-size" */
5576static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5577 struct proxy *defpx, const char *file, int line,
5578 char **err)
5579{
5580 if (too_many_args(1, args, err, NULL))
5581 return -1;
5582
5583 h2_settings_initial_window_size = atoi(args[1]);
5584 if (h2_settings_initial_window_size < 0) {
5585 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5586 return -1;
5587 }
5588 return 0;
5589}
5590
Willy Tarreau5242ef82017-07-27 11:47:28 +02005591/* config parser for global "tune.h2.max-concurrent-streams" */
5592static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5593 struct proxy *defpx, const char *file, int line,
5594 char **err)
5595{
5596 if (too_many_args(1, args, err, NULL))
5597 return -1;
5598
5599 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005600 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005601 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5602 return -1;
5603 }
5604 return 0;
5605}
5606
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005607/* config parser for global "tune.h2.max-frame-size" */
5608static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5609 struct proxy *defpx, const char *file, int line,
5610 char **err)
5611{
5612 if (too_many_args(1, args, err, NULL))
5613 return -1;
5614
5615 h2_settings_max_frame_size = atoi(args[1]);
5616 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5617 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5618 return -1;
5619 }
5620 return 0;
5621}
5622
Willy Tarreau62f52692017-10-08 23:01:42 +02005623
5624/****************************************/
5625/* MUX initialization and instanciation */
5626/***************************************/
5627
5628/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005629static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005630 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005631 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005632 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005633 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005634 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005635 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005636 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005637 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005638 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005639 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005640 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005641 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005642 .shutr = h2_shutr,
5643 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005644 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005645 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005646 .name = "H2",
5647};
5648
Christopher Faulet32f61c02018-04-10 14:33:41 +02005649/* PROTO selection : this mux registers PROTO token "h2" */
5650static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005651 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005652
Willy Tarreau0108d902018-11-25 19:14:37 +01005653INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5654
Willy Tarreauf8957272018-10-03 10:25:20 +02005655static struct mux_proto_list mux_proto_h2_htx =
5656 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
5657
5658INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5659
Willy Tarreau62f52692017-10-08 23:01:42 +02005660/* config keyword parsers */
5661static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005662 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005663 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005664 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005665 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005666 { 0, NULL, NULL }
5667}};
5668
Willy Tarreau0108d902018-11-25 19:14:37 +01005669INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);