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 | |
| 717 | if (h2s->send_wait) { |
| 718 | sw = h2s->send_wait; |
| 719 | sw->events &= ~SUB_RETRY_SEND; |
| 720 | tasklet_wakeup(sw->task); |
| 721 | h2s->send_wait = NULL; |
Willy Tarreau | 645b33d | 2018-12-20 15:35:57 +0100 | [diff] [blame] | 722 | LIST_DEL(&h2s->list); |
| 723 | LIST_INIT(&h2s->list); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 727 | /* alerts the data layer, trying to wake it up by all means, following |
| 728 | * this sequence : |
| 729 | * - if the h2s' data layer is subscribed to recv, then it's woken up for recv |
| 730 | * - if its subscribed to send, then it's woken up for send |
| 731 | * - if it was subscribed to neither, its ->wake() callback is called |
| 732 | * It is safe to call this function with a closed stream which doesn't have a |
| 733 | * conn_stream anymore. |
| 734 | */ |
| 735 | static void __maybe_unused h2s_alert(struct h2s *h2s) |
| 736 | { |
| 737 | if (h2s->recv_wait || h2s->send_wait) { |
| 738 | h2s_notify_recv(h2s); |
| 739 | h2s_notify_send(h2s); |
| 740 | } |
| 741 | else if (h2s->cs && h2s->cs->data_cb->wake != NULL) |
| 742 | h2s->cs->data_cb->wake(h2s->cs); |
| 743 | } |
| 744 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 745 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 746 | 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] | 747 | { |
| 748 | uint8_t *out = frame; |
| 749 | |
| 750 | *out = len >> 16; |
| 751 | write_n16(out + 1, len); |
| 752 | } |
| 753 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 754 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 755 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 756 | * the caller's responsibility to verify that there are at least <bytes> bytes |
Willy Tarreau | 9c7f2d1 | 2018-06-15 11:51:32 +0200 | [diff] [blame] | 757 | * available in the buffer's input prior to calling this function. The buffer |
| 758 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 759 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 760 | 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] | 761 | const struct buffer *b, int o) |
| 762 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 763 | 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] | 764 | } |
| 765 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 766 | 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] | 767 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 768 | 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] | 769 | } |
| 770 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 771 | 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] | 772 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 773 | 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] | 774 | } |
| 775 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 776 | 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] | 777 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 778 | 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] | 779 | } |
| 780 | |
| 781 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 782 | /* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>. |
| 783 | * The algorithm is not obvious. It turns out that H2 headers are neither |
| 784 | * aligned nor do they use regular sizes. And to add to the trouble, the buffer |
| 785 | * may wrap so each byte read must be checked. The header is formed like this : |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 786 | * |
| 787 | * b0 b1 b2 b3 b4 b5..b8 |
| 788 | * +----------+---------+--------+----+----+----------------------+ |
| 789 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 790 | * +----------+---------+--------+----+----+----------------------+ |
| 791 | * |
| 792 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 793 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 794 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 795 | * the word, and only one extra read is needed to fetch len[16:23]. |
Willy Tarreau | 9c7f2d1 | 2018-06-15 11:51:32 +0200 | [diff] [blame] | 796 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 797 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 798 | */ |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 799 | 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] | 800 | { |
| 801 | uint64_t w; |
| 802 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 803 | if (b_data(b) < o + 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 804 | return 0; |
| 805 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 806 | w = h2_get_n64(b, o + 1); |
| 807 | h->len = *(uint8_t*)b_peek(b, o) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 808 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 809 | h->ff = w >> 32; |
| 810 | h->ft = w >> 40; |
| 811 | h->len += w >> 48; |
| 812 | return 1; |
| 813 | } |
| 814 | |
| 815 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 816 | * h2_peek_frame_hdr() above. |
| 817 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 818 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 819 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 820 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 824 | 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] | 825 | { |
| 826 | int ret; |
| 827 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 828 | ret = h2_peek_frame_hdr(b, 0, h); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 829 | if (ret > 0) |
| 830 | h2_skip_frame_hdr(b); |
| 831 | return ret; |
| 832 | } |
| 833 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 834 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 835 | * its connection if the stream was not yet closed. Please use this exclusively |
| 836 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 837 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 838 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 839 | { |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 840 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 841 | h2s->h2c->nb_streams--; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 842 | if (!h2s->id) |
| 843 | h2s->h2c->nb_reserved--; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 844 | if (h2s->cs) |
| 845 | h2s->cs->flags |= CS_FL_REOS; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 846 | } |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 847 | h2s->st = H2_SS_CLOSED; |
| 848 | } |
| 849 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 850 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 851 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 852 | { |
| 853 | h2s_close(h2s); |
| 854 | eb32_delete(&h2s->by_id); |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 855 | if (b_size(&h2s->rxbuf)) { |
| 856 | b_free(&h2s->rxbuf); |
| 857 | offer_buffers(NULL, tasks_run_queue); |
| 858 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 859 | if (h2s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 860 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 861 | if (h2s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 862 | h2s->recv_wait->events &= ~SUB_RETRY_RECV; |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 863 | /* There's no need to explicitly call unsubscribe here, the only |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 864 | * reference left would be in the h2c send_list/fctl_list, and if |
| 865 | * we're in it, we're getting out anyway |
| 866 | */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 867 | LIST_DEL_INIT(&h2s->list); |
| 868 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 869 | tasklet_free(h2s->wait_event.task); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 870 | pool_free(pool_head_h2s, h2s); |
| 871 | } |
| 872 | |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 873 | /* allocates a new stream <id> for connection <h2c> and adds it into h2c's |
| 874 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 875 | * causes of errors can be any failed memory allocation. The caller is |
| 876 | * responsible for checking if the connection may support an extra stream |
| 877 | * prior to calling this function. |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 878 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 879 | static struct h2s *h2s_new(struct h2c *h2c, int id) |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 880 | { |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 881 | struct h2s *h2s; |
| 882 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 883 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 884 | if (!h2s) |
| 885 | goto out; |
| 886 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 887 | h2s->wait_event.task = tasklet_new(); |
| 888 | if (!h2s->wait_event.task) { |
| 889 | pool_free(pool_head_h2s, h2s); |
| 890 | goto out; |
| 891 | } |
| 892 | h2s->send_wait = NULL; |
| 893 | h2s->recv_wait = NULL; |
| 894 | h2s->wait_event.task->process = h2_deferred_shut; |
| 895 | h2s->wait_event.task->context = h2s; |
| 896 | h2s->wait_event.handle = NULL; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 897 | h2s->wait_event.events = 0; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 898 | LIST_INIT(&h2s->list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 899 | LIST_INIT(&h2s->sending_list); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 900 | h2s->h2c = h2c; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 901 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 902 | h2s->mws = h2c->miw; |
| 903 | h2s->flags = H2_SF_NONE; |
| 904 | h2s->errcode = H2_ERR_NO_ERROR; |
| 905 | h2s->st = H2_SS_IDLE; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 906 | h2s->status = 0; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 907 | h2s->body_len = 0; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 908 | h2s->rxbuf = BUF_NULL; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 909 | |
| 910 | if (h2c->flags & H2_CF_IS_BACK) { |
| 911 | h1m_init_req(&h2s->h1m); |
| 912 | h2s->h1m.err_pos = -1; // don't care about errors on the request path |
| 913 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 914 | } else { |
| 915 | h1m_init_res(&h2s->h1m); |
| 916 | h2s->h1m.err_pos = -1; // don't care about errors on the response path |
| 917 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 918 | } |
| 919 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 920 | h2s->by_id.key = h2s->id = id; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 921 | if (id > 0) |
| 922 | h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 923 | else |
| 924 | h2c->nb_reserved++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 925 | |
| 926 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 927 | h2c->nb_streams++; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 928 | h2c->stream_cnt++; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 929 | |
| 930 | return h2s; |
| 931 | |
| 932 | out_free_h2s: |
| 933 | pool_free(pool_head_h2s, h2s); |
| 934 | out: |
| 935 | return NULL; |
| 936 | } |
| 937 | |
| 938 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 939 | * case of memory allocation error. |
| 940 | */ |
| 941 | static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id) |
| 942 | { |
| 943 | struct session *sess = h2c->conn->owner; |
| 944 | struct conn_stream *cs; |
| 945 | struct h2s *h2s; |
| 946 | |
| 947 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 948 | goto out; |
| 949 | |
| 950 | h2s = h2s_new(h2c, id); |
| 951 | if (!h2s) |
| 952 | goto out; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 953 | |
| 954 | cs = cs_new(h2c->conn); |
| 955 | if (!cs) |
| 956 | goto out_close; |
| 957 | |
Olivier Houchard | 746fb77 | 2018-12-15 19:42:00 +0100 | [diff] [blame] | 958 | cs->flags |= CS_FL_NOT_FIRST; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 959 | h2s->cs = cs; |
| 960 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 961 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 962 | |
| 963 | if (stream_create_from_cs(cs) < 0) |
| 964 | goto out_free_cs; |
| 965 | |
Willy Tarreau | 590a051 | 2018-09-05 11:56:48 +0200 | [diff] [blame] | 966 | /* We want the accept date presented to the next stream to be the one |
| 967 | * we have now, the handshake time to be null (since the next stream |
| 968 | * is not delayed by a handshake), and the idle time to count since |
| 969 | * right now. |
| 970 | */ |
| 971 | sess->accept_date = date; |
| 972 | sess->tv_accept = now; |
| 973 | sess->t_handshake = 0; |
| 974 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 975 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 976 | if (h2_frt_has_too_many_cs(h2c)) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 977 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 978 | return h2s; |
| 979 | |
| 980 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 981 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 982 | cs_free(cs); |
| 983 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 984 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 985 | out: |
Willy Tarreau | 45efc07 | 2018-10-03 18:27:52 +0200 | [diff] [blame] | 986 | sess_log(sess); |
| 987 | return NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 988 | } |
| 989 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 990 | /* allocates a new stream associated to conn_stream <cs> on the h2c connection |
| 991 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 992 | * possible stream ID was reached. |
| 993 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 994 | 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] | 995 | { |
| 996 | struct h2s *h2s = NULL; |
| 997 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 998 | if (h2c->nb_streams >= h2c->streams_limit) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 999 | goto out; |
| 1000 | |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 1001 | if (h2_streams_left(h2c) < 1) |
| 1002 | goto out; |
| 1003 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1004 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 1005 | h2s = h2s_new(h2c, 0); |
| 1006 | if (!h2s) |
| 1007 | goto out; |
| 1008 | |
| 1009 | h2s->cs = cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1010 | h2s->sess = sess; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1011 | cs->ctx = h2s; |
| 1012 | h2c->nb_cs++; |
| 1013 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1014 | out: |
| 1015 | return h2s; |
| 1016 | } |
| 1017 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1018 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 1019 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 1020 | * the various settings codes. |
| 1021 | */ |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1022 | static int h2c_send_settings(struct h2c *h2c) |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1023 | { |
| 1024 | struct buffer *res; |
| 1025 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1026 | struct buffer buf; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1027 | int mfs; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1028 | int ret; |
| 1029 | |
| 1030 | if (h2c_mux_busy(h2c, NULL)) { |
| 1031 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1032 | return 0; |
| 1033 | } |
| 1034 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1035 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1036 | if (!res) { |
| 1037 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1038 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
| 1042 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 1043 | chunk_memcpy(&buf, |
| 1044 | "\x00\x00\x00" /* length : 0 for now */ |
| 1045 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 1046 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 1047 | 9); |
| 1048 | |
Willy Tarreau | 0bbad6b | 2019-02-26 16:01:52 +0100 | [diff] [blame] | 1049 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1050 | /* send settings_enable_push=0 */ |
| 1051 | chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6); |
| 1052 | } |
| 1053 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1054 | if (h2_settings_header_table_size != 4096) { |
| 1055 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 1056 | |
| 1057 | write_n32(str + 2, h2_settings_header_table_size); |
| 1058 | chunk_memcat(&buf, str, 6); |
| 1059 | } |
| 1060 | |
| 1061 | if (h2_settings_initial_window_size != 65535) { |
| 1062 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 1063 | |
| 1064 | write_n32(str + 2, h2_settings_initial_window_size); |
| 1065 | chunk_memcat(&buf, str, 6); |
| 1066 | } |
| 1067 | |
| 1068 | if (h2_settings_max_concurrent_streams != 0) { |
| 1069 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 1070 | |
| 1071 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 1072 | * the protocol, so never send this value! |
| 1073 | */ |
| 1074 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 1075 | chunk_memcat(&buf, str, 6); |
| 1076 | } |
| 1077 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1078 | mfs = h2_settings_max_frame_size; |
| 1079 | if (mfs > global.tune.bufsize) |
| 1080 | mfs = global.tune.bufsize; |
| 1081 | |
| 1082 | if (!mfs) |
| 1083 | mfs = global.tune.bufsize; |
| 1084 | |
| 1085 | if (mfs != 16384) { |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1086 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 1087 | |
| 1088 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 1089 | * match bufsize - rewrite size, but at the moment it seems |
| 1090 | * that clients don't take care of it. |
| 1091 | */ |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1092 | write_n32(str + 2, mfs); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1093 | chunk_memcat(&buf, str, 6); |
| 1094 | } |
| 1095 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1096 | h2_set_frame_size(buf.area, buf.data - 9); |
| 1097 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1098 | if (unlikely(ret <= 0)) { |
| 1099 | if (!ret) { |
| 1100 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1101 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1102 | return 0; |
| 1103 | } |
| 1104 | else { |
| 1105 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1106 | return 0; |
| 1107 | } |
| 1108 | } |
| 1109 | return ret; |
| 1110 | } |
| 1111 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1112 | /* Try to receive a connection preface, then upon success try to send our |
| 1113 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1114 | * missing data. It may return an error in h2c. |
| 1115 | */ |
| 1116 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 1117 | { |
| 1118 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1119 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1120 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1121 | 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] | 1122 | |
| 1123 | if (unlikely(ret1 <= 0)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1124 | if (ret1 < 0) |
| 1125 | sess_log(h2c->conn->owner); |
| 1126 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1127 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 1128 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1132 | ret2 = h2c_send_settings(h2c); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1133 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1134 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1135 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1136 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1137 | } |
| 1138 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1139 | /* Try to send a connection preface, then upon success try to send our |
| 1140 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1141 | * missing data. It may return an error in h2c. |
| 1142 | */ |
| 1143 | static int h2c_bck_send_preface(struct h2c *h2c) |
| 1144 | { |
| 1145 | struct buffer *res; |
| 1146 | |
| 1147 | if (h2c_mux_busy(h2c, NULL)) { |
| 1148 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | res = h2_get_buf(h2c, &h2c->mbuf); |
| 1153 | if (!res) { |
| 1154 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1155 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1156 | return 0; |
| 1157 | } |
| 1158 | |
| 1159 | if (!b_data(res)) { |
| 1160 | /* preface not yet sent */ |
| 1161 | b_istput(res, ist(H2_CONN_PREFACE)); |
| 1162 | } |
| 1163 | |
| 1164 | return h2c_send_settings(h2c); |
| 1165 | } |
| 1166 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1167 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 1168 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 1169 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 1170 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 1171 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 1172 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 1173 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 1174 | * case so that it is safe to use h2c_error() to report such errors. |
| 1175 | */ |
| 1176 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 1177 | { |
| 1178 | struct buffer *res; |
| 1179 | char str[17]; |
| 1180 | int ret; |
| 1181 | |
| 1182 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 1183 | return 1; // claim that it worked |
| 1184 | |
| 1185 | if (h2c_mux_busy(h2c, h2s)) { |
| 1186 | if (h2s) |
| 1187 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1188 | else |
| 1189 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1193 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1194 | if (!res) { |
| 1195 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1196 | if (h2s) |
| 1197 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1198 | else |
| 1199 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
| 1203 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 1204 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 1205 | |
| 1206 | if (h2c->last_sid < 0) |
| 1207 | h2c->last_sid = h2c->max_id; |
| 1208 | |
| 1209 | write_n32(str + 9, h2c->last_sid); |
| 1210 | write_n32(str + 13, h2c->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1211 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1212 | if (unlikely(ret <= 0)) { |
| 1213 | if (!ret) { |
| 1214 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1215 | if (h2s) |
| 1216 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1217 | else |
| 1218 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1219 | return 0; |
| 1220 | } |
| 1221 | else { |
| 1222 | /* we cannot report this error using GOAWAY, so we mark |
| 1223 | * it and claim a success. |
| 1224 | */ |
| 1225 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1226 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 1227 | return 1; |
| 1228 | } |
| 1229 | } |
| 1230 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 1231 | return ret; |
| 1232 | } |
| 1233 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1234 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 1235 | * during mux operations. This stream must be valid and cannot be closed |
| 1236 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 1237 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 1238 | * not yet. |
| 1239 | * |
| 1240 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1241 | * to write the message, it subscribes the stream to future notifications. |
| 1242 | */ |
| 1243 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1244 | { |
| 1245 | struct buffer *res; |
| 1246 | char str[13]; |
| 1247 | int ret; |
| 1248 | |
| 1249 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 1250 | return 1; |
| 1251 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1252 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1253 | * RST_STREAM in response to a RST_STREAM frame. |
| 1254 | */ |
| 1255 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1256 | ret = 1; |
| 1257 | goto ignore; |
| 1258 | } |
| 1259 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1260 | if (h2c_mux_busy(h2c, h2s)) { |
| 1261 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1265 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1266 | if (!res) { |
| 1267 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1268 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1269 | return 0; |
| 1270 | } |
| 1271 | |
| 1272 | /* len: 4, type: 3, flags: none */ |
| 1273 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1274 | write_n32(str + 5, h2s->id); |
| 1275 | write_n32(str + 9, h2s->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1276 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1277 | |
| 1278 | if (unlikely(ret <= 0)) { |
| 1279 | if (!ret) { |
| 1280 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1281 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1282 | return 0; |
| 1283 | } |
| 1284 | else { |
| 1285 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1286 | return 0; |
| 1287 | } |
| 1288 | } |
| 1289 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1290 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1291 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1292 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1293 | return ret; |
| 1294 | } |
| 1295 | |
| 1296 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 1297 | * 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] | 1298 | * error code, even if the stream is one of the dummy ones, and will update |
| 1299 | * h2s->st to H2_SS_CLOSED if it was not yet. |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1300 | * |
| 1301 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1302 | * 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] | 1303 | * 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] | 1304 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1305 | */ |
| 1306 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1307 | { |
| 1308 | struct buffer *res; |
| 1309 | char str[13]; |
| 1310 | int ret; |
| 1311 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1312 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1313 | * RST_STREAM in response to a RST_STREAM frame. |
| 1314 | */ |
| 1315 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1316 | ret = 1; |
| 1317 | goto ignore; |
| 1318 | } |
| 1319 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1320 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1321 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1322 | return 0; |
| 1323 | } |
| 1324 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1325 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1326 | if (!res) { |
| 1327 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1328 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1329 | return 0; |
| 1330 | } |
| 1331 | |
| 1332 | /* len: 4, type: 3, flags: none */ |
| 1333 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1334 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1335 | write_n32(str + 5, h2c->dsi); |
Willy Tarreau | e6888ff | 2018-12-23 18:26:26 +0100 | [diff] [blame] | 1336 | write_n32(str + 9, h2s->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1337 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1338 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1339 | if (unlikely(ret <= 0)) { |
| 1340 | if (!ret) { |
| 1341 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1342 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1343 | return 0; |
| 1344 | } |
| 1345 | else { |
| 1346 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1347 | return 0; |
| 1348 | } |
| 1349 | } |
| 1350 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1351 | ignore: |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1352 | if (h2s->id) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1353 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1354 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1355 | } |
| 1356 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1357 | return ret; |
| 1358 | } |
| 1359 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1360 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 1361 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 1362 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 1363 | * on success or zero if nothing was done. In case of lack of room to write the |
| 1364 | * message, it subscribes the requesting stream to future notifications. |
| 1365 | */ |
| 1366 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 1367 | { |
| 1368 | struct h2c *h2c = h2s->h2c; |
| 1369 | struct buffer *res; |
| 1370 | char str[9]; |
| 1371 | int ret; |
| 1372 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1373 | 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] | 1374 | return 1; |
| 1375 | |
| 1376 | if (h2c_mux_busy(h2c, h2s)) { |
| 1377 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1381 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1382 | if (!res) { |
| 1383 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1384 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1385 | return 0; |
| 1386 | } |
| 1387 | |
| 1388 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1389 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1390 | write_n32(str + 5, h2s->id); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1391 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1392 | if (likely(ret > 0)) { |
| 1393 | h2s->flags |= H2_SF_ES_SENT; |
| 1394 | } |
| 1395 | else if (!ret) { |
| 1396 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1397 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1398 | return 0; |
| 1399 | } |
| 1400 | else { |
| 1401 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1402 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1403 | } |
| 1404 | return ret; |
| 1405 | } |
| 1406 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1407 | /* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags> |
| 1408 | * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close |
| 1409 | * connection. The stream's state is automatically updated accordingly. If the |
| 1410 | * stream is orphaned, it is destroyed. |
| 1411 | */ |
| 1412 | static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags) |
| 1413 | { |
| 1414 | if (!h2s->cs) { |
| 1415 | /* this stream was already orphaned */ |
| 1416 | h2s_destroy(h2s); |
| 1417 | return; |
| 1418 | } |
| 1419 | |
| 1420 | h2s->cs->flags |= flags; |
| 1421 | if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS)) |
| 1422 | h2s->cs->flags |= CS_FL_ERROR; |
| 1423 | |
| 1424 | h2s_alert(h2s); |
| 1425 | |
| 1426 | if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR) |
| 1427 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 1428 | 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] | 1429 | h2s->st = H2_SS_HREM; |
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_HLOC) |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1431 | h2s_close(h2s); |
| 1432 | } |
| 1433 | |
| 1434 | /* wake the streams attached to the connection, whose id is greater than <last> |
| 1435 | * or unassigned. |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1436 | */ |
| 1437 | static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags) |
| 1438 | { |
| 1439 | struct eb32_node *node; |
| 1440 | struct h2s *h2s; |
| 1441 | |
| 1442 | if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR) |
Willy Tarreau | a851935 | 2018-12-18 16:44:28 +0100 | [diff] [blame] | 1443 | flags |= CS_FL_ERR_PENDING; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1444 | |
| 1445 | if (conn_xprt_read0_pending(h2c->conn)) |
Christopher Faulet | 203b2b0 | 2019-03-08 09:23:46 +0100 | [diff] [blame] | 1446 | flags |= (CS_FL_REOS|CS_FL_READ_NULL); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1447 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1448 | /* Wake all streams with ID > last */ |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1449 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1450 | while (node) { |
| 1451 | h2s = container_of(node, struct h2s, by_id); |
| 1452 | if (h2s->id <= last) |
| 1453 | break; |
| 1454 | node = eb32_next(node); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1455 | h2s_wake_one_stream(h2s, flags); |
| 1456 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1457 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1458 | /* Wake all streams with unassigned ID (ID == 0) */ |
| 1459 | node = eb32_lookup(&h2c->streams_by_id, 0); |
| 1460 | while (node) { |
| 1461 | h2s = container_of(node, struct h2s, by_id); |
| 1462 | if (h2s->id > 0) |
| 1463 | break; |
| 1464 | node = eb32_next(node); |
| 1465 | h2s_wake_one_stream(h2s, flags); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1466 | } |
| 1467 | } |
| 1468 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1469 | /* Increase all streams' outgoing window size by the difference passed in |
| 1470 | * argument. This is needed upon receipt of the settings frame if the initial |
| 1471 | * window size is different. The difference may be negative and the resulting |
| 1472 | * window size as well, for the time it takes to receive some window updates. |
| 1473 | */ |
| 1474 | static void h2c_update_all_ws(struct h2c *h2c, int diff) |
| 1475 | { |
| 1476 | struct h2s *h2s; |
| 1477 | struct eb32_node *node; |
| 1478 | |
| 1479 | if (!diff) |
| 1480 | return; |
| 1481 | |
| 1482 | node = eb32_first(&h2c->streams_by_id); |
| 1483 | while (node) { |
| 1484 | h2s = container_of(node, struct h2s, by_id); |
| 1485 | h2s->mws += diff; |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1486 | |
| 1487 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1488 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
| 1489 | if (h2s->send_wait) |
| 1490 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 1491 | |
| 1492 | } |
| 1493 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1494 | node = eb32_next(node); |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1499 | * 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] | 1500 | * return an error in h2c. The caller must have already verified frame length |
| 1501 | * and stream ID validity. Described in RFC7540#6.5. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1502 | */ |
| 1503 | static int h2c_handle_settings(struct h2c *h2c) |
| 1504 | { |
| 1505 | unsigned int offset; |
| 1506 | int error; |
| 1507 | |
| 1508 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1509 | if (h2c->dfl) { |
| 1510 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1511 | goto fail; |
| 1512 | } |
| 1513 | return 1; |
| 1514 | } |
| 1515 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1516 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1517 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1518 | return 0; |
| 1519 | |
| 1520 | /* parse the frame */ |
| 1521 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1522 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1523 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1524 | |
| 1525 | switch (type) { |
| 1526 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1527 | /* we need to update all existing streams with the |
| 1528 | * difference from the previous iws. |
| 1529 | */ |
| 1530 | if (arg < 0) { // RFC7540#6.5.2 |
| 1531 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1532 | goto fail; |
| 1533 | } |
| 1534 | h2c_update_all_ws(h2c, arg - h2c->miw); |
| 1535 | h2c->miw = arg; |
| 1536 | break; |
| 1537 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1538 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1539 | error = H2_ERR_PROTOCOL_ERROR; |
| 1540 | goto fail; |
| 1541 | } |
| 1542 | h2c->mfs = arg; |
| 1543 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1544 | case H2_SETTINGS_ENABLE_PUSH: |
| 1545 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1546 | error = H2_ERR_PROTOCOL_ERROR; |
| 1547 | goto fail; |
| 1548 | } |
| 1549 | break; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 1550 | case H2_SETTINGS_MAX_CONCURRENT_STREAMS: |
| 1551 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1552 | /* the limit is only for the backend; for the frontend it is our limit */ |
| 1553 | if ((unsigned int)arg > h2_settings_max_concurrent_streams) |
| 1554 | arg = h2_settings_max_concurrent_streams; |
| 1555 | h2c->streams_limit = arg; |
| 1556 | } |
| 1557 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | /* need to ACK this frame now */ |
| 1562 | h2c->st0 = H2_CS_FRAME_A; |
| 1563 | return 1; |
| 1564 | fail: |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1565 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1566 | h2c_error(h2c, error); |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
| 1570 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1571 | * success or one of the h2_status values. |
| 1572 | */ |
| 1573 | static int h2c_ack_settings(struct h2c *h2c) |
| 1574 | { |
| 1575 | struct buffer *res; |
| 1576 | char str[9]; |
| 1577 | int ret = -1; |
| 1578 | |
| 1579 | if (h2c_mux_busy(h2c, NULL)) { |
| 1580 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1581 | return 0; |
| 1582 | } |
| 1583 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1584 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1585 | if (!res) { |
| 1586 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1587 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1588 | return 0; |
| 1589 | } |
| 1590 | |
| 1591 | memcpy(str, |
| 1592 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1593 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1594 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1595 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1596 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1597 | if (unlikely(ret <= 0)) { |
| 1598 | if (!ret) { |
| 1599 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1600 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1601 | return 0; |
| 1602 | } |
| 1603 | else { |
| 1604 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1605 | return 0; |
| 1606 | } |
| 1607 | } |
| 1608 | return ret; |
| 1609 | } |
| 1610 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1611 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1612 | * 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] | 1613 | * missing data. The caller must have already verified frame length |
| 1614 | * and stream ID validity. |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1615 | */ |
| 1616 | static int h2c_handle_ping(struct h2c *h2c) |
| 1617 | { |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1618 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1619 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1620 | h2c->st0 = H2_CS_FRAME_A; |
| 1621 | return 1; |
| 1622 | } |
| 1623 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1624 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1625 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1626 | * error in h2c. |
| 1627 | */ |
| 1628 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1629 | { |
| 1630 | struct buffer *res; |
| 1631 | char str[13]; |
| 1632 | int ret = -1; |
| 1633 | |
| 1634 | if (h2c_mux_busy(h2c, NULL)) { |
| 1635 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1636 | return 0; |
| 1637 | } |
| 1638 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1639 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1640 | if (!res) { |
| 1641 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1642 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1643 | return 0; |
| 1644 | } |
| 1645 | |
| 1646 | /* length: 4, type: 8, flags: none */ |
| 1647 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1648 | write_n32(str + 5, sid); |
| 1649 | write_n32(str + 9, increment); |
| 1650 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1651 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1652 | |
| 1653 | if (unlikely(ret <= 0)) { |
| 1654 | if (!ret) { |
| 1655 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1656 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1657 | return 0; |
| 1658 | } |
| 1659 | else { |
| 1660 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1661 | return 0; |
| 1662 | } |
| 1663 | } |
| 1664 | return ret; |
| 1665 | } |
| 1666 | |
| 1667 | /* try to send pending window update for the connection. It's safe to call it |
| 1668 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1669 | * failure. It may return an error in h2c. |
| 1670 | */ |
| 1671 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1672 | { |
| 1673 | int ret = 1; |
| 1674 | |
| 1675 | if (h2c->rcvd_c <= 0) |
| 1676 | return 1; |
| 1677 | |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 1678 | if (!(h2c->flags & H2_CF_WINDOW_OPENED)) { |
| 1679 | /* increase the advertised connection window to 2G on |
| 1680 | * first update. |
| 1681 | */ |
| 1682 | h2c->flags |= H2_CF_WINDOW_OPENED; |
| 1683 | h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT; |
| 1684 | } |
| 1685 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1686 | /* send WU for the connection */ |
| 1687 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1688 | if (ret > 0) |
| 1689 | h2c->rcvd_c = 0; |
| 1690 | |
| 1691 | return ret; |
| 1692 | } |
| 1693 | |
| 1694 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1695 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1696 | * room or failure. It may return an error in h2c. |
| 1697 | */ |
| 1698 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1699 | { |
| 1700 | int ret = 1; |
| 1701 | |
| 1702 | if (h2c->rcvd_s <= 0) |
| 1703 | return 1; |
| 1704 | |
| 1705 | /* send WU for the stream */ |
| 1706 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1707 | if (ret > 0) |
| 1708 | h2c->rcvd_s = 0; |
| 1709 | |
| 1710 | return ret; |
| 1711 | } |
| 1712 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1713 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1714 | * success, 0 on missing data or one of the h2_status values. |
| 1715 | */ |
| 1716 | static int h2c_ack_ping(struct h2c *h2c) |
| 1717 | { |
| 1718 | struct buffer *res; |
| 1719 | char str[17]; |
| 1720 | int ret = -1; |
| 1721 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1722 | if (b_data(&h2c->dbuf) < 8) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1723 | return 0; |
| 1724 | |
| 1725 | if (h2c_mux_busy(h2c, NULL)) { |
| 1726 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1727 | return 0; |
| 1728 | } |
| 1729 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1730 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1731 | if (!res) { |
| 1732 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1733 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1734 | return 0; |
| 1735 | } |
| 1736 | |
| 1737 | memcpy(str, |
| 1738 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1739 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1740 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1741 | |
| 1742 | /* copy the original payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1743 | h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1744 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1745 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1746 | if (unlikely(ret <= 0)) { |
| 1747 | if (!ret) { |
| 1748 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1749 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1750 | return 0; |
| 1751 | } |
| 1752 | else { |
| 1753 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1754 | return 0; |
| 1755 | } |
| 1756 | } |
| 1757 | return ret; |
| 1758 | } |
| 1759 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1760 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1761 | * 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] | 1762 | * h2c or h2s. The caller must have already verified frame length and stream ID |
| 1763 | * validity. Described in RFC7540#6.9. |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1764 | */ |
| 1765 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1766 | { |
| 1767 | int32_t inc; |
| 1768 | int error; |
| 1769 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1770 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1771 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1772 | return 0; |
| 1773 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1774 | inc = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1775 | |
| 1776 | if (h2c->dsi != 0) { |
| 1777 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1778 | |
| 1779 | /* it's not an error to receive WU on a closed stream */ |
| 1780 | if (h2s->st == H2_SS_CLOSED) |
| 1781 | return 1; |
| 1782 | |
| 1783 | if (!inc) { |
| 1784 | error = H2_ERR_PROTOCOL_ERROR; |
| 1785 | goto strm_err; |
| 1786 | } |
| 1787 | |
| 1788 | if (h2s->mws >= 0 && h2s->mws + inc < 0) { |
| 1789 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1790 | goto strm_err; |
| 1791 | } |
| 1792 | |
| 1793 | h2s->mws += inc; |
| 1794 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1795 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 1796 | if (h2s->send_wait) |
| 1797 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 1798 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1799 | } |
| 1800 | } |
| 1801 | else { |
| 1802 | /* connection window update */ |
| 1803 | if (!inc) { |
| 1804 | error = H2_ERR_PROTOCOL_ERROR; |
| 1805 | goto conn_err; |
| 1806 | } |
| 1807 | |
| 1808 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1809 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1810 | goto conn_err; |
| 1811 | } |
| 1812 | |
| 1813 | h2c->mws += inc; |
| 1814 | } |
| 1815 | |
| 1816 | return 1; |
| 1817 | |
| 1818 | conn_err: |
| 1819 | h2c_error(h2c, error); |
| 1820 | return 0; |
| 1821 | |
| 1822 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 1823 | h2s_error(h2s, error); |
| 1824 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1825 | return 0; |
| 1826 | } |
| 1827 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1828 | /* 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] | 1829 | * the last ID. Returns > 0 on success or zero on missing data. The caller must |
| 1830 | * have already verified frame length and stream ID validity. Described in |
| 1831 | * RFC7540#6.8. |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1832 | */ |
| 1833 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1834 | { |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1835 | int last; |
| 1836 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1837 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1838 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1839 | return 0; |
| 1840 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1841 | last = h2_get_n32(&h2c->dbuf, 0); |
| 1842 | h2c->errcode = h2_get_n32(&h2c->dbuf, 4); |
Olivier Houchard | 9117780 | 2018-12-19 14:49:39 +0100 | [diff] [blame] | 1843 | h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1844 | if (h2c->last_sid < 0) |
| 1845 | h2c->last_sid = last; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1846 | return 1; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1847 | } |
| 1848 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1849 | /* 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] | 1850 | * invalid. Returns > 0 on success or zero on missing data. It may return an |
| 1851 | * error in h2c. The caller must have already verified frame length and stream |
| 1852 | * ID validity. Described in RFC7540#6.3. |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1853 | */ |
| 1854 | static int h2c_handle_priority(struct h2c *h2c) |
| 1855 | { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1856 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1857 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1858 | return 0; |
| 1859 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1860 | if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1861 | /* 7540#5.3 : can't depend on itself */ |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1862 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1863 | return 0; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1864 | } |
| 1865 | return 1; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1866 | } |
| 1867 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1868 | /* 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] | 1869 | * Returns > 0 on success or zero on missing data. The caller must have already |
| 1870 | * verified frame length and stream ID validity. Described in RFC7540#6.4. |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1871 | */ |
| 1872 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1873 | { |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1874 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1875 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1876 | return 0; |
| 1877 | |
| 1878 | /* late RST, already handled */ |
| 1879 | if (h2s->st == H2_SS_CLOSED) |
| 1880 | return 1; |
| 1881 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1882 | h2s->errcode = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1883 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1884 | |
| 1885 | if (h2s->cs) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 1886 | cs_set_error(h2s->cs); |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 1887 | h2s_alert(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | h2s->flags |= H2_SF_RST_RCVD; |
| 1891 | return 1; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1892 | } |
| 1893 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1894 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 1895 | * It may return an error in h2c or h2s. The caller must consider that the |
| 1896 | * return value is the new h2s in case one was allocated (most common case). |
| 1897 | * Described in RFC7540#6.2. Most of the |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1898 | * errors here are reported as connection errors since it's impossible to |
| 1899 | * recover from such errors after the compression context has been altered. |
| 1900 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1901 | 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] | 1902 | { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1903 | struct buffer rxbuf = BUF_NULL; |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1904 | unsigned long long body_len = 0; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1905 | uint32_t flags = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1906 | int error; |
| 1907 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1908 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1909 | return NULL; // empty buffer |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1910 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1911 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1912 | return NULL; // incomplete frame |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1913 | |
| 1914 | /* now either the frame is complete or the buffer is complete */ |
| 1915 | if (h2s->st != H2_SS_IDLE) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1916 | /* The stream exists/existed, this must be a trailers frame */ |
| 1917 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1918 | if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0) |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1919 | goto out; |
| 1920 | goto done; |
| 1921 | } |
Willy Tarreau | 1f03550 | 2019-01-30 11:44:07 +0100 | [diff] [blame] | 1922 | /* the connection was already killed by an RST, let's consume |
| 1923 | * the data and send another RST. |
| 1924 | */ |
| 1925 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
| 1926 | h2s = (struct h2s*)h2_error_stream; |
| 1927 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1928 | } |
| 1929 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 1930 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 1931 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1932 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1933 | goto conn_err; |
| 1934 | } |
Willy Tarreau | 415b1ee | 2019-01-02 13:59:43 +0100 | [diff] [blame] | 1935 | else if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 1936 | goto out; // IDLE but too many cs still present |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1937 | |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1938 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1939 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1940 | /* unrecoverable error ? */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1941 | if (h2c->st0 >= H2_CS_ERROR) |
| 1942 | goto out; |
| 1943 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1944 | if (error <= 0) { |
| 1945 | if (error == 0) |
| 1946 | goto out; // missing data |
| 1947 | |
| 1948 | /* Failed to decode this stream (e.g. too large request) |
| 1949 | * but the HPACK decompressor is still synchronized. |
| 1950 | */ |
| 1951 | h2s = (struct h2s*)h2_error_stream; |
| 1952 | goto send_rst; |
| 1953 | } |
| 1954 | |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1955 | /* 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] | 1956 | * positively from h2c_frt_stream_new(), the stream will report the error, |
| 1957 | * 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] | 1958 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 1959 | h2s = h2c_frt_stream_new(h2c, h2c->dsi); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1960 | if (!h2s) { |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 1961 | h2s = (struct h2s*)h2_refused_stream; |
| 1962 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | h2s->st = H2_SS_OPEN; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1966 | h2s->rxbuf = rxbuf; |
| 1967 | h2s->flags |= flags; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 1968 | h2s->body_len = body_len; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1969 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1970 | done: |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1971 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1972 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1973 | |
| 1974 | if (h2s->flags & H2_SF_ES_RCVD) { |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 1975 | if (h2s->cs) |
| 1976 | h2s->cs->flags |= CS_FL_EOI; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 1977 | if (h2s->st == H2_SS_OPEN) |
| 1978 | h2s->st = H2_SS_HREM; |
| 1979 | else |
| 1980 | h2s_close(h2s); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1981 | } |
| 1982 | |
Willy Tarreau | 3a429f0 | 2019-01-03 11:41:50 +0100 | [diff] [blame] | 1983 | /* update the max stream ID if the request is being processed */ |
| 1984 | if (h2s->id > h2c->max_id) |
| 1985 | h2c->max_id = h2s->id; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1986 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1987 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1988 | |
| 1989 | conn_err: |
| 1990 | h2c_error(h2c, error); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1991 | goto out; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1992 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1993 | out: |
| 1994 | h2_release_buf(h2c, &rxbuf); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1995 | return NULL; |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 1996 | |
| 1997 | send_rst: |
| 1998 | /* make the demux send an RST for the current stream. We may only |
| 1999 | * do this if we're certain that the HEADERS frame was properly |
| 2000 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2001 | */ |
| 2002 | h2_release_buf(h2c, &rxbuf); |
| 2003 | h2c->st0 = H2_CS_FRAME_E; |
| 2004 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2005 | } |
| 2006 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2007 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 2008 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 2009 | * errors here are reported as connection errors since it's impossible to |
| 2010 | * recover from such errors after the compression context has been altered. |
| 2011 | */ |
| 2012 | static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 2013 | { |
| 2014 | int error; |
| 2015 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2016 | if (!b_size(&h2c->dbuf)) |
| 2017 | return NULL; // empty buffer |
| 2018 | |
| 2019 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
| 2020 | return NULL; // incomplete frame |
| 2021 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2022 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2023 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2024 | /* unrecoverable error ? */ |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2025 | if (h2c->st0 >= H2_CS_ERROR) |
| 2026 | return NULL; |
| 2027 | |
Willy Tarreau | 08bb1d6 | 2019-01-30 16:55:48 +0100 | [diff] [blame] | 2028 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2029 | /* RFC7540#5.1 */ |
| 2030 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2031 | h2c->st0 = H2_CS_FRAME_E; |
| 2032 | return NULL; |
| 2033 | } |
| 2034 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2035 | if (error <= 0) { |
| 2036 | if (error == 0) |
| 2037 | return NULL; // missing data |
| 2038 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2039 | /* stream error : send RST_STREAM */ |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2040 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2041 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2042 | return NULL; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2043 | } |
| 2044 | |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2045 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 2046 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2047 | if (h2s->cs) |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2048 | h2s->cs->flags |= CS_FL_EOI; |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2049 | } |
| 2050 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2051 | 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] | 2052 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2053 | 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] | 2054 | h2s->st = H2_SS_HREM; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2055 | 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] | 2056 | h2s_close(h2s); |
| 2057 | |
| 2058 | return h2s; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2059 | } |
| 2060 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2061 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 2062 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 2063 | */ |
| 2064 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 2065 | { |
| 2066 | int error; |
| 2067 | |
| 2068 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 2069 | * to signal an end of stream (with the ES flag). |
| 2070 | */ |
| 2071 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2072 | if (!b_size(&h2c->dbuf) && h2c->dfl) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2073 | return 0; // empty buffer |
| 2074 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2075 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2076 | return 0; // incomplete frame |
| 2077 | |
| 2078 | /* now either the frame is complete or the buffer is complete */ |
| 2079 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2080 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2081 | /* RFC7540#6.1 */ |
| 2082 | error = H2_ERR_STREAM_CLOSED; |
| 2083 | goto strm_err; |
| 2084 | } |
| 2085 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2086 | if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) { |
| 2087 | /* RFC7540#8.1.2 */ |
| 2088 | error = H2_ERR_PROTOCOL_ERROR; |
| 2089 | goto strm_err; |
| 2090 | } |
| 2091 | |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 2092 | if (!h2_frt_transfer_data(h2s)) |
| 2093 | return 0; |
| 2094 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2095 | /* call the upper layers to process the frame, then let the upper layer |
| 2096 | * notify the stream about any change. |
| 2097 | */ |
| 2098 | if (!h2s->cs) { |
| 2099 | error = H2_ERR_STREAM_CLOSED; |
| 2100 | goto strm_err; |
| 2101 | } |
| 2102 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 2103 | if (h2c->st0 >= H2_CS_ERROR) |
| 2104 | return 0; |
| 2105 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2106 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2107 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2108 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2109 | } |
| 2110 | |
| 2111 | /* check for completion : the callee will change this to FRAME_A or |
| 2112 | * FRAME_H once done. |
| 2113 | */ |
| 2114 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2115 | return 0; |
| 2116 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2117 | /* last frame */ |
| 2118 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2119 | if (h2s->cs) |
| 2120 | h2s->cs->flags |= CS_FL_EOI; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2121 | if (h2s->st == H2_SS_OPEN) |
| 2122 | h2s->st = H2_SS_HREM; |
| 2123 | else |
| 2124 | h2s_close(h2s); |
| 2125 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2126 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2127 | |
| 2128 | if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) { |
| 2129 | /* RFC7540#8.1.2 */ |
| 2130 | error = H2_ERR_PROTOCOL_ERROR; |
| 2131 | goto strm_err; |
| 2132 | } |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2133 | } |
| 2134 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2135 | return 1; |
| 2136 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2137 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 2138 | h2s_error(h2s, error); |
| 2139 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2140 | return 0; |
| 2141 | } |
| 2142 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2143 | /* process Rx frames to be demultiplexed */ |
| 2144 | static void h2_process_demux(struct h2c *h2c) |
| 2145 | { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2146 | struct h2s *h2s = NULL, *tmp_h2s; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2147 | struct h2_fh hdr; |
| 2148 | unsigned int padlen = 0; |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 2149 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2150 | if (h2c->st0 >= H2_CS_ERROR) |
| 2151 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2152 | |
| 2153 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2154 | if (h2c->st0 == H2_CS_PREFACE) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2155 | if (h2c->flags & H2_CF_IS_BACK) |
| 2156 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2157 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 2158 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2159 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2160 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2161 | sess_log(h2c->conn->owner); |
| 2162 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2163 | goto fail; |
| 2164 | } |
| 2165 | |
| 2166 | h2c->max_id = 0; |
| 2167 | h2c->st0 = H2_CS_SETTINGS1; |
| 2168 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2169 | |
| 2170 | if (h2c->st0 == H2_CS_SETTINGS1) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2171 | /* ensure that what is pending is a valid SETTINGS frame |
| 2172 | * without an ACK. |
| 2173 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2174 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2175 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2176 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2177 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2178 | sess_log(h2c->conn->owner); |
| 2179 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2180 | goto fail; |
| 2181 | } |
| 2182 | |
| 2183 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 2184 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2185 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2186 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2187 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2188 | goto fail; |
| 2189 | } |
| 2190 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2191 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2192 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2193 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2194 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2195 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2196 | goto fail; |
| 2197 | } |
| 2198 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2199 | /* that's OK, switch to FRAME_P to process it. This is |
| 2200 | * a SETTINGS frame whose header has already been |
| 2201 | * deleted above. |
| 2202 | */ |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2203 | padlen = 0; |
| 2204 | goto new_frame; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2205 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2206 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2207 | |
| 2208 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2209 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2210 | int ret = 0; |
| 2211 | |
| 2212 | if (h2c->st0 >= H2_CS_ERROR) |
| 2213 | break; |
| 2214 | |
| 2215 | if (h2c->st0 == H2_CS_FRAME_H) { |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 2216 | if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2217 | break; |
| 2218 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2219 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2220 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2221 | if (!h2c->nb_streams) { |
| 2222 | /* only log if no other stream can report the error */ |
| 2223 | sess_log(h2c->conn->owner); |
| 2224 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2225 | break; |
| 2226 | } |
| 2227 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2228 | if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) { |
| 2229 | /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA), |
| 2230 | * we read the pad length and drop it from the remaining |
| 2231 | * payload (one byte + the 9 remaining ones = 10 total |
| 2232 | * removed), so we have a frame payload starting after the |
| 2233 | * pad len. Flow controlled frames (DATA) also count the |
| 2234 | * padlen in the flow control, so it must be adjusted. |
| 2235 | */ |
| 2236 | if (hdr.len < 1) { |
| 2237 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2238 | sess_log(h2c->conn->owner); |
| 2239 | goto fail; |
| 2240 | } |
| 2241 | hdr.len--; |
| 2242 | |
| 2243 | if (b_data(&h2c->dbuf) < 10) |
| 2244 | break; // missing padlen |
| 2245 | |
| 2246 | padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9); |
| 2247 | |
| 2248 | if (padlen > hdr.len) { |
| 2249 | /* RFC7540#6.1 : pad length = length of |
| 2250 | * frame payload or greater => error. |
| 2251 | */ |
| 2252 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2253 | sess_log(h2c->conn->owner); |
| 2254 | goto fail; |
| 2255 | } |
| 2256 | |
| 2257 | if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) { |
| 2258 | h2c->rcvd_c++; |
| 2259 | h2c->rcvd_s++; |
| 2260 | } |
| 2261 | b_del(&h2c->dbuf, 1); |
| 2262 | } |
| 2263 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2264 | |
| 2265 | new_frame: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2266 | h2c->dfl = hdr.len; |
| 2267 | h2c->dsi = hdr.sid; |
| 2268 | h2c->dft = hdr.ft; |
| 2269 | h2c->dff = hdr.ff; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2270 | h2c->dpl = padlen; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2271 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2272 | |
| 2273 | /* check for minimum basic frame format validity */ |
| 2274 | ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize); |
| 2275 | if (ret != H2_ERR_NO_ERROR) { |
| 2276 | h2c_error(h2c, ret); |
| 2277 | sess_log(h2c->conn->owner); |
| 2278 | goto fail; |
| 2279 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2283 | tmp_h2s = h2c_st_by_id(h2c, h2c->dsi); |
| 2284 | |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2285 | if (tmp_h2s != h2s && h2s && h2s->cs && |
| 2286 | (b_data(&h2s->rxbuf) || |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2287 | (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] | 2288 | /* we may have to signal the upper layers */ |
| 2289 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2290 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2291 | } |
| 2292 | h2s = tmp_h2s; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2293 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 2294 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2295 | goto strm_err; |
| 2296 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2297 | if (h2s->st == H2_SS_IDLE && |
| 2298 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 2299 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 2300 | * this state MUST be treated as a connection error |
| 2301 | */ |
| 2302 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2303 | if (!h2c->nb_streams) { |
| 2304 | /* only log if no other stream can report the error */ |
| 2305 | sess_log(h2c->conn->owner); |
| 2306 | } |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2307 | break; |
| 2308 | } |
| 2309 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2310 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 2311 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 2312 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2313 | * this state MUST be treated as a stream error. |
| 2314 | * 6.2, 6.6 and 6.10 further mandate that HEADERS/ |
| 2315 | * PUSH_PROMISE/CONTINUATION cause connection errors. |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2316 | */ |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2317 | if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) |
| 2318 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2319 | else |
| 2320 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2321 | goto strm_err; |
| 2322 | } |
| 2323 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2324 | /* Below the management of frames received in closed state is a |
| 2325 | * bit hackish because the spec makes strong differences between |
| 2326 | * streams closed by receiving RST, sending RST, and seeing ES |
| 2327 | * in both directions. In addition to this, the creation of a |
| 2328 | * new stream reusing the identifier of a closed one will be |
| 2329 | * detected here. Given that we cannot keep track of all closed |
| 2330 | * streams forever, we consider that unknown closed streams were |
| 2331 | * closed on RST received, which allows us to respond with an |
| 2332 | * RST without breaking the connection (eg: to abort a transfer). |
| 2333 | * Some frames have to be silently ignored as well. |
| 2334 | */ |
| 2335 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
Willy Tarreau | 3ad5d31 | 2019-01-29 18:33:26 +0100 | [diff] [blame] | 2336 | 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] | 2337 | /* #5.1.1: The identifier of a newly |
| 2338 | * established stream MUST be numerically |
| 2339 | * greater than all streams that the initiating |
| 2340 | * endpoint has opened or reserved. This |
| 2341 | * governs streams that are opened using a |
| 2342 | * HEADERS frame and streams that are reserved |
| 2343 | * using PUSH_PROMISE. An endpoint that |
| 2344 | * receives an unexpected stream identifier |
| 2345 | * MUST respond with a connection error. |
| 2346 | */ |
| 2347 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2348 | goto strm_err; |
| 2349 | } |
| 2350 | |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2351 | 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] | 2352 | /* RFC7540#5.1:closed: an endpoint that |
| 2353 | * receives any frame other than PRIORITY after |
| 2354 | * receiving a RST_STREAM MUST treat that as a |
| 2355 | * stream error of type STREAM_CLOSED. |
| 2356 | * |
| 2357 | * Note that old streams fall into this category |
| 2358 | * and will lead to an RST being sent. |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2359 | * |
| 2360 | * However, we cannot generalize this to all frame types. Those |
| 2361 | * carrying compression state must still be processed before |
| 2362 | * being dropped or we'll desynchronize the decoder. This can |
| 2363 | * happen with request trailers received after sending an |
| 2364 | * RST_STREAM, or with header/trailers responses received after |
| 2365 | * sending RST_STREAM (aborted stream). |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2366 | */ |
| 2367 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2368 | h2c->st0 = H2_CS_FRAME_E; |
| 2369 | goto strm_err; |
| 2370 | } |
| 2371 | |
| 2372 | /* RFC7540#5.1:closed: if this state is reached as a |
| 2373 | * result of sending a RST_STREAM frame, the peer that |
| 2374 | * receives the RST_STREAM might have already sent |
| 2375 | * frames on the stream that cannot be withdrawn. An |
| 2376 | * endpoint MUST ignore frames that it receives on |
| 2377 | * closed streams after it has sent a RST_STREAM |
| 2378 | * frame. An endpoint MAY choose to limit the period |
| 2379 | * over which it ignores frames and treat frames that |
| 2380 | * arrive after this time as being in error. |
| 2381 | */ |
Willy Tarreau | 24ff1f8 | 2019-01-30 19:20:09 +0100 | [diff] [blame] | 2382 | if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2383 | /* RFC7540#5.1:closed: any frame other than |
| 2384 | * PRIO/WU/RST in this state MUST be treated as |
| 2385 | * a connection error |
| 2386 | */ |
| 2387 | if (h2c->dft != H2_FT_RST_STREAM && |
| 2388 | h2c->dft != H2_FT_PRIORITY && |
| 2389 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 2390 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2391 | goto strm_err; |
| 2392 | } |
| 2393 | } |
| 2394 | } |
| 2395 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2396 | #if 0 |
| 2397 | // problem below: it is not possible to completely ignore such |
| 2398 | // streams as we need to maintain the compression state as well |
| 2399 | // and for this we need to completely process these frames (eg: |
| 2400 | // HEADERS frames) as well as counting DATA frames to emit |
| 2401 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 2402 | // This is a typical case of layer violation where the |
| 2403 | // transported contents are critical to the connection's |
| 2404 | // validity and must be ignored at the same time :-( |
| 2405 | |
| 2406 | /* graceful shutdown, ignore streams whose ID is higher than |
| 2407 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 2408 | */ |
| 2409 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2410 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2411 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2412 | h2c->dfl -= ret; |
| 2413 | ret = h2c->dfl == 0; |
| 2414 | goto strm_err; |
| 2415 | } |
| 2416 | #endif |
| 2417 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2418 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 2419 | case H2_FT_SETTINGS: |
| 2420 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2421 | ret = h2c_handle_settings(h2c); |
| 2422 | |
| 2423 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2424 | ret = h2c_ack_settings(h2c); |
| 2425 | break; |
| 2426 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 2427 | case H2_FT_PING: |
| 2428 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2429 | ret = h2c_handle_ping(h2c); |
| 2430 | |
| 2431 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2432 | ret = h2c_ack_ping(h2c); |
| 2433 | break; |
| 2434 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 2435 | case H2_FT_WINDOW_UPDATE: |
| 2436 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2437 | ret = h2c_handle_window_update(h2c, h2s); |
| 2438 | break; |
| 2439 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2440 | case H2_FT_CONTINUATION: |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2441 | /* RFC7540#6.10: CONTINUATION may only be preceeded by |
| 2442 | * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These |
| 2443 | * frames' parsers consume all following CONTINUATION |
| 2444 | * frames so this one is out of sequence. |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2445 | */ |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2446 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2447 | sess_log(h2c->conn->owner); |
| 2448 | goto fail; |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2449 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2450 | case H2_FT_HEADERS: |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2451 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2452 | if (h2c->flags & H2_CF_IS_BACK) |
| 2453 | tmp_h2s = h2c_bck_handle_headers(h2c, h2s); |
| 2454 | else |
| 2455 | tmp_h2s = h2c_frt_handle_headers(h2c, h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2456 | if (tmp_h2s) { |
| 2457 | h2s = tmp_h2s; |
| 2458 | ret = 1; |
| 2459 | } |
| 2460 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2461 | break; |
| 2462 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2463 | case H2_FT_DATA: |
| 2464 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2465 | ret = h2c_frt_handle_data(h2c, h2s); |
| 2466 | |
| 2467 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2468 | ret = h2c_send_strm_wu(h2c); |
| 2469 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2470 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 2471 | case H2_FT_PRIORITY: |
| 2472 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2473 | ret = h2c_handle_priority(h2c); |
| 2474 | break; |
| 2475 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2476 | case H2_FT_RST_STREAM: |
| 2477 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2478 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 2479 | break; |
| 2480 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 2481 | case H2_FT_GOAWAY: |
| 2482 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2483 | ret = h2c_handle_goaway(h2c); |
| 2484 | break; |
| 2485 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2486 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2487 | default: |
| 2488 | /* drop frames that we ignore. They may be larger than |
| 2489 | * the buffer so we drain all of their contents until |
| 2490 | * we reach the end. |
| 2491 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2492 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2493 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2494 | h2c->dfl -= ret; |
| 2495 | ret = h2c->dfl == 0; |
| 2496 | } |
| 2497 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2498 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2499 | /* We may have to send an RST if not done yet */ |
| 2500 | if (h2s->st == H2_SS_ERROR) |
| 2501 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2502 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2503 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2504 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2505 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2506 | /* error or missing data condition met above ? */ |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2507 | if (ret <= 0) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2508 | break; |
| 2509 | |
| 2510 | if (h2c->st0 != H2_CS_FRAME_H) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2511 | b_del(&h2c->dbuf, h2c->dfl); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2512 | h2c->st0 = H2_CS_FRAME_H; |
| 2513 | } |
| 2514 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2515 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2516 | if (h2c->rcvd_c > 0 && |
| 2517 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2518 | h2c_send_conn_wu(h2c); |
| 2519 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2520 | fail: |
| 2521 | /* we can go here on missing data, blocked response or error */ |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2522 | if (h2s && h2s->cs && |
| 2523 | (b_data(&h2s->rxbuf) || |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2524 | (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] | 2525 | /* we may have to signal the upper layers */ |
| 2526 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2527 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2528 | } |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2529 | |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 2530 | h2c_restart_reading(h2c); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2531 | } |
| 2532 | |
| 2533 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2534 | * the end. |
| 2535 | */ |
| 2536 | static int h2_process_mux(struct h2c *h2c) |
| 2537 | { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2538 | struct h2s *h2s; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2539 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2540 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2541 | if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) { |
| 2542 | if (unlikely(h2c_bck_send_preface(h2c) <= 0)) { |
| 2543 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2544 | if (h2c->st0 == H2_CS_ERROR) { |
| 2545 | h2c->st0 = H2_CS_ERROR2; |
| 2546 | sess_log(h2c->conn->owner); |
| 2547 | } |
| 2548 | goto fail; |
| 2549 | } |
| 2550 | h2c->st0 = H2_CS_SETTINGS1; |
| 2551 | } |
| 2552 | /* need to wait for the other side */ |
Willy Tarreau | 75a930a | 2018-12-12 08:03:58 +0100 | [diff] [blame] | 2553 | if (h2c->st0 < H2_CS_FRAME_H) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2554 | return 1; |
| 2555 | } |
| 2556 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2557 | /* start by sending possibly pending window updates */ |
| 2558 | if (h2c->rcvd_c > 0 && |
| 2559 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2560 | h2c_send_conn_wu(h2c) < 0) |
| 2561 | goto fail; |
| 2562 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2563 | /* First we always process the flow control list because the streams |
| 2564 | * waiting there were already elected for immediate emission but were |
| 2565 | * blocked just on this. |
| 2566 | */ |
| 2567 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2568 | list_for_each_entry(h2s, &h2c->fctl_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2569 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2570 | h2c->st0 >= H2_CS_ERROR) |
| 2571 | break; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2572 | if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE) |
| 2573 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2574 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2575 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2576 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2577 | h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2578 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2579 | tasklet_wakeup(h2s->send_wait->task); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2580 | } |
| 2581 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2582 | list_for_each_entry(h2s, &h2c->send_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2583 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2584 | break; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2585 | if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE) |
| 2586 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2587 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2588 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2589 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2590 | h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2591 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2592 | tasklet_wakeup(h2s->send_wait->task); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2593 | } |
| 2594 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2595 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2596 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2597 | if (h2c->st0 == H2_CS_ERROR) { |
| 2598 | if (h2c->max_id >= 0) { |
| 2599 | h2c_send_goaway_error(h2c, NULL); |
| 2600 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2601 | return 0; |
| 2602 | } |
| 2603 | |
| 2604 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2605 | } |
| 2606 | return 1; |
| 2607 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2608 | return (1); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2609 | } |
| 2610 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2611 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2612 | /* Attempt to read data, and subscribe if none available. |
| 2613 | * The function returns 1 if data has been received, otherwise zero. |
| 2614 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2615 | static int h2_recv(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2616 | { |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2617 | struct connection *conn = h2c->conn; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2618 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2619 | int max; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2620 | size_t ret; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2621 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2622 | if (h2c->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2623 | return (b_data(&h2c->dbuf)); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2624 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2625 | if (!h2_recv_allowed(h2c)) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2626 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2627 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2628 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2629 | if (!buf) { |
| 2630 | h2c->flags |= H2_CF_DEM_DALLOC; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2631 | return 0; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2632 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2633 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2634 | do { |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 2635 | b_realign_if_empty(buf); |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2636 | if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
| 2637 | /* HTX in use : try to pre-align the buffer like the |
| 2638 | * rxbufs will be to optimize memory copies. We'll make |
| 2639 | * sure that the frame header lands at the end of the |
| 2640 | * HTX block to alias it upon recv. We cannot use the |
| 2641 | * head because rcv_buf() will realign the buffer if |
| 2642 | * it's empty. Thus we cheat and pretend we already |
| 2643 | * have a few bytes there. |
| 2644 | */ |
| 2645 | max = buf_room_for_htx_data(buf) + 9; |
Willy Tarreau | c0960d1 | 2018-12-14 10:59:15 +0100 | [diff] [blame] | 2646 | buf->head = sizeof(struct htx) - 9; |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2647 | } |
| 2648 | else |
| 2649 | max = b_room(buf); |
| 2650 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2651 | if (max) |
| 2652 | ret = conn->xprt->rcv_buf(conn, buf, max, 0); |
| 2653 | else |
| 2654 | ret = 0; |
| 2655 | } while (ret > 0); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2656 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2657 | if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size)) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2658 | conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event); |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2659 | |
Olivier Houchard | a1411e6 | 2018-08-17 18:42:48 +0200 | [diff] [blame] | 2660 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2661 | h2_release_buf(h2c, &h2c->dbuf); |
Olivier Houchard | 4667773 | 2018-11-29 17:06:17 +0100 | [diff] [blame] | 2662 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2663 | } |
| 2664 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2665 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2666 | h2c->flags |= H2_CF_DEM_DFULL; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2667 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2668 | } |
| 2669 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2670 | /* Try to send data if possible. |
| 2671 | * The function returns 1 if data have been sent, otherwise zero. |
| 2672 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2673 | static int h2_send(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2674 | { |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2675 | struct connection *conn = h2c->conn; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2676 | int done; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2677 | int sent = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2678 | |
| 2679 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 2680 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2681 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2682 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2683 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2684 | /* a handshake was requested */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2685 | goto schedule; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2686 | } |
| 2687 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2688 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2689 | * pending streams into the existing buffer until it's reportedly full |
| 2690 | * or the end of send requests is reached. Then it tries to send this |
| 2691 | * buffer's contents out, marks it not full if at least one byte could |
| 2692 | * be sent, and tries again. |
| 2693 | * |
| 2694 | * The snd_buf() function normally takes a "flags" argument which may |
| 2695 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2696 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2697 | * connection is streaming lots of data (used to increase TLS record |
| 2698 | * size at the expense of latency). The former can be sent any time |
| 2699 | * there's a buffer full flag, as it indicates at least one stream |
| 2700 | * attempted to send and failed so there are pending data. An |
| 2701 | * alternative would be to set it as long as there's an active stream |
| 2702 | * but that would be problematic for ACKs until we have an absolute |
| 2703 | * guarantee that all waiters have at least one byte to send. The |
| 2704 | * latter should possibly not be set for now. |
| 2705 | */ |
| 2706 | |
| 2707 | done = 0; |
| 2708 | while (!done) { |
| 2709 | unsigned int flags = 0; |
| 2710 | |
| 2711 | /* fill as much as we can into the current buffer */ |
| 2712 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2713 | done = h2_process_mux(h2c); |
| 2714 | |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2715 | if (h2c->flags & H2_CF_MUX_MALLOC) |
| 2716 | break; |
| 2717 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2718 | if (conn->flags & CO_FL_ERROR) |
| 2719 | break; |
| 2720 | |
| 2721 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2722 | flags |= CO_SFL_MSG_MORE; |
| 2723 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2724 | if (b_data(&h2c->mbuf)) { |
| 2725 | 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] | 2726 | if (!ret) |
| 2727 | break; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2728 | sent = 1; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2729 | b_del(&h2c->mbuf, ret); |
| 2730 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2731 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2732 | |
| 2733 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2734 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2735 | } |
| 2736 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2737 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2738 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2739 | b_reset(&h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2740 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2741 | /* We're not full anymore, so we can wake any task that are waiting |
| 2742 | * for us. |
| 2743 | */ |
| 2744 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2745 | struct h2s *h2s; |
| 2746 | |
| 2747 | list_for_each_entry(h2s, &h2c->send_list, list) { |
| 2748 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2749 | break; |
| 2750 | if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE) |
| 2751 | continue; |
| 2752 | |
| 2753 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2754 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2755 | h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2756 | tasklet_wakeup(h2s->send_wait->task); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2757 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 2758 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2759 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2760 | /* We're done, no more to send */ |
| 2761 | if (!b_data(&h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2762 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2763 | schedule: |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2764 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
| 2765 | conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event); |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2766 | return sent; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2767 | } |
| 2768 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 2769 | /* this is the tasklet referenced in h2c->wait_event.task */ |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2770 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2771 | { |
| 2772 | struct h2c *h2c = ctx; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2773 | int ret = 0; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2774 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2775 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2776 | ret = h2_send(h2c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2777 | if (!(h2c->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2778 | ret |= h2_recv(h2c); |
Willy Tarreau | cef5c8e | 2018-12-18 10:29:54 +0100 | [diff] [blame] | 2779 | if (ret || b_data(&h2c->dbuf)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2780 | h2_process(h2c); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2781 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2782 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2783 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2784 | /* callback called on any event by the connection handler. |
| 2785 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2786 | * destruction of the connection (which normally doesn not happen in h2). |
| 2787 | */ |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2788 | static int h2_process(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2789 | { |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2790 | struct connection *conn = h2c->conn; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2791 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2792 | if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2793 | h2_process_demux(h2c); |
| 2794 | |
| 2795 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2796 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2797 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2798 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2799 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 2800 | } |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2801 | h2_send(h2c); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2802 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 2803 | if (unlikely(h2c->proxy->state == PR_STSTOPPED)) { |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2804 | /* frontend is stopping, reload likely in progress, let's try |
| 2805 | * to announce a graceful shutdown if not yet done. We don't |
| 2806 | * care if it fails, it will be tried again later. |
| 2807 | */ |
| 2808 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 2809 | if (h2c->last_sid < 0) |
| 2810 | h2c->last_sid = (1U << 31) - 1; |
| 2811 | h2c_send_goaway_error(h2c, NULL); |
| 2812 | } |
| 2813 | } |
| 2814 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2815 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2816 | * If we received early data, and the handshake is done, wake |
| 2817 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2818 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2819 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 2820 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 2821 | struct eb32_node *node; |
| 2822 | struct h2s *h2s; |
| 2823 | |
| 2824 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 2825 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 2826 | |
| 2827 | while (node) { |
| 2828 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | fde287c | 2018-12-19 18:33:16 +0100 | [diff] [blame] | 2829 | if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2830 | h2s_notify_recv(h2s); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2831 | node = eb32_next(node); |
| 2832 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2833 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2834 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 2835 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 2836 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 2837 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 2838 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 2839 | h2_wake_some_streams(h2c, 0, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2840 | |
| 2841 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2842 | /* no more stream, kill the connection now */ |
| 2843 | h2_release(conn); |
| 2844 | return -1; |
| 2845 | } |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2846 | } |
| 2847 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2848 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2849 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2850 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2851 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 2852 | h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 2853 | (h2c->st0 != H2_CS_ERROR && |
| 2854 | !b_data(&h2c->mbuf) && |
| 2855 | (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && |
| 2856 | ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list)))) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2857 | h2_release_buf(h2c, &h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2858 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2859 | if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2860 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2861 | 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] | 2862 | task_queue(h2c->task); |
| 2863 | } |
| 2864 | else |
| 2865 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2866 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2867 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2868 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2869 | return 0; |
| 2870 | } |
| 2871 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 2872 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2873 | static int h2_wake(struct connection *conn) |
| 2874 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2875 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2876 | |
| 2877 | return (h2_process(h2c)); |
| 2878 | } |
| 2879 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2880 | /* Connection timeout management. The principle is that if there's no receipt |
| 2881 | * nor sending for a certain amount of time, the connection is closed. If the |
| 2882 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 2883 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 2884 | * GOAWAY frame. |
| 2885 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2886 | 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] | 2887 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2888 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2889 | int expired = tick_is_expired(t->expire, now_ms); |
| 2890 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2891 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2892 | return t; |
| 2893 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2894 | task_delete(t); |
| 2895 | task_free(t); |
| 2896 | |
| 2897 | if (!h2c) { |
| 2898 | /* resources were already deleted */ |
| 2899 | return NULL; |
| 2900 | } |
| 2901 | |
| 2902 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2903 | h2c_error(h2c, H2_ERR_NO_ERROR); |
| 2904 | h2_wake_some_streams(h2c, 0, 0); |
| 2905 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2906 | if (b_data(&h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2907 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 2908 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2909 | } |
| 2910 | |
| 2911 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2912 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2913 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 2914 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2915 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2916 | if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) { |
| 2917 | 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] | 2918 | if (ret > 0) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2919 | b_del(&h2c->mbuf, ret); |
| 2920 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2921 | } |
| 2922 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2923 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2924 | /* either we can release everything now or it will be done later once |
| 2925 | * the last stream closes. |
| 2926 | */ |
| 2927 | if (eb_is_empty(&h2c->streams_by_id)) |
| 2928 | h2_release(h2c->conn); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2929 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2930 | return NULL; |
| 2931 | } |
| 2932 | |
| 2933 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2934 | /*******************************************/ |
| 2935 | /* functions below are used by the streams */ |
| 2936 | /*******************************************/ |
| 2937 | |
| 2938 | /* |
| 2939 | * Attach a new stream to a connection |
| 2940 | * (Used for outgoing connections) |
| 2941 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2942 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2943 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2944 | struct conn_stream *cs; |
| 2945 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2946 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2947 | |
| 2948 | cs = cs_new(conn); |
| 2949 | if (!cs) |
| 2950 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2951 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2952 | if (!h2s) { |
| 2953 | cs_free(cs); |
| 2954 | return NULL; |
| 2955 | } |
| 2956 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2957 | } |
| 2958 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 2959 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 2960 | * We have to scan because we may have some orphan streams. It might be |
| 2961 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 2962 | * orphans. |
| 2963 | */ |
| 2964 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 2965 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2966 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 2967 | struct h2s *h2s; |
| 2968 | struct eb32_node *node; |
| 2969 | |
| 2970 | node = eb32_first(&h2c->streams_by_id); |
| 2971 | while (node) { |
| 2972 | h2s = container_of(node, struct h2s, by_id); |
| 2973 | if (h2s->cs) |
| 2974 | return h2s->cs; |
| 2975 | node = eb32_next(node); |
| 2976 | } |
| 2977 | return NULL; |
| 2978 | } |
| 2979 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2980 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 2981 | * Destroy the mux and the associated connection, if it is no longer used |
| 2982 | */ |
| 2983 | static void h2_destroy(struct connection *conn) |
| 2984 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2985 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 2986 | |
| 2987 | if (eb_is_empty(&h2c->streams_by_id)) |
| 2988 | h2_release(h2c->conn); |
| 2989 | } |
| 2990 | |
| 2991 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2992 | * Detach the stream from the connection and possibly release the connection. |
| 2993 | */ |
| 2994 | static void h2_detach(struct conn_stream *cs) |
| 2995 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2996 | struct h2s *h2s = cs->ctx; |
| 2997 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2998 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2999 | |
| 3000 | cs->ctx = NULL; |
| 3001 | if (!h2s) |
| 3002 | return; |
| 3003 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3004 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3005 | h2c = h2s->h2c; |
| 3006 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 3007 | h2c->nb_cs--; |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 3008 | if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY && |
| 3009 | !h2_frt_has_too_many_cs(h2c)) { |
| 3010 | /* frontend connection was blocking new streams creation */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3011 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 3012 | h2c_restart_reading(h2c); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3013 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3014 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3015 | /* this stream may be blocked waiting for some data to leave (possibly |
| 3016 | * an ES or RST frame), so orphan it in this case. |
| 3017 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 3018 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 3019 | (h2c->st0 < H2_CS_ERROR) && |
Olivier Houchard | 16ff261 | 2019-03-21 15:48:46 +0100 | [diff] [blame] | 3020 | (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] | 3021 | return; |
| 3022 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3023 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 3024 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 3025 | /* unblock the connection if it was blocked on this |
| 3026 | * stream. |
| 3027 | */ |
| 3028 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 3029 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 3030 | h2c_restart_reading(h2c); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3031 | } |
| 3032 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 3033 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3034 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3035 | if (h2c->flags & H2_CF_IS_BACK && |
| 3036 | (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3037 | if (!(h2c->conn->flags & |
| 3038 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 3039 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3040 | h2c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3041 | if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) { |
| 3042 | h2c->conn->owner = NULL; |
| 3043 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3044 | if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn)) |
| 3045 | /* The server doesn't want it, let's kill the connection right away */ |
| 3046 | h2c->conn->mux->destroy(h2c->conn); |
| 3047 | return; |
| 3048 | } |
| 3049 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3050 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3051 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3052 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 3053 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 3054 | return; |
| 3055 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3056 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3057 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3058 | h2c->conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 3059 | if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3060 | struct server *srv = objt_server(h2c->conn->target); |
| 3061 | |
| 3062 | if (srv) { |
| 3063 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 3064 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 3065 | else |
| 3066 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 3067 | } |
| 3068 | |
| 3069 | } |
| 3070 | } |
| 3071 | } |
| 3072 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3073 | /* We don't want to close right now unless we're removing the |
| 3074 | * last stream, and either the connection is in error, or it |
| 3075 | * reached the ID already specified in a GOAWAY frame received |
| 3076 | * or sent (as seen by last_sid >= 0). |
| 3077 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3078 | if (h2c_is_dead(h2c)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3079 | /* no more stream will come, kill it now */ |
| 3080 | h2_release(h2c->conn); |
| 3081 | } |
| 3082 | else if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3083 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3084 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3085 | task_queue(h2c->task); |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 3086 | } |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3087 | else |
| 3088 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3089 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3090 | } |
| 3091 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3092 | /* Performs a synchronous or asynchronous shutr(). |
| 3093 | * FIXME: guess what the return code tries to indicate! |
| 3094 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3095 | static int h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3096 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3097 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3098 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3099 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 3100 | 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] | 3101 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3102 | |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3103 | /* a connstream may require us to immediately kill the whole connection |
| 3104 | * for example because of a "tcp-request content reject" rule that is |
| 3105 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3106 | * close the connection. |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3107 | */ |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3108 | if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) && |
| 3109 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3110 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3111 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3112 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3113 | else if (!(h2s->flags & H2_SF_HEADERS_SENT)) { |
| 3114 | /* Nothing was never sent for this stream, so reset with |
| 3115 | * REFUSED_STREAM error to let the client retry the |
| 3116 | * request. |
| 3117 | */ |
| 3118 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3119 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3120 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3121 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3122 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3123 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3124 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3125 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3126 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3127 | h2s_close(h2s); |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3128 | |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3129 | return 0; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3130 | add_to_list: |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3131 | if (LIST_ISEMPTY(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3132 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3133 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3134 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3135 | h2s->send_wait = sw; |
| 3136 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3137 | h2s->send_wait = sw; |
| 3138 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3139 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3140 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3141 | /* Let the handler know we want shutr */ |
| 3142 | sw->handle = (void *)((long)sw->handle | 1); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3143 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3144 | } |
| 3145 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3146 | /* Performs a synchronous or asynchronous shutw(). |
| 3147 | * FIXME: guess what the return code tries to indicate! |
| 3148 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3149 | static int h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3150 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3151 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3152 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3153 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 3154 | 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] | 3155 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3156 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3157 | if (h2s->flags & H2_SF_HEADERS_SENT) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3158 | /* we can cleanly close using an empty data frame only after headers */ |
| 3159 | |
| 3160 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 3161 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3162 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3163 | |
| 3164 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3165 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3166 | else |
| 3167 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3168 | } else { |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3169 | /* a connstream may require us to immediately kill the whole connection |
| 3170 | * for example because of a "tcp-request content reject" rule that is |
| 3171 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3172 | * close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3173 | */ |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3174 | if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) && |
| 3175 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3176 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3177 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3178 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3179 | else { |
| 3180 | /* Nothing was never sent for this stream, so reset with |
| 3181 | * REFUSED_STREAM error to let the client retry the |
| 3182 | * request. |
| 3183 | */ |
| 3184 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3185 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3186 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3187 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3188 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3189 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3190 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3191 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3192 | } |
| 3193 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3194 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3195 | tasklet_wakeup(h2c->wait_event.task); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3196 | return 0; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3197 | |
| 3198 | add_to_list: |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3199 | if (LIST_ISEMPTY(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3200 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3201 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3202 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3203 | h2s->send_wait = sw; |
| 3204 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3205 | h2s->send_wait = sw; |
| 3206 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3207 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3208 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3209 | /* let the handler know we want to shutw */ |
| 3210 | sw->handle = (void *)((long)(sw->handle) | 2); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3211 | return 1; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3212 | } |
| 3213 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3214 | /* This is the tasklet referenced in h2s->wait_event.task, it is used for |
| 3215 | * deferred shutdowns when the h2_detach() was done but the mux buffer was full |
| 3216 | * and prevented the last frame from being emitted. |
| 3217 | */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3218 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3219 | { |
| 3220 | struct h2s *h2s = ctx; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3221 | long reason = (long)h2s->wait_event.handle; |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3222 | int ret = 0; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3223 | |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3224 | if (h2s->send_wait) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3225 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3226 | h2s->send_wait = NULL; |
| 3227 | LIST_DEL(&h2s->list); |
| 3228 | LIST_INIT(&h2s->list); |
| 3229 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3230 | if (reason & 2) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3231 | ret |= h2_do_shutw(h2s); |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3232 | if (reason & 1) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3233 | ret |= h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3234 | |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3235 | /* We're no longer trying to send anything, let's destroy the h2s */ |
| 3236 | if (!ret) { |
| 3237 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3238 | h2s_destroy(h2s); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3239 | |
| 3240 | if (h2c_is_dead(h2c)) |
| 3241 | h2_release(h2c->conn); |
| 3242 | } |
| 3243 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3244 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3245 | } |
| 3246 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3247 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3248 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3249 | { |
| 3250 | struct h2s *h2s = cs->ctx; |
| 3251 | |
| 3252 | if (!mode) |
| 3253 | return; |
| 3254 | |
| 3255 | h2_do_shutr(h2s); |
| 3256 | } |
| 3257 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3258 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3259 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3260 | { |
| 3261 | struct h2s *h2s = cs->ctx; |
| 3262 | |
| 3263 | h2_do_shutw(h2s); |
| 3264 | } |
| 3265 | |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3266 | /* 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] | 3267 | * HTX request or response depending on the connection's side. Returns a |
| 3268 | * positive value on success, a negative value on failure, or 0 if it couldn't |
| 3269 | * proceed. May report connection errors in h2c->errcode if the frame is |
| 3270 | * non-decodable and the connection unrecoverable. In absence of connection |
| 3271 | * 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] | 3272 | * |
| 3273 | * The function may fold CONTINUATION frames into the initial HEADERS frame |
| 3274 | * by removing padding and next frame header, then moving the CONTINUATION |
| 3275 | * frame's payload and adjusting h2c->dfl to match the new aggregated frame, |
| 3276 | * leaving a hole between the main frame and the beginning of the next one. |
| 3277 | * The possibly remaining incomplete or next frame at the end may be moved |
| 3278 | * if the aggregated frame is not deleted, in order to fill the hole. Wrapped |
| 3279 | * HEADERS frames are unwrapped into a temporary buffer before decoding. |
| 3280 | * |
| 3281 | * A buffer at the beginning of processing may look like this : |
| 3282 | * |
| 3283 | * ,---.---------.-----.--------------.--------------.------.---. |
| 3284 | * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///| |
| 3285 | * `---^---------^-----^--------------^--------------^------^---' |
| 3286 | * | | <-----> | | |
| 3287 | * area | dpl | wrap |
| 3288 | * |<--------------> | |
| 3289 | * | dfl | |
| 3290 | * |<-------------------------------------------------->| |
| 3291 | * head data |
| 3292 | * |
| 3293 | * Padding is automatically overwritten when folding, participating to the |
| 3294 | * hole size after dfl : |
| 3295 | * |
| 3296 | * ,---.------------------------.-----.--------------.------.---. |
| 3297 | * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///| |
| 3298 | * `---^------------------------^-----^--------------^------^---' |
| 3299 | * | | <-----> | | |
| 3300 | * area | hole | wrap |
| 3301 | * |<-----------------------> | |
| 3302 | * | dfl | |
| 3303 | * |<-------------------------------------------------->| |
| 3304 | * head data |
| 3305 | * |
| 3306 | * Please note that the HEADERS frame is always deprived from its PADLEN byte |
| 3307 | * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY |
| 3308 | * bit. |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3309 | * |
| 3310 | * The <flags> field must point to either the stream's flags or to a copy of it |
| 3311 | * so that the function can update the following flags : |
| 3312 | * - H2_SF_DATA_CLEN when content-length is seen |
| 3313 | * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion |
| 3314 | * - H2_SF_HEADERS_RCVD once the frame is successfully decoded |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3315 | * |
| 3316 | * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to |
| 3317 | * decoding, in order to detect if we're dealing with a headers or a trailers |
| 3318 | * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3319 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3320 | 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] | 3321 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3322 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3323 | struct buffer *tmp = get_trash_chunk(); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3324 | struct http_hdr list[MAX_HTTP_HDR * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3325 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3326 | unsigned int msgf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3327 | struct htx *htx = NULL; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3328 | int flen; // header frame len |
| 3329 | int hole = 0; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3330 | int ret = 0; |
| 3331 | int outlen; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3332 | int wrap; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3333 | int try = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3334 | |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3335 | next_frame: |
| 3336 | if (b_data(&h2c->dbuf) - hole < h2c->dfl) |
| 3337 | goto leave; // incomplete input frame |
| 3338 | |
| 3339 | /* No END_HEADERS means there's one or more CONTINUATION frames. In |
| 3340 | * this case, we'll try to paste it immediately after the initial |
| 3341 | * HEADERS frame payload and kill any possible padding. The initial |
| 3342 | * frame's length will be increased to represent the concatenation |
| 3343 | * of the two frames. The next frame is read from position <tlen> |
| 3344 | * and written at position <flen> (minus padding if some is present). |
| 3345 | */ |
| 3346 | if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) { |
| 3347 | struct h2_fh hdr; |
| 3348 | int clen; // CONTINUATION frame's payload length |
| 3349 | |
| 3350 | if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) { |
| 3351 | /* no more data, the buffer may be full, either due to |
| 3352 | * too large a frame or because of too large a hole that |
| 3353 | * we're going to compact at the end. |
| 3354 | */ |
| 3355 | goto leave; |
| 3356 | } |
| 3357 | |
| 3358 | if (hdr.ft != H2_FT_CONTINUATION) { |
| 3359 | /* RFC7540#6.10: frame of unexpected type */ |
| 3360 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3361 | goto fail; |
| 3362 | } |
| 3363 | |
| 3364 | if (hdr.sid != h2c->dsi) { |
| 3365 | /* RFC7540#6.10: frame of different stream */ |
| 3366 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3367 | goto fail; |
| 3368 | } |
| 3369 | |
| 3370 | if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { |
| 3371 | /* RFC7540#4.2: invalid frame length */ |
| 3372 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3373 | goto fail; |
| 3374 | } |
| 3375 | |
| 3376 | /* detect when we must stop aggragating frames */ |
| 3377 | h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS; |
| 3378 | |
| 3379 | /* Take as much as we can of the CONTINUATION frame's payload */ |
| 3380 | clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9); |
| 3381 | if (clen > hdr.len) |
| 3382 | clen = hdr.len; |
| 3383 | |
| 3384 | /* Move the frame's payload over the padding, hole and frame |
| 3385 | * header. At least one of hole or dpl is null (see diagrams |
| 3386 | * above). The hole moves after the new aggragated frame. |
| 3387 | */ |
| 3388 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9)); |
| 3389 | h2c->dfl += clen - h2c->dpl; |
| 3390 | hole += h2c->dpl + 9; |
| 3391 | h2c->dpl = 0; |
| 3392 | goto next_frame; |
| 3393 | } |
| 3394 | |
| 3395 | flen = h2c->dfl - h2c->dpl; |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3396 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3397 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3398 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3399 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3400 | copy = alloc_trash_chunk(); |
| 3401 | if (!copy) { |
| 3402 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3403 | goto fail; |
| 3404 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3405 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3406 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3407 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3408 | } |
| 3409 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3410 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3411 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3412 | if (read_n32(hdrs) == h2c->dsi) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3413 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3414 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3415 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3416 | } |
| 3417 | |
Willy Tarreau | a01f45e | 2018-12-31 07:41:24 +0100 | [diff] [blame] | 3418 | if (flen < 5) { |
| 3419 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3420 | goto fail; |
| 3421 | } |
| 3422 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3423 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3424 | flen -= 5; |
| 3425 | } |
| 3426 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3427 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3428 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3429 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3430 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3431 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3432 | /* we can't retry a failed decompression operation so we must be very |
| 3433 | * careful not to take any risks. In practice the output buffer is |
| 3434 | * always empty except maybe for trailers, in which case we simply have |
| 3435 | * to wait for the upper layer to finish consuming what is available. |
| 3436 | */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3437 | |
| 3438 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3439 | htx = htx_from_buf(rxbuf); |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3440 | if (!htx_is_empty(htx)) { |
| 3441 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3442 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3443 | } |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3444 | } else { |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3445 | if (b_data(rxbuf)) { |
| 3446 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3447 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3448 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3449 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3450 | rxbuf->head = 0; |
| 3451 | try = b_size(rxbuf); |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3452 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3453 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3454 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3455 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3456 | sizeof(list)/sizeof(list[0]), tmp); |
| 3457 | if (outlen < 0) { |
| 3458 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3459 | goto fail; |
| 3460 | } |
| 3461 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3462 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3463 | * the parser's state to commit these changes and allow us to later |
| 3464 | * fail solely on the stream if needed. |
| 3465 | */ |
| 3466 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3467 | h2c->dfl = hole = 0; |
| 3468 | h2c->st0 = H2_CS_FRAME_H; |
| 3469 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3470 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3471 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3472 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3473 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3474 | goto trailers; |
| 3475 | |
| 3476 | /* This is the first HEADERS frame so it's a headers block */ |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3477 | if (htx) { |
| 3478 | /* HTX mode */ |
| 3479 | if (h2c->flags & H2_CF_IS_BACK) |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3480 | outlen = h2_make_htx_response(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3481 | else |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3482 | outlen = h2_make_htx_request(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3483 | } else { |
| 3484 | /* HTTP/1 mode */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3485 | 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] | 3486 | if (outlen > 0) |
| 3487 | b_add(rxbuf, outlen); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3488 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3489 | |
| 3490 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3491 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3492 | goto fail; |
| 3493 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3494 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3495 | if (msgf & H2_MSGF_BODY) { |
| 3496 | /* a payload is present */ |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3497 | if (msgf & H2_MSGF_BODY_CL) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3498 | *flags |= H2_SF_DATA_CLEN; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3499 | if (htx) |
| 3500 | htx->extra = *body_len; |
| 3501 | } |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3502 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3503 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3504 | } |
| 3505 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3506 | done: |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3507 | /* indicate that a HEADERS frame was received for this stream, except |
| 3508 | * for 1xx responses. For 1xx responses, another HEADERS frame is |
| 3509 | * expected. |
| 3510 | */ |
| 3511 | if (!(msgf & H2_MSGF_RSP_1XX)) |
| 3512 | *flags |= H2_SF_HEADERS_RCVD; |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3513 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3514 | 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] | 3515 | /* Mark the end of message, either using EOM in HTX or with the |
| 3516 | * trailing CRLF after the end of trailers. Note that DATA_CHNK |
| 3517 | * is not set during headers with END_STREAM. |
| 3518 | */ |
| 3519 | if (htx) { |
| 3520 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 3521 | goto fail; |
| 3522 | } |
| 3523 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3524 | if (!b_putblk(rxbuf, "\r\n", 2)) |
| 3525 | goto fail; |
| 3526 | } |
| 3527 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3528 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3529 | /* success */ |
| 3530 | ret = 1; |
| 3531 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3532 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3533 | /* 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] | 3534 | * move the remaining data over it. |
| 3535 | */ |
| 3536 | if (hole) { |
| 3537 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3538 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3539 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3540 | b_sub(&h2c->dbuf, hole); |
| 3541 | } |
| 3542 | |
| 3543 | if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) { |
| 3544 | /* too large frames */ |
| 3545 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3546 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3547 | } |
| 3548 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3549 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3550 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3551 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3552 | return ret; |
| 3553 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3554 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3555 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3556 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3557 | |
| 3558 | trailers: |
| 3559 | /* This is the last HEADERS frame hence a trailer */ |
| 3560 | |
| 3561 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3562 | /* It's a trailer but it's missing ES flag */ |
| 3563 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3564 | goto fail; |
| 3565 | } |
| 3566 | |
| 3567 | /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD |
| 3568 | * block, and when using chunks we must send the 0 CRLF marker. For |
| 3569 | * other modes, the trailers are silently dropped. |
| 3570 | */ |
| 3571 | if (htx) { |
| 3572 | if (!htx_add_endof(htx, HTX_BLK_EOD)) |
| 3573 | goto fail; |
Willy Tarreau | 5255f28 | 2019-01-03 18:41:05 +0100 | [diff] [blame] | 3574 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3575 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3576 | } |
| 3577 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3578 | /* Legacy mode with chunked encoding : we must finalize the |
| 3579 | * data block message emit the trailing CRLF */ |
| 3580 | if (!b_putblk(rxbuf, "0\r\n", 3)) |
| 3581 | goto fail; |
Willy Tarreau | e2b05cc | 2019-01-03 16:18:34 +0100 | [diff] [blame] | 3582 | |
| 3583 | outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try); |
| 3584 | if (outlen > 0) |
| 3585 | b_add(rxbuf, outlen); |
| 3586 | else |
| 3587 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3588 | } |
| 3589 | |
| 3590 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3591 | } |
| 3592 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3593 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 3594 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 3595 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 3596 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 3597 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 3598 | * 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] | 3599 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3600 | * send the current frame, 0 if it couldn't complete, in which case |
| 3601 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3602 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3603 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3604 | * have checked the frame header and ensured that the frame was complete or the |
| 3605 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3606 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3607 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3608 | { |
| 3609 | struct h2c *h2c = h2s->h2c; |
| 3610 | int block1, block2; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3611 | unsigned int flen = 0; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3612 | unsigned int chklen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3613 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3614 | struct buffer *csbuf; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3615 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3616 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3617 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3618 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3619 | if (!csbuf) { |
| 3620 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3621 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3622 | } |
| 3623 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3624 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3625 | flen = h2c->dfl - h2c->dpl; |
Olivier Houchard | 2f30883 | 2018-12-19 15:53:53 +0100 | [diff] [blame] | 3626 | if (h2c->proxy->options2 & PR_O2_USE_HTX) |
| 3627 | htx = htx_from_buf(csbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3628 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3629 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3630 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3631 | if (flen > b_data(&h2c->dbuf)) { |
| 3632 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3633 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3634 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3635 | } |
| 3636 | |
Willy Tarreau | a9b7796 | 2019-01-31 07:23:00 +0100 | [diff] [blame] | 3637 | if (htx) { |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3638 | block1 = htx_free_data_space(htx); |
| 3639 | if (!block1) { |
| 3640 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3641 | goto fail; |
| 3642 | } |
| 3643 | if (flen > block1) |
| 3644 | flen = block1; |
| 3645 | |
| 3646 | /* here, flen is the max we can copy into the output buffer */ |
| 3647 | block1 = b_contig_data(&h2c->dbuf, 0); |
| 3648 | if (flen > block1) |
| 3649 | flen = block1; |
| 3650 | |
| 3651 | if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) { |
| 3652 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3653 | goto fail; |
| 3654 | } |
| 3655 | |
| 3656 | b_del(&h2c->dbuf, flen); |
| 3657 | h2c->dfl -= flen; |
| 3658 | h2c->rcvd_c += flen; |
| 3659 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3660 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3661 | if (h2s->flags & H2_SF_DATA_CLEN) { |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3662 | h2s->body_len -= flen; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3663 | htx->extra = h2s->body_len; |
| 3664 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3665 | goto try_again; |
| 3666 | } |
| 3667 | else if (unlikely(b_space_wraps(csbuf))) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3668 | /* it doesn't fit and the buffer is fragmented, |
| 3669 | * so let's defragment it and try again. |
| 3670 | */ |
| 3671 | b_slow_realign(csbuf, trash.area, 0); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3672 | } |
| 3673 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3674 | /* chunked-encoding requires more room */ |
| 3675 | if (h2s->flags & H2_SF_DATA_CHNK) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3676 | chklen = MIN(flen, b_room(csbuf)); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3677 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 3678 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 3679 | (chklen < 1048576) ? 4 : 8; |
| 3680 | chklen += 4; // CRLF, CRLF |
| 3681 | } |
| 3682 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3683 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3684 | if (flen + chklen > b_room(csbuf)) { |
| 3685 | if (chklen >= b_room(csbuf)) { |
| 3686 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3687 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3688 | } |
| 3689 | flen = b_room(csbuf) - chklen; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3693 | /* emit the chunk size */ |
| 3694 | unsigned int chksz = flen; |
| 3695 | char str[10]; |
| 3696 | char *beg; |
| 3697 | |
| 3698 | beg = str + sizeof(str); |
| 3699 | *--beg = '\n'; |
| 3700 | *--beg = '\r'; |
| 3701 | do { |
| 3702 | *--beg = hextab[chksz & 0xF]; |
| 3703 | } while (chksz >>= 4); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3704 | b_putblk(csbuf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3705 | } |
| 3706 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3707 | /* Block1 is the length of the first block before the buffer wraps, |
| 3708 | * block2 is the optional second block to reach the end of the frame. |
| 3709 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3710 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3711 | if (block1 > flen) |
| 3712 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3713 | block2 = flen - block1; |
| 3714 | |
| 3715 | if (block1) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3716 | b_putblk(csbuf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3717 | |
| 3718 | if (block2) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3719 | b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3720 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3721 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3722 | /* emit the CRLF */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3723 | b_putblk(csbuf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3724 | } |
| 3725 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3726 | /* now mark the input data as consumed (will be deleted from the buffer |
| 3727 | * by the caller when seeing FRAME_A after sending the window update). |
| 3728 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3729 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3730 | h2c->dfl -= flen; |
| 3731 | h2c->rcvd_c += flen; |
| 3732 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 3733 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3734 | if (h2s->flags & H2_SF_DATA_CLEN) |
| 3735 | h2s->body_len -= flen; |
| 3736 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3737 | if (h2c->dfl > h2c->dpl) { |
| 3738 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3739 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3740 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3741 | } |
| 3742 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3743 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3744 | /* here we're done with the frame, all the payload (except padding) was |
| 3745 | * transferred. |
| 3746 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3747 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3748 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 3749 | if (htx) { |
| 3750 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3751 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3752 | goto fail; |
| 3753 | } |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3754 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3755 | else if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3756 | /* emit the trailing 0 CRLF CRLF */ |
| 3757 | if (b_room(csbuf) < 5) { |
| 3758 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3759 | goto fail; |
| 3760 | } |
| 3761 | chklen += 5; |
| 3762 | b_putblk(csbuf, "0\r\n\r\n", 5); |
| 3763 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3764 | } |
| 3765 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 3766 | h2c->rcvd_c += h2c->dpl; |
| 3767 | h2c->rcvd_s += h2c->dpl; |
| 3768 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3769 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3770 | if (htx) |
| 3771 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3772 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3773 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3774 | if (htx) |
| 3775 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3776 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3777 | } |
| 3778 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3779 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 3780 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 3781 | * number of bytes sent. The caller must check the stream's status to detect |
| 3782 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3783 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3784 | 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] | 3785 | { |
| 3786 | struct http_hdr list[MAX_HTTP_HDR]; |
| 3787 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3788 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3789 | struct buffer outbuf; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3790 | union h1_sl sl; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3791 | int es_now = 0; |
| 3792 | int ret = 0; |
| 3793 | int hdr; |
| 3794 | |
| 3795 | if (h2c_mux_busy(h2c, h2s)) { |
| 3796 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3797 | return 0; |
| 3798 | } |
| 3799 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3800 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3801 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3802 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3803 | return 0; |
| 3804 | } |
| 3805 | |
| 3806 | /* First, try to parse the H1 response and index it into <list>. |
| 3807 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 3808 | * block does not wrap and we can safely read it this way without |
| 3809 | * having to realign the buffer. |
| 3810 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3811 | 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] | 3812 | list, sizeof(list)/sizeof(list[0]), h1m, &sl); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3813 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 3814 | /* incomplete or invalid response, this is abnormal coming from |
| 3815 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 3816 | * so that won't be fixed, raise an error now. |
| 3817 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3818 | * FIXME: we should instead add the ability to only return a |
| 3819 | * 502 bad gateway. But in theory this is not supposed to |
| 3820 | * happen. |
| 3821 | */ |
| 3822 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3823 | ret = 0; |
| 3824 | goto end; |
| 3825 | } |
| 3826 | |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3827 | h2s->status = sl.st.status; |
Willy Tarreau | db72da0 | 2018-09-13 11:52:20 +0200 | [diff] [blame] | 3828 | |
| 3829 | /* certain statuses have no body or an empty one, regardless of |
| 3830 | * what the headers say. |
| 3831 | */ |
| 3832 | if (sl.st.status >= 100 && sl.st.status < 200) { |
| 3833 | h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK); |
| 3834 | h1m->curr_len = h1m->body_len = 0; |
| 3835 | } |
| 3836 | else if (sl.st.status == 204 || sl.st.status == 304) { |
| 3837 | /* no contents, claim c-len is present and set to zero */ |
| 3838 | h1m->flags &= ~H1_MF_CHNK; |
| 3839 | h1m->flags |= H1_MF_CLEN; |
| 3840 | h1m->curr_len = h1m->body_len = 0; |
| 3841 | } |
| 3842 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3843 | chunk_reset(&outbuf); |
| 3844 | |
| 3845 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3846 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3847 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3848 | outbuf.data = 0; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3849 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3850 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3851 | break; |
| 3852 | realign_again: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3853 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3854 | } |
| 3855 | |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3856 | if (outbuf.size < 9) |
| 3857 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3858 | |
| 3859 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3860 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 3861 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3862 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3863 | |
| 3864 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3865 | if (unlikely(list[0].v.len != 3)) { |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 3866 | /* this is an unparsable response */ |
| 3867 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3868 | ret = 0; |
| 3869 | goto end; |
| 3870 | } |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3871 | |
| 3872 | if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3873 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3874 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3875 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3876 | } |
| 3877 | |
| 3878 | /* encode all headers, stop at empty name */ |
| 3879 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 3880 | /* these ones do not exist in H2 and must be dropped. */ |
| 3881 | if (isteq(list[hdr].n, ist("connection")) || |
| 3882 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 3883 | isteq(list[hdr].n, ist("keep-alive")) || |
| 3884 | isteq(list[hdr].n, ist("upgrade")) || |
| 3885 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3886 | continue; |
| 3887 | |
| 3888 | if (isteq(list[hdr].n, ist(""))) |
| 3889 | break; // end |
| 3890 | |
| 3891 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 3892 | /* output full */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3893 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3894 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3895 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3896 | } |
| 3897 | } |
| 3898 | |
| 3899 | /* we may need to add END_STREAM */ |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 3900 | 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] | 3901 | es_now = 1; |
| 3902 | |
| 3903 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3904 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3905 | |
| 3906 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3907 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3908 | |
| 3909 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3910 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3911 | |
| 3912 | /* commit the H2 response */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3913 | b_add(&h2c->mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3914 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3915 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3916 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3917 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3918 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3919 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3920 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3921 | h2s->flags |= H2_SF_ES_SENT; |
| 3922 | if (h2s->st == H2_SS_OPEN) |
| 3923 | h2s->st = H2_SS_HLOC; |
| 3924 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3925 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3926 | } |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3927 | else if (h2s->status >= 100 && h2s->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3928 | /* 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] | 3929 | h1m_init_res(h1m); |
Willy Tarreau | 9b8cd1f | 2018-09-12 09:24:38 +0200 | [diff] [blame] | 3930 | 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] | 3931 | h2s->h1m.flags |= H1_MF_TOLOWER; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3932 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 3933 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 3934 | |
| 3935 | /* 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] | 3936 | |
| 3937 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 3938 | //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] | 3939 | return ret; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3940 | full: |
| 3941 | h1m_init_res(h1m); |
| 3942 | h1m->err_pos = -1; // don't care about errors on the response path |
| 3943 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3944 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3945 | ret = 0; |
| 3946 | goto end; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3947 | } |
| 3948 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3949 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 3950 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 3951 | * the number of bytes sent. The caller must check the stream's status to |
| 3952 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3953 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3954 | 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] | 3955 | { |
| 3956 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3957 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3958 | struct buffer outbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3959 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 3960 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3961 | int es_now = 0; |
| 3962 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3963 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3964 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3965 | |
| 3966 | if (h2c_mux_busy(h2c, h2s)) { |
| 3967 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3968 | goto end; |
| 3969 | } |
| 3970 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3971 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3972 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3973 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3974 | goto end; |
| 3975 | } |
| 3976 | |
| 3977 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3978 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3979 | goto end; |
| 3980 | |
| 3981 | chunk_reset(&outbuf); |
| 3982 | |
| 3983 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3984 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3985 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3986 | outbuf.data = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3987 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3988 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3989 | break; |
| 3990 | realign_again: |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 3991 | /* If there are pending data in the output buffer, and we have |
| 3992 | * less than 1/4 of the mbuf's size and everything fits, we'll |
| 3993 | * still perform a copy anyway. Otherwise we'll pretend the mbuf |
| 3994 | * is full and wait, to save some slow realign calls. |
| 3995 | */ |
| 3996 | if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) { |
| 3997 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3998 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3999 | goto end; |
| 4000 | } |
| 4001 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4002 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4003 | } |
| 4004 | |
| 4005 | if (outbuf.size < 9) { |
| 4006 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4007 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4008 | goto end; |
| 4009 | } |
| 4010 | |
| 4011 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4012 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4013 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4014 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4015 | |
| 4016 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 4017 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4018 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 4019 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4020 | break; |
| 4021 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4022 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4023 | if ((long long)size > h1m->curr_len) |
| 4024 | size = h1m->curr_len; |
| 4025 | break; |
| 4026 | default: /* te:chunked : parse chunks */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4027 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 4028 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4029 | if (!ret) |
| 4030 | goto end; |
| 4031 | |
| 4032 | if (ret < 0) { |
| 4033 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4034 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4035 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4036 | goto end; |
| 4037 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4038 | max -= ret; |
| 4039 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4040 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4041 | h1m->state = H1_MSG_CHUNK_SIZE; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4042 | } |
| 4043 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4044 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4045 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 4046 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4047 | if (!ret) |
| 4048 | goto end; |
| 4049 | |
| 4050 | if (ret < 0) { |
| 4051 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4052 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4053 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4054 | goto end; |
| 4055 | } |
| 4056 | |
| 4057 | size = chunk; |
| 4058 | h1m->curr_len = chunk; |
| 4059 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4060 | max -= ret; |
| 4061 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4062 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4063 | h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4064 | if (!size) |
| 4065 | goto send_empty; |
| 4066 | } |
| 4067 | |
| 4068 | /* in MSG_DATA state, continue below */ |
| 4069 | size = h1m->curr_len; |
| 4070 | break; |
| 4071 | } |
| 4072 | |
| 4073 | /* we have in <size> the exact number of bytes we need to copy from |
| 4074 | * the H1 buffer. We need to check this against the connection's and |
| 4075 | * the stream's send windows, and to ensure that this fits in the max |
| 4076 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4077 | * the frame header). The connection's flow control is applied last so |
| 4078 | * that we can use a separate list of streams which are immediately |
| 4079 | * unblocked on window opening. Note: we don't implement padding. |
| 4080 | */ |
| 4081 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4082 | if (size > max) |
| 4083 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4084 | |
| 4085 | if (size > h2s->mws) |
| 4086 | size = h2s->mws; |
| 4087 | |
| 4088 | if (size <= 0) { |
| 4089 | h2s->flags |= H2_SF_BLK_SFCTL; |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 4090 | if (h2s->send_wait) { |
| 4091 | LIST_DEL(&h2s->list); |
| 4092 | LIST_INIT(&h2s->list); |
| 4093 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4094 | goto end; |
| 4095 | } |
| 4096 | |
| 4097 | if (h2c->mfs && size > h2c->mfs) |
| 4098 | size = h2c->mfs; |
| 4099 | |
| 4100 | if (size + 9 > outbuf.size) { |
| 4101 | /* we have an opportunity for enlarging the too small |
| 4102 | * available space, let's try. |
| 4103 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4104 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4105 | goto realign_again; |
| 4106 | size = outbuf.size - 9; |
| 4107 | } |
| 4108 | |
| 4109 | if (size <= 0) { |
| 4110 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4111 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4112 | goto end; |
| 4113 | } |
| 4114 | |
| 4115 | if (size > h2c->mws) |
| 4116 | size = h2c->mws; |
| 4117 | |
| 4118 | if (size <= 0) { |
| 4119 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4120 | goto end; |
| 4121 | } |
| 4122 | |
| 4123 | /* copy whatever we can */ |
| 4124 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4125 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4126 | if (ret == 1) |
| 4127 | len2 = 0; |
| 4128 | |
| 4129 | if (!ret || len1 + len2 < size) { |
| 4130 | /* FIXME: must normally never happen */ |
| 4131 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4132 | goto end; |
| 4133 | } |
| 4134 | |
| 4135 | /* limit len1/len2 to size */ |
| 4136 | if (len1 + len2 > size) { |
| 4137 | int sub = len1 + len2 - size; |
| 4138 | |
| 4139 | if (len2 > sub) |
| 4140 | len2 -= sub; |
| 4141 | else { |
| 4142 | sub -= len2; |
| 4143 | len2 = 0; |
| 4144 | len1 -= sub; |
| 4145 | } |
| 4146 | } |
| 4147 | |
| 4148 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4149 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4150 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4151 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4152 | |
| 4153 | send_empty: |
| 4154 | /* we may need to add END_STREAM */ |
| 4155 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 4156 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4157 | * |
| 4158 | * FIXME: what we do here is not correct because we send end_stream |
| 4159 | * before knowing if we'll have to send a HEADERS frame for the |
| 4160 | * trailers. More importantly we're not consuming the trailing CRLF |
| 4161 | * after the end of trailers, so it will be left to the caller to |
| 4162 | * eat it. The right way to do it would be to measure trailers here |
| 4163 | * and to send ES only if there are no trailers. |
| 4164 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4165 | */ |
| 4166 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4167 | !h1m->curr_len || h1m->state >= H1_MSG_DONE) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4168 | es_now = 1; |
| 4169 | |
| 4170 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4171 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4172 | |
| 4173 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4174 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4175 | |
| 4176 | /* commit the H2 response */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4177 | b_add(&h2c->mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4178 | |
| 4179 | /* consume incoming H1 response */ |
| 4180 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4181 | max -= size; |
| 4182 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4183 | total += size; |
| 4184 | h1m->curr_len -= size; |
| 4185 | h2s->mws -= size; |
| 4186 | h2c->mws -= size; |
| 4187 | |
| 4188 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4189 | h1m->state = H1_MSG_CHUNK_CRLF; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4190 | goto new_frame; |
| 4191 | } |
| 4192 | } |
| 4193 | |
| 4194 | if (es_now) { |
| 4195 | if (h2s->st == H2_SS_OPEN) |
| 4196 | h2s->st = H2_SS_HLOC; |
| 4197 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4198 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4199 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4200 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 4201 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4202 | total += max; |
| 4203 | ofs += max; |
| 4204 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4205 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4206 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4207 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4208 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4209 | h2s->flags |= H2_SF_ES_SENT; |
| 4210 | } |
| 4211 | |
| 4212 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4213 | 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] | 4214 | return total; |
| 4215 | } |
| 4216 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4217 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 4218 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4219 | * must check the stream's status to detect any error which might have happened |
| 4220 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4221 | * from the message. The htx message is assumed to be valid since produced from |
| 4222 | * the internal code, hence it contains a start line, an optional series of |
| 4223 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4224 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4225 | */ |
| 4226 | static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
| 4227 | { |
| 4228 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4229 | struct h2c *h2c = h2s->h2c; |
| 4230 | struct htx_blk *blk; |
| 4231 | struct htx_blk *blk_end; |
| 4232 | struct buffer outbuf; |
| 4233 | struct htx_sl *sl; |
| 4234 | enum htx_blk_type type; |
| 4235 | int es_now = 0; |
| 4236 | int ret = 0; |
| 4237 | int hdr; |
| 4238 | int idx; |
| 4239 | |
| 4240 | if (h2c_mux_busy(h2c, h2s)) { |
| 4241 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4242 | return 0; |
| 4243 | } |
| 4244 | |
| 4245 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4246 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4247 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4248 | return 0; |
| 4249 | } |
| 4250 | |
| 4251 | /* determine the first block which must not be deleted, blk_end may |
| 4252 | * be NULL if all blocks have to be deleted. |
| 4253 | */ |
| 4254 | idx = htx_get_head(htx); |
| 4255 | blk_end = NULL; |
| 4256 | while (idx != -1) { |
| 4257 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4258 | idx = htx_get_next(htx, idx); |
| 4259 | if (type == HTX_BLK_EOH) { |
| 4260 | if (idx != -1) |
| 4261 | blk_end = htx_get_blk(htx, idx); |
| 4262 | break; |
| 4263 | } |
| 4264 | } |
| 4265 | |
| 4266 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4267 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4268 | ALREADY_CHECKED(sl); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4269 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4270 | if (h2s->status < 100 || h2s->status > 999) |
| 4271 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4272 | |
| 4273 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4274 | hdr = 0; |
| 4275 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4276 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4277 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4278 | blk = htx_get_blk(htx, idx); |
| 4279 | type = htx_get_blk_type(blk); |
| 4280 | |
| 4281 | if (type == HTX_BLK_UNUSED) |
| 4282 | continue; |
| 4283 | |
| 4284 | if (type != HTX_BLK_HDR) |
| 4285 | break; |
| 4286 | |
| 4287 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4288 | goto fail; |
| 4289 | |
| 4290 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4291 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4292 | hdr++; |
| 4293 | } |
| 4294 | |
| 4295 | /* marker for end of headers */ |
| 4296 | list[hdr].n = ist(""); |
| 4297 | |
| 4298 | if (h2s->status == 204 || h2s->status == 304) { |
| 4299 | /* no contents, claim c-len is present and set to zero */ |
| 4300 | es_now = 1; |
| 4301 | } |
| 4302 | |
| 4303 | chunk_reset(&outbuf); |
| 4304 | |
| 4305 | while (1) { |
| 4306 | outbuf.area = b_tail(&h2c->mbuf); |
| 4307 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4308 | outbuf.data = 0; |
| 4309 | |
| 4310 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4311 | break; |
| 4312 | realign_again: |
| 4313 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4314 | } |
| 4315 | |
| 4316 | if (outbuf.size < 9) |
| 4317 | goto full; |
| 4318 | |
| 4319 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4320 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4321 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4322 | outbuf.data = 9; |
| 4323 | |
| 4324 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4325 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4326 | if (b_space_wraps(&h2c->mbuf)) |
| 4327 | goto realign_again; |
| 4328 | goto full; |
| 4329 | } |
| 4330 | |
| 4331 | /* encode all headers, stop at empty name */ |
| 4332 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4333 | /* these ones do not exist in H2 and must be dropped. */ |
| 4334 | if (isteq(list[hdr].n, ist("connection")) || |
| 4335 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4336 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4337 | isteq(list[hdr].n, ist("upgrade")) || |
| 4338 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4339 | continue; |
| 4340 | |
| 4341 | if (isteq(list[hdr].n, ist(""))) |
| 4342 | break; // end |
| 4343 | |
| 4344 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4345 | /* output full */ |
| 4346 | if (b_space_wraps(&h2c->mbuf)) |
| 4347 | goto realign_again; |
| 4348 | goto full; |
| 4349 | } |
| 4350 | } |
| 4351 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4352 | /* we may need to add END_STREAM except for 1xx responses. |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4353 | * FIXME: we should also set it when we know for sure that the |
| 4354 | * content-length is zero as well as on 204/304 |
| 4355 | */ |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4356 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM && |
| 4357 | (h2s->status >= 200 || h2s->status == 101)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4358 | es_now = 1; |
| 4359 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4360 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4361 | es_now = 1; |
| 4362 | |
| 4363 | /* update the frame's size */ |
| 4364 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4365 | |
| 4366 | if (es_now) |
| 4367 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4368 | |
| 4369 | /* commit the H2 response */ |
| 4370 | b_add(&h2c->mbuf, outbuf.data); |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4371 | |
| 4372 | /* indicates the HEADERS frame was sent, except for 1xx responses. For |
| 4373 | * 1xx responses, another HEADERS frame is expected. |
| 4374 | */ |
| 4375 | if (h2s->status >= 200 || h2s->status == 101) |
| 4376 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4377 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4378 | if (es_now) { |
| 4379 | h2s->flags |= H2_SF_ES_SENT; |
| 4380 | if (h2s->st == H2_SS_OPEN) |
| 4381 | h2s->st = H2_SS_HLOC; |
| 4382 | else |
| 4383 | h2s_close(h2s); |
| 4384 | } |
| 4385 | |
| 4386 | /* OK we could properly deliver the response */ |
| 4387 | |
| 4388 | /* remove all header blocks including the EOH and compute the |
| 4389 | * corresponding size. |
| 4390 | * |
| 4391 | * FIXME: We should remove everything when es_now is set. |
| 4392 | */ |
| 4393 | ret = 0; |
| 4394 | idx = htx_get_head(htx); |
| 4395 | blk = htx_get_blk(htx, idx); |
| 4396 | while (blk != blk_end) { |
| 4397 | ret += htx_get_blksz(blk); |
| 4398 | blk = htx_remove_blk(htx, blk); |
| 4399 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4400 | |
| 4401 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4402 | htx_remove_blk(htx, blk_end); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4403 | end: |
| 4404 | return ret; |
| 4405 | full: |
| 4406 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4407 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4408 | ret = 0; |
| 4409 | goto end; |
| 4410 | fail: |
| 4411 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4412 | * list etc go here (unrecoverable errors). |
| 4413 | */ |
| 4414 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4415 | ret = 0; |
| 4416 | goto end; |
| 4417 | } |
| 4418 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4419 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4420 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4421 | * must check the stream's status to detect any error which might have happened |
| 4422 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4423 | * from the message. The htx message is assumed to be valid since produced from |
| 4424 | * the internal code, hence it contains a start line, an optional series of |
| 4425 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4426 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4427 | */ |
| 4428 | static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
| 4429 | { |
| 4430 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4431 | struct h2c *h2c = h2s->h2c; |
| 4432 | struct htx_blk *blk; |
| 4433 | struct htx_blk *blk_end; |
| 4434 | struct buffer outbuf; |
| 4435 | struct htx_sl *sl; |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4436 | struct ist meth, path, auth; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4437 | enum htx_blk_type type; |
| 4438 | int es_now = 0; |
| 4439 | int ret = 0; |
| 4440 | int hdr; |
| 4441 | int idx; |
| 4442 | |
| 4443 | if (h2c_mux_busy(h2c, h2s)) { |
| 4444 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4445 | return 0; |
| 4446 | } |
| 4447 | |
| 4448 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4449 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4450 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4451 | return 0; |
| 4452 | } |
| 4453 | |
| 4454 | /* determine the first block which must not be deleted, blk_end may |
| 4455 | * be NULL if all blocks have to be deleted. |
| 4456 | */ |
| 4457 | idx = htx_get_head(htx); |
| 4458 | blk_end = NULL; |
| 4459 | while (idx != -1) { |
| 4460 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4461 | idx = htx_get_next(htx, idx); |
| 4462 | if (type == HTX_BLK_EOH) { |
| 4463 | if (idx != -1) |
| 4464 | blk_end = htx_get_blk(htx, idx); |
| 4465 | break; |
| 4466 | } |
| 4467 | } |
| 4468 | |
| 4469 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4470 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4471 | ALREADY_CHECKED(sl); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4472 | meth = htx_sl_req_meth(sl); |
| 4473 | path = htx_sl_req_uri(sl); |
| 4474 | |
| 4475 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4476 | hdr = 0; |
| 4477 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4478 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4479 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4480 | blk = htx_get_blk(htx, idx); |
| 4481 | type = htx_get_blk_type(blk); |
| 4482 | |
| 4483 | if (type == HTX_BLK_UNUSED) |
| 4484 | continue; |
| 4485 | |
| 4486 | if (type != HTX_BLK_HDR) |
| 4487 | break; |
| 4488 | |
| 4489 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4490 | goto fail; |
| 4491 | |
| 4492 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4493 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4494 | hdr++; |
| 4495 | } |
| 4496 | |
| 4497 | /* marker for end of headers */ |
| 4498 | list[hdr].n = ist(""); |
| 4499 | |
| 4500 | chunk_reset(&outbuf); |
| 4501 | |
| 4502 | while (1) { |
| 4503 | outbuf.area = b_tail(&h2c->mbuf); |
| 4504 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4505 | outbuf.data = 0; |
| 4506 | |
| 4507 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4508 | break; |
| 4509 | realign_again: |
| 4510 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4511 | } |
| 4512 | |
| 4513 | if (outbuf.size < 9) |
| 4514 | goto full; |
| 4515 | |
| 4516 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4517 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4518 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4519 | outbuf.data = 9; |
| 4520 | |
| 4521 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4522 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4523 | if (b_space_wraps(&h2c->mbuf)) |
| 4524 | goto realign_again; |
| 4525 | goto full; |
| 4526 | } |
| 4527 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4528 | /* RFC7540 #8.3: the CONNECT method must have : |
| 4529 | * - :authority set to the URI part (host:port) |
| 4530 | * - :method set to CONNECT |
| 4531 | * - :scheme and :path omitted |
| 4532 | */ |
| 4533 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 4534 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
| 4535 | if (!hpack_encode_scheme(&outbuf, ist("https"))) { |
| 4536 | /* output full */ |
| 4537 | if (b_space_wraps(&h2c->mbuf)) |
| 4538 | goto realign_again; |
| 4539 | goto full; |
| 4540 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4541 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4542 | /* encode the path, which necessarily is the second one */ |
| 4543 | if (!hpack_encode_path(&outbuf, path)) { |
| 4544 | /* output full */ |
| 4545 | if (b_space_wraps(&h2c->mbuf)) |
| 4546 | goto realign_again; |
| 4547 | goto full; |
| 4548 | } |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4549 | |
| 4550 | /* look for the Host header and place it in :authority */ |
| 4551 | auth = ist2(NULL, 0); |
| 4552 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4553 | if (isteq(list[hdr].n, ist(""))) |
| 4554 | break; // end |
| 4555 | |
| 4556 | if (isteq(list[hdr].n, ist("host"))) { |
| 4557 | auth = list[hdr].v; |
| 4558 | break; |
| 4559 | } |
| 4560 | } |
| 4561 | } |
| 4562 | else { |
| 4563 | /* for CONNECT, :authority is taken from the path */ |
| 4564 | auth = path; |
| 4565 | } |
| 4566 | |
| 4567 | if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) { |
| 4568 | /* output full */ |
| 4569 | if (b_space_wraps(&h2c->mbuf)) |
| 4570 | goto realign_again; |
| 4571 | goto full; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4572 | } |
| 4573 | |
| 4574 | /* encode all headers, stop at empty name */ |
| 4575 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4576 | /* these ones do not exist in H2 and must be dropped. */ |
| 4577 | if (isteq(list[hdr].n, ist("connection")) || |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4578 | isteq(list[hdr].n, ist("host")) || |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4579 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4580 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4581 | isteq(list[hdr].n, ist("upgrade")) || |
| 4582 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4583 | continue; |
| 4584 | |
| 4585 | if (isteq(list[hdr].n, ist(""))) |
| 4586 | break; // end |
| 4587 | |
| 4588 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4589 | /* output full */ |
| 4590 | if (b_space_wraps(&h2c->mbuf)) |
| 4591 | goto realign_again; |
| 4592 | goto full; |
| 4593 | } |
| 4594 | } |
| 4595 | |
| 4596 | /* we may need to add END_STREAM if we have no body : |
| 4597 | * - request already closed, or : |
| 4598 | * - no transfer-encoding, and : |
| 4599 | * - no content-length or content-length:0 |
| 4600 | * Fixme: this doesn't take into account CONNECT requests. |
| 4601 | */ |
| 4602 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4603 | es_now = 1; |
| 4604 | |
| 4605 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4606 | es_now = 1; |
| 4607 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4608 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4609 | es_now = 1; |
| 4610 | |
| 4611 | /* update the frame's size */ |
| 4612 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4613 | |
| 4614 | if (es_now) |
| 4615 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4616 | |
| 4617 | /* commit the H2 response */ |
| 4618 | b_add(&h2c->mbuf, outbuf.data); |
| 4619 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4620 | h2s->st = H2_SS_OPEN; |
| 4621 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4622 | if (es_now) { |
| 4623 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4624 | h2s->flags |= H2_SF_ES_SENT; |
| 4625 | h2s->st = H2_SS_HLOC; |
| 4626 | } |
| 4627 | |
| 4628 | /* remove all header blocks including the EOH and compute the |
| 4629 | * corresponding size. |
| 4630 | * |
| 4631 | * FIXME: We should remove everything when es_now is set. |
| 4632 | */ |
| 4633 | ret = 0; |
| 4634 | idx = htx_get_head(htx); |
| 4635 | blk = htx_get_blk(htx, idx); |
| 4636 | while (blk != blk_end) { |
| 4637 | ret += htx_get_blksz(blk); |
| 4638 | blk = htx_remove_blk(htx, blk); |
| 4639 | } |
| 4640 | |
| 4641 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4642 | htx_remove_blk(htx, blk_end); |
| 4643 | |
| 4644 | end: |
| 4645 | return ret; |
| 4646 | full: |
| 4647 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4648 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4649 | ret = 0; |
| 4650 | goto end; |
| 4651 | fail: |
| 4652 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4653 | * list etc go here (unrecoverable errors). |
| 4654 | */ |
| 4655 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4656 | ret = 0; |
| 4657 | goto end; |
| 4658 | } |
| 4659 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4660 | /* 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] | 4661 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 4662 | * caller must check the stream's status to detect any error which might have |
| 4663 | * happened subsequently to a successful send. Returns the number of data bytes |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4664 | * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte. |
| 4665 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4666 | 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] | 4667 | { |
| 4668 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4669 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4670 | struct buffer outbuf; |
| 4671 | size_t total = 0; |
| 4672 | int es_now = 0; |
| 4673 | int bsize; /* htx block size */ |
| 4674 | int fsize; /* h2 frame size */ |
| 4675 | struct htx_blk *blk; |
| 4676 | enum htx_blk_type type; |
| 4677 | int idx; |
| 4678 | |
| 4679 | if (h2c_mux_busy(h2c, h2s)) { |
| 4680 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4681 | goto end; |
| 4682 | } |
| 4683 | |
| 4684 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4685 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4686 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4687 | goto end; |
| 4688 | } |
| 4689 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4690 | htx = htx_from_buf(buf); |
| 4691 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4692 | /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However, |
| 4693 | * while looping, we can meet an HTX_BLK_EOM block that we'll leave to |
| 4694 | * the caller to handle. |
| 4695 | */ |
| 4696 | |
| 4697 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4698 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4699 | goto end; |
| 4700 | |
| 4701 | idx = htx_get_head(htx); |
| 4702 | blk = htx_get_blk(htx, idx); |
| 4703 | type = htx_get_blk_type(blk); // DATA or EOD or EOM |
| 4704 | bsize = htx_get_blksz(blk); |
| 4705 | fsize = bsize; |
| 4706 | |
| 4707 | if (type == HTX_BLK_EOD) { |
| 4708 | /* if we have an EOD, we're dealing with chunked data. We may |
| 4709 | * have a set of trailers after us that the caller will want to |
| 4710 | * deal with. Let's simply remove the EOD and return. |
| 4711 | */ |
| 4712 | htx_remove_blk(htx, blk); |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4713 | total++; // EOD counts as one byte |
| 4714 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4715 | goto end; |
| 4716 | } |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 4717 | else if (type == HTX_BLK_EOM) { |
| 4718 | if (h2s->flags & H2_SF_ES_SENT) { |
| 4719 | /* ES already sent */ |
| 4720 | htx_remove_blk(htx, blk); |
| 4721 | total++; // EOM counts as one byte |
| 4722 | count--; |
| 4723 | goto end; |
| 4724 | } |
| 4725 | } |
| 4726 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4727 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4728 | |
| 4729 | /* Perform some optimizations to reduce the number of buffer copies. |
| 4730 | * First, if the mux's buffer is empty and the htx area contains |
| 4731 | * exactly one data block of the same size as the requested count, and |
| 4732 | * this count fits within the frame size, the stream's window size, and |
| 4733 | * the connection's window size, then it's possible to simply swap the |
| 4734 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 4735 | * length to match the entire DATA HTX block in the middle. In this |
| 4736 | * case we perform a true zero-copy operation from end-to-end. This is |
| 4737 | * the situation that happens all the time with large files. Second, if |
| 4738 | * this is not possible, but the mux's output buffer is empty, we still |
| 4739 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 4740 | * making the intermediary buffer's area point to the output buffer's |
| 4741 | * area. In this case we want to skip the HTX header to make sure that |
| 4742 | * copies remain aligned and that this operation remains possible all |
| 4743 | * the time. This goes for headers, data blocks and any data extracted |
| 4744 | * from the HTX blocks. |
| 4745 | */ |
| 4746 | if (unlikely(fsize == count && |
| 4747 | htx->used == 1 && type == HTX_BLK_DATA && |
| 4748 | fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
| 4749 | void *old_area = h2c->mbuf.area; |
| 4750 | |
| 4751 | if (b_data(&h2c->mbuf)) { |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4752 | /* Too bad there are data left there. We're willing to memcpy/memmove |
| 4753 | * up to 1/4 of the buffer, which means that it's OK to copy a large |
| 4754 | * frame into a buffer containing few data if it needs to be realigned, |
| 4755 | * and that it's also OK to copy few data without realigning. Otherwise |
| 4756 | * 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] | 4757 | */ |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4758 | if (fsize + 9 <= b_room(&h2c->mbuf) && |
| 4759 | (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 || |
| 4760 | (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] | 4761 | goto copy; |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4762 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4763 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4764 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4765 | goto end; |
| 4766 | } |
| 4767 | |
| 4768 | /* map an H2 frame to the HTX block so that we can put the |
| 4769 | * frame header there. |
| 4770 | */ |
| 4771 | h2c->mbuf.area = buf->area; |
Olivier Houchard | 84cca66 | 2018-12-14 16:28:08 +0100 | [diff] [blame] | 4772 | h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4773 | h2c->mbuf.data = fsize + 9; |
| 4774 | outbuf.area = b_head(&h2c->mbuf); |
| 4775 | |
| 4776 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 4777 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4778 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4779 | h2_set_frame_size(outbuf.area, fsize); |
| 4780 | |
| 4781 | /* update windows */ |
| 4782 | h2s->mws -= fsize; |
| 4783 | h2c->mws -= fsize; |
| 4784 | |
| 4785 | /* and exchange with our old area */ |
| 4786 | buf->area = old_area; |
| 4787 | buf->data = buf->head = 0; |
| 4788 | total += fsize; |
| 4789 | goto end; |
| 4790 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4791 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4792 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4793 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 4794 | |
| 4795 | while (1) { |
| 4796 | outbuf.area = b_tail(&h2c->mbuf); |
| 4797 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4798 | outbuf.data = 0; |
| 4799 | |
| 4800 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4801 | break; |
| 4802 | realign_again: |
| 4803 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4804 | } |
| 4805 | |
| 4806 | if (outbuf.size < 9) { |
| 4807 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4808 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4809 | goto end; |
| 4810 | } |
| 4811 | |
| 4812 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 4813 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4814 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4815 | outbuf.data = 9; |
| 4816 | |
| 4817 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 4818 | * the HTX buffer. We need to check this against the connection's and |
| 4819 | * the stream's send windows, and to ensure that this fits in the max |
| 4820 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4821 | * the frame header). The connection's flow control is applied last so |
| 4822 | * that we can use a separate list of streams which are immediately |
| 4823 | * unblocked on window opening. Note: we don't implement padding. |
| 4824 | */ |
| 4825 | |
| 4826 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 4827 | * empty frame, thus we force it to zero here. |
| 4828 | */ |
| 4829 | if (type == HTX_BLK_EOM) |
| 4830 | bsize = fsize = 0; |
| 4831 | |
| 4832 | if (!fsize) |
| 4833 | goto send_empty; |
| 4834 | |
| 4835 | if (h2s->mws <= 0) { |
| 4836 | h2s->flags |= H2_SF_BLK_SFCTL; |
| 4837 | if (h2s->send_wait) { |
| 4838 | LIST_DEL(&h2s->list); |
| 4839 | LIST_INIT(&h2s->list); |
| 4840 | } |
| 4841 | goto end; |
| 4842 | } |
| 4843 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4844 | if (fsize > count) |
| 4845 | fsize = count; |
| 4846 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4847 | if (fsize > h2s->mws) |
| 4848 | fsize = h2s->mws; // >0 |
| 4849 | |
| 4850 | if (h2c->mfs && fsize > h2c->mfs) |
| 4851 | fsize = h2c->mfs; // >0 |
| 4852 | |
| 4853 | if (fsize + 9 > outbuf.size) { |
| 4854 | /* we have an opportunity for enlarging the too small |
| 4855 | * available space, let's try. |
| 4856 | * FIXME: is this really interesting to do? Maybe we'll |
| 4857 | * spend lots of time realigning instead of using two |
| 4858 | * frames. |
| 4859 | */ |
| 4860 | if (b_space_wraps(&h2c->mbuf)) |
| 4861 | goto realign_again; |
| 4862 | fsize = outbuf.size - 9; |
| 4863 | |
| 4864 | if (fsize <= 0) { |
| 4865 | /* no need to send an empty frame here */ |
| 4866 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4867 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4868 | goto end; |
| 4869 | } |
| 4870 | } |
| 4871 | |
| 4872 | if (h2c->mws <= 0) { |
| 4873 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4874 | goto end; |
| 4875 | } |
| 4876 | |
| 4877 | if (fsize > h2c->mws) |
| 4878 | fsize = h2c->mws; |
| 4879 | |
| 4880 | /* now let's copy this this into the output buffer */ |
| 4881 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 4882 | h2s->mws -= fsize; |
| 4883 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4884 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4885 | |
| 4886 | send_empty: |
| 4887 | /* update the frame's size */ |
| 4888 | h2_set_frame_size(outbuf.area, fsize); |
| 4889 | |
| 4890 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 4891 | * meeting EOM. We should optimize this later. |
| 4892 | */ |
| 4893 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4894 | total++; // EOM counts as one byte |
| 4895 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4896 | es_now = 1; |
| 4897 | } |
| 4898 | |
| 4899 | if (es_now) |
| 4900 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 4901 | |
| 4902 | /* commit the H2 response */ |
| 4903 | b_add(&h2c->mbuf, fsize + 9); |
| 4904 | |
| 4905 | /* consume incoming HTX block, including EOM */ |
| 4906 | total += fsize; |
| 4907 | if (fsize == bsize) { |
| 4908 | htx_remove_blk(htx, blk); |
| 4909 | if (fsize) |
| 4910 | goto new_frame; |
| 4911 | } else { |
| 4912 | /* we've truncated this block */ |
| 4913 | htx_cut_data_blk(htx, blk, fsize); |
| 4914 | } |
| 4915 | |
| 4916 | if (es_now) { |
| 4917 | if (h2s->st == H2_SS_OPEN) |
| 4918 | h2s->st = H2_SS_HLOC; |
| 4919 | else |
| 4920 | h2s_close(h2s); |
| 4921 | |
| 4922 | h2s->flags |= H2_SF_ES_SENT; |
| 4923 | } |
| 4924 | |
| 4925 | end: |
| 4926 | return total; |
| 4927 | } |
| 4928 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4929 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 4930 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 4931 | * processed. The caller must check the stream's status to detect any error |
| 4932 | * which might have happened subsequently to a successful send. The htx blocks |
| 4933 | * are automatically removed from the message. The htx message is assumed to be |
| 4934 | * valid since produced from the internal code. Processing stops when meeting |
| 4935 | * the EOM, which is also removed. All trailers are processed at once and sent |
| 4936 | * as a single frame. The ES flag is always set. |
| 4937 | */ |
| 4938 | static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx) |
| 4939 | { |
| 4940 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4941 | struct h2c *h2c = h2s->h2c; |
| 4942 | struct htx_blk *blk; |
| 4943 | struct htx_blk *blk_end; |
| 4944 | struct buffer outbuf; |
| 4945 | struct h1m h1m; |
| 4946 | enum htx_blk_type type; |
| 4947 | uint32_t size; |
| 4948 | int ret = 0; |
| 4949 | int hdr; |
| 4950 | int idx; |
| 4951 | void *start; |
| 4952 | |
| 4953 | if (h2c_mux_busy(h2c, h2s)) { |
| 4954 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4955 | goto end; |
| 4956 | } |
| 4957 | |
| 4958 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4959 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4960 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4961 | goto end; |
| 4962 | } |
| 4963 | |
| 4964 | /* The principle is that we parse each and every trailers block using |
| 4965 | * the H1 headers parser, and append it to the list. We don't proceed |
| 4966 | * until EOM is met. blk_end will point to the EOM block. |
| 4967 | */ |
| 4968 | hdr = 0; |
| 4969 | memset(list, 0, sizeof(list)); |
| 4970 | blk_end = NULL; |
| 4971 | |
| 4972 | for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) { |
| 4973 | blk = htx_get_blk(htx, idx); |
| 4974 | type = htx_get_blk_type(blk); |
| 4975 | |
| 4976 | if (type == HTX_BLK_UNUSED) |
| 4977 | continue; |
| 4978 | |
| 4979 | if (type != HTX_BLK_TLR) { |
| 4980 | if (type == HTX_BLK_EOM) |
| 4981 | blk_end = blk; |
| 4982 | break; |
| 4983 | } |
| 4984 | |
| 4985 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4986 | goto fail; |
| 4987 | |
| 4988 | size = htx_get_blksz(blk); |
| 4989 | start = htx_get_blk_ptr(htx, blk); |
| 4990 | |
| 4991 | h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER; |
| 4992 | h1m.err_pos = 0; |
| 4993 | ret = h1_headers_to_hdr_list(start, start + size, |
| 4994 | list + hdr, sizeof(list)/sizeof(list[0]) - hdr, |
| 4995 | &h1m, NULL); |
| 4996 | if (ret < 0) |
| 4997 | goto fail; |
| 4998 | |
| 4999 | /* ret == 0 if an incomplete trailers block was found (missing |
| 5000 | * empty line), or > 0 if it was found. We have to continue on |
| 5001 | * incomplete messages because the trailers block might be |
| 5002 | * incomplete. |
| 5003 | */ |
| 5004 | |
| 5005 | /* search the new end */ |
| 5006 | while (hdr <= sizeof(list)/sizeof(list[0])) { |
| 5007 | if (!list[hdr].n.len) |
| 5008 | break; |
| 5009 | hdr++; |
| 5010 | } |
| 5011 | } |
| 5012 | |
| 5013 | if (!blk_end) |
| 5014 | goto end; // end not found yet |
| 5015 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5016 | chunk_reset(&outbuf); |
| 5017 | |
| 5018 | while (1) { |
| 5019 | outbuf.area = b_tail(&h2c->mbuf); |
| 5020 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 5021 | outbuf.data = 0; |
| 5022 | |
| 5023 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 5024 | break; |
| 5025 | realign_again: |
| 5026 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 5027 | } |
| 5028 | |
| 5029 | if (outbuf.size < 9) |
| 5030 | goto full; |
| 5031 | |
| 5032 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 5033 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 5034 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5035 | outbuf.data = 9; |
| 5036 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5037 | /* encode all headers */ |
| 5038 | for (idx = 0; idx < hdr; idx++) { |
| 5039 | /* these ones do not exist in H2 or must not appear in |
| 5040 | * trailers and must be dropped. |
| 5041 | */ |
| 5042 | if (isteq(list[idx].n, ist("host")) || |
| 5043 | isteq(list[idx].n, ist("content-length")) || |
| 5044 | isteq(list[idx].n, ist("connection")) || |
| 5045 | isteq(list[idx].n, ist("proxy-connection")) || |
| 5046 | isteq(list[idx].n, ist("keep-alive")) || |
| 5047 | isteq(list[idx].n, ist("upgrade")) || |
| 5048 | isteq(list[idx].n, ist("te")) || |
| 5049 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 5050 | continue; |
| 5051 | |
| 5052 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 5053 | /* output full */ |
| 5054 | if (b_space_wraps(&h2c->mbuf)) |
| 5055 | goto realign_again; |
| 5056 | goto full; |
| 5057 | } |
| 5058 | } |
| 5059 | |
Willy Tarreau | 67b8cae | 2019-02-21 18:16:35 +0100 | [diff] [blame] | 5060 | if (!hdr) { |
| 5061 | /* here we have a problem, we've received an empty trailers |
| 5062 | * block followed by an EOM. Because of this we can't send a |
| 5063 | * HEADERS frame, so we have to cheat and instead send an empty |
| 5064 | * DATA frame conveying the ES flag. |
| 5065 | */ |
| 5066 | outbuf.area[3] = H2_FT_DATA; |
| 5067 | outbuf.area[4] = H2_F_DATA_END_STREAM; |
| 5068 | } |
| 5069 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5070 | /* update the frame's size */ |
| 5071 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 5072 | |
| 5073 | /* commit the H2 response */ |
| 5074 | b_add(&h2c->mbuf, outbuf.data); |
| 5075 | h2s->flags |= H2_SF_ES_SENT; |
| 5076 | |
| 5077 | if (h2s->st == H2_SS_OPEN) |
| 5078 | h2s->st = H2_SS_HLOC; |
| 5079 | else |
| 5080 | h2s_close(h2s); |
| 5081 | |
| 5082 | /* OK we could properly deliver the response */ |
| 5083 | done: |
| 5084 | /* remove all header blocks including EOM and compute the corresponding size. */ |
| 5085 | ret = 0; |
| 5086 | idx = htx_get_head(htx); |
| 5087 | blk = htx_get_blk(htx, idx); |
| 5088 | while (blk != blk_end) { |
| 5089 | ret += htx_get_blksz(blk); |
| 5090 | blk = htx_remove_blk(htx, blk); |
| 5091 | } |
| 5092 | blk = htx_remove_blk(htx, blk); |
| 5093 | end: |
| 5094 | return ret; |
| 5095 | full: |
| 5096 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5097 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5098 | ret = 0; |
| 5099 | goto end; |
| 5100 | fail: |
| 5101 | /* unparsable HTX messages, too large ones to be produced in the local |
| 5102 | * list etc go here (unrecoverable errors). |
| 5103 | */ |
| 5104 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5105 | ret = 0; |
| 5106 | goto end; |
| 5107 | } |
| 5108 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5109 | /* Called from the upper layer, to subscribe to events, such as being able to send. |
| 5110 | * The <param> argument here is supposed to be a pointer to a wait_event struct |
| 5111 | * which will be passed to h2s->recv_wait or h2s->send_wait depending on the |
| 5112 | * event_type. The event_type must only be a combination of SUB_RETRY_RECV and |
| 5113 | * SUB_RETRY_SEND, other values will lead to -1 being returned. It always |
| 5114 | * returns 0 except for the error above. |
| 5115 | */ |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5116 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 5117 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5118 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5119 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5120 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5121 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5122 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5123 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5124 | if (!(sw->events & SUB_RETRY_RECV)) { |
| 5125 | sw->events |= SUB_RETRY_RECV; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 5126 | sw->handle = h2s; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5127 | h2s->recv_wait = sw; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5128 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5129 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5130 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5131 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5132 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5133 | if (!(sw->events & SUB_RETRY_SEND)) { |
| 5134 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 5135 | sw->handle = h2s; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5136 | h2s->send_wait = sw; |
| 5137 | if (!(h2s->flags & H2_SF_BLK_SFCTL)) { |
| 5138 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 5139 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 5140 | else |
| 5141 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 5142 | } |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 5143 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5144 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5145 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5146 | if (event_type != 0) |
| 5147 | return -1; |
| 5148 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5149 | } |
| 5150 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5151 | /* Called from the upper layer, to unsubscribe some events (undo h2_subscribe). |
| 5152 | * The <param> argument here is supposed to be a pointer to the same wait_event |
| 5153 | * struct that was passed to h2_subscribe() otherwise nothing will be changed. |
| 5154 | * It always returns zero. |
| 5155 | */ |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5156 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 5157 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5158 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5159 | struct h2s *h2s = cs->ctx; |
| 5160 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5161 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5162 | sw = param; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5163 | if (h2s->recv_wait == sw) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5164 | sw->events &= ~SUB_RETRY_RECV; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5165 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5166 | } |
| 5167 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5168 | if (event_type & SUB_RETRY_SEND) { |
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->send_wait == sw) { |
| 5171 | LIST_DEL(&h2s->list); |
| 5172 | LIST_INIT(&h2s->list); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5173 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5174 | h2s->send_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5175 | } |
| 5176 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5177 | if (event_type & SUB_CALL_UNSUBSCRIBE) { |
| 5178 | sw = param; |
| 5179 | if (h2s->send_wait == sw) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5180 | sw->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5181 | h2s->send_wait = NULL; |
Olivier Houchard | f29cd5c | 2018-12-20 11:56:28 +0100 | [diff] [blame] | 5182 | LIST_DEL(&h2s->list); |
| 5183 | LIST_INIT(&h2s->list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5184 | } |
| 5185 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5186 | return 0; |
| 5187 | } |
| 5188 | |
| 5189 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5190 | /* Called from the upper layer, to receive data */ |
| 5191 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 5192 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5193 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5194 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5195 | struct htx *h2s_htx = NULL; |
| 5196 | struct htx *buf_htx = NULL; |
| 5197 | struct htx_ret htx_ret; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5198 | size_t ret = 0; |
| 5199 | |
| 5200 | /* transfer possibly pending data to the upper layer */ |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5201 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
| 5202 | /* in HTX mode we ignore the count argument */ |
| 5203 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5204 | if (htx_is_empty(h2s_htx)) { |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5205 | /* Here htx_to_buf() will set buffer data to 0 because |
| 5206 | * the HTX is empty. |
| 5207 | */ |
| 5208 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5209 | goto end; |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5210 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5211 | |
| 5212 | buf_htx = htx_from_buf(buf); |
Christopher Faulet | a413e95 | 2019-01-21 11:49:37 +0100 | [diff] [blame] | 5213 | count = htx_free_data_space(buf_htx); |
| 5214 | if (flags & CO_RFL_KEEP_RSV) { |
| 5215 | if (count <= global.tune.maxrewrite) |
| 5216 | goto end; |
| 5217 | count -= global.tune.maxrewrite; |
| 5218 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5219 | |
Willy Tarreau | 0c22fa7 | 2018-12-04 15:21:35 +0100 | [diff] [blame] | 5220 | 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] | 5221 | |
| 5222 | if (h2s_htx->flags & HTX_FL_PARSING_ERROR) |
| 5223 | buf_htx->flags |= HTX_FL_PARSING_ERROR; |
| 5224 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 5225 | 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] | 5226 | htx_to_buf(buf_htx, buf); |
| 5227 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5228 | ret = htx_ret.ret; |
| 5229 | } |
| 5230 | else { |
| 5231 | ret = b_xfer(buf, &h2s->rxbuf, count); |
| 5232 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5233 | |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5234 | end: |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5235 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5236 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5237 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5238 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5239 | if (cs->flags & CS_FL_REOS) |
| 5240 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5241 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5242 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5243 | if (b_size(&h2s->rxbuf)) { |
| 5244 | b_free(&h2s->rxbuf); |
| 5245 | offer_buffers(NULL, tasks_run_queue); |
| 5246 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5247 | } |
| 5248 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5249 | if (ret && h2c->dsi == h2s->id) { |
| 5250 | /* demux is blocking on this stream's buffer */ |
| 5251 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 872e2fa | 2019-01-03 08:27:41 +0100 | [diff] [blame] | 5252 | h2c_restart_reading(h2c); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5253 | } |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5254 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5255 | return ret; |
| 5256 | } |
| 5257 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5258 | /* stops all senders of this connection for example when the mux buffer is full. |
| 5259 | * They are moved from the sending_list to either fctl_list or send_list. |
| 5260 | */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5261 | static void h2_stop_senders(struct h2c *h2c) |
| 5262 | { |
| 5263 | struct h2s *h2s, *h2s_back; |
| 5264 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5265 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) { |
| 5266 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5267 | task_remove_from_task_list((struct task *)h2s->send_wait->task); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5268 | h2s->send_wait->events |= SUB_RETRY_SEND; |
| 5269 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5270 | } |
| 5271 | } |
| 5272 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5273 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 5274 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 5275 | * flags may be updated on the conn_stream. |
| 5276 | */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 5277 | 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] | 5278 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5279 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5280 | size_t orig_count = count; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 5281 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5282 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5283 | struct htx *htx; |
| 5284 | struct htx_blk *blk; |
| 5285 | enum htx_blk_type btype; |
| 5286 | uint32_t bsize; |
| 5287 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5288 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5289 | /* If we were not just woken because we wanted to send but couldn't, |
| 5290 | * and there's somebody else that is waiting to send, do nothing, |
| 5291 | * we will subscribe later and be put at the end of the list |
| 5292 | */ |
| 5293 | LIST_DEL_INIT(&h2s->sending_list); |
| 5294 | if ((!(h2s->send_wait) || !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) && |
| 5295 | (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) |
| 5296 | return 0; |
| 5297 | |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5298 | if (h2s->send_wait) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5299 | /* We want to stay in the send_list, so prepare ourself to be |
| 5300 | * eventually recalled if needed, and only remove ourself from |
| 5301 | * the list if we managed to send anything. |
| 5302 | */ |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5303 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5304 | h2s->send_wait->events |= SUB_RETRY_SEND; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5305 | } |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 5306 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 5307 | return 0; |
| 5308 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5309 | /* htx will be enough to decide if we're using HTX or legacy */ |
| 5310 | htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL; |
| 5311 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5312 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 5313 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 5314 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5315 | if (h2s->id == 0) { |
| 5316 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 5317 | |
| 5318 | if (id < 0) { |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5319 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5320 | return 0; |
| 5321 | } |
| 5322 | |
| 5323 | eb32_delete(&h2s->by_id); |
| 5324 | h2s->by_id.key = h2s->id = id; |
| 5325 | h2s->h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 5326 | h2s->h2c->nb_reserved--; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5327 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 5328 | } |
| 5329 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5330 | if (htx) { |
Willy Tarreau | c14999b | 2018-12-06 14:09:09 +0100 | [diff] [blame] | 5331 | while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) && |
| 5332 | count && !htx_is_empty(htx)) { |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5333 | idx = htx_get_head(htx); |
| 5334 | blk = htx_get_blk(htx, idx); |
| 5335 | btype = htx_get_blk_type(blk); |
| 5336 | bsize = htx_get_blksz(blk); |
| 5337 | |
| 5338 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5339 | case HTX_BLK_REQ_SL: |
| 5340 | /* start-line before headers */ |
| 5341 | ret = h2s_htx_bck_make_req_headers(h2s, htx); |
| 5342 | if (ret > 0) { |
| 5343 | total += ret; |
| 5344 | count -= ret; |
| 5345 | if (ret < bsize) |
| 5346 | goto done; |
| 5347 | } |
| 5348 | break; |
| 5349 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 5350 | case HTX_BLK_RES_SL: |
| 5351 | /* start-line before headers */ |
| 5352 | ret = h2s_htx_frt_make_resp_headers(h2s, htx); |
| 5353 | if (ret > 0) { |
| 5354 | total += ret; |
| 5355 | count -= ret; |
| 5356 | if (ret < bsize) |
| 5357 | goto done; |
| 5358 | } |
| 5359 | break; |
| 5360 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5361 | case HTX_BLK_DATA: |
| 5362 | case HTX_BLK_EOD: |
| 5363 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5364 | /* all these cause the emission of a DATA frame (possibly empty). |
| 5365 | * This EOM necessarily is one before trailers, as the EOM following |
| 5366 | * trailers would have been consumed by the trailers parser. |
| 5367 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5368 | ret = h2s_htx_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5369 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5370 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5371 | total += ret; |
| 5372 | count -= ret; |
| 5373 | if (ret < bsize) |
| 5374 | goto done; |
| 5375 | } |
| 5376 | break; |
| 5377 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5378 | case HTX_BLK_TLR: |
| 5379 | /* This is the first trailers block, all the subsequent ones AND |
| 5380 | * the EOM will be swallowed by the parser. |
| 5381 | */ |
| 5382 | ret = h2s_htx_make_trailers(h2s, htx); |
| 5383 | if (ret > 0) { |
| 5384 | total += ret; |
| 5385 | count -= ret; |
| 5386 | if (ret < bsize) |
| 5387 | goto done; |
| 5388 | } |
| 5389 | break; |
| 5390 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5391 | default: |
| 5392 | htx_remove_blk(htx, blk); |
| 5393 | total += bsize; |
| 5394 | count -= bsize; |
| 5395 | break; |
| 5396 | } |
| 5397 | } |
| 5398 | goto done; |
| 5399 | } |
| 5400 | |
| 5401 | /* legacy transfer mode */ |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5402 | while (h2s->h1m.state < H1_MSG_DONE && count) { |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 5403 | if (h2s->h1m.state <= H1_MSG_LAST_LF) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5404 | if (h2s->h2c->flags & H2_CF_IS_BACK) |
| 5405 | ret = -1; |
| 5406 | else |
| 5407 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5408 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5409 | else if (h2s->h1m.state < H1_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5410 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5411 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5412 | else if (h2s->h1m.state == H1_MSG_TRAILERS) { |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5413 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5414 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5415 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5416 | if (unlikely((int)ret <= 0)) { |
| 5417 | if ((int)ret < 0) |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5418 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5419 | break; |
| 5420 | } |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 5421 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5422 | total += count; |
| 5423 | count = 0; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5424 | h2s->h1m.state = H1_MSG_DONE; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5425 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 5426 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5427 | else { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5428 | cs_set_error(cs); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5429 | break; |
| 5430 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5431 | |
| 5432 | total += ret; |
| 5433 | count -= ret; |
| 5434 | |
| 5435 | if (h2s->st >= H2_SS_ERROR) |
| 5436 | break; |
| 5437 | |
| 5438 | if (h2s->flags & H2_SF_BLK_ANY) |
| 5439 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5440 | } |
| 5441 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5442 | done: |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5443 | if (h2s->st >= H2_SS_ERROR) { |
| 5444 | /* trim any possibly pending data after we close (extra CR-LF, |
| 5445 | * unprocessed trailers, abnormal extra data, ...) |
| 5446 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5447 | total += count; |
| 5448 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5449 | } |
| 5450 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5451 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 5452 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5453 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 5454 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 5455 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5456 | } |
| 5457 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5458 | if (htx) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5459 | htx_to_buf(htx, buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5460 | } else { |
| 5461 | b_del(buf, total); |
| 5462 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5463 | |
| 5464 | /* The mux is full, cancel the pending tasks */ |
| 5465 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 5466 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 5467 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5468 | |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5469 | /* If we're running HTX, and we read the whole buffer, then pretend |
| 5470 | * we read exactly what the caller specified, as with HTX the caller |
| 5471 | * will always give the buffer size, instead of the amount of data |
| 5472 | * available. |
| 5473 | */ |
| 5474 | if (htx && !b_data(buf)) |
| 5475 | total = orig_count; |
| 5476 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5477 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5478 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5479 | tasklet_wakeup(h2s->h2c->wait_event.task); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5480 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5481 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 5482 | /* If we're waiting for flow control, and we got a shutr on the |
| 5483 | * connection, we will never be unlocked, so add an error on |
| 5484 | * the conn_stream. |
| 5485 | */ |
| 5486 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 5487 | !b_data(&h2s->h2c->dbuf) && |
| 5488 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 5489 | if (cs->flags & CS_FL_EOS) |
| 5490 | cs->flags |= CS_FL_ERROR; |
| 5491 | else |
| 5492 | cs->flags |= CS_FL_ERR_PENDING; |
| 5493 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5494 | if (total > 0 && h2s->send_wait) { |
| 5495 | /* Ok we managed to send something, leave the send_list */ |
| 5496 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 5497 | h2s->send_wait = NULL; |
| 5498 | LIST_DEL_INIT(&h2s->list); |
| 5499 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5500 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5501 | } |
| 5502 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5503 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5504 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5505 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5506 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5507 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5508 | struct eb32_node *node; |
| 5509 | int fctl_cnt = 0; |
| 5510 | int send_cnt = 0; |
| 5511 | int tree_cnt = 0; |
| 5512 | int orph_cnt = 0; |
| 5513 | |
| 5514 | if (!h2c) |
| 5515 | return; |
| 5516 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5517 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5518 | fctl_cnt++; |
| 5519 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5520 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5521 | send_cnt++; |
| 5522 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5523 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5524 | node = eb32_first(&h2c->streams_by_id); |
| 5525 | while (node) { |
| 5526 | h2s = container_of(node, struct h2s, by_id); |
| 5527 | tree_cnt++; |
| 5528 | if (!h2s->cs) |
| 5529 | orph_cnt++; |
| 5530 | node = eb32_next(node); |
| 5531 | } |
| 5532 | |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5533 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5534 | " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d" |
| 5535 | " .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] | 5536 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5537 | 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] | 5538 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5539 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5540 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5541 | h2c->msi, |
| 5542 | (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf), |
| 5543 | (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf)); |
| 5544 | |
| 5545 | if (h2s) { |
| 5546 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5547 | h2s, h2s->id, h2s->flags, |
| 5548 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5549 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5550 | h2s->cs); |
| 5551 | if (h2s->cs) |
| 5552 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5553 | h2s->cs->flags, h2s->cs->data); |
| 5554 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5555 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5556 | |
| 5557 | /*******************************************************/ |
| 5558 | /* functions below are dedicated to the config parsers */ |
| 5559 | /*******************************************************/ |
| 5560 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5561 | /* config parser for global "tune.h2.header-table-size" */ |
| 5562 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5563 | struct proxy *defpx, const char *file, int line, |
| 5564 | char **err) |
| 5565 | { |
| 5566 | if (too_many_args(1, args, err, NULL)) |
| 5567 | return -1; |
| 5568 | |
| 5569 | h2_settings_header_table_size = atoi(args[1]); |
| 5570 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5571 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5572 | return -1; |
| 5573 | } |
| 5574 | return 0; |
| 5575 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5576 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5577 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5578 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5579 | struct proxy *defpx, const char *file, int line, |
| 5580 | char **err) |
| 5581 | { |
| 5582 | if (too_many_args(1, args, err, NULL)) |
| 5583 | return -1; |
| 5584 | |
| 5585 | h2_settings_initial_window_size = atoi(args[1]); |
| 5586 | if (h2_settings_initial_window_size < 0) { |
| 5587 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5588 | return -1; |
| 5589 | } |
| 5590 | return 0; |
| 5591 | } |
| 5592 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5593 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5594 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5595 | struct proxy *defpx, const char *file, int line, |
| 5596 | char **err) |
| 5597 | { |
| 5598 | if (too_many_args(1, args, err, NULL)) |
| 5599 | return -1; |
| 5600 | |
| 5601 | h2_settings_max_concurrent_streams = atoi(args[1]); |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 5602 | if ((int)h2_settings_max_concurrent_streams < 0) { |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5603 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5604 | return -1; |
| 5605 | } |
| 5606 | return 0; |
| 5607 | } |
| 5608 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5609 | /* config parser for global "tune.h2.max-frame-size" */ |
| 5610 | static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx, |
| 5611 | struct proxy *defpx, const char *file, int line, |
| 5612 | char **err) |
| 5613 | { |
| 5614 | if (too_many_args(1, args, err, NULL)) |
| 5615 | return -1; |
| 5616 | |
| 5617 | h2_settings_max_frame_size = atoi(args[1]); |
| 5618 | if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) { |
| 5619 | memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]); |
| 5620 | return -1; |
| 5621 | } |
| 5622 | return 0; |
| 5623 | } |
| 5624 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5625 | |
| 5626 | /****************************************/ |
| 5627 | /* MUX initialization and instanciation */ |
| 5628 | /***************************************/ |
| 5629 | |
| 5630 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5631 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5632 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5633 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5634 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5635 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5636 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5637 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5638 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5639 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5640 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5641 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5642 | .avail_streams = h2_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 5643 | .used_streams = h2_used_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5644 | .shutr = h2_shutr, |
| 5645 | .shutw = h2_shutw, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5646 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 5647 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5648 | .name = "H2", |
| 5649 | }; |
| 5650 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5651 | /* PROTO selection : this mux registers PROTO token "h2" */ |
| 5652 | static struct mux_proto_list mux_proto_h2 = |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5653 | { .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] | 5654 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5655 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 5656 | |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5657 | static struct mux_proto_list mux_proto_h2_htx = |
| 5658 | { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops }; |
| 5659 | |
| 5660 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx); |
| 5661 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5662 | /* config keyword parsers */ |
| 5663 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5664 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5665 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5666 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5667 | { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5668 | { 0, NULL, NULL } |
| 5669 | }}; |
| 5670 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5671 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |