Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | afba57a | 2018-12-11 13:44:24 +0100 | [diff] [blame] | 15 | #include <common/h1.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 16 | #include <common/h2.h> |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 17 | #include <common/hpack-dec.h> |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 18 | #include <common/hpack-enc.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 19 | #include <common/hpack-tbl.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 20 | #include <common/htx.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 21 | #include <common/initcall.h> |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 22 | #include <common/net_helper.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 23 | #include <proto/connection.h> |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 24 | #include <proto/http_htx.h> |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 25 | #include <proto/session.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 26 | #include <proto/stream.h> |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 27 | #include <proto/stream_interface.h> |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 28 | #include <types/session.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 29 | #include <eb32tree.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 30 | |
| 31 | |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 32 | /* dummy streams returned for closed, error, refused, idle and states */ |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 33 | static const struct h2s *h2_closed_stream; |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 34 | static const struct h2s *h2_error_stream; |
Willy Tarreau | 8d0d58b | 2018-12-23 18:29:12 +0100 | [diff] [blame] | 35 | static const struct h2s *h2_refused_stream; |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 36 | static const struct h2s *h2_idle_stream; |
| 37 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 38 | /* Connection flags (32 bit), in h2c->flags */ |
| 39 | #define H2_CF_NONE 0x00000000 |
| 40 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 41 | /* 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 Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 46 | /* 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 Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 51 | #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 Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 53 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 54 | #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 Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 58 | #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 Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 60 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 61 | /* other flags */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 62 | #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 Tarreau | b3fb56d | 2018-10-03 13:56:38 +0200 | [diff] [blame] | 65 | #define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 66 | #define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 67 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 68 | /* H2 connection state, in h2c->st0 */ |
| 69 | enum 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 Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 74 | 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 Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 76 | 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 */ |
| 82 | struct 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 Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 90 | uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 91 | 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 Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 97 | struct buffer dbuf; /* demux buffer */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 98 | |
| 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 Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 103 | uint8_t dpl; /* demux pad length (part of dfl), init to 0 */ |
| 104 | /* 8 bit hole here */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 105 | int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */ |
| 106 | |
| 107 | /* states for the mux direction */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 108 | struct buffer mbuf; /* mux buffer */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 109 | 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 Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 118 | int timeout; /* idle timeout duration in ticks */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 119 | int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */ |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 120 | unsigned int nb_streams; /* number of streams in the tree */ |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 121 | unsigned int nb_cs; /* number of attached conn_streams */ |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 122 | unsigned int nb_reserved; /* number of reserved streams */ |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 123 | unsigned int stream_cnt; /* total number of streams seen */ |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 124 | struct proxy *proxy; /* the proxy this connection was created for */ |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 125 | struct task *task; /* timeout management task */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 126 | 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 Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 129 | struct list sending_list; /* list of h2s scheduled to send data */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 130 | struct buffer_wait buf_wait; /* wait list for buffer allocations */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 131 | struct wait_event wait_event; /* To be used if we're waiting for I/Os */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 132 | }; |
| 133 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 134 | /* H2 stream state, in h2s->st */ |
| 135 | enum 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 Tarreau | 96060ba | 2017-10-16 18:34:34 +0200 | [diff] [blame] | 142 | H2_SS_ERROR, // an error needs to be sent using RST_STREAM |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 143 | 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 Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 155 | /* 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 Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 162 | /* 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 Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 176 | #define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 177 | #define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 178 | |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 179 | #define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream |
| 180 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 181 | /* 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 | */ |
| 184 | struct h2s { |
| 185 | struct conn_stream *cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 186 | struct session *sess; |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 187 | struct h2c *h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 188 | struct h1m h1m; /* request or response parser state for H1 */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 189 | struct eb32_node by_id; /* place in h2c's streams_by_id */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 190 | 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 Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 195 | uint16_t status; /* HTTP response status */ |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 196 | unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */ |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 197 | struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 198 | struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */ |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 199 | 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 Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 201 | struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 202 | struct list sending_list; /* To be used when adding in h2c->sending_list */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 203 | }; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 204 | |
Willy Tarreau | c640514 | 2017-09-21 20:23:50 +0200 | [diff] [blame] | 205 | /* descriptor for an h2 frame header */ |
| 206 | struct 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 Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 213 | /* the h2c connection pool */ |
| 214 | DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c)); |
| 215 | |
| 216 | /* the h2s stream pool */ |
| 217 | DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s)); |
| 218 | |
Willy Tarreau | dc57236 | 2018-12-12 08:08:05 +0100 | [diff] [blame] | 219 | /* 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 Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 226 | /* a few settings from the global section */ |
| 227 | static int h2_settings_header_table_size = 4096; /* initial value */ |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 228 | static int h2_settings_initial_window_size = 65535; /* initial value */ |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 229 | static unsigned int h2_settings_max_concurrent_streams = 100; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 230 | static int h2_settings_max_frame_size = 0; /* unset */ |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 231 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 232 | /* a dmumy closed stream */ |
| 233 | static const struct h2s *h2_closed_stream = &(const struct h2s){ |
| 234 | .cs = NULL, |
| 235 | .h2c = NULL, |
| 236 | .st = H2_SS_CLOSED, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 237 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 238 | .flags = H2_SF_RST_RCVD, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 239 | .id = 0, |
| 240 | }; |
| 241 | |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 242 | /* a dmumy closed stream returning a PROTOCOL_ERROR error */ |
| 243 | static 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 Tarreau | 8d0d58b | 2018-12-23 18:29:12 +0100 | [diff] [blame] | 252 | /* a dmumy closed stream returning a REFUSED_STREAM error */ |
| 253 | static 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 Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 262 | /* and a dummy idle stream for use with any unannounced stream */ |
| 263 | static const struct h2s *h2_idle_stream = &(const struct h2s){ |
| 264 | .cs = NULL, |
| 265 | .h2c = NULL, |
| 266 | .st = H2_SS_IDLE, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 267 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 268 | .id = 0, |
| 269 | }; |
| 270 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 271 | static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state); |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 272 | static int h2_send(struct h2c *h2c); |
| 273 | static int h2_recv(struct h2c *h2c); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 274 | static int h2_process(struct h2c *h2c); |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 275 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state); |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 276 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id); |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 277 | static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len); |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 278 | static int h2_frt_transfer_data(struct h2s *h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 279 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state); |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 280 | static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess); |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 281 | static void h2s_alert(struct h2s *h2s); |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 282 | |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 283 | static __inline int |
| 284 | h2c_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 Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 299 | /*****************************************************/ |
| 300 | /* functions below are for dynamic buffer management */ |
| 301 | /*****************************************************/ |
| 302 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 303 | /* 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 Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 310 | * - 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 Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 317 | * - otherwise must may not attempt |
| 318 | */ |
| 319 | static inline int h2_recv_allowed(const struct h2c *h2c) |
| 320 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 321 | if (b_data(&h2c->dbuf) == 0 && |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 322 | (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 Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 328 | !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 329 | return 1; |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 334 | /* restarts reading on the connection if it was not enabled */ |
| 335 | static inline void h2c_restart_reading(const struct h2c *h2c) |
| 336 | { |
| 337 | if (!h2_recv_allowed(h2c)) |
| 338 | return; |
Willy Tarreau | 872e2fa | 2019-01-03 08:27:41 +0100 | [diff] [blame] | 339 | if (!b_data(&h2c->dbuf) && (h2c->wait_event.events & SUB_RETRY_RECV)) |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 340 | return; |
| 341 | tasklet_wakeup(h2c->wait_event.task); |
| 342 | } |
| 343 | |
| 344 | |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 345 | /* returns true if the front connection has too many conn_streams attached */ |
| 346 | static inline int h2_frt_has_too_many_cs(const struct h2c *h2c) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 347 | { |
Willy Tarreau | a875466 | 2018-12-23 20:43:58 +0100 | [diff] [blame] | 348 | return h2c->nb_cs > h2_settings_max_concurrent_streams; |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 349 | } |
| 350 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 351 | /* 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 Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 355 | */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 356 | static int h2_buf_available(void *target) |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 357 | { |
| 358 | struct h2c *h2c = target; |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 359 | struct h2s *h2s; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 360 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 361 | if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) { |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 362 | h2c->flags &= ~H2_CF_DEM_DALLOC; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 363 | h2c_restart_reading(h2c); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 364 | return 1; |
| 365 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 366 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 367 | if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) { |
| 368 | h2c->flags &= ~H2_CF_MUX_MALLOC; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 369 | |
| 370 | if (h2c->flags & H2_CF_DEM_MROOM) { |
| 371 | h2c->flags &= ~H2_CF_DEM_MROOM; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 372 | h2c_restart_reading(h2c); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 373 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 374 | return 1; |
| 375 | } |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 376 | |
| 377 | if ((h2c->flags & H2_CF_DEM_SALLOC) && |
| 378 | (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs && |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 379 | b_alloc_margin(&h2s->rxbuf, 0)) { |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 380 | h2c->flags &= ~H2_CF_DEM_SALLOC; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 381 | h2c_restart_reading(h2c); |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 382 | return 1; |
| 383 | } |
| 384 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 385 | return 0; |
| 386 | } |
| 387 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 388 | static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 389 | { |
| 390 | struct buffer *buf = NULL; |
| 391 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 392 | 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 Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 396 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 397 | LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 398 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 399 | __conn_xprt_stop_recv(h2c->conn); |
| 400 | } |
| 401 | return buf; |
| 402 | } |
| 403 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 404 | static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 405 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 406 | if (bptr->size) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 407 | b_free(bptr); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 408 | offer_buffers(h2c->buf_wait.target, tasks_run_queue); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 412 | /* returns the number of allocatable outgoing streams for the connection taking |
| 413 | * the last_sid and the reserved ones into account. |
| 414 | */ |
| 415 | static 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 Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 430 | /* 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 | */ |
| 434 | static int h2_used_streams(struct connection *conn) |
| 435 | { |
| 436 | struct h2c *h2c = conn->ctx; |
| 437 | |
| 438 | return h2c->nb_cs; |
| 439 | } |
| 440 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 441 | /* returns the number of concurrent streams available on the connection */ |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 442 | static int h2_avail_streams(struct connection *conn) |
| 443 | { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 444 | struct server *srv = objt_server(conn->target); |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 445 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 446 | int ret1, ret2; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 447 | |
Willy Tarreau | 6afec46 | 2019-01-28 06:40:19 +0100 | [diff] [blame] | 448 | /* 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 Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 454 | /* note: may be negative if a SETTINGS frame changes the limit */ |
| 455 | ret1 = h2c->streams_limit - h2c->nb_streams; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 456 | |
| 457 | /* we must also consider the limit imposed by stream IDs */ |
| 458 | ret2 = h2_streams_left(h2c); |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 459 | ret1 = MIN(ret1, ret2); |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 460 | if (ret1 > 0 && srv && srv->max_reuse >= 0) { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 461 | 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 Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 465 | } |
| 466 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 467 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 468 | /*****************************************************************/ |
| 469 | /* functions below are dedicated to the mux setup and management */ |
| 470 | /*****************************************************************/ |
| 471 | |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 472 | /* 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 Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 477 | static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess) |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 478 | { |
| 479 | struct h2c *h2c; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 480 | struct task *t = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 481 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 482 | h2c = pool_alloc(pool_head_h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 483 | if (!h2c) |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 484 | goto fail_no_h2c; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 485 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 486 | if (conn->ctx) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 487 | 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 Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 497 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 498 | h2c->proxy = prx; |
Willy Tarreau | 3340029 | 2017-11-05 11:23:40 +0100 | [diff] [blame] | 499 | h2c->task = NULL; |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 500 | 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 Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 510 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 511 | h2c->wait_event.task = tasklet_new(); |
| 512 | if (!h2c->wait_event.task) |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 513 | goto fail; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 514 | h2c->wait_event.task->process = h2_io_cb; |
| 515 | h2c->wait_event.task->context = h2c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 516 | h2c->wait_event.events = 0; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 517 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 518 | 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 Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 525 | h2c->streams_limit = h2_settings_max_concurrent_streams; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 526 | h2c->max_id = -1; |
| 527 | h2c->errcode = H2_ERR_NO_ERROR; |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 528 | h2c->rcvd_c = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 529 | h2c->rcvd_s = 0; |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 530 | h2c->nb_streams = 0; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 531 | h2c->nb_cs = 0; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 532 | h2c->nb_reserved = 0; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 533 | h2c->stream_cnt = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 534 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 535 | h2c->dbuf = BUF_NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 536 | h2c->dsi = -1; |
| 537 | h2c->msi = -1; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 538 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 539 | h2c->last_sid = -1; |
| 540 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 541 | h2c->mbuf = BUF_NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 542 | h2c->miw = 65535; /* mux initial window size */ |
| 543 | h2c->mws = 65535; /* mux window size */ |
| 544 | h2c->mfs = 16384; /* initial max frame size */ |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 545 | h2c->streams_by_id = EB_ROOT; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 546 | LIST_INIT(&h2c->send_list); |
| 547 | LIST_INIT(&h2c->fctl_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 548 | LIST_INIT(&h2c->sending_list); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 549 | LIST_INIT(&h2c->buf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 550 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 551 | if (t) |
| 552 | task_queue(t); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 553 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 554 | 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 Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 558 | * is stored as conn->ctx by the caller. |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 559 | */ |
| 560 | struct h2s *h2s; |
| 561 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 562 | h2s = h2c_bck_stream_new(h2c, conn->ctx, sess); |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 563 | if (!h2s) |
| 564 | goto fail_stream; |
| 565 | } |
| 566 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 567 | conn->ctx = h2c; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 568 | |
Willy Tarreau | 0f38358 | 2018-10-03 14:22:21 +0200 | [diff] [blame] | 569 | /* prepare to read something */ |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 570 | h2c_restart_reading(h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 571 | return 0; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 572 | fail_stream: |
| 573 | hpack_dht_free(h2c->ddht); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 574 | fail: |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 575 | if (t) |
| 576 | task_free(t); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 577 | if (h2c->wait_event.task) |
| 578 | tasklet_free(h2c->wait_event.task); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 579 | pool_free(pool_head_h2c, h2c); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 580 | fail_no_h2c: |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 581 | return -1; |
| 582 | } |
| 583 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 584 | /* returns the next allocatable outgoing stream ID for the H2 connection, or |
| 585 | * -1 if no more is allocatable. |
| 586 | */ |
| 587 | static inline int32_t h2c_get_next_sid(const struct h2c *h2c) |
| 588 | { |
| 589 | int32_t id = (h2c->max_id + 1) | 1; |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 590 | |
| 591 | if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid)) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 592 | id = -1; |
| 593 | return id; |
| 594 | } |
| 595 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 596 | /* returns the stream associated with id <id> or NULL if not found */ |
| 597 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 598 | { |
| 599 | struct eb32_node *node; |
| 600 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 601 | if (id == 0) |
| 602 | return (struct h2s *)h2_closed_stream; |
| 603 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 604 | if (id > h2c->max_id) |
| 605 | return (struct h2s *)h2_idle_stream; |
| 606 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 607 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 608 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 609 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 610 | |
| 611 | return container_of(node, struct h2s, by_id); |
| 612 | } |
| 613 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 614 | /* release function for a connection. This one should be called to free all |
| 615 | * resources allocated to the mux. |
| 616 | */ |
| 617 | static void h2_release(struct connection *conn) |
| 618 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 619 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 620 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 621 | if (h2c) { |
| 622 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 623 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 624 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 625 | LIST_DEL(&h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 626 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 627 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 628 | h2_release_buf(h2c, &h2c->dbuf); |
| 629 | h2_release_buf(h2c, &h2c->mbuf); |
| 630 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 631 | if (h2c->task) { |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 632 | h2c->task->context = NULL; |
| 633 | task_wakeup(h2c->task, TASK_WOKEN_OTHER); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 634 | h2c->task = NULL; |
| 635 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 636 | if (h2c->wait_event.task) |
| 637 | tasklet_free(h2c->wait_event.task); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 638 | if (h2c->wait_event.events != 0) |
| 639 | conn->xprt->unsubscribe(conn, h2c->wait_event.events, |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 640 | &h2c->wait_event); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 641 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 642 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | conn->mux = NULL; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 646 | conn->ctx = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 647 | |
| 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 Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 656 | /******************************************************/ |
| 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 Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 661 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 662 | { |
| 663 | return h2s ? h2s->id : 0; |
| 664 | } |
| 665 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 666 | /* returns true of the mux is currently busy as seen from stream <h2s> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 667 | static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s) |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 668 | { |
| 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 Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 678 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 679 | static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err) |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 680 | { |
| 681 | h2c->errcode = err; |
| 682 | h2c->st0 = H2_CS_ERROR; |
| 683 | } |
| 684 | |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 685 | /* 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 Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 688 | static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err) |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 689 | { |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 690 | if (h2s->id && h2s->st != H2_SS_ERROR) { |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 691 | h2s->errcode = err; |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 692 | if (h2s->st < H2_SS_ERROR) |
| 693 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 694 | if (h2s->cs) |
| 695 | cs_set_error(h2s->cs); |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 699 | /* attempt to notify the data layer of recv availability */ |
| 700 | static 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 */ |
| 713 | static void __maybe_unused h2s_notify_send(struct h2s *h2s) |
| 714 | { |
| 715 | struct wait_event *sw; |
| 716 | |
Olivier Houchard | fd1e96d | 2019-03-25 14:04:25 +0100 | [diff] [blame] | 717 | if (h2s->send_wait && !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) { |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 718 | sw = h2s->send_wait; |
| 719 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fd1e96d | 2019-03-25 14:04:25 +0100 | [diff] [blame] | 720 | sw->events |= SUB_CALL_UNSUBSCRIBE; |
| 721 | LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 722 | tasklet_wakeup(sw->task); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 726 | /* alerts the data layer, trying to wake it up by all means, following |
| 727 | * this sequence : |
| 728 | * - if the h2s' data layer is subscribed to recv, then it's woken up for recv |
| 729 | * - if its subscribed to send, then it's woken up for send |
| 730 | * - if it was subscribed to neither, its ->wake() callback is called |
| 731 | * It is safe to call this function with a closed stream which doesn't have a |
| 732 | * conn_stream anymore. |
| 733 | */ |
| 734 | static void __maybe_unused h2s_alert(struct h2s *h2s) |
| 735 | { |
| 736 | if (h2s->recv_wait || h2s->send_wait) { |
| 737 | h2s_notify_recv(h2s); |
| 738 | h2s_notify_send(h2s); |
| 739 | } |
| 740 | else if (h2s->cs && h2s->cs->data_cb->wake != NULL) |
| 741 | h2s->cs->data_cb->wake(h2s->cs); |
| 742 | } |
| 743 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 744 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 745 | static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len) |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 746 | { |
| 747 | uint8_t *out = frame; |
| 748 | |
| 749 | *out = len >> 16; |
| 750 | write_n16(out + 1, len); |
| 751 | } |
| 752 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 753 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 754 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 755 | * the caller's responsibility to verify that there are at least <bytes> bytes |
Willy Tarreau | 9c7f2d1 | 2018-06-15 11:51:32 +0200 | [diff] [blame] | 756 | * available in the buffer's input prior to calling this function. The buffer |
| 757 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 758 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 759 | static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes, |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 760 | const struct buffer *b, int o) |
| 761 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 762 | readv_bytes(dst, bytes, b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b)); |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 763 | } |
| 764 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 765 | static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o) |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 766 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 767 | return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b)); |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 768 | } |
| 769 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 770 | static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o) |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 771 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 772 | return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b)); |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 773 | } |
| 774 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 775 | static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o) |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 776 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 777 | return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b)); |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 781 | /* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>. |
| 782 | * The algorithm is not obvious. It turns out that H2 headers are neither |
| 783 | * aligned nor do they use regular sizes. And to add to the trouble, the buffer |
| 784 | * may wrap so each byte read must be checked. The header is formed like this : |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 785 | * |
| 786 | * b0 b1 b2 b3 b4 b5..b8 |
| 787 | * +----------+---------+--------+----+----+----------------------+ |
| 788 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 789 | * +----------+---------+--------+----+----+----------------------+ |
| 790 | * |
| 791 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 792 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 793 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 794 | * the word, and only one extra read is needed to fetch len[16:23]. |
Willy Tarreau | 9c7f2d1 | 2018-06-15 11:51:32 +0200 | [diff] [blame] | 795 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 796 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 797 | */ |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 798 | static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 799 | { |
| 800 | uint64_t w; |
| 801 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 802 | if (b_data(b) < o + 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 803 | return 0; |
| 804 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 805 | w = h2_get_n64(b, o + 1); |
| 806 | h->len = *(uint8_t*)b_peek(b, o) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 807 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 808 | h->ff = w >> 32; |
| 809 | h->ft = w >> 40; |
| 810 | h->len += w >> 48; |
| 811 | return 1; |
| 812 | } |
| 813 | |
| 814 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 815 | * h2_peek_frame_hdr() above. |
| 816 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 817 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 818 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 819 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 823 | static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 824 | { |
| 825 | int ret; |
| 826 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 827 | ret = h2_peek_frame_hdr(b, 0, h); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 828 | if (ret > 0) |
| 829 | h2_skip_frame_hdr(b); |
| 830 | return ret; |
| 831 | } |
| 832 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 833 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 834 | * its connection if the stream was not yet closed. Please use this exclusively |
| 835 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 836 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 837 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 838 | { |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 839 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 840 | h2s->h2c->nb_streams--; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 841 | if (!h2s->id) |
| 842 | h2s->h2c->nb_reserved--; |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 843 | if (h2s->cs) { |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 844 | h2s->cs->flags |= CS_FL_REOS; |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 845 | if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf)) |
| 846 | h2s_notify_recv(h2s); |
| 847 | } |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 848 | } |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 849 | h2s->st = H2_SS_CLOSED; |
| 850 | } |
| 851 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 852 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 853 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 854 | { |
| 855 | h2s_close(h2s); |
| 856 | eb32_delete(&h2s->by_id); |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 857 | if (b_size(&h2s->rxbuf)) { |
| 858 | b_free(&h2s->rxbuf); |
| 859 | offer_buffers(NULL, tasks_run_queue); |
| 860 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 861 | if (h2s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 862 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 863 | if (h2s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 864 | h2s->recv_wait->events &= ~SUB_RETRY_RECV; |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 865 | /* There's no need to explicitly call unsubscribe here, the only |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 866 | * reference left would be in the h2c send_list/fctl_list, and if |
| 867 | * we're in it, we're getting out anyway |
| 868 | */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 869 | LIST_DEL_INIT(&h2s->list); |
| 870 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 871 | tasklet_free(h2s->wait_event.task); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 872 | pool_free(pool_head_h2s, h2s); |
| 873 | } |
| 874 | |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 875 | /* allocates a new stream <id> for connection <h2c> and adds it into h2c's |
| 876 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 877 | * causes of errors can be any failed memory allocation. The caller is |
| 878 | * responsible for checking if the connection may support an extra stream |
| 879 | * prior to calling this function. |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 880 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 881 | static struct h2s *h2s_new(struct h2c *h2c, int id) |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 882 | { |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 883 | struct h2s *h2s; |
| 884 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 885 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 886 | if (!h2s) |
| 887 | goto out; |
| 888 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 889 | h2s->wait_event.task = tasklet_new(); |
| 890 | if (!h2s->wait_event.task) { |
| 891 | pool_free(pool_head_h2s, h2s); |
| 892 | goto out; |
| 893 | } |
| 894 | h2s->send_wait = NULL; |
| 895 | h2s->recv_wait = NULL; |
| 896 | h2s->wait_event.task->process = h2_deferred_shut; |
| 897 | h2s->wait_event.task->context = h2s; |
| 898 | h2s->wait_event.handle = NULL; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 899 | h2s->wait_event.events = 0; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 900 | LIST_INIT(&h2s->list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 901 | LIST_INIT(&h2s->sending_list); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 902 | h2s->h2c = h2c; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 903 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 904 | h2s->mws = h2c->miw; |
| 905 | h2s->flags = H2_SF_NONE; |
| 906 | h2s->errcode = H2_ERR_NO_ERROR; |
| 907 | h2s->st = H2_SS_IDLE; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 908 | h2s->status = 0; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 909 | h2s->body_len = 0; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 910 | h2s->rxbuf = BUF_NULL; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 911 | |
| 912 | if (h2c->flags & H2_CF_IS_BACK) { |
| 913 | h1m_init_req(&h2s->h1m); |
| 914 | h2s->h1m.err_pos = -1; // don't care about errors on the request path |
| 915 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 916 | } else { |
| 917 | h1m_init_res(&h2s->h1m); |
| 918 | h2s->h1m.err_pos = -1; // don't care about errors on the response path |
| 919 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 920 | } |
| 921 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 922 | h2s->by_id.key = h2s->id = id; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 923 | if (id > 0) |
| 924 | h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 925 | else |
| 926 | h2c->nb_reserved++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 927 | |
| 928 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 929 | h2c->nb_streams++; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 930 | h2c->stream_cnt++; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 931 | |
| 932 | return h2s; |
| 933 | |
| 934 | out_free_h2s: |
| 935 | pool_free(pool_head_h2s, h2s); |
| 936 | out: |
| 937 | return NULL; |
| 938 | } |
| 939 | |
| 940 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 941 | * case of memory allocation error. |
| 942 | */ |
| 943 | static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id) |
| 944 | { |
| 945 | struct session *sess = h2c->conn->owner; |
| 946 | struct conn_stream *cs; |
| 947 | struct h2s *h2s; |
| 948 | |
| 949 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 950 | goto out; |
| 951 | |
| 952 | h2s = h2s_new(h2c, id); |
| 953 | if (!h2s) |
| 954 | goto out; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 955 | |
| 956 | cs = cs_new(h2c->conn); |
| 957 | if (!cs) |
| 958 | goto out_close; |
| 959 | |
Olivier Houchard | 746fb77 | 2018-12-15 19:42:00 +0100 | [diff] [blame] | 960 | cs->flags |= CS_FL_NOT_FIRST; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 961 | h2s->cs = cs; |
| 962 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 963 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 964 | |
| 965 | if (stream_create_from_cs(cs) < 0) |
| 966 | goto out_free_cs; |
| 967 | |
Willy Tarreau | 590a051 | 2018-09-05 11:56:48 +0200 | [diff] [blame] | 968 | /* We want the accept date presented to the next stream to be the one |
| 969 | * we have now, the handshake time to be null (since the next stream |
| 970 | * is not delayed by a handshake), and the idle time to count since |
| 971 | * right now. |
| 972 | */ |
| 973 | sess->accept_date = date; |
| 974 | sess->tv_accept = now; |
| 975 | sess->t_handshake = 0; |
| 976 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 977 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 978 | if (h2_frt_has_too_many_cs(h2c)) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 979 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 980 | return h2s; |
| 981 | |
| 982 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 983 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 984 | cs_free(cs); |
| 985 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 986 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 987 | out: |
Willy Tarreau | 45efc07 | 2018-10-03 18:27:52 +0200 | [diff] [blame] | 988 | sess_log(sess); |
| 989 | return NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 990 | } |
| 991 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 992 | /* allocates a new stream associated to conn_stream <cs> on the h2c connection |
| 993 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 994 | * possible stream ID was reached. |
| 995 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 996 | static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 997 | { |
| 998 | struct h2s *h2s = NULL; |
| 999 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 1000 | if (h2c->nb_streams >= h2c->streams_limit) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1001 | goto out; |
| 1002 | |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 1003 | if (h2_streams_left(h2c) < 1) |
| 1004 | goto out; |
| 1005 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1006 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 1007 | h2s = h2s_new(h2c, 0); |
| 1008 | if (!h2s) |
| 1009 | goto out; |
| 1010 | |
| 1011 | h2s->cs = cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1012 | h2s->sess = sess; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1013 | cs->ctx = h2s; |
| 1014 | h2c->nb_cs++; |
| 1015 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1016 | out: |
| 1017 | return h2s; |
| 1018 | } |
| 1019 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1020 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 1021 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 1022 | * the various settings codes. |
| 1023 | */ |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1024 | static int h2c_send_settings(struct h2c *h2c) |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1025 | { |
| 1026 | struct buffer *res; |
| 1027 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1028 | struct buffer buf; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1029 | int mfs; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1030 | int ret; |
| 1031 | |
| 1032 | if (h2c_mux_busy(h2c, NULL)) { |
| 1033 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1037 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1038 | if (!res) { |
| 1039 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1040 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
| 1044 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 1045 | chunk_memcpy(&buf, |
| 1046 | "\x00\x00\x00" /* length : 0 for now */ |
| 1047 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 1048 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 1049 | 9); |
| 1050 | |
Willy Tarreau | 0bbad6b | 2019-02-26 16:01:52 +0100 | [diff] [blame] | 1051 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1052 | /* send settings_enable_push=0 */ |
| 1053 | chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6); |
| 1054 | } |
| 1055 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1056 | if (h2_settings_header_table_size != 4096) { |
| 1057 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 1058 | |
| 1059 | write_n32(str + 2, h2_settings_header_table_size); |
| 1060 | chunk_memcat(&buf, str, 6); |
| 1061 | } |
| 1062 | |
| 1063 | if (h2_settings_initial_window_size != 65535) { |
| 1064 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 1065 | |
| 1066 | write_n32(str + 2, h2_settings_initial_window_size); |
| 1067 | chunk_memcat(&buf, str, 6); |
| 1068 | } |
| 1069 | |
| 1070 | if (h2_settings_max_concurrent_streams != 0) { |
| 1071 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 1072 | |
| 1073 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 1074 | * the protocol, so never send this value! |
| 1075 | */ |
| 1076 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 1077 | chunk_memcat(&buf, str, 6); |
| 1078 | } |
| 1079 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1080 | mfs = h2_settings_max_frame_size; |
| 1081 | if (mfs > global.tune.bufsize) |
| 1082 | mfs = global.tune.bufsize; |
| 1083 | |
| 1084 | if (!mfs) |
| 1085 | mfs = global.tune.bufsize; |
| 1086 | |
| 1087 | if (mfs != 16384) { |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1088 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 1089 | |
| 1090 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 1091 | * match bufsize - rewrite size, but at the moment it seems |
| 1092 | * that clients don't take care of it. |
| 1093 | */ |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1094 | write_n32(str + 2, mfs); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1095 | chunk_memcat(&buf, str, 6); |
| 1096 | } |
| 1097 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1098 | h2_set_frame_size(buf.area, buf.data - 9); |
| 1099 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1100 | if (unlikely(ret <= 0)) { |
| 1101 | if (!ret) { |
| 1102 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1103 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1104 | return 0; |
| 1105 | } |
| 1106 | else { |
| 1107 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1108 | return 0; |
| 1109 | } |
| 1110 | } |
| 1111 | return ret; |
| 1112 | } |
| 1113 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1114 | /* Try to receive a connection preface, then upon success try to send our |
| 1115 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1116 | * missing data. It may return an error in h2c. |
| 1117 | */ |
| 1118 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 1119 | { |
| 1120 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1121 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1122 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1123 | ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE)); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1124 | |
| 1125 | if (unlikely(ret1 <= 0)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1126 | if (ret1 < 0) |
| 1127 | sess_log(h2c->conn->owner); |
| 1128 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1129 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 1130 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1131 | return 0; |
| 1132 | } |
| 1133 | |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1134 | ret2 = h2c_send_settings(h2c); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1135 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1136 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1137 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1138 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1139 | } |
| 1140 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1141 | /* Try to send a connection preface, then upon success try to send our |
| 1142 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1143 | * missing data. It may return an error in h2c. |
| 1144 | */ |
| 1145 | static int h2c_bck_send_preface(struct h2c *h2c) |
| 1146 | { |
| 1147 | struct buffer *res; |
| 1148 | |
| 1149 | if (h2c_mux_busy(h2c, NULL)) { |
| 1150 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | res = h2_get_buf(h2c, &h2c->mbuf); |
| 1155 | if (!res) { |
| 1156 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1157 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1158 | return 0; |
| 1159 | } |
| 1160 | |
| 1161 | if (!b_data(res)) { |
| 1162 | /* preface not yet sent */ |
| 1163 | b_istput(res, ist(H2_CONN_PREFACE)); |
| 1164 | } |
| 1165 | |
| 1166 | return h2c_send_settings(h2c); |
| 1167 | } |
| 1168 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1169 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 1170 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 1171 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 1172 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 1173 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 1174 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 1175 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 1176 | * case so that it is safe to use h2c_error() to report such errors. |
| 1177 | */ |
| 1178 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 1179 | { |
| 1180 | struct buffer *res; |
| 1181 | char str[17]; |
| 1182 | int ret; |
| 1183 | |
| 1184 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 1185 | return 1; // claim that it worked |
| 1186 | |
| 1187 | if (h2c_mux_busy(h2c, h2s)) { |
| 1188 | if (h2s) |
| 1189 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1190 | else |
| 1191 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1192 | return 0; |
| 1193 | } |
| 1194 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1195 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1196 | if (!res) { |
| 1197 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1198 | if (h2s) |
| 1199 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1200 | else |
| 1201 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
| 1205 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 1206 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 1207 | |
| 1208 | if (h2c->last_sid < 0) |
| 1209 | h2c->last_sid = h2c->max_id; |
| 1210 | |
| 1211 | write_n32(str + 9, h2c->last_sid); |
| 1212 | write_n32(str + 13, h2c->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1213 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1214 | if (unlikely(ret <= 0)) { |
| 1215 | if (!ret) { |
| 1216 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1217 | if (h2s) |
| 1218 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1219 | else |
| 1220 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1221 | return 0; |
| 1222 | } |
| 1223 | else { |
| 1224 | /* we cannot report this error using GOAWAY, so we mark |
| 1225 | * it and claim a success. |
| 1226 | */ |
| 1227 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1228 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 1229 | return 1; |
| 1230 | } |
| 1231 | } |
| 1232 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 1233 | return ret; |
| 1234 | } |
| 1235 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1236 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 1237 | * during mux operations. This stream must be valid and cannot be closed |
| 1238 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 1239 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 1240 | * not yet. |
| 1241 | * |
| 1242 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1243 | * to write the message, it subscribes the stream to future notifications. |
| 1244 | */ |
| 1245 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1246 | { |
| 1247 | struct buffer *res; |
| 1248 | char str[13]; |
| 1249 | int ret; |
| 1250 | |
| 1251 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 1252 | return 1; |
| 1253 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1254 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1255 | * RST_STREAM in response to a RST_STREAM frame. |
| 1256 | */ |
| 1257 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1258 | ret = 1; |
| 1259 | goto ignore; |
| 1260 | } |
| 1261 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1262 | if (h2c_mux_busy(h2c, h2s)) { |
| 1263 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1264 | return 0; |
| 1265 | } |
| 1266 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1267 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1268 | if (!res) { |
| 1269 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1270 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1271 | return 0; |
| 1272 | } |
| 1273 | |
| 1274 | /* len: 4, type: 3, flags: none */ |
| 1275 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1276 | write_n32(str + 5, h2s->id); |
| 1277 | write_n32(str + 9, h2s->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1278 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1279 | |
| 1280 | if (unlikely(ret <= 0)) { |
| 1281 | if (!ret) { |
| 1282 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1283 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1284 | return 0; |
| 1285 | } |
| 1286 | else { |
| 1287 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1288 | return 0; |
| 1289 | } |
| 1290 | } |
| 1291 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1292 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1293 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1294 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1295 | return ret; |
| 1296 | } |
| 1297 | |
| 1298 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 1299 | * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the |
Willy Tarreau | e6888ff | 2018-12-23 18:26:26 +0100 | [diff] [blame] | 1300 | * error code, even if the stream is one of the dummy ones, and will update |
| 1301 | * h2s->st to H2_SS_CLOSED if it was not yet. |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1302 | * |
| 1303 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1304 | * to write the message, it blocks the demuxer and subscribes it to future |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 1305 | * notifications. It's worth mentioning that an RST may even be sent for a |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1306 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1307 | */ |
| 1308 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1309 | { |
| 1310 | struct buffer *res; |
| 1311 | char str[13]; |
| 1312 | int ret; |
| 1313 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1314 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1315 | * RST_STREAM in response to a RST_STREAM frame. |
| 1316 | */ |
| 1317 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1318 | ret = 1; |
| 1319 | goto ignore; |
| 1320 | } |
| 1321 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1322 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1323 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1324 | return 0; |
| 1325 | } |
| 1326 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1327 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1328 | if (!res) { |
| 1329 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1330 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1331 | return 0; |
| 1332 | } |
| 1333 | |
| 1334 | /* len: 4, type: 3, flags: none */ |
| 1335 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1336 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1337 | write_n32(str + 5, h2c->dsi); |
Willy Tarreau | e6888ff | 2018-12-23 18:26:26 +0100 | [diff] [blame] | 1338 | write_n32(str + 9, h2s->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1339 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1340 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1341 | if (unlikely(ret <= 0)) { |
| 1342 | if (!ret) { |
| 1343 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1344 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1345 | return 0; |
| 1346 | } |
| 1347 | else { |
| 1348 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1349 | return 0; |
| 1350 | } |
| 1351 | } |
| 1352 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1353 | ignore: |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1354 | if (h2s->id) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1355 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1356 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1357 | } |
| 1358 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1359 | return ret; |
| 1360 | } |
| 1361 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1362 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 1363 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 1364 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 1365 | * on success or zero if nothing was done. In case of lack of room to write the |
| 1366 | * message, it subscribes the requesting stream to future notifications. |
| 1367 | */ |
| 1368 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 1369 | { |
| 1370 | struct h2c *h2c = h2s->h2c; |
| 1371 | struct buffer *res; |
| 1372 | char str[9]; |
| 1373 | int ret; |
| 1374 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1375 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1376 | return 1; |
| 1377 | |
| 1378 | if (h2c_mux_busy(h2c, h2s)) { |
| 1379 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1380 | return 0; |
| 1381 | } |
| 1382 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1383 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1384 | if (!res) { |
| 1385 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1386 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1387 | return 0; |
| 1388 | } |
| 1389 | |
| 1390 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1391 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1392 | write_n32(str + 5, h2s->id); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1393 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1394 | if (likely(ret > 0)) { |
| 1395 | h2s->flags |= H2_SF_ES_SENT; |
| 1396 | } |
| 1397 | else if (!ret) { |
| 1398 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1399 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1400 | return 0; |
| 1401 | } |
| 1402 | else { |
| 1403 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1404 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1405 | } |
| 1406 | return ret; |
| 1407 | } |
| 1408 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1409 | /* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags> |
| 1410 | * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close |
| 1411 | * connection. The stream's state is automatically updated accordingly. If the |
| 1412 | * stream is orphaned, it is destroyed. |
| 1413 | */ |
| 1414 | static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags) |
| 1415 | { |
| 1416 | if (!h2s->cs) { |
| 1417 | /* this stream was already orphaned */ |
| 1418 | h2s_destroy(h2s); |
| 1419 | return; |
| 1420 | } |
| 1421 | |
| 1422 | h2s->cs->flags |= flags; |
| 1423 | if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS)) |
| 1424 | h2s->cs->flags |= CS_FL_ERROR; |
| 1425 | |
| 1426 | h2s_alert(h2s); |
| 1427 | |
| 1428 | if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR) |
| 1429 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 1430 | else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN) |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1431 | h2s->st = H2_SS_HREM; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 1432 | else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC) |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1433 | h2s_close(h2s); |
| 1434 | } |
| 1435 | |
| 1436 | /* wake the streams attached to the connection, whose id is greater than <last> |
| 1437 | * or unassigned. |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1438 | */ |
| 1439 | static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags) |
| 1440 | { |
| 1441 | struct eb32_node *node; |
| 1442 | struct h2s *h2s; |
| 1443 | |
| 1444 | if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR) |
Willy Tarreau | a851935 | 2018-12-18 16:44:28 +0100 | [diff] [blame] | 1445 | flags |= CS_FL_ERR_PENDING; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1446 | |
| 1447 | if (conn_xprt_read0_pending(h2c->conn)) |
Christopher Faulet | 87a8f35 | 2019-03-22 14:51:36 +0100 | [diff] [blame] | 1448 | flags |= CS_FL_REOS; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1449 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1450 | /* Wake all streams with ID > last */ |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1451 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1452 | while (node) { |
| 1453 | h2s = container_of(node, struct h2s, by_id); |
| 1454 | if (h2s->id <= last) |
| 1455 | break; |
| 1456 | node = eb32_next(node); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1457 | h2s_wake_one_stream(h2s, flags); |
| 1458 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1459 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1460 | /* Wake all streams with unassigned ID (ID == 0) */ |
| 1461 | node = eb32_lookup(&h2c->streams_by_id, 0); |
| 1462 | while (node) { |
| 1463 | h2s = container_of(node, struct h2s, by_id); |
| 1464 | if (h2s->id > 0) |
| 1465 | break; |
| 1466 | node = eb32_next(node); |
| 1467 | h2s_wake_one_stream(h2s, flags); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1468 | } |
| 1469 | } |
| 1470 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1471 | /* Increase all streams' outgoing window size by the difference passed in |
| 1472 | * argument. This is needed upon receipt of the settings frame if the initial |
| 1473 | * window size is different. The difference may be negative and the resulting |
| 1474 | * window size as well, for the time it takes to receive some window updates. |
| 1475 | */ |
| 1476 | static void h2c_update_all_ws(struct h2c *h2c, int diff) |
| 1477 | { |
| 1478 | struct h2s *h2s; |
| 1479 | struct eb32_node *node; |
| 1480 | |
| 1481 | if (!diff) |
| 1482 | return; |
| 1483 | |
| 1484 | node = eb32_first(&h2c->streams_by_id); |
| 1485 | while (node) { |
| 1486 | h2s = container_of(node, struct h2s, by_id); |
| 1487 | h2s->mws += diff; |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1488 | |
| 1489 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1490 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
| 1491 | if (h2s->send_wait) |
| 1492 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 1493 | |
| 1494 | } |
| 1495 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1496 | node = eb32_next(node); |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1501 | * ACKs it if needed. Returns > 0 on success or zero on missing data. It may |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1502 | * return an error in h2c. The caller must have already verified frame length |
| 1503 | * and stream ID validity. Described in RFC7540#6.5. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1504 | */ |
| 1505 | static int h2c_handle_settings(struct h2c *h2c) |
| 1506 | { |
| 1507 | unsigned int offset; |
| 1508 | int error; |
| 1509 | |
| 1510 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1511 | if (h2c->dfl) { |
| 1512 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1513 | goto fail; |
| 1514 | } |
| 1515 | return 1; |
| 1516 | } |
| 1517 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1518 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1519 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1520 | return 0; |
| 1521 | |
| 1522 | /* parse the frame */ |
| 1523 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1524 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1525 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1526 | |
| 1527 | switch (type) { |
| 1528 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1529 | /* we need to update all existing streams with the |
| 1530 | * difference from the previous iws. |
| 1531 | */ |
| 1532 | if (arg < 0) { // RFC7540#6.5.2 |
| 1533 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1534 | goto fail; |
| 1535 | } |
| 1536 | h2c_update_all_ws(h2c, arg - h2c->miw); |
| 1537 | h2c->miw = arg; |
| 1538 | break; |
| 1539 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1540 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1541 | error = H2_ERR_PROTOCOL_ERROR; |
| 1542 | goto fail; |
| 1543 | } |
| 1544 | h2c->mfs = arg; |
| 1545 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1546 | case H2_SETTINGS_ENABLE_PUSH: |
| 1547 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1548 | error = H2_ERR_PROTOCOL_ERROR; |
| 1549 | goto fail; |
| 1550 | } |
| 1551 | break; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 1552 | case H2_SETTINGS_MAX_CONCURRENT_STREAMS: |
| 1553 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1554 | /* the limit is only for the backend; for the frontend it is our limit */ |
| 1555 | if ((unsigned int)arg > h2_settings_max_concurrent_streams) |
| 1556 | arg = h2_settings_max_concurrent_streams; |
| 1557 | h2c->streams_limit = arg; |
| 1558 | } |
| 1559 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | /* need to ACK this frame now */ |
| 1564 | h2c->st0 = H2_CS_FRAME_A; |
| 1565 | return 1; |
| 1566 | fail: |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1567 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1568 | h2c_error(h2c, error); |
| 1569 | return 0; |
| 1570 | } |
| 1571 | |
| 1572 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1573 | * success or one of the h2_status values. |
| 1574 | */ |
| 1575 | static int h2c_ack_settings(struct h2c *h2c) |
| 1576 | { |
| 1577 | struct buffer *res; |
| 1578 | char str[9]; |
| 1579 | int ret = -1; |
| 1580 | |
| 1581 | if (h2c_mux_busy(h2c, NULL)) { |
| 1582 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1583 | return 0; |
| 1584 | } |
| 1585 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1586 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1587 | if (!res) { |
| 1588 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1589 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1590 | return 0; |
| 1591 | } |
| 1592 | |
| 1593 | memcpy(str, |
| 1594 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1595 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1596 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1597 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1598 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1599 | if (unlikely(ret <= 0)) { |
| 1600 | if (!ret) { |
| 1601 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1602 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1603 | return 0; |
| 1604 | } |
| 1605 | else { |
| 1606 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1607 | return 0; |
| 1608 | } |
| 1609 | } |
| 1610 | return ret; |
| 1611 | } |
| 1612 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1613 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1614 | * the pointer to the payload in <payload>. Returns > 0 on success or zero on |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1615 | * missing data. The caller must have already verified frame length |
| 1616 | * and stream ID validity. |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1617 | */ |
| 1618 | static int h2c_handle_ping(struct h2c *h2c) |
| 1619 | { |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1620 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1621 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1622 | h2c->st0 = H2_CS_FRAME_A; |
| 1623 | return 1; |
| 1624 | } |
| 1625 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1626 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1627 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1628 | * error in h2c. |
| 1629 | */ |
| 1630 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1631 | { |
| 1632 | struct buffer *res; |
| 1633 | char str[13]; |
| 1634 | int ret = -1; |
| 1635 | |
| 1636 | if (h2c_mux_busy(h2c, NULL)) { |
| 1637 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1638 | return 0; |
| 1639 | } |
| 1640 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1641 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1642 | if (!res) { |
| 1643 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1644 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1645 | return 0; |
| 1646 | } |
| 1647 | |
| 1648 | /* length: 4, type: 8, flags: none */ |
| 1649 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1650 | write_n32(str + 5, sid); |
| 1651 | write_n32(str + 9, increment); |
| 1652 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1653 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1654 | |
| 1655 | if (unlikely(ret <= 0)) { |
| 1656 | if (!ret) { |
| 1657 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1658 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1659 | return 0; |
| 1660 | } |
| 1661 | else { |
| 1662 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1663 | return 0; |
| 1664 | } |
| 1665 | } |
| 1666 | return ret; |
| 1667 | } |
| 1668 | |
| 1669 | /* try to send pending window update for the connection. It's safe to call it |
| 1670 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1671 | * failure. It may return an error in h2c. |
| 1672 | */ |
| 1673 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1674 | { |
| 1675 | int ret = 1; |
| 1676 | |
| 1677 | if (h2c->rcvd_c <= 0) |
| 1678 | return 1; |
| 1679 | |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 1680 | if (!(h2c->flags & H2_CF_WINDOW_OPENED)) { |
| 1681 | /* increase the advertised connection window to 2G on |
| 1682 | * first update. |
| 1683 | */ |
| 1684 | h2c->flags |= H2_CF_WINDOW_OPENED; |
| 1685 | h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT; |
| 1686 | } |
| 1687 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1688 | /* send WU for the connection */ |
| 1689 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1690 | if (ret > 0) |
| 1691 | h2c->rcvd_c = 0; |
| 1692 | |
| 1693 | return ret; |
| 1694 | } |
| 1695 | |
| 1696 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1697 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1698 | * room or failure. It may return an error in h2c. |
| 1699 | */ |
| 1700 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1701 | { |
| 1702 | int ret = 1; |
| 1703 | |
| 1704 | if (h2c->rcvd_s <= 0) |
| 1705 | return 1; |
| 1706 | |
| 1707 | /* send WU for the stream */ |
| 1708 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1709 | if (ret > 0) |
| 1710 | h2c->rcvd_s = 0; |
| 1711 | |
| 1712 | return ret; |
| 1713 | } |
| 1714 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1715 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1716 | * success, 0 on missing data or one of the h2_status values. |
| 1717 | */ |
| 1718 | static int h2c_ack_ping(struct h2c *h2c) |
| 1719 | { |
| 1720 | struct buffer *res; |
| 1721 | char str[17]; |
| 1722 | int ret = -1; |
| 1723 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1724 | if (b_data(&h2c->dbuf) < 8) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1725 | return 0; |
| 1726 | |
| 1727 | if (h2c_mux_busy(h2c, NULL)) { |
| 1728 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1729 | return 0; |
| 1730 | } |
| 1731 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1732 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1733 | if (!res) { |
| 1734 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1735 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1736 | return 0; |
| 1737 | } |
| 1738 | |
| 1739 | memcpy(str, |
| 1740 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1741 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1742 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1743 | |
| 1744 | /* copy the original payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1745 | h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1746 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1747 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1748 | if (unlikely(ret <= 0)) { |
| 1749 | if (!ret) { |
| 1750 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1751 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1752 | return 0; |
| 1753 | } |
| 1754 | else { |
| 1755 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1756 | return 0; |
| 1757 | } |
| 1758 | } |
| 1759 | return ret; |
| 1760 | } |
| 1761 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1762 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1763 | * Returns > 0 on success or zero on missing data. It may return an error in |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1764 | * h2c or h2s. The caller must have already verified frame length and stream ID |
| 1765 | * validity. Described in RFC7540#6.9. |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1766 | */ |
| 1767 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1768 | { |
| 1769 | int32_t inc; |
| 1770 | int error; |
| 1771 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1772 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1773 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1774 | return 0; |
| 1775 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1776 | inc = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1777 | |
| 1778 | if (h2c->dsi != 0) { |
| 1779 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1780 | |
| 1781 | /* it's not an error to receive WU on a closed stream */ |
| 1782 | if (h2s->st == H2_SS_CLOSED) |
| 1783 | return 1; |
| 1784 | |
| 1785 | if (!inc) { |
| 1786 | error = H2_ERR_PROTOCOL_ERROR; |
| 1787 | goto strm_err; |
| 1788 | } |
| 1789 | |
| 1790 | if (h2s->mws >= 0 && h2s->mws + inc < 0) { |
| 1791 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1792 | goto strm_err; |
| 1793 | } |
| 1794 | |
| 1795 | h2s->mws += inc; |
| 1796 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1797 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 1798 | if (h2s->send_wait) |
| 1799 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 1800 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1801 | } |
| 1802 | } |
| 1803 | else { |
| 1804 | /* connection window update */ |
| 1805 | if (!inc) { |
| 1806 | error = H2_ERR_PROTOCOL_ERROR; |
| 1807 | goto conn_err; |
| 1808 | } |
| 1809 | |
| 1810 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1811 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1812 | goto conn_err; |
| 1813 | } |
| 1814 | |
| 1815 | h2c->mws += inc; |
| 1816 | } |
| 1817 | |
| 1818 | return 1; |
| 1819 | |
| 1820 | conn_err: |
| 1821 | h2c_error(h2c, error); |
| 1822 | return 0; |
| 1823 | |
| 1824 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 1825 | h2s_error(h2s, error); |
| 1826 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1827 | return 0; |
| 1828 | } |
| 1829 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1830 | /* processes a GOAWAY frame, and signals all streams whose ID is greater than |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1831 | * the last ID. Returns > 0 on success or zero on missing data. The caller must |
| 1832 | * have already verified frame length and stream ID validity. Described in |
| 1833 | * RFC7540#6.8. |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1834 | */ |
| 1835 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1836 | { |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1837 | int last; |
| 1838 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1839 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1840 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1841 | return 0; |
| 1842 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1843 | last = h2_get_n32(&h2c->dbuf, 0); |
| 1844 | h2c->errcode = h2_get_n32(&h2c->dbuf, 4); |
Olivier Houchard | 9117780 | 2018-12-19 14:49:39 +0100 | [diff] [blame] | 1845 | h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1846 | if (h2c->last_sid < 0) |
| 1847 | h2c->last_sid = last; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1848 | return 1; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1849 | } |
| 1850 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1851 | /* processes a PRIORITY frame, and either skips it or rejects if it is |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1852 | * invalid. Returns > 0 on success or zero on missing data. It may return an |
| 1853 | * error in h2c. The caller must have already verified frame length and stream |
| 1854 | * ID validity. Described in RFC7540#6.3. |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1855 | */ |
| 1856 | static int h2c_handle_priority(struct h2c *h2c) |
| 1857 | { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1858 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1859 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1860 | return 0; |
| 1861 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1862 | if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1863 | /* 7540#5.3 : can't depend on itself */ |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1864 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1865 | return 0; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1866 | } |
| 1867 | return 1; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1868 | } |
| 1869 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1870 | /* processes an RST_STREAM frame, and sets the 32-bit error code on the stream. |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1871 | * Returns > 0 on success or zero on missing data. The caller must have already |
| 1872 | * verified frame length and stream ID validity. Described in RFC7540#6.4. |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1873 | */ |
| 1874 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1875 | { |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1876 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1877 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1878 | return 0; |
| 1879 | |
| 1880 | /* late RST, already handled */ |
| 1881 | if (h2s->st == H2_SS_CLOSED) |
| 1882 | return 1; |
| 1883 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1884 | h2s->errcode = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1885 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1886 | |
| 1887 | if (h2s->cs) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 1888 | cs_set_error(h2s->cs); |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 1889 | h2s_alert(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | h2s->flags |= H2_SF_RST_RCVD; |
| 1893 | return 1; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1894 | } |
| 1895 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1896 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 1897 | * It may return an error in h2c or h2s. The caller must consider that the |
| 1898 | * return value is the new h2s in case one was allocated (most common case). |
| 1899 | * Described in RFC7540#6.2. Most of the |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1900 | * errors here are reported as connection errors since it's impossible to |
| 1901 | * recover from such errors after the compression context has been altered. |
| 1902 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1903 | static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1904 | { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1905 | struct buffer rxbuf = BUF_NULL; |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1906 | unsigned long long body_len = 0; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1907 | uint32_t flags = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1908 | int error; |
| 1909 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1910 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1911 | return NULL; // empty buffer |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1912 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1913 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1914 | return NULL; // incomplete frame |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1915 | |
| 1916 | /* now either the frame is complete or the buffer is complete */ |
| 1917 | if (h2s->st != H2_SS_IDLE) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1918 | /* The stream exists/existed, this must be a trailers frame */ |
| 1919 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1920 | if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0) |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1921 | goto out; |
| 1922 | goto done; |
| 1923 | } |
Willy Tarreau | 1f03550 | 2019-01-30 11:44:07 +0100 | [diff] [blame] | 1924 | /* the connection was already killed by an RST, let's consume |
| 1925 | * the data and send another RST. |
| 1926 | */ |
| 1927 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
| 1928 | h2s = (struct h2s*)h2_error_stream; |
| 1929 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1930 | } |
| 1931 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 1932 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 1933 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1934 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1935 | goto conn_err; |
| 1936 | } |
Willy Tarreau | 415b1ee | 2019-01-02 13:59:43 +0100 | [diff] [blame] | 1937 | else if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 1938 | goto out; // IDLE but too many cs still present |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1939 | |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1940 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1941 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1942 | /* unrecoverable error ? */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1943 | if (h2c->st0 >= H2_CS_ERROR) |
| 1944 | goto out; |
| 1945 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1946 | if (error <= 0) { |
| 1947 | if (error == 0) |
| 1948 | goto out; // missing data |
| 1949 | |
| 1950 | /* Failed to decode this stream (e.g. too large request) |
| 1951 | * but the HPACK decompressor is still synchronized. |
| 1952 | */ |
| 1953 | h2s = (struct h2s*)h2_error_stream; |
| 1954 | goto send_rst; |
| 1955 | } |
| 1956 | |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1957 | /* Note: we don't emit any other logs below because ff we return |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 1958 | * positively from h2c_frt_stream_new(), the stream will report the error, |
| 1959 | * and if we return in error, h2c_frt_stream_new() will emit the error. |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1960 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 1961 | h2s = h2c_frt_stream_new(h2c, h2c->dsi); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1962 | if (!h2s) { |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 1963 | h2s = (struct h2s*)h2_refused_stream; |
| 1964 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | h2s->st = H2_SS_OPEN; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1968 | h2s->rxbuf = rxbuf; |
| 1969 | h2s->flags |= flags; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 1970 | h2s->body_len = body_len; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1971 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1972 | done: |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1973 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1974 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1975 | |
| 1976 | if (h2s->flags & H2_SF_ES_RCVD) { |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 1977 | if (h2s->cs) |
| 1978 | h2s->cs->flags |= CS_FL_EOI; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 1979 | if (h2s->st == H2_SS_OPEN) |
| 1980 | h2s->st = H2_SS_HREM; |
| 1981 | else |
| 1982 | h2s_close(h2s); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1983 | } |
| 1984 | |
Willy Tarreau | 3a429f0 | 2019-01-03 11:41:50 +0100 | [diff] [blame] | 1985 | /* update the max stream ID if the request is being processed */ |
| 1986 | if (h2s->id > h2c->max_id) |
| 1987 | h2c->max_id = h2s->id; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1988 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1989 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1990 | |
| 1991 | conn_err: |
| 1992 | h2c_error(h2c, error); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1993 | goto out; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1994 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1995 | out: |
| 1996 | h2_release_buf(h2c, &rxbuf); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1997 | return NULL; |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 1998 | |
| 1999 | send_rst: |
| 2000 | /* make the demux send an RST for the current stream. We may only |
| 2001 | * do this if we're certain that the HEADERS frame was properly |
| 2002 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2003 | */ |
| 2004 | h2_release_buf(h2c, &rxbuf); |
| 2005 | h2c->st0 = H2_CS_FRAME_E; |
| 2006 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2007 | } |
| 2008 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2009 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 2010 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 2011 | * errors here are reported as connection errors since it's impossible to |
| 2012 | * recover from such errors after the compression context has been altered. |
| 2013 | */ |
| 2014 | static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 2015 | { |
| 2016 | int error; |
| 2017 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2018 | if (!b_size(&h2c->dbuf)) |
| 2019 | return NULL; // empty buffer |
| 2020 | |
| 2021 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
| 2022 | return NULL; // incomplete frame |
| 2023 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2024 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2025 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2026 | /* unrecoverable error ? */ |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2027 | if (h2c->st0 >= H2_CS_ERROR) |
| 2028 | return NULL; |
| 2029 | |
Willy Tarreau | 08bb1d6 | 2019-01-30 16:55:48 +0100 | [diff] [blame] | 2030 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2031 | /* RFC7540#5.1 */ |
| 2032 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2033 | h2c->st0 = H2_CS_FRAME_E; |
| 2034 | return NULL; |
| 2035 | } |
| 2036 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2037 | if (error <= 0) { |
| 2038 | if (error == 0) |
| 2039 | return NULL; // missing data |
| 2040 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2041 | /* stream error : send RST_STREAM */ |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2042 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2043 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2044 | return NULL; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2045 | } |
| 2046 | |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2047 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 2048 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2049 | if (h2s->cs) |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2050 | h2s->cs->flags |= CS_FL_EOI; |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2051 | } |
| 2052 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2053 | if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR) |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2054 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2055 | else if (h2s->cs && (h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN) |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2056 | h2s->st = H2_SS_HREM; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2057 | else if ((!h2s || h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC) |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2058 | h2s_close(h2s); |
| 2059 | |
| 2060 | return h2s; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2061 | } |
| 2062 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2063 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 2064 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 2065 | */ |
| 2066 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 2067 | { |
| 2068 | int error; |
| 2069 | |
| 2070 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 2071 | * to signal an end of stream (with the ES flag). |
| 2072 | */ |
| 2073 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2074 | if (!b_size(&h2c->dbuf) && h2c->dfl) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2075 | return 0; // empty buffer |
| 2076 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2077 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2078 | return 0; // incomplete frame |
| 2079 | |
| 2080 | /* now either the frame is complete or the buffer is complete */ |
| 2081 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2082 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2083 | /* RFC7540#6.1 */ |
| 2084 | error = H2_ERR_STREAM_CLOSED; |
| 2085 | goto strm_err; |
| 2086 | } |
| 2087 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2088 | if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) { |
| 2089 | /* RFC7540#8.1.2 */ |
| 2090 | error = H2_ERR_PROTOCOL_ERROR; |
| 2091 | goto strm_err; |
| 2092 | } |
| 2093 | |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 2094 | if (!h2_frt_transfer_data(h2s)) |
| 2095 | return 0; |
| 2096 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2097 | /* call the upper layers to process the frame, then let the upper layer |
| 2098 | * notify the stream about any change. |
| 2099 | */ |
| 2100 | if (!h2s->cs) { |
| 2101 | error = H2_ERR_STREAM_CLOSED; |
| 2102 | goto strm_err; |
| 2103 | } |
| 2104 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 2105 | if (h2c->st0 >= H2_CS_ERROR) |
| 2106 | return 0; |
| 2107 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2108 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2109 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2110 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | /* check for completion : the callee will change this to FRAME_A or |
| 2114 | * FRAME_H once done. |
| 2115 | */ |
| 2116 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2117 | return 0; |
| 2118 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2119 | /* last frame */ |
| 2120 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2121 | if (h2s->cs) |
| 2122 | h2s->cs->flags |= CS_FL_EOI; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2123 | if (h2s->st == H2_SS_OPEN) |
| 2124 | h2s->st = H2_SS_HREM; |
| 2125 | else |
| 2126 | h2s_close(h2s); |
| 2127 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2128 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2129 | |
| 2130 | if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) { |
| 2131 | /* RFC7540#8.1.2 */ |
| 2132 | error = H2_ERR_PROTOCOL_ERROR; |
| 2133 | goto strm_err; |
| 2134 | } |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2135 | } |
| 2136 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2137 | return 1; |
| 2138 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2139 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 2140 | h2s_error(h2s, error); |
| 2141 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2142 | return 0; |
| 2143 | } |
| 2144 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2145 | /* process Rx frames to be demultiplexed */ |
| 2146 | static void h2_process_demux(struct h2c *h2c) |
| 2147 | { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2148 | struct h2s *h2s = NULL, *tmp_h2s; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2149 | struct h2_fh hdr; |
| 2150 | unsigned int padlen = 0; |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 2151 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2152 | if (h2c->st0 >= H2_CS_ERROR) |
| 2153 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2154 | |
| 2155 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2156 | if (h2c->st0 == H2_CS_PREFACE) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2157 | if (h2c->flags & H2_CF_IS_BACK) |
| 2158 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2159 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 2160 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2161 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2162 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2163 | sess_log(h2c->conn->owner); |
| 2164 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2165 | goto fail; |
| 2166 | } |
| 2167 | |
| 2168 | h2c->max_id = 0; |
| 2169 | h2c->st0 = H2_CS_SETTINGS1; |
| 2170 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2171 | |
| 2172 | if (h2c->st0 == H2_CS_SETTINGS1) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2173 | /* ensure that what is pending is a valid SETTINGS frame |
| 2174 | * without an ACK. |
| 2175 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2176 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2177 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2178 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2179 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2180 | sess_log(h2c->conn->owner); |
| 2181 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2182 | goto fail; |
| 2183 | } |
| 2184 | |
| 2185 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 2186 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2187 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2188 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2189 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2190 | goto fail; |
| 2191 | } |
| 2192 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2193 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2194 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2195 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2196 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2197 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2198 | goto fail; |
| 2199 | } |
| 2200 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2201 | /* that's OK, switch to FRAME_P to process it. This is |
| 2202 | * a SETTINGS frame whose header has already been |
| 2203 | * deleted above. |
| 2204 | */ |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2205 | padlen = 0; |
| 2206 | goto new_frame; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2207 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2208 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2209 | |
| 2210 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2211 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2212 | int ret = 0; |
| 2213 | |
| 2214 | if (h2c->st0 >= H2_CS_ERROR) |
| 2215 | break; |
| 2216 | |
| 2217 | if (h2c->st0 == H2_CS_FRAME_H) { |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 2218 | if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2219 | break; |
| 2220 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2221 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2222 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2223 | if (!h2c->nb_streams) { |
| 2224 | /* only log if no other stream can report the error */ |
| 2225 | sess_log(h2c->conn->owner); |
| 2226 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2227 | break; |
| 2228 | } |
| 2229 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2230 | if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) { |
| 2231 | /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA), |
| 2232 | * we read the pad length and drop it from the remaining |
| 2233 | * payload (one byte + the 9 remaining ones = 10 total |
| 2234 | * removed), so we have a frame payload starting after the |
| 2235 | * pad len. Flow controlled frames (DATA) also count the |
| 2236 | * padlen in the flow control, so it must be adjusted. |
| 2237 | */ |
| 2238 | if (hdr.len < 1) { |
| 2239 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2240 | sess_log(h2c->conn->owner); |
| 2241 | goto fail; |
| 2242 | } |
| 2243 | hdr.len--; |
| 2244 | |
| 2245 | if (b_data(&h2c->dbuf) < 10) |
| 2246 | break; // missing padlen |
| 2247 | |
| 2248 | padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9); |
| 2249 | |
| 2250 | if (padlen > hdr.len) { |
| 2251 | /* RFC7540#6.1 : pad length = length of |
| 2252 | * frame payload or greater => error. |
| 2253 | */ |
| 2254 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2255 | sess_log(h2c->conn->owner); |
| 2256 | goto fail; |
| 2257 | } |
| 2258 | |
| 2259 | if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) { |
| 2260 | h2c->rcvd_c++; |
| 2261 | h2c->rcvd_s++; |
| 2262 | } |
| 2263 | b_del(&h2c->dbuf, 1); |
| 2264 | } |
| 2265 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2266 | |
| 2267 | new_frame: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2268 | h2c->dfl = hdr.len; |
| 2269 | h2c->dsi = hdr.sid; |
| 2270 | h2c->dft = hdr.ft; |
| 2271 | h2c->dff = hdr.ff; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2272 | h2c->dpl = padlen; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2273 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2274 | |
| 2275 | /* check for minimum basic frame format validity */ |
| 2276 | ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize); |
| 2277 | if (ret != H2_ERR_NO_ERROR) { |
| 2278 | h2c_error(h2c, ret); |
| 2279 | sess_log(h2c->conn->owner); |
| 2280 | goto fail; |
| 2281 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2285 | tmp_h2s = h2c_st_by_id(h2c, h2c->dsi); |
| 2286 | |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2287 | if (tmp_h2s != h2s && h2s && h2s->cs && |
| 2288 | (b_data(&h2s->rxbuf) || |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2289 | (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS|CS_FL_EOI)))) { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2290 | /* we may have to signal the upper layers */ |
| 2291 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2292 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2293 | } |
| 2294 | h2s = tmp_h2s; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2295 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 2296 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2297 | goto strm_err; |
| 2298 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2299 | if (h2s->st == H2_SS_IDLE && |
| 2300 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 2301 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 2302 | * this state MUST be treated as a connection error |
| 2303 | */ |
| 2304 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2305 | if (!h2c->nb_streams) { |
| 2306 | /* only log if no other stream can report the error */ |
| 2307 | sess_log(h2c->conn->owner); |
| 2308 | } |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2309 | break; |
| 2310 | } |
| 2311 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2312 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 2313 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 2314 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2315 | * this state MUST be treated as a stream error. |
| 2316 | * 6.2, 6.6 and 6.10 further mandate that HEADERS/ |
| 2317 | * PUSH_PROMISE/CONTINUATION cause connection errors. |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2318 | */ |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2319 | if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) |
| 2320 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2321 | else |
| 2322 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2323 | goto strm_err; |
| 2324 | } |
| 2325 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2326 | /* Below the management of frames received in closed state is a |
| 2327 | * bit hackish because the spec makes strong differences between |
| 2328 | * streams closed by receiving RST, sending RST, and seeing ES |
| 2329 | * in both directions. In addition to this, the creation of a |
| 2330 | * new stream reusing the identifier of a closed one will be |
| 2331 | * detected here. Given that we cannot keep track of all closed |
| 2332 | * streams forever, we consider that unknown closed streams were |
| 2333 | * closed on RST received, which allows us to respond with an |
| 2334 | * RST without breaking the connection (eg: to abort a transfer). |
| 2335 | * Some frames have to be silently ignored as well. |
| 2336 | */ |
| 2337 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
Willy Tarreau | 3ad5d31 | 2019-01-29 18:33:26 +0100 | [diff] [blame] | 2338 | if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2339 | /* #5.1.1: The identifier of a newly |
| 2340 | * established stream MUST be numerically |
| 2341 | * greater than all streams that the initiating |
| 2342 | * endpoint has opened or reserved. This |
| 2343 | * governs streams that are opened using a |
| 2344 | * HEADERS frame and streams that are reserved |
| 2345 | * using PUSH_PROMISE. An endpoint that |
| 2346 | * receives an unexpected stream identifier |
| 2347 | * MUST respond with a connection error. |
| 2348 | */ |
| 2349 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2350 | goto strm_err; |
| 2351 | } |
| 2352 | |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2353 | if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2354 | /* RFC7540#5.1:closed: an endpoint that |
| 2355 | * receives any frame other than PRIORITY after |
| 2356 | * receiving a RST_STREAM MUST treat that as a |
| 2357 | * stream error of type STREAM_CLOSED. |
| 2358 | * |
| 2359 | * Note that old streams fall into this category |
| 2360 | * and will lead to an RST being sent. |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2361 | * |
| 2362 | * However, we cannot generalize this to all frame types. Those |
| 2363 | * carrying compression state must still be processed before |
| 2364 | * being dropped or we'll desynchronize the decoder. This can |
| 2365 | * happen with request trailers received after sending an |
| 2366 | * RST_STREAM, or with header/trailers responses received after |
| 2367 | * sending RST_STREAM (aborted stream). |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2368 | */ |
| 2369 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2370 | h2c->st0 = H2_CS_FRAME_E; |
| 2371 | goto strm_err; |
| 2372 | } |
| 2373 | |
| 2374 | /* RFC7540#5.1:closed: if this state is reached as a |
| 2375 | * result of sending a RST_STREAM frame, the peer that |
| 2376 | * receives the RST_STREAM might have already sent |
| 2377 | * frames on the stream that cannot be withdrawn. An |
| 2378 | * endpoint MUST ignore frames that it receives on |
| 2379 | * closed streams after it has sent a RST_STREAM |
| 2380 | * frame. An endpoint MAY choose to limit the period |
| 2381 | * over which it ignores frames and treat frames that |
| 2382 | * arrive after this time as being in error. |
| 2383 | */ |
Willy Tarreau | 24ff1f8 | 2019-01-30 19:20:09 +0100 | [diff] [blame] | 2384 | if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2385 | /* RFC7540#5.1:closed: any frame other than |
| 2386 | * PRIO/WU/RST in this state MUST be treated as |
| 2387 | * a connection error |
| 2388 | */ |
| 2389 | if (h2c->dft != H2_FT_RST_STREAM && |
| 2390 | h2c->dft != H2_FT_PRIORITY && |
| 2391 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 2392 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2393 | goto strm_err; |
| 2394 | } |
| 2395 | } |
| 2396 | } |
| 2397 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2398 | #if 0 |
| 2399 | // problem below: it is not possible to completely ignore such |
| 2400 | // streams as we need to maintain the compression state as well |
| 2401 | // and for this we need to completely process these frames (eg: |
| 2402 | // HEADERS frames) as well as counting DATA frames to emit |
| 2403 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 2404 | // This is a typical case of layer violation where the |
| 2405 | // transported contents are critical to the connection's |
| 2406 | // validity and must be ignored at the same time :-( |
| 2407 | |
| 2408 | /* graceful shutdown, ignore streams whose ID is higher than |
| 2409 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 2410 | */ |
| 2411 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2412 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2413 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2414 | h2c->dfl -= ret; |
| 2415 | ret = h2c->dfl == 0; |
| 2416 | goto strm_err; |
| 2417 | } |
| 2418 | #endif |
| 2419 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2420 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 2421 | case H2_FT_SETTINGS: |
| 2422 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2423 | ret = h2c_handle_settings(h2c); |
| 2424 | |
| 2425 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2426 | ret = h2c_ack_settings(h2c); |
| 2427 | break; |
| 2428 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 2429 | case H2_FT_PING: |
| 2430 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2431 | ret = h2c_handle_ping(h2c); |
| 2432 | |
| 2433 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2434 | ret = h2c_ack_ping(h2c); |
| 2435 | break; |
| 2436 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 2437 | case H2_FT_WINDOW_UPDATE: |
| 2438 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2439 | ret = h2c_handle_window_update(h2c, h2s); |
| 2440 | break; |
| 2441 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2442 | case H2_FT_CONTINUATION: |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2443 | /* RFC7540#6.10: CONTINUATION may only be preceeded by |
| 2444 | * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These |
| 2445 | * frames' parsers consume all following CONTINUATION |
| 2446 | * frames so this one is out of sequence. |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2447 | */ |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2448 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2449 | sess_log(h2c->conn->owner); |
| 2450 | goto fail; |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2451 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2452 | case H2_FT_HEADERS: |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2453 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2454 | if (h2c->flags & H2_CF_IS_BACK) |
| 2455 | tmp_h2s = h2c_bck_handle_headers(h2c, h2s); |
| 2456 | else |
| 2457 | tmp_h2s = h2c_frt_handle_headers(h2c, h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2458 | if (tmp_h2s) { |
| 2459 | h2s = tmp_h2s; |
| 2460 | ret = 1; |
| 2461 | } |
| 2462 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2463 | break; |
| 2464 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2465 | case H2_FT_DATA: |
| 2466 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2467 | ret = h2c_frt_handle_data(h2c, h2s); |
| 2468 | |
| 2469 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2470 | ret = h2c_send_strm_wu(h2c); |
| 2471 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2472 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 2473 | case H2_FT_PRIORITY: |
| 2474 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2475 | ret = h2c_handle_priority(h2c); |
| 2476 | break; |
| 2477 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2478 | case H2_FT_RST_STREAM: |
| 2479 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2480 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 2481 | break; |
| 2482 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 2483 | case H2_FT_GOAWAY: |
| 2484 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2485 | ret = h2c_handle_goaway(h2c); |
| 2486 | break; |
| 2487 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2488 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2489 | default: |
| 2490 | /* drop frames that we ignore. They may be larger than |
| 2491 | * the buffer so we drain all of their contents until |
| 2492 | * we reach the end. |
| 2493 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2494 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2495 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2496 | h2c->dfl -= ret; |
| 2497 | ret = h2c->dfl == 0; |
| 2498 | } |
| 2499 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2500 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2501 | /* We may have to send an RST if not done yet */ |
| 2502 | if (h2s->st == H2_SS_ERROR) |
| 2503 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2504 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2505 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2506 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2507 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2508 | /* error or missing data condition met above ? */ |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2509 | if (ret <= 0) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2510 | break; |
| 2511 | |
| 2512 | if (h2c->st0 != H2_CS_FRAME_H) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2513 | b_del(&h2c->dbuf, h2c->dfl); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2514 | h2c->st0 = H2_CS_FRAME_H; |
| 2515 | } |
| 2516 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2517 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2518 | if (h2c->rcvd_c > 0 && |
| 2519 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2520 | h2c_send_conn_wu(h2c); |
| 2521 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2522 | fail: |
| 2523 | /* we can go here on missing data, blocked response or error */ |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2524 | if (h2s && h2s->cs && |
| 2525 | (b_data(&h2s->rxbuf) || |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2526 | (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS|CS_FL_EOI)))) { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2527 | /* we may have to signal the upper layers */ |
| 2528 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2529 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2530 | } |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2531 | |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 2532 | h2c_restart_reading(h2c); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2533 | } |
| 2534 | |
| 2535 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2536 | * the end. |
| 2537 | */ |
| 2538 | static int h2_process_mux(struct h2c *h2c) |
| 2539 | { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2540 | struct h2s *h2s; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2541 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2542 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2543 | if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) { |
| 2544 | if (unlikely(h2c_bck_send_preface(h2c) <= 0)) { |
| 2545 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2546 | if (h2c->st0 == H2_CS_ERROR) { |
| 2547 | h2c->st0 = H2_CS_ERROR2; |
| 2548 | sess_log(h2c->conn->owner); |
| 2549 | } |
| 2550 | goto fail; |
| 2551 | } |
| 2552 | h2c->st0 = H2_CS_SETTINGS1; |
| 2553 | } |
| 2554 | /* need to wait for the other side */ |
Willy Tarreau | 75a930a | 2018-12-12 08:03:58 +0100 | [diff] [blame] | 2555 | if (h2c->st0 < H2_CS_FRAME_H) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2556 | return 1; |
| 2557 | } |
| 2558 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2559 | /* start by sending possibly pending window updates */ |
| 2560 | if (h2c->rcvd_c > 0 && |
| 2561 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2562 | h2c_send_conn_wu(h2c) < 0) |
| 2563 | goto fail; |
| 2564 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2565 | /* First we always process the flow control list because the streams |
| 2566 | * waiting there were already elected for immediate emission but were |
| 2567 | * blocked just on this. |
| 2568 | */ |
| 2569 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2570 | list_for_each_entry(h2s, &h2c->fctl_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2571 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2572 | h2c->st0 >= H2_CS_ERROR) |
| 2573 | break; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2574 | if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE) |
| 2575 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2576 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2577 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2578 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2579 | h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2580 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2581 | tasklet_wakeup(h2s->send_wait->task); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2582 | } |
| 2583 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2584 | list_for_each_entry(h2s, &h2c->send_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2585 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2586 | break; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2587 | if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE) |
| 2588 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2589 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2590 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2591 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2592 | h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2593 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2594 | tasklet_wakeup(h2s->send_wait->task); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2595 | } |
| 2596 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2597 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2598 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2599 | if (h2c->st0 == H2_CS_ERROR) { |
| 2600 | if (h2c->max_id >= 0) { |
| 2601 | h2c_send_goaway_error(h2c, NULL); |
| 2602 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2603 | return 0; |
| 2604 | } |
| 2605 | |
| 2606 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2607 | } |
| 2608 | return 1; |
| 2609 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2610 | return (1); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2611 | } |
| 2612 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2613 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2614 | /* Attempt to read data, and subscribe if none available. |
| 2615 | * The function returns 1 if data has been received, otherwise zero. |
| 2616 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2617 | static int h2_recv(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2618 | { |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2619 | struct connection *conn = h2c->conn; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2620 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2621 | int max; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2622 | size_t ret; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2623 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2624 | if (h2c->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2625 | return (b_data(&h2c->dbuf)); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2626 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2627 | if (!h2_recv_allowed(h2c)) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2628 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2629 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2630 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2631 | if (!buf) { |
| 2632 | h2c->flags |= H2_CF_DEM_DALLOC; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2633 | return 0; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2634 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2635 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2636 | do { |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 2637 | b_realign_if_empty(buf); |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2638 | if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
| 2639 | /* HTX in use : try to pre-align the buffer like the |
| 2640 | * rxbufs will be to optimize memory copies. We'll make |
| 2641 | * sure that the frame header lands at the end of the |
| 2642 | * HTX block to alias it upon recv. We cannot use the |
| 2643 | * head because rcv_buf() will realign the buffer if |
| 2644 | * it's empty. Thus we cheat and pretend we already |
| 2645 | * have a few bytes there. |
| 2646 | */ |
| 2647 | max = buf_room_for_htx_data(buf) + 9; |
Willy Tarreau | c0960d1 | 2018-12-14 10:59:15 +0100 | [diff] [blame] | 2648 | buf->head = sizeof(struct htx) - 9; |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2649 | } |
| 2650 | else |
| 2651 | max = b_room(buf); |
| 2652 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2653 | if (max) |
| 2654 | ret = conn->xprt->rcv_buf(conn, buf, max, 0); |
| 2655 | else |
| 2656 | ret = 0; |
| 2657 | } while (ret > 0); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2658 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2659 | if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size)) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2660 | conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event); |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2661 | |
Olivier Houchard | a1411e6 | 2018-08-17 18:42:48 +0200 | [diff] [blame] | 2662 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2663 | h2_release_buf(h2c, &h2c->dbuf); |
Olivier Houchard | 4667773 | 2018-11-29 17:06:17 +0100 | [diff] [blame] | 2664 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2665 | } |
| 2666 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2667 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2668 | h2c->flags |= H2_CF_DEM_DFULL; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2669 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2670 | } |
| 2671 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2672 | /* Try to send data if possible. |
| 2673 | * The function returns 1 if data have been sent, otherwise zero. |
| 2674 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2675 | static int h2_send(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2676 | { |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2677 | struct connection *conn = h2c->conn; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2678 | int done; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2679 | int sent = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2680 | |
| 2681 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 2682 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2683 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2684 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2685 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2686 | /* a handshake was requested */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2687 | goto schedule; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2688 | } |
| 2689 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2690 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2691 | * pending streams into the existing buffer until it's reportedly full |
| 2692 | * or the end of send requests is reached. Then it tries to send this |
| 2693 | * buffer's contents out, marks it not full if at least one byte could |
| 2694 | * be sent, and tries again. |
| 2695 | * |
| 2696 | * The snd_buf() function normally takes a "flags" argument which may |
| 2697 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2698 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2699 | * connection is streaming lots of data (used to increase TLS record |
| 2700 | * size at the expense of latency). The former can be sent any time |
| 2701 | * there's a buffer full flag, as it indicates at least one stream |
| 2702 | * attempted to send and failed so there are pending data. An |
| 2703 | * alternative would be to set it as long as there's an active stream |
| 2704 | * but that would be problematic for ACKs until we have an absolute |
| 2705 | * guarantee that all waiters have at least one byte to send. The |
| 2706 | * latter should possibly not be set for now. |
| 2707 | */ |
| 2708 | |
| 2709 | done = 0; |
| 2710 | while (!done) { |
| 2711 | unsigned int flags = 0; |
| 2712 | |
| 2713 | /* fill as much as we can into the current buffer */ |
| 2714 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2715 | done = h2_process_mux(h2c); |
| 2716 | |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2717 | if (h2c->flags & H2_CF_MUX_MALLOC) |
| 2718 | break; |
| 2719 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2720 | if (conn->flags & CO_FL_ERROR) |
| 2721 | break; |
| 2722 | |
| 2723 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2724 | flags |= CO_SFL_MSG_MORE; |
| 2725 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2726 | if (b_data(&h2c->mbuf)) { |
| 2727 | int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2728 | if (!ret) |
| 2729 | break; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2730 | sent = 1; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2731 | b_del(&h2c->mbuf, ret); |
| 2732 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2733 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2734 | |
| 2735 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2736 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2737 | } |
| 2738 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2739 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2740 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2741 | b_reset(&h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2742 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2743 | /* We're not full anymore, so we can wake any task that are waiting |
| 2744 | * for us. |
| 2745 | */ |
| 2746 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2747 | struct h2s *h2s; |
| 2748 | |
| 2749 | list_for_each_entry(h2s, &h2c->send_list, list) { |
| 2750 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2751 | break; |
| 2752 | if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE) |
| 2753 | continue; |
| 2754 | |
| 2755 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2756 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2757 | h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2758 | tasklet_wakeup(h2s->send_wait->task); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2759 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 2760 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2761 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2762 | /* We're done, no more to send */ |
| 2763 | if (!b_data(&h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2764 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2765 | schedule: |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2766 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
| 2767 | conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event); |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2768 | return sent; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2769 | } |
| 2770 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 2771 | /* this is the tasklet referenced in h2c->wait_event.task */ |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2772 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2773 | { |
| 2774 | struct h2c *h2c = ctx; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2775 | int ret = 0; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2776 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2777 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2778 | ret = h2_send(h2c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2779 | if (!(h2c->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2780 | ret |= h2_recv(h2c); |
Willy Tarreau | cef5c8e | 2018-12-18 10:29:54 +0100 | [diff] [blame] | 2781 | if (ret || b_data(&h2c->dbuf)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2782 | h2_process(h2c); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2783 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2784 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2785 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2786 | /* callback called on any event by the connection handler. |
| 2787 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2788 | * destruction of the connection (which normally doesn not happen in h2). |
| 2789 | */ |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2790 | static int h2_process(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2791 | { |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2792 | struct connection *conn = h2c->conn; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2793 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2794 | if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2795 | h2_process_demux(h2c); |
| 2796 | |
| 2797 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2798 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2799 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2800 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2801 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 2802 | } |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2803 | h2_send(h2c); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2804 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 2805 | if (unlikely(h2c->proxy->state == PR_STSTOPPED)) { |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2806 | /* frontend is stopping, reload likely in progress, let's try |
| 2807 | * to announce a graceful shutdown if not yet done. We don't |
| 2808 | * care if it fails, it will be tried again later. |
| 2809 | */ |
| 2810 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 2811 | if (h2c->last_sid < 0) |
| 2812 | h2c->last_sid = (1U << 31) - 1; |
| 2813 | h2c_send_goaway_error(h2c, NULL); |
| 2814 | } |
| 2815 | } |
| 2816 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2817 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2818 | * If we received early data, and the handshake is done, wake |
| 2819 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2820 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2821 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 2822 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 2823 | struct eb32_node *node; |
| 2824 | struct h2s *h2s; |
| 2825 | |
| 2826 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 2827 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 2828 | |
| 2829 | while (node) { |
| 2830 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | fde287c | 2018-12-19 18:33:16 +0100 | [diff] [blame] | 2831 | if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2832 | h2s_notify_recv(h2s); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2833 | node = eb32_next(node); |
| 2834 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2835 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2836 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 2837 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 2838 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 2839 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 2840 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 2841 | h2_wake_some_streams(h2c, 0, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2842 | |
| 2843 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2844 | /* no more stream, kill the connection now */ |
| 2845 | h2_release(conn); |
| 2846 | return -1; |
| 2847 | } |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2848 | } |
| 2849 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2850 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2851 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2852 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2853 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 2854 | h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 2855 | (h2c->st0 != H2_CS_ERROR && |
| 2856 | !b_data(&h2c->mbuf) && |
| 2857 | (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && |
| 2858 | ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list)))) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2859 | h2_release_buf(h2c, &h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2860 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2861 | if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2862 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2863 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2864 | task_queue(h2c->task); |
| 2865 | } |
| 2866 | else |
| 2867 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2868 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2869 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2870 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2871 | return 0; |
| 2872 | } |
| 2873 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 2874 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2875 | static int h2_wake(struct connection *conn) |
| 2876 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2877 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2878 | |
| 2879 | return (h2_process(h2c)); |
| 2880 | } |
| 2881 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2882 | /* Connection timeout management. The principle is that if there's no receipt |
| 2883 | * nor sending for a certain amount of time, the connection is closed. If the |
| 2884 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 2885 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 2886 | * GOAWAY frame. |
| 2887 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2888 | static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2889 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2890 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2891 | int expired = tick_is_expired(t->expire, now_ms); |
| 2892 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2893 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2894 | return t; |
| 2895 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2896 | task_delete(t); |
| 2897 | task_free(t); |
| 2898 | |
| 2899 | if (!h2c) { |
| 2900 | /* resources were already deleted */ |
| 2901 | return NULL; |
| 2902 | } |
| 2903 | |
| 2904 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2905 | h2c_error(h2c, H2_ERR_NO_ERROR); |
| 2906 | h2_wake_some_streams(h2c, 0, 0); |
| 2907 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2908 | if (b_data(&h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2909 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 2910 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2911 | } |
| 2912 | |
| 2913 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2914 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2915 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 2916 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2917 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2918 | if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) { |
| 2919 | int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2920 | if (ret > 0) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2921 | b_del(&h2c->mbuf, ret); |
| 2922 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2923 | } |
| 2924 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2925 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2926 | /* either we can release everything now or it will be done later once |
| 2927 | * the last stream closes. |
| 2928 | */ |
| 2929 | if (eb_is_empty(&h2c->streams_by_id)) |
| 2930 | h2_release(h2c->conn); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2931 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2932 | return NULL; |
| 2933 | } |
| 2934 | |
| 2935 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2936 | /*******************************************/ |
| 2937 | /* functions below are used by the streams */ |
| 2938 | /*******************************************/ |
| 2939 | |
| 2940 | /* |
| 2941 | * Attach a new stream to a connection |
| 2942 | * (Used for outgoing connections) |
| 2943 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2944 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2945 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2946 | struct conn_stream *cs; |
| 2947 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2948 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2949 | |
| 2950 | cs = cs_new(conn); |
| 2951 | if (!cs) |
| 2952 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2953 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2954 | if (!h2s) { |
| 2955 | cs_free(cs); |
| 2956 | return NULL; |
| 2957 | } |
| 2958 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2959 | } |
| 2960 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 2961 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 2962 | * We have to scan because we may have some orphan streams. It might be |
| 2963 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 2964 | * orphans. |
| 2965 | */ |
| 2966 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 2967 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2968 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 2969 | struct h2s *h2s; |
| 2970 | struct eb32_node *node; |
| 2971 | |
| 2972 | node = eb32_first(&h2c->streams_by_id); |
| 2973 | while (node) { |
| 2974 | h2s = container_of(node, struct h2s, by_id); |
| 2975 | if (h2s->cs) |
| 2976 | return h2s->cs; |
| 2977 | node = eb32_next(node); |
| 2978 | } |
| 2979 | return NULL; |
| 2980 | } |
| 2981 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2982 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 2983 | * Destroy the mux and the associated connection, if it is no longer used |
| 2984 | */ |
| 2985 | static void h2_destroy(struct connection *conn) |
| 2986 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2987 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 2988 | |
| 2989 | if (eb_is_empty(&h2c->streams_by_id)) |
| 2990 | h2_release(h2c->conn); |
| 2991 | } |
| 2992 | |
| 2993 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2994 | * Detach the stream from the connection and possibly release the connection. |
| 2995 | */ |
| 2996 | static void h2_detach(struct conn_stream *cs) |
| 2997 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2998 | struct h2s *h2s = cs->ctx; |
| 2999 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3000 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3001 | |
| 3002 | cs->ctx = NULL; |
| 3003 | if (!h2s) |
| 3004 | return; |
| 3005 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3006 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3007 | h2c = h2s->h2c; |
| 3008 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 3009 | h2c->nb_cs--; |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 3010 | if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY && |
| 3011 | !h2_frt_has_too_many_cs(h2c)) { |
| 3012 | /* frontend connection was blocking new streams creation */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3013 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 3014 | h2c_restart_reading(h2c); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3015 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3016 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3017 | /* this stream may be blocked waiting for some data to leave (possibly |
| 3018 | * an ES or RST frame), so orphan it in this case. |
| 3019 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 3020 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 3021 | (h2c->st0 < H2_CS_ERROR) && |
Olivier Houchard | 16ff261 | 2019-03-21 15:48:46 +0100 | [diff] [blame] | 3022 | (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) && (h2s->send_wait || h2s->recv_wait)) |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3023 | return; |
| 3024 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3025 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 3026 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 3027 | /* unblock the connection if it was blocked on this |
| 3028 | * stream. |
| 3029 | */ |
| 3030 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 3031 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 3032 | h2c_restart_reading(h2c); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3033 | } |
| 3034 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 3035 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3036 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3037 | if (h2c->flags & H2_CF_IS_BACK && |
| 3038 | (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3039 | if (!(h2c->conn->flags & |
| 3040 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 3041 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3042 | h2c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3043 | if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) { |
| 3044 | h2c->conn->owner = NULL; |
| 3045 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3046 | if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn)) |
| 3047 | /* The server doesn't want it, let's kill the connection right away */ |
| 3048 | h2c->conn->mux->destroy(h2c->conn); |
| 3049 | return; |
| 3050 | } |
| 3051 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3052 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3053 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3054 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 3055 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 3056 | return; |
| 3057 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3058 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3059 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3060 | h2c->conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 3061 | if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3062 | struct server *srv = objt_server(h2c->conn->target); |
| 3063 | |
| 3064 | if (srv) { |
| 3065 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 3066 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 3067 | else |
| 3068 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 3069 | } |
| 3070 | |
| 3071 | } |
| 3072 | } |
| 3073 | } |
| 3074 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3075 | /* We don't want to close right now unless we're removing the |
| 3076 | * last stream, and either the connection is in error, or it |
| 3077 | * reached the ID already specified in a GOAWAY frame received |
| 3078 | * or sent (as seen by last_sid >= 0). |
| 3079 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3080 | if (h2c_is_dead(h2c)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3081 | /* no more stream will come, kill it now */ |
| 3082 | h2_release(h2c->conn); |
| 3083 | } |
| 3084 | else if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3085 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3086 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3087 | task_queue(h2c->task); |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 3088 | } |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3089 | else |
| 3090 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3091 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3092 | } |
| 3093 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3094 | /* Performs a synchronous or asynchronous shutr(). |
| 3095 | * FIXME: guess what the return code tries to indicate! |
| 3096 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3097 | static int h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3098 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3099 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3100 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3101 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 3102 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3103 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3104 | |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3105 | /* a connstream may require us to immediately kill the whole connection |
| 3106 | * for example because of a "tcp-request content reject" rule that is |
| 3107 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3108 | * close the connection. |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3109 | */ |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3110 | if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) && |
| 3111 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3112 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3113 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3114 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3115 | else if (!(h2s->flags & H2_SF_HEADERS_SENT)) { |
| 3116 | /* Nothing was never sent for this stream, so reset with |
| 3117 | * REFUSED_STREAM error to let the client retry the |
| 3118 | * request. |
| 3119 | */ |
| 3120 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3121 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3122 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3123 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3124 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3125 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3126 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3127 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3128 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3129 | h2s_close(h2s); |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3130 | |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3131 | return 0; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3132 | add_to_list: |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3133 | if (LIST_ISEMPTY(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3134 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3135 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3136 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3137 | h2s->send_wait = sw; |
| 3138 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3139 | h2s->send_wait = sw; |
| 3140 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3141 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3142 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3143 | /* Let the handler know we want shutr */ |
| 3144 | sw->handle = (void *)((long)sw->handle | 1); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3145 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3146 | } |
| 3147 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3148 | /* Performs a synchronous or asynchronous shutw(). |
| 3149 | * FIXME: guess what the return code tries to indicate! |
| 3150 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3151 | static int h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3152 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3153 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3154 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3155 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 3156 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3157 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3158 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3159 | if (h2s->flags & H2_SF_HEADERS_SENT) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3160 | /* we can cleanly close using an empty data frame only after headers */ |
| 3161 | |
| 3162 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 3163 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3164 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3165 | |
| 3166 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3167 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3168 | else |
| 3169 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3170 | } else { |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3171 | /* a connstream may require us to immediately kill the whole connection |
| 3172 | * for example because of a "tcp-request content reject" rule that is |
| 3173 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3174 | * close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3175 | */ |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3176 | if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) && |
| 3177 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3178 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3179 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3180 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3181 | else { |
| 3182 | /* Nothing was never sent for this stream, so reset with |
| 3183 | * REFUSED_STREAM error to let the client retry the |
| 3184 | * request. |
| 3185 | */ |
| 3186 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3187 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3188 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3189 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3190 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3191 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3192 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3193 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3194 | } |
| 3195 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3196 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3197 | tasklet_wakeup(h2c->wait_event.task); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3198 | return 0; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3199 | |
| 3200 | add_to_list: |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3201 | if (LIST_ISEMPTY(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3202 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3203 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3204 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3205 | h2s->send_wait = sw; |
| 3206 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3207 | h2s->send_wait = sw; |
| 3208 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3209 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3210 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3211 | /* let the handler know we want to shutw */ |
| 3212 | sw->handle = (void *)((long)(sw->handle) | 2); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3213 | return 1; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3214 | } |
| 3215 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3216 | /* This is the tasklet referenced in h2s->wait_event.task, it is used for |
| 3217 | * deferred shutdowns when the h2_detach() was done but the mux buffer was full |
| 3218 | * and prevented the last frame from being emitted. |
| 3219 | */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3220 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3221 | { |
| 3222 | struct h2s *h2s = ctx; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3223 | long reason = (long)h2s->wait_event.handle; |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3224 | int ret = 0; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3225 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3226 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3227 | if (h2s->send_wait) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3228 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3229 | h2s->send_wait->events |= SUB_RETRY_SEND; |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3230 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3231 | if (reason & 2) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3232 | ret |= h2_do_shutw(h2s); |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3233 | if (reason & 1) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3234 | ret |= h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3235 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3236 | if (!ret) { |
| 3237 | /* We're done trying to send, remove ourself from the send_list */ |
| 3238 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 3239 | h2s->send_wait = NULL; |
| 3240 | LIST_DEL_INIT(&h2s->list); |
| 3241 | } |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3242 | /* We're no longer trying to send anything, let's destroy the h2s */ |
Olivier Houchard | 01d4cb5 | 2019-03-25 13:25:02 +0100 | [diff] [blame] | 3243 | if (!ret && (h2s->cs == NULL)) { |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3244 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3245 | h2s_destroy(h2s); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3246 | |
| 3247 | if (h2c_is_dead(h2c)) |
| 3248 | h2_release(h2c->conn); |
| 3249 | } |
| 3250 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3251 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3252 | } |
| 3253 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3254 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3255 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3256 | { |
| 3257 | struct h2s *h2s = cs->ctx; |
| 3258 | |
| 3259 | if (!mode) |
| 3260 | return; |
| 3261 | |
| 3262 | h2_do_shutr(h2s); |
| 3263 | } |
| 3264 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3265 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3266 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3267 | { |
| 3268 | struct h2s *h2s = cs->ctx; |
| 3269 | |
| 3270 | h2_do_shutw(h2s); |
| 3271 | } |
| 3272 | |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3273 | /* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3274 | * HTX request or response depending on the connection's side. Returns a |
| 3275 | * positive value on success, a negative value on failure, or 0 if it couldn't |
| 3276 | * proceed. May report connection errors in h2c->errcode if the frame is |
| 3277 | * non-decodable and the connection unrecoverable. In absence of connection |
| 3278 | * error when a failure is reported, the caller must assume a stream error. |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3279 | * |
| 3280 | * The function may fold CONTINUATION frames into the initial HEADERS frame |
| 3281 | * by removing padding and next frame header, then moving the CONTINUATION |
| 3282 | * frame's payload and adjusting h2c->dfl to match the new aggregated frame, |
| 3283 | * leaving a hole between the main frame and the beginning of the next one. |
| 3284 | * The possibly remaining incomplete or next frame at the end may be moved |
| 3285 | * if the aggregated frame is not deleted, in order to fill the hole. Wrapped |
| 3286 | * HEADERS frames are unwrapped into a temporary buffer before decoding. |
| 3287 | * |
| 3288 | * A buffer at the beginning of processing may look like this : |
| 3289 | * |
| 3290 | * ,---.---------.-----.--------------.--------------.------.---. |
| 3291 | * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///| |
| 3292 | * `---^---------^-----^--------------^--------------^------^---' |
| 3293 | * | | <-----> | | |
| 3294 | * area | dpl | wrap |
| 3295 | * |<--------------> | |
| 3296 | * | dfl | |
| 3297 | * |<-------------------------------------------------->| |
| 3298 | * head data |
| 3299 | * |
| 3300 | * Padding is automatically overwritten when folding, participating to the |
| 3301 | * hole size after dfl : |
| 3302 | * |
| 3303 | * ,---.------------------------.-----.--------------.------.---. |
| 3304 | * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///| |
| 3305 | * `---^------------------------^-----^--------------^------^---' |
| 3306 | * | | <-----> | | |
| 3307 | * area | hole | wrap |
| 3308 | * |<-----------------------> | |
| 3309 | * | dfl | |
| 3310 | * |<-------------------------------------------------->| |
| 3311 | * head data |
| 3312 | * |
| 3313 | * Please note that the HEADERS frame is always deprived from its PADLEN byte |
| 3314 | * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY |
| 3315 | * bit. |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3316 | * |
| 3317 | * The <flags> field must point to either the stream's flags or to a copy of it |
| 3318 | * so that the function can update the following flags : |
| 3319 | * - H2_SF_DATA_CLEN when content-length is seen |
| 3320 | * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion |
| 3321 | * - H2_SF_HEADERS_RCVD once the frame is successfully decoded |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3322 | * |
| 3323 | * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to |
| 3324 | * decoding, in order to detect if we're dealing with a headers or a trailers |
| 3325 | * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3326 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3327 | static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3328 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3329 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3330 | struct buffer *tmp = get_trash_chunk(); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3331 | struct http_hdr list[MAX_HTTP_HDR * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3332 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3333 | unsigned int msgf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3334 | struct htx *htx = NULL; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3335 | int flen; // header frame len |
| 3336 | int hole = 0; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3337 | int ret = 0; |
| 3338 | int outlen; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3339 | int wrap; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3340 | int try = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3341 | |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3342 | next_frame: |
| 3343 | if (b_data(&h2c->dbuf) - hole < h2c->dfl) |
| 3344 | goto leave; // incomplete input frame |
| 3345 | |
| 3346 | /* No END_HEADERS means there's one or more CONTINUATION frames. In |
| 3347 | * this case, we'll try to paste it immediately after the initial |
| 3348 | * HEADERS frame payload and kill any possible padding. The initial |
| 3349 | * frame's length will be increased to represent the concatenation |
| 3350 | * of the two frames. The next frame is read from position <tlen> |
| 3351 | * and written at position <flen> (minus padding if some is present). |
| 3352 | */ |
| 3353 | if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) { |
| 3354 | struct h2_fh hdr; |
| 3355 | int clen; // CONTINUATION frame's payload length |
| 3356 | |
| 3357 | if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) { |
| 3358 | /* no more data, the buffer may be full, either due to |
| 3359 | * too large a frame or because of too large a hole that |
| 3360 | * we're going to compact at the end. |
| 3361 | */ |
| 3362 | goto leave; |
| 3363 | } |
| 3364 | |
| 3365 | if (hdr.ft != H2_FT_CONTINUATION) { |
| 3366 | /* RFC7540#6.10: frame of unexpected type */ |
| 3367 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3368 | goto fail; |
| 3369 | } |
| 3370 | |
| 3371 | if (hdr.sid != h2c->dsi) { |
| 3372 | /* RFC7540#6.10: frame of different stream */ |
| 3373 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3374 | goto fail; |
| 3375 | } |
| 3376 | |
| 3377 | if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { |
| 3378 | /* RFC7540#4.2: invalid frame length */ |
| 3379 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3380 | goto fail; |
| 3381 | } |
| 3382 | |
| 3383 | /* detect when we must stop aggragating frames */ |
| 3384 | h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS; |
| 3385 | |
| 3386 | /* Take as much as we can of the CONTINUATION frame's payload */ |
| 3387 | clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9); |
| 3388 | if (clen > hdr.len) |
| 3389 | clen = hdr.len; |
| 3390 | |
| 3391 | /* Move the frame's payload over the padding, hole and frame |
| 3392 | * header. At least one of hole or dpl is null (see diagrams |
| 3393 | * above). The hole moves after the new aggragated frame. |
| 3394 | */ |
| 3395 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9)); |
| 3396 | h2c->dfl += clen - h2c->dpl; |
| 3397 | hole += h2c->dpl + 9; |
| 3398 | h2c->dpl = 0; |
| 3399 | goto next_frame; |
| 3400 | } |
| 3401 | |
| 3402 | flen = h2c->dfl - h2c->dpl; |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3403 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3404 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3405 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3406 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3407 | copy = alloc_trash_chunk(); |
| 3408 | if (!copy) { |
| 3409 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3410 | goto fail; |
| 3411 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3412 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3413 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3414 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3415 | } |
| 3416 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3417 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3418 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3419 | if (read_n32(hdrs) == h2c->dsi) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3420 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3421 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3422 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3423 | } |
| 3424 | |
Willy Tarreau | a01f45e | 2018-12-31 07:41:24 +0100 | [diff] [blame] | 3425 | if (flen < 5) { |
| 3426 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3427 | goto fail; |
| 3428 | } |
| 3429 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3430 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3431 | flen -= 5; |
| 3432 | } |
| 3433 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3434 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3435 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3436 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3437 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3438 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3439 | /* we can't retry a failed decompression operation so we must be very |
| 3440 | * careful not to take any risks. In practice the output buffer is |
| 3441 | * always empty except maybe for trailers, in which case we simply have |
| 3442 | * to wait for the upper layer to finish consuming what is available. |
| 3443 | */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3444 | |
| 3445 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3446 | htx = htx_from_buf(rxbuf); |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3447 | if (!htx_is_empty(htx)) { |
| 3448 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3449 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3450 | } |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3451 | } else { |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3452 | if (b_data(rxbuf)) { |
| 3453 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3454 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3455 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3456 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3457 | rxbuf->head = 0; |
| 3458 | try = b_size(rxbuf); |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3459 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3460 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3461 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3462 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3463 | sizeof(list)/sizeof(list[0]), tmp); |
| 3464 | if (outlen < 0) { |
| 3465 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3466 | goto fail; |
| 3467 | } |
| 3468 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3469 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3470 | * the parser's state to commit these changes and allow us to later |
| 3471 | * fail solely on the stream if needed. |
| 3472 | */ |
| 3473 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3474 | h2c->dfl = hole = 0; |
| 3475 | h2c->st0 = H2_CS_FRAME_H; |
| 3476 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3477 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3478 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3479 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3480 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3481 | goto trailers; |
| 3482 | |
| 3483 | /* This is the first HEADERS frame so it's a headers block */ |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3484 | if (htx) { |
| 3485 | /* HTX mode */ |
| 3486 | if (h2c->flags & H2_CF_IS_BACK) |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3487 | outlen = h2_make_htx_response(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3488 | else |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3489 | outlen = h2_make_htx_request(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3490 | } else { |
| 3491 | /* HTTP/1 mode */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3492 | outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len); |
Willy Tarreau | 8319593 | 2019-01-03 10:26:23 +0100 | [diff] [blame] | 3493 | if (outlen > 0) |
| 3494 | b_add(rxbuf, outlen); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3495 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3496 | |
| 3497 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3498 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3499 | goto fail; |
| 3500 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3501 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3502 | if (msgf & H2_MSGF_BODY) { |
| 3503 | /* a payload is present */ |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3504 | if (msgf & H2_MSGF_BODY_CL) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3505 | *flags |= H2_SF_DATA_CLEN; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3506 | if (htx) |
| 3507 | htx->extra = *body_len; |
| 3508 | } |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3509 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3510 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3511 | } |
| 3512 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3513 | done: |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3514 | /* indicate that a HEADERS frame was received for this stream, except |
| 3515 | * for 1xx responses. For 1xx responses, another HEADERS frame is |
| 3516 | * expected. |
| 3517 | */ |
| 3518 | if (!(msgf & H2_MSGF_RSP_1XX)) |
| 3519 | *flags |= H2_SF_HEADERS_RCVD; |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3520 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3521 | if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3522 | /* Mark the end of message, either using EOM in HTX or with the |
| 3523 | * trailing CRLF after the end of trailers. Note that DATA_CHNK |
| 3524 | * is not set during headers with END_STREAM. |
| 3525 | */ |
| 3526 | if (htx) { |
| 3527 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 3528 | goto fail; |
| 3529 | } |
| 3530 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3531 | if (!b_putblk(rxbuf, "\r\n", 2)) |
| 3532 | goto fail; |
| 3533 | } |
| 3534 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3535 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3536 | /* success */ |
| 3537 | ret = 1; |
| 3538 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3539 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3540 | /* If there is a hole left and it's not at the end, we are forced to |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3541 | * move the remaining data over it. |
| 3542 | */ |
| 3543 | if (hole) { |
| 3544 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3545 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3546 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3547 | b_sub(&h2c->dbuf, hole); |
| 3548 | } |
| 3549 | |
| 3550 | if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) { |
| 3551 | /* too large frames */ |
| 3552 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3553 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3554 | } |
| 3555 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3556 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3557 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3558 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3559 | return ret; |
| 3560 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3561 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3562 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3563 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3564 | |
| 3565 | trailers: |
| 3566 | /* This is the last HEADERS frame hence a trailer */ |
| 3567 | |
| 3568 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3569 | /* It's a trailer but it's missing ES flag */ |
| 3570 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3571 | goto fail; |
| 3572 | } |
| 3573 | |
| 3574 | /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD |
| 3575 | * block, and when using chunks we must send the 0 CRLF marker. For |
| 3576 | * other modes, the trailers are silently dropped. |
| 3577 | */ |
| 3578 | if (htx) { |
| 3579 | if (!htx_add_endof(htx, HTX_BLK_EOD)) |
| 3580 | goto fail; |
Willy Tarreau | 5255f28 | 2019-01-03 18:41:05 +0100 | [diff] [blame] | 3581 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3582 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3583 | } |
| 3584 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3585 | /* Legacy mode with chunked encoding : we must finalize the |
| 3586 | * data block message emit the trailing CRLF */ |
| 3587 | if (!b_putblk(rxbuf, "0\r\n", 3)) |
| 3588 | goto fail; |
Willy Tarreau | e2b05cc | 2019-01-03 16:18:34 +0100 | [diff] [blame] | 3589 | |
| 3590 | outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try); |
| 3591 | if (outlen > 0) |
| 3592 | b_add(rxbuf, outlen); |
| 3593 | else |
| 3594 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3595 | } |
| 3596 | |
| 3597 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3598 | } |
| 3599 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3600 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 3601 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 3602 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 3603 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 3604 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 3605 | * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3606 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3607 | * send the current frame, 0 if it couldn't complete, in which case |
| 3608 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3609 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3610 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3611 | * have checked the frame header and ensured that the frame was complete or the |
| 3612 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3613 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3614 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3615 | { |
| 3616 | struct h2c *h2c = h2s->h2c; |
| 3617 | int block1, block2; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3618 | unsigned int flen = 0; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3619 | unsigned int chklen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3620 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3621 | struct buffer *csbuf; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3622 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3623 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3624 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3625 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3626 | if (!csbuf) { |
| 3627 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3628 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3629 | } |
| 3630 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3631 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3632 | flen = h2c->dfl - h2c->dpl; |
Olivier Houchard | 2f30883 | 2018-12-19 15:53:53 +0100 | [diff] [blame] | 3633 | if (h2c->proxy->options2 & PR_O2_USE_HTX) |
| 3634 | htx = htx_from_buf(csbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3635 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3636 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3637 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3638 | if (flen > b_data(&h2c->dbuf)) { |
| 3639 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3640 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3641 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3642 | } |
| 3643 | |
Willy Tarreau | a9b7796 | 2019-01-31 07:23:00 +0100 | [diff] [blame] | 3644 | if (htx) { |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3645 | block1 = htx_free_data_space(htx); |
| 3646 | if (!block1) { |
| 3647 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3648 | goto fail; |
| 3649 | } |
| 3650 | if (flen > block1) |
| 3651 | flen = block1; |
| 3652 | |
| 3653 | /* here, flen is the max we can copy into the output buffer */ |
| 3654 | block1 = b_contig_data(&h2c->dbuf, 0); |
| 3655 | if (flen > block1) |
| 3656 | flen = block1; |
| 3657 | |
| 3658 | if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) { |
| 3659 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3660 | goto fail; |
| 3661 | } |
| 3662 | |
| 3663 | b_del(&h2c->dbuf, flen); |
| 3664 | h2c->dfl -= flen; |
| 3665 | h2c->rcvd_c += flen; |
| 3666 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3667 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3668 | if (h2s->flags & H2_SF_DATA_CLEN) { |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3669 | h2s->body_len -= flen; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3670 | htx->extra = h2s->body_len; |
| 3671 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3672 | goto try_again; |
| 3673 | } |
| 3674 | else if (unlikely(b_space_wraps(csbuf))) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3675 | /* it doesn't fit and the buffer is fragmented, |
| 3676 | * so let's defragment it and try again. |
| 3677 | */ |
| 3678 | b_slow_realign(csbuf, trash.area, 0); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3679 | } |
| 3680 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3681 | /* chunked-encoding requires more room */ |
| 3682 | if (h2s->flags & H2_SF_DATA_CHNK) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3683 | chklen = MIN(flen, b_room(csbuf)); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3684 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 3685 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 3686 | (chklen < 1048576) ? 4 : 8; |
| 3687 | chklen += 4; // CRLF, CRLF |
| 3688 | } |
| 3689 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3690 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3691 | if (flen + chklen > b_room(csbuf)) { |
| 3692 | if (chklen >= b_room(csbuf)) { |
| 3693 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3694 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3695 | } |
| 3696 | flen = b_room(csbuf) - chklen; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3697 | } |
| 3698 | |
| 3699 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3700 | /* emit the chunk size */ |
| 3701 | unsigned int chksz = flen; |
| 3702 | char str[10]; |
| 3703 | char *beg; |
| 3704 | |
| 3705 | beg = str + sizeof(str); |
| 3706 | *--beg = '\n'; |
| 3707 | *--beg = '\r'; |
| 3708 | do { |
| 3709 | *--beg = hextab[chksz & 0xF]; |
| 3710 | } while (chksz >>= 4); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3711 | b_putblk(csbuf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3712 | } |
| 3713 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3714 | /* Block1 is the length of the first block before the buffer wraps, |
| 3715 | * block2 is the optional second block to reach the end of the frame. |
| 3716 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3717 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3718 | if (block1 > flen) |
| 3719 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3720 | block2 = flen - block1; |
| 3721 | |
| 3722 | if (block1) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3723 | b_putblk(csbuf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3724 | |
| 3725 | if (block2) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3726 | b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3727 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3728 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3729 | /* emit the CRLF */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3730 | b_putblk(csbuf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3731 | } |
| 3732 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3733 | /* now mark the input data as consumed (will be deleted from the buffer |
| 3734 | * by the caller when seeing FRAME_A after sending the window update). |
| 3735 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3736 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3737 | h2c->dfl -= flen; |
| 3738 | h2c->rcvd_c += flen; |
| 3739 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 3740 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3741 | if (h2s->flags & H2_SF_DATA_CLEN) |
| 3742 | h2s->body_len -= flen; |
| 3743 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3744 | if (h2c->dfl > h2c->dpl) { |
| 3745 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3746 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3747 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3748 | } |
| 3749 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3750 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3751 | /* here we're done with the frame, all the payload (except padding) was |
| 3752 | * transferred. |
| 3753 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3754 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3755 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 3756 | if (htx) { |
| 3757 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3758 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3759 | goto fail; |
| 3760 | } |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3761 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3762 | else if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3763 | /* emit the trailing 0 CRLF CRLF */ |
| 3764 | if (b_room(csbuf) < 5) { |
| 3765 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3766 | goto fail; |
| 3767 | } |
| 3768 | chklen += 5; |
| 3769 | b_putblk(csbuf, "0\r\n\r\n", 5); |
| 3770 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3771 | } |
| 3772 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 3773 | h2c->rcvd_c += h2c->dpl; |
| 3774 | h2c->rcvd_s += h2c->dpl; |
| 3775 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3776 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3777 | if (htx) |
| 3778 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3779 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3780 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3781 | if (htx) |
| 3782 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3783 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3784 | } |
| 3785 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3786 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 3787 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 3788 | * number of bytes sent. The caller must check the stream's status to detect |
| 3789 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3790 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3791 | static size_t h2s_frt_make_resp_headers(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3792 | { |
| 3793 | struct http_hdr list[MAX_HTTP_HDR]; |
| 3794 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3795 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3796 | struct buffer outbuf; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3797 | union h1_sl sl; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3798 | int es_now = 0; |
| 3799 | int ret = 0; |
| 3800 | int hdr; |
| 3801 | |
| 3802 | if (h2c_mux_busy(h2c, h2s)) { |
| 3803 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3804 | return 0; |
| 3805 | } |
| 3806 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3807 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3808 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3809 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3810 | return 0; |
| 3811 | } |
| 3812 | |
| 3813 | /* First, try to parse the H1 response and index it into <list>. |
| 3814 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 3815 | * block does not wrap and we can safely read it this way without |
| 3816 | * having to realign the buffer. |
| 3817 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3818 | ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max, |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3819 | list, sizeof(list)/sizeof(list[0]), h1m, &sl); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3820 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 3821 | /* incomplete or invalid response, this is abnormal coming from |
| 3822 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 3823 | * so that won't be fixed, raise an error now. |
| 3824 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3825 | * FIXME: we should instead add the ability to only return a |
| 3826 | * 502 bad gateway. But in theory this is not supposed to |
| 3827 | * happen. |
| 3828 | */ |
| 3829 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3830 | ret = 0; |
| 3831 | goto end; |
| 3832 | } |
| 3833 | |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3834 | h2s->status = sl.st.status; |
Willy Tarreau | db72da0 | 2018-09-13 11:52:20 +0200 | [diff] [blame] | 3835 | |
| 3836 | /* certain statuses have no body or an empty one, regardless of |
| 3837 | * what the headers say. |
| 3838 | */ |
| 3839 | if (sl.st.status >= 100 && sl.st.status < 200) { |
| 3840 | h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK); |
| 3841 | h1m->curr_len = h1m->body_len = 0; |
| 3842 | } |
| 3843 | else if (sl.st.status == 204 || sl.st.status == 304) { |
| 3844 | /* no contents, claim c-len is present and set to zero */ |
| 3845 | h1m->flags &= ~H1_MF_CHNK; |
| 3846 | h1m->flags |= H1_MF_CLEN; |
| 3847 | h1m->curr_len = h1m->body_len = 0; |
| 3848 | } |
| 3849 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3850 | chunk_reset(&outbuf); |
| 3851 | |
| 3852 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3853 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3854 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3855 | outbuf.data = 0; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3856 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3857 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3858 | break; |
| 3859 | realign_again: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3860 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3861 | } |
| 3862 | |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3863 | if (outbuf.size < 9) |
| 3864 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3865 | |
| 3866 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3867 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 3868 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3869 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3870 | |
| 3871 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3872 | if (unlikely(list[0].v.len != 3)) { |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 3873 | /* this is an unparsable response */ |
| 3874 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3875 | ret = 0; |
| 3876 | goto end; |
| 3877 | } |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3878 | |
| 3879 | if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3880 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3881 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3882 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3883 | } |
| 3884 | |
| 3885 | /* encode all headers, stop at empty name */ |
| 3886 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 3887 | /* these ones do not exist in H2 and must be dropped. */ |
| 3888 | if (isteq(list[hdr].n, ist("connection")) || |
| 3889 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 3890 | isteq(list[hdr].n, ist("keep-alive")) || |
| 3891 | isteq(list[hdr].n, ist("upgrade")) || |
| 3892 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3893 | continue; |
| 3894 | |
| 3895 | if (isteq(list[hdr].n, ist(""))) |
| 3896 | break; // end |
| 3897 | |
| 3898 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 3899 | /* output full */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3900 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3901 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3902 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3903 | } |
| 3904 | } |
| 3905 | |
| 3906 | /* we may need to add END_STREAM */ |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 3907 | if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3908 | es_now = 1; |
| 3909 | |
| 3910 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3911 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3912 | |
| 3913 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3914 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3915 | |
| 3916 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3917 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3918 | |
| 3919 | /* commit the H2 response */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3920 | b_add(&h2c->mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3921 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3922 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3923 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3924 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3925 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3926 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3927 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3928 | h2s->flags |= H2_SF_ES_SENT; |
| 3929 | if (h2s->st == H2_SS_OPEN) |
| 3930 | h2s->st = H2_SS_HLOC; |
| 3931 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3932 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3933 | } |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3934 | else if (h2s->status >= 100 && h2s->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3935 | /* we'll let the caller check if it has more headers to send */ |
Willy Tarreau | 7f437ff | 2018-09-11 13:51:19 +0200 | [diff] [blame] | 3936 | h1m_init_res(h1m); |
Willy Tarreau | 9b8cd1f | 2018-09-12 09:24:38 +0200 | [diff] [blame] | 3937 | h1m->err_pos = -1; // don't care about errors on the response path |
Willy Tarreau | eb528db | 2018-09-12 09:54:00 +0200 | [diff] [blame] | 3938 | h2s->h1m.flags |= H1_MF_TOLOWER; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3939 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 3940 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 3941 | |
| 3942 | /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */ |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3943 | |
| 3944 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 3945 | //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 Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3946 | return ret; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3947 | full: |
| 3948 | h1m_init_res(h1m); |
| 3949 | h1m->err_pos = -1; // don't care about errors on the response path |
| 3950 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3951 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3952 | ret = 0; |
| 3953 | goto end; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3954 | } |
| 3955 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3956 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 3957 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 3958 | * the number of bytes sent. The caller must check the stream's status to |
| 3959 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3960 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3961 | static size_t h2s_frt_make_resp_data(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3962 | { |
| 3963 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3964 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3965 | struct buffer outbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3966 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 3967 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3968 | int es_now = 0; |
| 3969 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3970 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3971 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3972 | |
| 3973 | if (h2c_mux_busy(h2c, h2s)) { |
| 3974 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3975 | goto end; |
| 3976 | } |
| 3977 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3978 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3979 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3980 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3981 | goto end; |
| 3982 | } |
| 3983 | |
| 3984 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3985 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3986 | goto end; |
| 3987 | |
| 3988 | chunk_reset(&outbuf); |
| 3989 | |
| 3990 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3991 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3992 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3993 | outbuf.data = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3994 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3995 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3996 | break; |
| 3997 | realign_again: |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 3998 | /* If there are pending data in the output buffer, and we have |
| 3999 | * less than 1/4 of the mbuf's size and everything fits, we'll |
| 4000 | * still perform a copy anyway. Otherwise we'll pretend the mbuf |
| 4001 | * is full and wait, to save some slow realign calls. |
| 4002 | */ |
| 4003 | if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) { |
| 4004 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4005 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4006 | goto end; |
| 4007 | } |
| 4008 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4009 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4010 | } |
| 4011 | |
| 4012 | if (outbuf.size < 9) { |
| 4013 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4014 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4015 | goto end; |
| 4016 | } |
| 4017 | |
| 4018 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4019 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4020 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4021 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4022 | |
| 4023 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 4024 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4025 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 4026 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4027 | break; |
| 4028 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4029 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4030 | if ((long long)size > h1m->curr_len) |
| 4031 | size = h1m->curr_len; |
| 4032 | break; |
| 4033 | default: /* te:chunked : parse chunks */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4034 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 4035 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4036 | if (!ret) |
| 4037 | goto end; |
| 4038 | |
| 4039 | if (ret < 0) { |
| 4040 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4041 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4042 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4043 | goto end; |
| 4044 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4045 | max -= ret; |
| 4046 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4047 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4048 | h1m->state = H1_MSG_CHUNK_SIZE; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4049 | } |
| 4050 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4051 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4052 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 4053 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4054 | if (!ret) |
| 4055 | goto end; |
| 4056 | |
| 4057 | if (ret < 0) { |
| 4058 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4059 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4060 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4061 | goto end; |
| 4062 | } |
| 4063 | |
| 4064 | size = chunk; |
| 4065 | h1m->curr_len = chunk; |
| 4066 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4067 | max -= ret; |
| 4068 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4069 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4070 | h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4071 | if (!size) |
| 4072 | goto send_empty; |
| 4073 | } |
| 4074 | |
| 4075 | /* in MSG_DATA state, continue below */ |
| 4076 | size = h1m->curr_len; |
| 4077 | break; |
| 4078 | } |
| 4079 | |
| 4080 | /* we have in <size> the exact number of bytes we need to copy from |
| 4081 | * the H1 buffer. We need to check this against the connection's and |
| 4082 | * the stream's send windows, and to ensure that this fits in the max |
| 4083 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4084 | * the frame header). The connection's flow control is applied last so |
| 4085 | * that we can use a separate list of streams which are immediately |
| 4086 | * unblocked on window opening. Note: we don't implement padding. |
| 4087 | */ |
| 4088 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4089 | if (size > max) |
| 4090 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4091 | |
| 4092 | if (size > h2s->mws) |
| 4093 | size = h2s->mws; |
| 4094 | |
| 4095 | if (size <= 0) { |
| 4096 | h2s->flags |= H2_SF_BLK_SFCTL; |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 4097 | if (h2s->send_wait) { |
| 4098 | LIST_DEL(&h2s->list); |
| 4099 | LIST_INIT(&h2s->list); |
| 4100 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4101 | goto end; |
| 4102 | } |
| 4103 | |
| 4104 | if (h2c->mfs && size > h2c->mfs) |
| 4105 | size = h2c->mfs; |
| 4106 | |
| 4107 | if (size + 9 > outbuf.size) { |
| 4108 | /* we have an opportunity for enlarging the too small |
| 4109 | * available space, let's try. |
| 4110 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4111 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4112 | goto realign_again; |
| 4113 | size = outbuf.size - 9; |
| 4114 | } |
| 4115 | |
| 4116 | if (size <= 0) { |
| 4117 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4118 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4119 | goto end; |
| 4120 | } |
| 4121 | |
| 4122 | if (size > h2c->mws) |
| 4123 | size = h2c->mws; |
| 4124 | |
| 4125 | if (size <= 0) { |
| 4126 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4127 | goto end; |
| 4128 | } |
| 4129 | |
| 4130 | /* copy whatever we can */ |
| 4131 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4132 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4133 | if (ret == 1) |
| 4134 | len2 = 0; |
| 4135 | |
| 4136 | if (!ret || len1 + len2 < size) { |
| 4137 | /* FIXME: must normally never happen */ |
| 4138 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4139 | goto end; |
| 4140 | } |
| 4141 | |
| 4142 | /* limit len1/len2 to size */ |
| 4143 | if (len1 + len2 > size) { |
| 4144 | int sub = len1 + len2 - size; |
| 4145 | |
| 4146 | if (len2 > sub) |
| 4147 | len2 -= sub; |
| 4148 | else { |
| 4149 | sub -= len2; |
| 4150 | len2 = 0; |
| 4151 | len1 -= sub; |
| 4152 | } |
| 4153 | } |
| 4154 | |
| 4155 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4156 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4157 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4158 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4159 | |
| 4160 | send_empty: |
| 4161 | /* we may need to add END_STREAM */ |
| 4162 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 4163 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4164 | * |
| 4165 | * FIXME: what we do here is not correct because we send end_stream |
| 4166 | * before knowing if we'll have to send a HEADERS frame for the |
| 4167 | * trailers. More importantly we're not consuming the trailing CRLF |
| 4168 | * after the end of trailers, so it will be left to the caller to |
| 4169 | * eat it. The right way to do it would be to measure trailers here |
| 4170 | * and to send ES only if there are no trailers. |
| 4171 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4172 | */ |
| 4173 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4174 | !h1m->curr_len || h1m->state >= H1_MSG_DONE) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4175 | es_now = 1; |
| 4176 | |
| 4177 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4178 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4179 | |
| 4180 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4181 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4182 | |
| 4183 | /* commit the H2 response */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4184 | b_add(&h2c->mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4185 | |
| 4186 | /* consume incoming H1 response */ |
| 4187 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4188 | max -= size; |
| 4189 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4190 | total += size; |
| 4191 | h1m->curr_len -= size; |
| 4192 | h2s->mws -= size; |
| 4193 | h2c->mws -= size; |
| 4194 | |
| 4195 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4196 | h1m->state = H1_MSG_CHUNK_CRLF; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4197 | goto new_frame; |
| 4198 | } |
| 4199 | } |
| 4200 | |
| 4201 | if (es_now) { |
| 4202 | if (h2s->st == H2_SS_OPEN) |
| 4203 | h2s->st = H2_SS_HLOC; |
| 4204 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4205 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4206 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4207 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 4208 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4209 | total += max; |
| 4210 | ofs += max; |
| 4211 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4212 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4213 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4214 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4215 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4216 | h2s->flags |= H2_SF_ES_SENT; |
| 4217 | } |
| 4218 | |
| 4219 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4220 | 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 Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4221 | return total; |
| 4222 | } |
| 4223 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4224 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 4225 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4226 | * must check the stream's status to detect any error which might have happened |
| 4227 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4228 | * from the message. The htx message is assumed to be valid since produced from |
| 4229 | * the internal code, hence it contains a start line, an optional series of |
| 4230 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4231 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4232 | */ |
| 4233 | static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
| 4234 | { |
| 4235 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4236 | struct h2c *h2c = h2s->h2c; |
| 4237 | struct htx_blk *blk; |
| 4238 | struct htx_blk *blk_end; |
| 4239 | struct buffer outbuf; |
| 4240 | struct htx_sl *sl; |
| 4241 | enum htx_blk_type type; |
| 4242 | int es_now = 0; |
| 4243 | int ret = 0; |
| 4244 | int hdr; |
| 4245 | int idx; |
| 4246 | |
| 4247 | if (h2c_mux_busy(h2c, h2s)) { |
| 4248 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4249 | return 0; |
| 4250 | } |
| 4251 | |
| 4252 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4253 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4254 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4255 | return 0; |
| 4256 | } |
| 4257 | |
| 4258 | /* determine the first block which must not be deleted, blk_end may |
| 4259 | * be NULL if all blocks have to be deleted. |
| 4260 | */ |
| 4261 | idx = htx_get_head(htx); |
| 4262 | blk_end = NULL; |
| 4263 | while (idx != -1) { |
| 4264 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4265 | idx = htx_get_next(htx, idx); |
| 4266 | if (type == HTX_BLK_EOH) { |
| 4267 | if (idx != -1) |
| 4268 | blk_end = htx_get_blk(htx, idx); |
| 4269 | break; |
| 4270 | } |
| 4271 | } |
| 4272 | |
| 4273 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4274 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4275 | ALREADY_CHECKED(sl); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4276 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4277 | if (h2s->status < 100 || h2s->status > 999) |
| 4278 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4279 | |
| 4280 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4281 | hdr = 0; |
| 4282 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4283 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4284 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4285 | blk = htx_get_blk(htx, idx); |
| 4286 | type = htx_get_blk_type(blk); |
| 4287 | |
| 4288 | if (type == HTX_BLK_UNUSED) |
| 4289 | continue; |
| 4290 | |
| 4291 | if (type != HTX_BLK_HDR) |
| 4292 | break; |
| 4293 | |
| 4294 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4295 | goto fail; |
| 4296 | |
| 4297 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4298 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4299 | hdr++; |
| 4300 | } |
| 4301 | |
| 4302 | /* marker for end of headers */ |
| 4303 | list[hdr].n = ist(""); |
| 4304 | |
| 4305 | if (h2s->status == 204 || h2s->status == 304) { |
| 4306 | /* no contents, claim c-len is present and set to zero */ |
| 4307 | es_now = 1; |
| 4308 | } |
| 4309 | |
| 4310 | chunk_reset(&outbuf); |
| 4311 | |
| 4312 | while (1) { |
| 4313 | outbuf.area = b_tail(&h2c->mbuf); |
| 4314 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4315 | outbuf.data = 0; |
| 4316 | |
| 4317 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4318 | break; |
| 4319 | realign_again: |
| 4320 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4321 | } |
| 4322 | |
| 4323 | if (outbuf.size < 9) |
| 4324 | goto full; |
| 4325 | |
| 4326 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4327 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4328 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4329 | outbuf.data = 9; |
| 4330 | |
| 4331 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4332 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4333 | if (b_space_wraps(&h2c->mbuf)) |
| 4334 | goto realign_again; |
| 4335 | goto full; |
| 4336 | } |
| 4337 | |
| 4338 | /* encode all headers, stop at empty name */ |
| 4339 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4340 | /* these ones do not exist in H2 and must be dropped. */ |
| 4341 | if (isteq(list[hdr].n, ist("connection")) || |
| 4342 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4343 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4344 | isteq(list[hdr].n, ist("upgrade")) || |
| 4345 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4346 | continue; |
| 4347 | |
| 4348 | if (isteq(list[hdr].n, ist(""))) |
| 4349 | break; // end |
| 4350 | |
| 4351 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4352 | /* output full */ |
| 4353 | if (b_space_wraps(&h2c->mbuf)) |
| 4354 | goto realign_again; |
| 4355 | goto full; |
| 4356 | } |
| 4357 | } |
| 4358 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4359 | /* we may need to add END_STREAM except for 1xx responses. |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4360 | * FIXME: we should also set it when we know for sure that the |
| 4361 | * content-length is zero as well as on 204/304 |
| 4362 | */ |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4363 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM && |
| 4364 | (h2s->status >= 200 || h2s->status == 101)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4365 | es_now = 1; |
| 4366 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4367 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4368 | es_now = 1; |
| 4369 | |
| 4370 | /* update the frame's size */ |
| 4371 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4372 | |
| 4373 | if (es_now) |
| 4374 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4375 | |
| 4376 | /* commit the H2 response */ |
| 4377 | b_add(&h2c->mbuf, outbuf.data); |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4378 | |
| 4379 | /* indicates the HEADERS frame was sent, except for 1xx responses. For |
| 4380 | * 1xx responses, another HEADERS frame is expected. |
| 4381 | */ |
| 4382 | if (h2s->status >= 200 || h2s->status == 101) |
| 4383 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4384 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4385 | if (es_now) { |
| 4386 | h2s->flags |= H2_SF_ES_SENT; |
| 4387 | if (h2s->st == H2_SS_OPEN) |
| 4388 | h2s->st = H2_SS_HLOC; |
| 4389 | else |
| 4390 | h2s_close(h2s); |
| 4391 | } |
| 4392 | |
| 4393 | /* OK we could properly deliver the response */ |
| 4394 | |
| 4395 | /* remove all header blocks including the EOH and compute the |
| 4396 | * corresponding size. |
| 4397 | * |
| 4398 | * FIXME: We should remove everything when es_now is set. |
| 4399 | */ |
| 4400 | ret = 0; |
| 4401 | idx = htx_get_head(htx); |
| 4402 | blk = htx_get_blk(htx, idx); |
| 4403 | while (blk != blk_end) { |
| 4404 | ret += htx_get_blksz(blk); |
| 4405 | blk = htx_remove_blk(htx, blk); |
| 4406 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4407 | |
| 4408 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4409 | htx_remove_blk(htx, blk_end); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4410 | end: |
| 4411 | return ret; |
| 4412 | full: |
| 4413 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4414 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4415 | ret = 0; |
| 4416 | goto end; |
| 4417 | fail: |
| 4418 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4419 | * list etc go here (unrecoverable errors). |
| 4420 | */ |
| 4421 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4422 | ret = 0; |
| 4423 | goto end; |
| 4424 | } |
| 4425 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4426 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4427 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4428 | * must check the stream's status to detect any error which might have happened |
| 4429 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4430 | * from the message. The htx message is assumed to be valid since produced from |
| 4431 | * the internal code, hence it contains a start line, an optional series of |
| 4432 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4433 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4434 | */ |
| 4435 | static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
| 4436 | { |
| 4437 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4438 | struct h2c *h2c = h2s->h2c; |
| 4439 | struct htx_blk *blk; |
| 4440 | struct htx_blk *blk_end; |
| 4441 | struct buffer outbuf; |
| 4442 | struct htx_sl *sl; |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4443 | struct ist meth, path, auth; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4444 | enum htx_blk_type type; |
| 4445 | int es_now = 0; |
| 4446 | int ret = 0; |
| 4447 | int hdr; |
| 4448 | int idx; |
| 4449 | |
| 4450 | if (h2c_mux_busy(h2c, h2s)) { |
| 4451 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4452 | return 0; |
| 4453 | } |
| 4454 | |
| 4455 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4456 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4457 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4458 | return 0; |
| 4459 | } |
| 4460 | |
| 4461 | /* determine the first block which must not be deleted, blk_end may |
| 4462 | * be NULL if all blocks have to be deleted. |
| 4463 | */ |
| 4464 | idx = htx_get_head(htx); |
| 4465 | blk_end = NULL; |
| 4466 | while (idx != -1) { |
| 4467 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4468 | idx = htx_get_next(htx, idx); |
| 4469 | if (type == HTX_BLK_EOH) { |
| 4470 | if (idx != -1) |
| 4471 | blk_end = htx_get_blk(htx, idx); |
| 4472 | break; |
| 4473 | } |
| 4474 | } |
| 4475 | |
| 4476 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4477 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4478 | ALREADY_CHECKED(sl); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4479 | meth = htx_sl_req_meth(sl); |
| 4480 | path = htx_sl_req_uri(sl); |
| 4481 | |
| 4482 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4483 | hdr = 0; |
| 4484 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4485 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4486 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4487 | blk = htx_get_blk(htx, idx); |
| 4488 | type = htx_get_blk_type(blk); |
| 4489 | |
| 4490 | if (type == HTX_BLK_UNUSED) |
| 4491 | continue; |
| 4492 | |
| 4493 | if (type != HTX_BLK_HDR) |
| 4494 | break; |
| 4495 | |
| 4496 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4497 | goto fail; |
| 4498 | |
| 4499 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4500 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4501 | hdr++; |
| 4502 | } |
| 4503 | |
| 4504 | /* marker for end of headers */ |
| 4505 | list[hdr].n = ist(""); |
| 4506 | |
| 4507 | chunk_reset(&outbuf); |
| 4508 | |
| 4509 | while (1) { |
| 4510 | outbuf.area = b_tail(&h2c->mbuf); |
| 4511 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4512 | outbuf.data = 0; |
| 4513 | |
| 4514 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4515 | break; |
| 4516 | realign_again: |
| 4517 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4518 | } |
| 4519 | |
| 4520 | if (outbuf.size < 9) |
| 4521 | goto full; |
| 4522 | |
| 4523 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4524 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4525 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4526 | outbuf.data = 9; |
| 4527 | |
| 4528 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4529 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4530 | if (b_space_wraps(&h2c->mbuf)) |
| 4531 | goto realign_again; |
| 4532 | goto full; |
| 4533 | } |
| 4534 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4535 | /* RFC7540 #8.3: the CONNECT method must have : |
| 4536 | * - :authority set to the URI part (host:port) |
| 4537 | * - :method set to CONNECT |
| 4538 | * - :scheme and :path omitted |
| 4539 | */ |
| 4540 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 4541 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
| 4542 | if (!hpack_encode_scheme(&outbuf, ist("https"))) { |
| 4543 | /* output full */ |
| 4544 | if (b_space_wraps(&h2c->mbuf)) |
| 4545 | goto realign_again; |
| 4546 | goto full; |
| 4547 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4548 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4549 | /* encode the path, which necessarily is the second one */ |
| 4550 | if (!hpack_encode_path(&outbuf, path)) { |
| 4551 | /* output full */ |
| 4552 | if (b_space_wraps(&h2c->mbuf)) |
| 4553 | goto realign_again; |
| 4554 | goto full; |
| 4555 | } |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4556 | |
| 4557 | /* look for the Host header and place it in :authority */ |
| 4558 | auth = ist2(NULL, 0); |
| 4559 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4560 | if (isteq(list[hdr].n, ist(""))) |
| 4561 | break; // end |
| 4562 | |
| 4563 | if (isteq(list[hdr].n, ist("host"))) { |
| 4564 | auth = list[hdr].v; |
| 4565 | break; |
| 4566 | } |
| 4567 | } |
| 4568 | } |
| 4569 | else { |
| 4570 | /* for CONNECT, :authority is taken from the path */ |
| 4571 | auth = path; |
| 4572 | } |
| 4573 | |
| 4574 | if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) { |
| 4575 | /* output full */ |
| 4576 | if (b_space_wraps(&h2c->mbuf)) |
| 4577 | goto realign_again; |
| 4578 | goto full; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4579 | } |
| 4580 | |
| 4581 | /* encode all headers, stop at empty name */ |
| 4582 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4583 | /* these ones do not exist in H2 and must be dropped. */ |
| 4584 | if (isteq(list[hdr].n, ist("connection")) || |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4585 | isteq(list[hdr].n, ist("host")) || |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4586 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4587 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4588 | isteq(list[hdr].n, ist("upgrade")) || |
| 4589 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4590 | continue; |
| 4591 | |
| 4592 | if (isteq(list[hdr].n, ist(""))) |
| 4593 | break; // end |
| 4594 | |
| 4595 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4596 | /* output full */ |
| 4597 | if (b_space_wraps(&h2c->mbuf)) |
| 4598 | goto realign_again; |
| 4599 | goto full; |
| 4600 | } |
| 4601 | } |
| 4602 | |
| 4603 | /* we may need to add END_STREAM if we have no body : |
| 4604 | * - request already closed, or : |
| 4605 | * - no transfer-encoding, and : |
| 4606 | * - no content-length or content-length:0 |
| 4607 | * Fixme: this doesn't take into account CONNECT requests. |
| 4608 | */ |
| 4609 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4610 | es_now = 1; |
| 4611 | |
| 4612 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4613 | es_now = 1; |
| 4614 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4615 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4616 | es_now = 1; |
| 4617 | |
| 4618 | /* update the frame's size */ |
| 4619 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4620 | |
| 4621 | if (es_now) |
| 4622 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4623 | |
| 4624 | /* commit the H2 response */ |
| 4625 | b_add(&h2c->mbuf, outbuf.data); |
| 4626 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4627 | h2s->st = H2_SS_OPEN; |
| 4628 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4629 | if (es_now) { |
| 4630 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4631 | h2s->flags |= H2_SF_ES_SENT; |
| 4632 | h2s->st = H2_SS_HLOC; |
| 4633 | } |
| 4634 | |
| 4635 | /* remove all header blocks including the EOH and compute the |
| 4636 | * corresponding size. |
| 4637 | * |
| 4638 | * FIXME: We should remove everything when es_now is set. |
| 4639 | */ |
| 4640 | ret = 0; |
| 4641 | idx = htx_get_head(htx); |
| 4642 | blk = htx_get_blk(htx, idx); |
| 4643 | while (blk != blk_end) { |
| 4644 | ret += htx_get_blksz(blk); |
| 4645 | blk = htx_remove_blk(htx, blk); |
| 4646 | } |
| 4647 | |
| 4648 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4649 | htx_remove_blk(htx, blk_end); |
| 4650 | |
| 4651 | end: |
| 4652 | return ret; |
| 4653 | full: |
| 4654 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4655 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4656 | ret = 0; |
| 4657 | goto end; |
| 4658 | fail: |
| 4659 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4660 | * list etc go here (unrecoverable errors). |
| 4661 | */ |
| 4662 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4663 | ret = 0; |
| 4664 | goto end; |
| 4665 | } |
| 4666 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4667 | /* Try to send a DATA frame matching HTTP response present in HTX structure |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4668 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 4669 | * caller must check the stream's status to detect any error which might have |
| 4670 | * happened subsequently to a successful send. Returns the number of data bytes |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4671 | * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte. |
| 4672 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4673 | static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4674 | { |
| 4675 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4676 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4677 | struct buffer outbuf; |
| 4678 | size_t total = 0; |
| 4679 | int es_now = 0; |
| 4680 | int bsize; /* htx block size */ |
| 4681 | int fsize; /* h2 frame size */ |
| 4682 | struct htx_blk *blk; |
| 4683 | enum htx_blk_type type; |
| 4684 | int idx; |
| 4685 | |
| 4686 | if (h2c_mux_busy(h2c, h2s)) { |
| 4687 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4688 | goto end; |
| 4689 | } |
| 4690 | |
| 4691 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4692 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4693 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4694 | goto end; |
| 4695 | } |
| 4696 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4697 | htx = htx_from_buf(buf); |
| 4698 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4699 | /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However, |
| 4700 | * while looping, we can meet an HTX_BLK_EOM block that we'll leave to |
| 4701 | * the caller to handle. |
| 4702 | */ |
| 4703 | |
| 4704 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4705 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4706 | goto end; |
| 4707 | |
| 4708 | idx = htx_get_head(htx); |
| 4709 | blk = htx_get_blk(htx, idx); |
| 4710 | type = htx_get_blk_type(blk); // DATA or EOD or EOM |
| 4711 | bsize = htx_get_blksz(blk); |
| 4712 | fsize = bsize; |
| 4713 | |
| 4714 | if (type == HTX_BLK_EOD) { |
| 4715 | /* if we have an EOD, we're dealing with chunked data. We may |
| 4716 | * have a set of trailers after us that the caller will want to |
| 4717 | * deal with. Let's simply remove the EOD and return. |
| 4718 | */ |
| 4719 | htx_remove_blk(htx, blk); |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4720 | total++; // EOD counts as one byte |
| 4721 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4722 | goto end; |
| 4723 | } |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 4724 | else if (type == HTX_BLK_EOM) { |
| 4725 | if (h2s->flags & H2_SF_ES_SENT) { |
| 4726 | /* ES already sent */ |
| 4727 | htx_remove_blk(htx, blk); |
| 4728 | total++; // EOM counts as one byte |
| 4729 | count--; |
| 4730 | goto end; |
| 4731 | } |
| 4732 | } |
| 4733 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4734 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4735 | |
| 4736 | /* Perform some optimizations to reduce the number of buffer copies. |
| 4737 | * First, if the mux's buffer is empty and the htx area contains |
| 4738 | * exactly one data block of the same size as the requested count, and |
| 4739 | * this count fits within the frame size, the stream's window size, and |
| 4740 | * the connection's window size, then it's possible to simply swap the |
| 4741 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 4742 | * length to match the entire DATA HTX block in the middle. In this |
| 4743 | * case we perform a true zero-copy operation from end-to-end. This is |
| 4744 | * the situation that happens all the time with large files. Second, if |
| 4745 | * this is not possible, but the mux's output buffer is empty, we still |
| 4746 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 4747 | * making the intermediary buffer's area point to the output buffer's |
| 4748 | * area. In this case we want to skip the HTX header to make sure that |
| 4749 | * copies remain aligned and that this operation remains possible all |
| 4750 | * the time. This goes for headers, data blocks and any data extracted |
| 4751 | * from the HTX blocks. |
| 4752 | */ |
| 4753 | if (unlikely(fsize == count && |
| 4754 | htx->used == 1 && type == HTX_BLK_DATA && |
| 4755 | fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
| 4756 | void *old_area = h2c->mbuf.area; |
| 4757 | |
| 4758 | if (b_data(&h2c->mbuf)) { |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4759 | /* Too bad there are data left there. We're willing to memcpy/memmove |
| 4760 | * up to 1/4 of the buffer, which means that it's OK to copy a large |
| 4761 | * frame into a buffer containing few data if it needs to be realigned, |
| 4762 | * and that it's also OK to copy few data without realigning. Otherwise |
| 4763 | * we'll pretend the mbuf is full and wait for it to become empty. |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4764 | */ |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4765 | if (fsize + 9 <= b_room(&h2c->mbuf) && |
| 4766 | (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 || |
| 4767 | (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf)))) |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4768 | goto copy; |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4769 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4770 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4771 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4772 | goto end; |
| 4773 | } |
| 4774 | |
| 4775 | /* map an H2 frame to the HTX block so that we can put the |
| 4776 | * frame header there. |
| 4777 | */ |
| 4778 | h2c->mbuf.area = buf->area; |
Olivier Houchard | 84cca66 | 2018-12-14 16:28:08 +0100 | [diff] [blame] | 4779 | h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4780 | h2c->mbuf.data = fsize + 9; |
| 4781 | outbuf.area = b_head(&h2c->mbuf); |
| 4782 | |
| 4783 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 4784 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4785 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4786 | h2_set_frame_size(outbuf.area, fsize); |
| 4787 | |
| 4788 | /* update windows */ |
| 4789 | h2s->mws -= fsize; |
| 4790 | h2c->mws -= fsize; |
| 4791 | |
| 4792 | /* and exchange with our old area */ |
| 4793 | buf->area = old_area; |
| 4794 | buf->data = buf->head = 0; |
| 4795 | total += fsize; |
| 4796 | goto end; |
| 4797 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4798 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4799 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4800 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 4801 | |
| 4802 | while (1) { |
| 4803 | outbuf.area = b_tail(&h2c->mbuf); |
| 4804 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4805 | outbuf.data = 0; |
| 4806 | |
| 4807 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4808 | break; |
| 4809 | realign_again: |
| 4810 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4811 | } |
| 4812 | |
| 4813 | if (outbuf.size < 9) { |
| 4814 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4815 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4816 | goto end; |
| 4817 | } |
| 4818 | |
| 4819 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 4820 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4821 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4822 | outbuf.data = 9; |
| 4823 | |
| 4824 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 4825 | * the HTX buffer. We need to check this against the connection's and |
| 4826 | * the stream's send windows, and to ensure that this fits in the max |
| 4827 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4828 | * the frame header). The connection's flow control is applied last so |
| 4829 | * that we can use a separate list of streams which are immediately |
| 4830 | * unblocked on window opening. Note: we don't implement padding. |
| 4831 | */ |
| 4832 | |
| 4833 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 4834 | * empty frame, thus we force it to zero here. |
| 4835 | */ |
| 4836 | if (type == HTX_BLK_EOM) |
| 4837 | bsize = fsize = 0; |
| 4838 | |
| 4839 | if (!fsize) |
| 4840 | goto send_empty; |
| 4841 | |
| 4842 | if (h2s->mws <= 0) { |
| 4843 | h2s->flags |= H2_SF_BLK_SFCTL; |
| 4844 | if (h2s->send_wait) { |
| 4845 | LIST_DEL(&h2s->list); |
| 4846 | LIST_INIT(&h2s->list); |
| 4847 | } |
| 4848 | goto end; |
| 4849 | } |
| 4850 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4851 | if (fsize > count) |
| 4852 | fsize = count; |
| 4853 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4854 | if (fsize > h2s->mws) |
| 4855 | fsize = h2s->mws; // >0 |
| 4856 | |
| 4857 | if (h2c->mfs && fsize > h2c->mfs) |
| 4858 | fsize = h2c->mfs; // >0 |
| 4859 | |
| 4860 | if (fsize + 9 > outbuf.size) { |
| 4861 | /* we have an opportunity for enlarging the too small |
| 4862 | * available space, let's try. |
| 4863 | * FIXME: is this really interesting to do? Maybe we'll |
| 4864 | * spend lots of time realigning instead of using two |
| 4865 | * frames. |
| 4866 | */ |
| 4867 | if (b_space_wraps(&h2c->mbuf)) |
| 4868 | goto realign_again; |
| 4869 | fsize = outbuf.size - 9; |
| 4870 | |
| 4871 | if (fsize <= 0) { |
| 4872 | /* no need to send an empty frame here */ |
| 4873 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4874 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4875 | goto end; |
| 4876 | } |
| 4877 | } |
| 4878 | |
| 4879 | if (h2c->mws <= 0) { |
| 4880 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4881 | goto end; |
| 4882 | } |
| 4883 | |
| 4884 | if (fsize > h2c->mws) |
| 4885 | fsize = h2c->mws; |
| 4886 | |
| 4887 | /* now let's copy this this into the output buffer */ |
| 4888 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 4889 | h2s->mws -= fsize; |
| 4890 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4891 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4892 | |
| 4893 | send_empty: |
| 4894 | /* update the frame's size */ |
| 4895 | h2_set_frame_size(outbuf.area, fsize); |
| 4896 | |
| 4897 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 4898 | * meeting EOM. We should optimize this later. |
| 4899 | */ |
| 4900 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4901 | total++; // EOM counts as one byte |
| 4902 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4903 | es_now = 1; |
| 4904 | } |
| 4905 | |
| 4906 | if (es_now) |
| 4907 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 4908 | |
| 4909 | /* commit the H2 response */ |
| 4910 | b_add(&h2c->mbuf, fsize + 9); |
| 4911 | |
| 4912 | /* consume incoming HTX block, including EOM */ |
| 4913 | total += fsize; |
| 4914 | if (fsize == bsize) { |
| 4915 | htx_remove_blk(htx, blk); |
| 4916 | if (fsize) |
| 4917 | goto new_frame; |
| 4918 | } else { |
| 4919 | /* we've truncated this block */ |
| 4920 | htx_cut_data_blk(htx, blk, fsize); |
| 4921 | } |
| 4922 | |
| 4923 | if (es_now) { |
| 4924 | if (h2s->st == H2_SS_OPEN) |
| 4925 | h2s->st = H2_SS_HLOC; |
| 4926 | else |
| 4927 | h2s_close(h2s); |
| 4928 | |
| 4929 | h2s->flags |= H2_SF_ES_SENT; |
| 4930 | } |
| 4931 | |
| 4932 | end: |
| 4933 | return total; |
| 4934 | } |
| 4935 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4936 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 4937 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 4938 | * processed. The caller must check the stream's status to detect any error |
| 4939 | * which might have happened subsequently to a successful send. The htx blocks |
| 4940 | * are automatically removed from the message. The htx message is assumed to be |
| 4941 | * valid since produced from the internal code. Processing stops when meeting |
| 4942 | * the EOM, which is also removed. All trailers are processed at once and sent |
| 4943 | * as a single frame. The ES flag is always set. |
| 4944 | */ |
| 4945 | static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx) |
| 4946 | { |
| 4947 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4948 | struct h2c *h2c = h2s->h2c; |
| 4949 | struct htx_blk *blk; |
| 4950 | struct htx_blk *blk_end; |
| 4951 | struct buffer outbuf; |
| 4952 | struct h1m h1m; |
| 4953 | enum htx_blk_type type; |
| 4954 | uint32_t size; |
| 4955 | int ret = 0; |
| 4956 | int hdr; |
| 4957 | int idx; |
| 4958 | void *start; |
| 4959 | |
| 4960 | if (h2c_mux_busy(h2c, h2s)) { |
| 4961 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4962 | goto end; |
| 4963 | } |
| 4964 | |
| 4965 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4966 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4967 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4968 | goto end; |
| 4969 | } |
| 4970 | |
| 4971 | /* The principle is that we parse each and every trailers block using |
| 4972 | * the H1 headers parser, and append it to the list. We don't proceed |
| 4973 | * until EOM is met. blk_end will point to the EOM block. |
| 4974 | */ |
| 4975 | hdr = 0; |
| 4976 | memset(list, 0, sizeof(list)); |
| 4977 | blk_end = NULL; |
| 4978 | |
| 4979 | for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) { |
| 4980 | blk = htx_get_blk(htx, idx); |
| 4981 | type = htx_get_blk_type(blk); |
| 4982 | |
| 4983 | if (type == HTX_BLK_UNUSED) |
| 4984 | continue; |
| 4985 | |
| 4986 | if (type != HTX_BLK_TLR) { |
| 4987 | if (type == HTX_BLK_EOM) |
| 4988 | blk_end = blk; |
| 4989 | break; |
| 4990 | } |
| 4991 | |
| 4992 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4993 | goto fail; |
| 4994 | |
| 4995 | size = htx_get_blksz(blk); |
| 4996 | start = htx_get_blk_ptr(htx, blk); |
| 4997 | |
| 4998 | h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER; |
| 4999 | h1m.err_pos = 0; |
| 5000 | ret = h1_headers_to_hdr_list(start, start + size, |
| 5001 | list + hdr, sizeof(list)/sizeof(list[0]) - hdr, |
| 5002 | &h1m, NULL); |
| 5003 | if (ret < 0) |
| 5004 | goto fail; |
| 5005 | |
| 5006 | /* ret == 0 if an incomplete trailers block was found (missing |
| 5007 | * empty line), or > 0 if it was found. We have to continue on |
| 5008 | * incomplete messages because the trailers block might be |
| 5009 | * incomplete. |
| 5010 | */ |
| 5011 | |
| 5012 | /* search the new end */ |
| 5013 | while (hdr <= sizeof(list)/sizeof(list[0])) { |
| 5014 | if (!list[hdr].n.len) |
| 5015 | break; |
| 5016 | hdr++; |
| 5017 | } |
| 5018 | } |
| 5019 | |
| 5020 | if (!blk_end) |
| 5021 | goto end; // end not found yet |
| 5022 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5023 | chunk_reset(&outbuf); |
| 5024 | |
| 5025 | while (1) { |
| 5026 | outbuf.area = b_tail(&h2c->mbuf); |
| 5027 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 5028 | outbuf.data = 0; |
| 5029 | |
| 5030 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 5031 | break; |
| 5032 | realign_again: |
| 5033 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 5034 | } |
| 5035 | |
| 5036 | if (outbuf.size < 9) |
| 5037 | goto full; |
| 5038 | |
| 5039 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 5040 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 5041 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5042 | outbuf.data = 9; |
| 5043 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5044 | /* encode all headers */ |
| 5045 | for (idx = 0; idx < hdr; idx++) { |
| 5046 | /* these ones do not exist in H2 or must not appear in |
| 5047 | * trailers and must be dropped. |
| 5048 | */ |
| 5049 | if (isteq(list[idx].n, ist("host")) || |
| 5050 | isteq(list[idx].n, ist("content-length")) || |
| 5051 | isteq(list[idx].n, ist("connection")) || |
| 5052 | isteq(list[idx].n, ist("proxy-connection")) || |
| 5053 | isteq(list[idx].n, ist("keep-alive")) || |
| 5054 | isteq(list[idx].n, ist("upgrade")) || |
| 5055 | isteq(list[idx].n, ist("te")) || |
| 5056 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 5057 | continue; |
| 5058 | |
| 5059 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 5060 | /* output full */ |
| 5061 | if (b_space_wraps(&h2c->mbuf)) |
| 5062 | goto realign_again; |
| 5063 | goto full; |
| 5064 | } |
| 5065 | } |
| 5066 | |
Willy Tarreau | 67b8cae | 2019-02-21 18:16:35 +0100 | [diff] [blame] | 5067 | if (!hdr) { |
| 5068 | /* here we have a problem, we've received an empty trailers |
| 5069 | * block followed by an EOM. Because of this we can't send a |
| 5070 | * HEADERS frame, so we have to cheat and instead send an empty |
| 5071 | * DATA frame conveying the ES flag. |
| 5072 | */ |
| 5073 | outbuf.area[3] = H2_FT_DATA; |
| 5074 | outbuf.area[4] = H2_F_DATA_END_STREAM; |
| 5075 | } |
| 5076 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5077 | /* update the frame's size */ |
| 5078 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 5079 | |
| 5080 | /* commit the H2 response */ |
| 5081 | b_add(&h2c->mbuf, outbuf.data); |
| 5082 | h2s->flags |= H2_SF_ES_SENT; |
| 5083 | |
| 5084 | if (h2s->st == H2_SS_OPEN) |
| 5085 | h2s->st = H2_SS_HLOC; |
| 5086 | else |
| 5087 | h2s_close(h2s); |
| 5088 | |
| 5089 | /* OK we could properly deliver the response */ |
| 5090 | done: |
| 5091 | /* remove all header blocks including EOM and compute the corresponding size. */ |
| 5092 | ret = 0; |
| 5093 | idx = htx_get_head(htx); |
| 5094 | blk = htx_get_blk(htx, idx); |
| 5095 | while (blk != blk_end) { |
| 5096 | ret += htx_get_blksz(blk); |
| 5097 | blk = htx_remove_blk(htx, blk); |
| 5098 | } |
| 5099 | blk = htx_remove_blk(htx, blk); |
| 5100 | end: |
| 5101 | return ret; |
| 5102 | full: |
| 5103 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5104 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5105 | ret = 0; |
| 5106 | goto end; |
| 5107 | fail: |
| 5108 | /* unparsable HTX messages, too large ones to be produced in the local |
| 5109 | * list etc go here (unrecoverable errors). |
| 5110 | */ |
| 5111 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5112 | ret = 0; |
| 5113 | goto end; |
| 5114 | } |
| 5115 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5116 | /* Called from the upper layer, to subscribe to events, such as being able to send. |
| 5117 | * The <param> argument here is supposed to be a pointer to a wait_event struct |
| 5118 | * which will be passed to h2s->recv_wait or h2s->send_wait depending on the |
| 5119 | * event_type. The event_type must only be a combination of SUB_RETRY_RECV and |
| 5120 | * SUB_RETRY_SEND, other values will lead to -1 being returned. It always |
| 5121 | * returns 0 except for the error above. |
| 5122 | */ |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5123 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 5124 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5125 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5126 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5127 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5128 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5129 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5130 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5131 | if (!(sw->events & SUB_RETRY_RECV)) { |
| 5132 | sw->events |= SUB_RETRY_RECV; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 5133 | sw->handle = h2s; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5134 | h2s->recv_wait = sw; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5135 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5136 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5137 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5138 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5139 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5140 | if (!(sw->events & SUB_RETRY_SEND)) { |
| 5141 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 5142 | sw->handle = h2s; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5143 | h2s->send_wait = sw; |
| 5144 | if (!(h2s->flags & H2_SF_BLK_SFCTL)) { |
| 5145 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 5146 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 5147 | else |
| 5148 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 5149 | } |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 5150 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5151 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5152 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5153 | if (event_type != 0) |
| 5154 | return -1; |
| 5155 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5156 | } |
| 5157 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5158 | /* Called from the upper layer, to unsubscribe some events (undo h2_subscribe). |
| 5159 | * The <param> argument here is supposed to be a pointer to the same wait_event |
| 5160 | * struct that was passed to h2_subscribe() otherwise nothing will be changed. |
| 5161 | * It always returns zero. |
| 5162 | */ |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5163 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 5164 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5165 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5166 | struct h2s *h2s = cs->ctx; |
| 5167 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5168 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5169 | sw = param; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5170 | if (h2s->recv_wait == sw) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5171 | sw->events &= ~SUB_RETRY_RECV; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5172 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5173 | } |
| 5174 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5175 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5176 | sw = param; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5177 | if (h2s->send_wait == sw) { |
| 5178 | LIST_DEL(&h2s->list); |
| 5179 | LIST_INIT(&h2s->list); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5180 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5181 | h2s->send_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5182 | } |
| 5183 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5184 | if (event_type & SUB_CALL_UNSUBSCRIBE) { |
| 5185 | sw = param; |
| 5186 | if (h2s->send_wait == sw) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5187 | sw->events &= ~SUB_CALL_UNSUBSCRIBE; |
Willy Tarreau | e73256f | 2019-03-25 18:02:54 +0100 | [diff] [blame] | 5188 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5189 | h2s->send_wait = NULL; |
Olivier Houchard | f29cd5c | 2018-12-20 11:56:28 +0100 | [diff] [blame] | 5190 | LIST_DEL(&h2s->list); |
| 5191 | LIST_INIT(&h2s->list); |
Olivier Houchard | 3ea3513 | 2019-03-25 14:10:42 +0100 | [diff] [blame] | 5192 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5193 | } |
| 5194 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5195 | return 0; |
| 5196 | } |
| 5197 | |
| 5198 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5199 | /* Called from the upper layer, to receive data */ |
| 5200 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 5201 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5202 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5203 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5204 | struct htx *h2s_htx = NULL; |
| 5205 | struct htx *buf_htx = NULL; |
| 5206 | struct htx_ret htx_ret; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5207 | size_t ret = 0; |
| 5208 | |
| 5209 | /* transfer possibly pending data to the upper layer */ |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5210 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
| 5211 | /* in HTX mode we ignore the count argument */ |
| 5212 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5213 | if (htx_is_empty(h2s_htx)) { |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5214 | /* Here htx_to_buf() will set buffer data to 0 because |
| 5215 | * the HTX is empty. |
| 5216 | */ |
| 5217 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5218 | goto end; |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5219 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5220 | |
| 5221 | buf_htx = htx_from_buf(buf); |
Christopher Faulet | a413e95 | 2019-01-21 11:49:37 +0100 | [diff] [blame] | 5222 | count = htx_free_data_space(buf_htx); |
| 5223 | if (flags & CO_RFL_KEEP_RSV) { |
| 5224 | if (count <= global.tune.maxrewrite) |
| 5225 | goto end; |
| 5226 | count -= global.tune.maxrewrite; |
| 5227 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5228 | |
Willy Tarreau | 0c22fa7 | 2018-12-04 15:21:35 +0100 | [diff] [blame] | 5229 | htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM); |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5230 | |
| 5231 | if (h2s_htx->flags & HTX_FL_PARSING_ERROR) |
| 5232 | buf_htx->flags |= HTX_FL_PARSING_ERROR; |
| 5233 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 5234 | buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0); |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5235 | htx_to_buf(buf_htx, buf); |
| 5236 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5237 | ret = htx_ret.ret; |
| 5238 | } |
| 5239 | else { |
| 5240 | ret = b_xfer(buf, &h2s->rxbuf, count); |
| 5241 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5242 | |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5243 | end: |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5244 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5245 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5246 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5247 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5248 | if (cs->flags & CS_FL_REOS) |
| 5249 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5250 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5251 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5252 | if (b_size(&h2s->rxbuf)) { |
| 5253 | b_free(&h2s->rxbuf); |
| 5254 | offer_buffers(NULL, tasks_run_queue); |
| 5255 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5256 | } |
| 5257 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5258 | if (ret && h2c->dsi == h2s->id) { |
| 5259 | /* demux is blocking on this stream's buffer */ |
| 5260 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 872e2fa | 2019-01-03 08:27:41 +0100 | [diff] [blame] | 5261 | h2c_restart_reading(h2c); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5262 | } |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5263 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5264 | return ret; |
| 5265 | } |
| 5266 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5267 | /* stops all senders of this connection for example when the mux buffer is full. |
| 5268 | * They are moved from the sending_list to either fctl_list or send_list. |
| 5269 | */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5270 | static void h2_stop_senders(struct h2c *h2c) |
| 5271 | { |
| 5272 | struct h2s *h2s, *h2s_back; |
| 5273 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5274 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) { |
| 5275 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | e73256f | 2019-03-25 18:02:54 +0100 | [diff] [blame] | 5276 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5277 | h2s->send_wait->events |= SUB_RETRY_SEND; |
| 5278 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5279 | } |
| 5280 | } |
| 5281 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5282 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 5283 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 5284 | * flags may be updated on the conn_stream. |
| 5285 | */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 5286 | static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5287 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5288 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5289 | size_t orig_count = count; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 5290 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5291 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5292 | struct htx *htx; |
| 5293 | struct htx_blk *blk; |
| 5294 | enum htx_blk_type btype; |
| 5295 | uint32_t bsize; |
| 5296 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5297 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5298 | /* If we were not just woken because we wanted to send but couldn't, |
| 5299 | * and there's somebody else that is waiting to send, do nothing, |
| 5300 | * we will subscribe later and be put at the end of the list |
| 5301 | */ |
| 5302 | LIST_DEL_INIT(&h2s->sending_list); |
| 5303 | if ((!(h2s->send_wait) || !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) && |
| 5304 | (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) |
| 5305 | return 0; |
| 5306 | |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5307 | if (h2s->send_wait) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5308 | /* We want to stay in the send_list, so prepare ourself to be |
| 5309 | * eventually recalled if needed, and only remove ourself from |
| 5310 | * the list if we managed to send anything. |
| 5311 | */ |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5312 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5313 | h2s->send_wait->events |= SUB_RETRY_SEND; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5314 | } |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 5315 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 5316 | return 0; |
| 5317 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5318 | /* htx will be enough to decide if we're using HTX or legacy */ |
| 5319 | htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL; |
| 5320 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5321 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 5322 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 5323 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5324 | if (h2s->id == 0) { |
| 5325 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 5326 | |
| 5327 | if (id < 0) { |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5328 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5329 | return 0; |
| 5330 | } |
| 5331 | |
| 5332 | eb32_delete(&h2s->by_id); |
| 5333 | h2s->by_id.key = h2s->id = id; |
| 5334 | h2s->h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 5335 | h2s->h2c->nb_reserved--; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5336 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 5337 | } |
| 5338 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5339 | if (htx) { |
Willy Tarreau | c14999b | 2018-12-06 14:09:09 +0100 | [diff] [blame] | 5340 | while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) && |
| 5341 | count && !htx_is_empty(htx)) { |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5342 | idx = htx_get_head(htx); |
| 5343 | blk = htx_get_blk(htx, idx); |
| 5344 | btype = htx_get_blk_type(blk); |
| 5345 | bsize = htx_get_blksz(blk); |
| 5346 | |
| 5347 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5348 | case HTX_BLK_REQ_SL: |
| 5349 | /* start-line before headers */ |
| 5350 | ret = h2s_htx_bck_make_req_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 Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 5359 | case HTX_BLK_RES_SL: |
| 5360 | /* start-line before headers */ |
| 5361 | ret = h2s_htx_frt_make_resp_headers(h2s, htx); |
| 5362 | if (ret > 0) { |
| 5363 | total += ret; |
| 5364 | count -= ret; |
| 5365 | if (ret < bsize) |
| 5366 | goto done; |
| 5367 | } |
| 5368 | break; |
| 5369 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5370 | case HTX_BLK_DATA: |
| 5371 | case HTX_BLK_EOD: |
| 5372 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5373 | /* all these cause the emission of a DATA frame (possibly empty). |
| 5374 | * This EOM necessarily is one before trailers, as the EOM following |
| 5375 | * trailers would have been consumed by the trailers parser. |
| 5376 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5377 | ret = h2s_htx_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5378 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5379 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5380 | total += ret; |
| 5381 | count -= ret; |
| 5382 | if (ret < bsize) |
| 5383 | goto done; |
| 5384 | } |
| 5385 | break; |
| 5386 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5387 | case HTX_BLK_TLR: |
| 5388 | /* This is the first trailers block, all the subsequent ones AND |
| 5389 | * the EOM will be swallowed by the parser. |
| 5390 | */ |
| 5391 | ret = h2s_htx_make_trailers(h2s, htx); |
| 5392 | if (ret > 0) { |
| 5393 | total += ret; |
| 5394 | count -= ret; |
| 5395 | if (ret < bsize) |
| 5396 | goto done; |
| 5397 | } |
| 5398 | break; |
| 5399 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5400 | default: |
| 5401 | htx_remove_blk(htx, blk); |
| 5402 | total += bsize; |
| 5403 | count -= bsize; |
| 5404 | break; |
| 5405 | } |
| 5406 | } |
| 5407 | goto done; |
| 5408 | } |
| 5409 | |
| 5410 | /* legacy transfer mode */ |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5411 | while (h2s->h1m.state < H1_MSG_DONE && count) { |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 5412 | if (h2s->h1m.state <= H1_MSG_LAST_LF) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5413 | if (h2s->h2c->flags & H2_CF_IS_BACK) |
| 5414 | ret = -1; |
| 5415 | else |
| 5416 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5417 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5418 | else if (h2s->h1m.state < H1_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5419 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5420 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5421 | else if (h2s->h1m.state == H1_MSG_TRAILERS) { |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5422 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5423 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5424 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5425 | if (unlikely((int)ret <= 0)) { |
| 5426 | if ((int)ret < 0) |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5427 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5428 | break; |
| 5429 | } |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 5430 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5431 | total += count; |
| 5432 | count = 0; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5433 | h2s->h1m.state = H1_MSG_DONE; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5434 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 5435 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5436 | else { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5437 | cs_set_error(cs); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5438 | break; |
| 5439 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5440 | |
| 5441 | total += ret; |
| 5442 | count -= ret; |
| 5443 | |
| 5444 | if (h2s->st >= H2_SS_ERROR) |
| 5445 | break; |
| 5446 | |
| 5447 | if (h2s->flags & H2_SF_BLK_ANY) |
| 5448 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5449 | } |
| 5450 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5451 | done: |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5452 | if (h2s->st >= H2_SS_ERROR) { |
| 5453 | /* trim any possibly pending data after we close (extra CR-LF, |
| 5454 | * unprocessed trailers, abnormal extra data, ...) |
| 5455 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5456 | total += count; |
| 5457 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5458 | } |
| 5459 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5460 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 5461 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5462 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 5463 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 5464 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5465 | } |
| 5466 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5467 | if (htx) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5468 | htx_to_buf(htx, buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5469 | } else { |
| 5470 | b_del(buf, total); |
| 5471 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5472 | |
| 5473 | /* The mux is full, cancel the pending tasks */ |
| 5474 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 5475 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 5476 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5477 | |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5478 | /* If we're running HTX, and we read the whole buffer, then pretend |
| 5479 | * we read exactly what the caller specified, as with HTX the caller |
| 5480 | * will always give the buffer size, instead of the amount of data |
| 5481 | * available. |
| 5482 | */ |
| 5483 | if (htx && !b_data(buf)) |
| 5484 | total = orig_count; |
| 5485 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5486 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5487 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5488 | tasklet_wakeup(h2s->h2c->wait_event.task); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5489 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5490 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 5491 | /* If we're waiting for flow control, and we got a shutr on the |
| 5492 | * connection, we will never be unlocked, so add an error on |
| 5493 | * the conn_stream. |
| 5494 | */ |
| 5495 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 5496 | !b_data(&h2s->h2c->dbuf) && |
| 5497 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 5498 | if (cs->flags & CS_FL_EOS) |
| 5499 | cs->flags |= CS_FL_ERROR; |
| 5500 | else |
| 5501 | cs->flags |= CS_FL_ERR_PENDING; |
| 5502 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5503 | if (total > 0 && h2s->send_wait) { |
| 5504 | /* Ok we managed to send something, leave the send_list */ |
| 5505 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 5506 | h2s->send_wait = NULL; |
| 5507 | LIST_DEL_INIT(&h2s->list); |
| 5508 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5509 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5510 | } |
| 5511 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5512 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5513 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5514 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5515 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5516 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5517 | struct eb32_node *node; |
| 5518 | int fctl_cnt = 0; |
| 5519 | int send_cnt = 0; |
| 5520 | int tree_cnt = 0; |
| 5521 | int orph_cnt = 0; |
| 5522 | |
| 5523 | if (!h2c) |
| 5524 | return; |
| 5525 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5526 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5527 | fctl_cnt++; |
| 5528 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5529 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5530 | send_cnt++; |
| 5531 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5532 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5533 | node = eb32_first(&h2c->streams_by_id); |
| 5534 | while (node) { |
| 5535 | h2s = container_of(node, struct h2s, by_id); |
| 5536 | tree_cnt++; |
| 5537 | if (!h2s->cs) |
| 5538 | orph_cnt++; |
| 5539 | node = eb32_next(node); |
| 5540 | } |
| 5541 | |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5542 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5543 | " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d" |
| 5544 | " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d .mbuf=%u@%p+%u/%u", |
Willy Tarreau | 616ac81 | 2018-07-24 14:12:42 +0200 | [diff] [blame] | 5545 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5546 | h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt, |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5547 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5548 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5549 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5550 | h2c->msi, |
| 5551 | (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf), |
| 5552 | (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf)); |
| 5553 | |
| 5554 | if (h2s) { |
| 5555 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5556 | h2s, h2s->id, h2s->flags, |
| 5557 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5558 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5559 | h2s->cs); |
| 5560 | if (h2s->cs) |
| 5561 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5562 | h2s->cs->flags, h2s->cs->data); |
| 5563 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5564 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5565 | |
| 5566 | /*******************************************************/ |
| 5567 | /* functions below are dedicated to the config parsers */ |
| 5568 | /*******************************************************/ |
| 5569 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5570 | /* config parser for global "tune.h2.header-table-size" */ |
| 5571 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5572 | struct proxy *defpx, const char *file, int line, |
| 5573 | char **err) |
| 5574 | { |
| 5575 | if (too_many_args(1, args, err, NULL)) |
| 5576 | return -1; |
| 5577 | |
| 5578 | h2_settings_header_table_size = atoi(args[1]); |
| 5579 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5580 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5581 | return -1; |
| 5582 | } |
| 5583 | return 0; |
| 5584 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5585 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5586 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5587 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5588 | struct proxy *defpx, const char *file, int line, |
| 5589 | char **err) |
| 5590 | { |
| 5591 | if (too_many_args(1, args, err, NULL)) |
| 5592 | return -1; |
| 5593 | |
| 5594 | h2_settings_initial_window_size = atoi(args[1]); |
| 5595 | if (h2_settings_initial_window_size < 0) { |
| 5596 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5597 | return -1; |
| 5598 | } |
| 5599 | return 0; |
| 5600 | } |
| 5601 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5602 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5603 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5604 | struct proxy *defpx, const char *file, int line, |
| 5605 | char **err) |
| 5606 | { |
| 5607 | if (too_many_args(1, args, err, NULL)) |
| 5608 | return -1; |
| 5609 | |
| 5610 | h2_settings_max_concurrent_streams = atoi(args[1]); |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 5611 | if ((int)h2_settings_max_concurrent_streams < 0) { |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5612 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5613 | return -1; |
| 5614 | } |
| 5615 | return 0; |
| 5616 | } |
| 5617 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5618 | /* config parser for global "tune.h2.max-frame-size" */ |
| 5619 | static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx, |
| 5620 | struct proxy *defpx, const char *file, int line, |
| 5621 | char **err) |
| 5622 | { |
| 5623 | if (too_many_args(1, args, err, NULL)) |
| 5624 | return -1; |
| 5625 | |
| 5626 | h2_settings_max_frame_size = atoi(args[1]); |
| 5627 | if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) { |
| 5628 | memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]); |
| 5629 | return -1; |
| 5630 | } |
| 5631 | return 0; |
| 5632 | } |
| 5633 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5634 | |
| 5635 | /****************************************/ |
| 5636 | /* MUX initialization and instanciation */ |
| 5637 | /***************************************/ |
| 5638 | |
| 5639 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5640 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5641 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5642 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5643 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5644 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5645 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5646 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5647 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5648 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5649 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5650 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5651 | .avail_streams = h2_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 5652 | .used_streams = h2_used_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5653 | .shutr = h2_shutr, |
| 5654 | .shutw = h2_shutw, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5655 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 5656 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5657 | .name = "H2", |
| 5658 | }; |
| 5659 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5660 | /* PROTO selection : this mux registers PROTO token "h2" */ |
| 5661 | static struct mux_proto_list mux_proto_h2 = |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5662 | { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops }; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5663 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5664 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 5665 | |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5666 | static struct mux_proto_list mux_proto_h2_htx = |
| 5667 | { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops }; |
| 5668 | |
| 5669 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx); |
| 5670 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5671 | /* config keyword parsers */ |
| 5672 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5673 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5674 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5675 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5676 | { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5677 | { 0, NULL, NULL } |
| 5678 | }}; |
| 5679 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5680 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |