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 */ |
| 199 | struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */ |
| 200 | struct wait_event *send_wait; /* The streeam is waiting for flow control */ |
| 201 | struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 202 | }; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 203 | |
Willy Tarreau | c640514 | 2017-09-21 20:23:50 +0200 | [diff] [blame] | 204 | /* descriptor for an h2 frame header */ |
| 205 | struct h2_fh { |
| 206 | uint32_t len; /* length, host order, 24 bits */ |
| 207 | uint32_t sid; /* stream id, host order, 31 bits */ |
| 208 | uint8_t ft; /* frame type */ |
| 209 | uint8_t ff; /* frame flags */ |
| 210 | }; |
| 211 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 212 | /* the h2c connection pool */ |
| 213 | DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c)); |
| 214 | |
| 215 | /* the h2s stream pool */ |
| 216 | DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s)); |
| 217 | |
Willy Tarreau | dc57236 | 2018-12-12 08:08:05 +0100 | [diff] [blame] | 218 | /* The default connection window size is 65535, it may only be enlarged using |
| 219 | * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1, |
| 220 | * we'll pretend we already received the difference between the two to send |
| 221 | * an equivalent window update to enlarge it to 2G-1. |
| 222 | */ |
| 223 | #define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535) |
| 224 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 225 | /* a few settings from the global section */ |
| 226 | static int h2_settings_header_table_size = 4096; /* initial value */ |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 227 | static int h2_settings_initial_window_size = 65535; /* initial value */ |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 228 | static unsigned int h2_settings_max_concurrent_streams = 100; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 229 | static int h2_settings_max_frame_size = 0; /* unset */ |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 230 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 231 | /* a dmumy closed stream */ |
| 232 | static const struct h2s *h2_closed_stream = &(const struct h2s){ |
| 233 | .cs = NULL, |
| 234 | .h2c = NULL, |
| 235 | .st = H2_SS_CLOSED, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 236 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 237 | .flags = H2_SF_RST_RCVD, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 238 | .id = 0, |
| 239 | }; |
| 240 | |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 241 | /* a dmumy closed stream returning a PROTOCOL_ERROR error */ |
| 242 | static const struct h2s *h2_error_stream = &(const struct h2s){ |
| 243 | .cs = NULL, |
| 244 | .h2c = NULL, |
| 245 | .st = H2_SS_CLOSED, |
| 246 | .errcode = H2_ERR_PROTOCOL_ERROR, |
| 247 | .flags = 0, |
| 248 | .id = 0, |
| 249 | }; |
| 250 | |
Willy Tarreau | 8d0d58b | 2018-12-23 18:29:12 +0100 | [diff] [blame] | 251 | /* a dmumy closed stream returning a REFUSED_STREAM error */ |
| 252 | static const struct h2s *h2_refused_stream = &(const struct h2s){ |
| 253 | .cs = NULL, |
| 254 | .h2c = NULL, |
| 255 | .st = H2_SS_CLOSED, |
| 256 | .errcode = H2_ERR_REFUSED_STREAM, |
| 257 | .flags = 0, |
| 258 | .id = 0, |
| 259 | }; |
| 260 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 261 | /* and a dummy idle stream for use with any unannounced stream */ |
| 262 | static const struct h2s *h2_idle_stream = &(const struct h2s){ |
| 263 | .cs = NULL, |
| 264 | .h2c = NULL, |
| 265 | .st = H2_SS_IDLE, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 266 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 267 | .id = 0, |
| 268 | }; |
| 269 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 270 | 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] | 271 | static int h2_send(struct h2c *h2c); |
| 272 | static int h2_recv(struct h2c *h2c); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 273 | static int h2_process(struct h2c *h2c); |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 274 | 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] | 275 | 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] | 276 | 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] | 277 | static int h2_frt_transfer_data(struct h2s *h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 278 | 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] | 279 | 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] | 280 | static void h2s_alert(struct h2s *h2s); |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 281 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 282 | /*****************************************************/ |
| 283 | /* functions below are for dynamic buffer management */ |
| 284 | /*****************************************************/ |
| 285 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 286 | /* indicates whether or not the we may call the h2_recv() function to attempt |
| 287 | * to receive data into the buffer and/or demux pending data. The condition is |
| 288 | * a bit complex due to some API limits for now. The rules are the following : |
| 289 | * - if an error or a shutdown was detected on the connection and the buffer |
| 290 | * is empty, we must not attempt to receive |
| 291 | * - if the demux buf failed to be allocated, we must not try to receive and |
| 292 | * we know there is nothing pending |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 293 | * - if no flag indicates a blocking condition, we may attempt to receive, |
| 294 | * regardless of whether the demux buffer is full or not, so that only |
| 295 | * de demux part decides whether or not to block. This is needed because |
| 296 | * the connection API indeed prevents us from re-enabling receipt that is |
| 297 | * already enabled in a polled state, so we must always immediately stop |
| 298 | * as soon as the demux can't proceed so as never to hit an end of read |
| 299 | * with data pending in the buffers. |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 300 | * - otherwise must may not attempt |
| 301 | */ |
| 302 | static inline int h2_recv_allowed(const struct h2c *h2c) |
| 303 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 304 | if (b_data(&h2c->dbuf) == 0 && |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 305 | (h2c->st0 >= H2_CS_ERROR || |
| 306 | h2c->conn->flags & CO_FL_ERROR || |
| 307 | conn_xprt_read0_pending(h2c->conn))) |
| 308 | return 0; |
| 309 | |
| 310 | if (!(h2c->flags & H2_CF_DEM_DALLOC) && |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 311 | !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 312 | return 1; |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 317 | /* restarts reading on the connection if it was not enabled */ |
| 318 | static inline void h2c_restart_reading(const struct h2c *h2c) |
| 319 | { |
| 320 | if (!h2_recv_allowed(h2c)) |
| 321 | return; |
Willy Tarreau | 872e2fa | 2019-01-03 08:27:41 +0100 | [diff] [blame] | 322 | if (!b_data(&h2c->dbuf) && (h2c->wait_event.events & SUB_RETRY_RECV)) |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 323 | return; |
| 324 | tasklet_wakeup(h2c->wait_event.task); |
| 325 | } |
| 326 | |
| 327 | |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 328 | /* returns true if the front connection has too many conn_streams attached */ |
| 329 | 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] | 330 | { |
Willy Tarreau | a875466 | 2018-12-23 20:43:58 +0100 | [diff] [blame] | 331 | return h2c->nb_cs > h2_settings_max_concurrent_streams; |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 334 | /* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c |
| 335 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 336 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 337 | * impossible to wake up and we prefer to be woken up later. |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 338 | */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 339 | static int h2_buf_available(void *target) |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 340 | { |
| 341 | struct h2c *h2c = target; |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 342 | struct h2s *h2s; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 343 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 344 | 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] | 345 | h2c->flags &= ~H2_CF_DEM_DALLOC; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 346 | h2c_restart_reading(h2c); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 347 | return 1; |
| 348 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 349 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 350 | if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) { |
| 351 | h2c->flags &= ~H2_CF_MUX_MALLOC; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 352 | |
| 353 | if (h2c->flags & H2_CF_DEM_MROOM) { |
| 354 | h2c->flags &= ~H2_CF_DEM_MROOM; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 355 | h2c_restart_reading(h2c); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 356 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 357 | return 1; |
| 358 | } |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 359 | |
| 360 | if ((h2c->flags & H2_CF_DEM_SALLOC) && |
| 361 | (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs && |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 362 | b_alloc_margin(&h2s->rxbuf, 0)) { |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 363 | h2c->flags &= ~H2_CF_DEM_SALLOC; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 364 | h2c_restart_reading(h2c); |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 365 | return 1; |
| 366 | } |
| 367 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 368 | return 0; |
| 369 | } |
| 370 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 371 | 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] | 372 | { |
| 373 | struct buffer *buf = NULL; |
| 374 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 375 | if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) && |
| 376 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 377 | h2c->buf_wait.target = h2c; |
| 378 | h2c->buf_wait.wakeup_cb = h2_buf_available; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 379 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 380 | LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 381 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 382 | __conn_xprt_stop_recv(h2c->conn); |
| 383 | } |
| 384 | return buf; |
| 385 | } |
| 386 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 387 | static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 388 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 389 | if (bptr->size) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 390 | b_free(bptr); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 391 | offer_buffers(h2c->buf_wait.target, tasks_run_queue); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 395 | /* returns the number of allocatable outgoing streams for the connection taking |
| 396 | * the last_sid and the reserved ones into account. |
| 397 | */ |
| 398 | static inline int h2_streams_left(const struct h2c *h2c) |
| 399 | { |
| 400 | int ret; |
| 401 | |
| 402 | /* consider the number of outgoing streams we're allowed to create before |
| 403 | * reaching the last GOAWAY frame seen. max_id is the last assigned id, |
| 404 | * nb_reserved is the number of streams which don't yet have an ID. |
| 405 | */ |
| 406 | ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF; |
| 407 | ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1; |
| 408 | if (ret < 0) |
| 409 | ret = 0; |
| 410 | return ret; |
| 411 | } |
| 412 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 413 | /* returns the number of streams in use on a connection to figure if it's |
| 414 | * idle or not. We check nb_cs and not nb_streams as the caller will want |
| 415 | * to know if it was the last one after a detach(). |
| 416 | */ |
| 417 | static int h2_used_streams(struct connection *conn) |
| 418 | { |
| 419 | struct h2c *h2c = conn->ctx; |
| 420 | |
| 421 | return h2c->nb_cs; |
| 422 | } |
| 423 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 424 | /* returns the number of concurrent streams available on the connection */ |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 425 | static int h2_avail_streams(struct connection *conn) |
| 426 | { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 427 | struct server *srv = objt_server(conn->target); |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 428 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 429 | int ret1, ret2; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 430 | |
Willy Tarreau | 6afec46 | 2019-01-28 06:40:19 +0100 | [diff] [blame] | 431 | /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional |
| 432 | * streams on the connection. |
| 433 | */ |
| 434 | if (h2c->last_sid >= 0) |
| 435 | return 0; |
| 436 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 437 | /* note: may be negative if a SETTINGS frame changes the limit */ |
| 438 | ret1 = h2c->streams_limit - h2c->nb_streams; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 439 | |
| 440 | /* we must also consider the limit imposed by stream IDs */ |
| 441 | ret2 = h2_streams_left(h2c); |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 442 | ret1 = MIN(ret1, ret2); |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 443 | if (ret1 > 0 && srv && srv->max_reuse >= 0) { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 444 | ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0; |
| 445 | ret1 = MIN(ret1, ret2); |
| 446 | } |
| 447 | return ret1; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 448 | } |
| 449 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 450 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 451 | /*****************************************************************/ |
| 452 | /* functions below are dedicated to the mux setup and management */ |
| 453 | /*****************************************************************/ |
| 454 | |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 455 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 456 | * is already initialized before installing the mux, so we detect incoming |
| 457 | * connections from the fact that the context is still NULL. Returns < 0 on |
| 458 | * error. |
| 459 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 460 | 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] | 461 | { |
| 462 | struct h2c *h2c; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 463 | struct task *t = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 464 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 465 | h2c = pool_alloc(pool_head_h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 466 | if (!h2c) |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 467 | goto fail_no_h2c; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 468 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 469 | if (conn->ctx) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 470 | h2c->flags = H2_CF_IS_BACK; |
| 471 | h2c->shut_timeout = h2c->timeout = prx->timeout.server; |
| 472 | if (tick_isset(prx->timeout.serverfin)) |
| 473 | h2c->shut_timeout = prx->timeout.serverfin; |
| 474 | } else { |
| 475 | h2c->flags = H2_CF_NONE; |
| 476 | h2c->shut_timeout = h2c->timeout = prx->timeout.client; |
| 477 | if (tick_isset(prx->timeout.clientfin)) |
| 478 | h2c->shut_timeout = prx->timeout.clientfin; |
| 479 | } |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 480 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 481 | h2c->proxy = prx; |
Willy Tarreau | 3340029 | 2017-11-05 11:23:40 +0100 | [diff] [blame] | 482 | h2c->task = NULL; |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 483 | if (tick_isset(h2c->timeout)) { |
| 484 | t = task_new(tid_bit); |
| 485 | if (!t) |
| 486 | goto fail; |
| 487 | |
| 488 | h2c->task = t; |
| 489 | t->process = h2_timeout_task; |
| 490 | t->context = h2c; |
| 491 | t->expire = tick_add(now_ms, h2c->timeout); |
| 492 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 493 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 494 | h2c->wait_event.task = tasklet_new(); |
| 495 | if (!h2c->wait_event.task) |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 496 | goto fail; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 497 | h2c->wait_event.task->process = h2_io_cb; |
| 498 | h2c->wait_event.task->context = h2c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 499 | h2c->wait_event.events = 0; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 500 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 501 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 502 | if (!h2c->ddht) |
| 503 | goto fail; |
| 504 | |
| 505 | /* Initialise the context. */ |
| 506 | h2c->st0 = H2_CS_PREFACE; |
| 507 | h2c->conn = conn; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 508 | h2c->streams_limit = h2_settings_max_concurrent_streams; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 509 | h2c->max_id = -1; |
| 510 | h2c->errcode = H2_ERR_NO_ERROR; |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 511 | h2c->rcvd_c = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 512 | h2c->rcvd_s = 0; |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 513 | h2c->nb_streams = 0; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 514 | h2c->nb_cs = 0; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 515 | h2c->nb_reserved = 0; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 516 | h2c->stream_cnt = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 517 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 518 | h2c->dbuf = BUF_NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 519 | h2c->dsi = -1; |
| 520 | h2c->msi = -1; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 521 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 522 | h2c->last_sid = -1; |
| 523 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 524 | h2c->mbuf = BUF_NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 525 | h2c->miw = 65535; /* mux initial window size */ |
| 526 | h2c->mws = 65535; /* mux window size */ |
| 527 | h2c->mfs = 16384; /* initial max frame size */ |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 528 | h2c->streams_by_id = EB_ROOT; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 529 | LIST_INIT(&h2c->send_list); |
| 530 | LIST_INIT(&h2c->fctl_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 531 | LIST_INIT(&h2c->sending_list); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 532 | LIST_INIT(&h2c->buf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 533 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 534 | if (t) |
| 535 | task_queue(t); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 536 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 537 | if (h2c->flags & H2_CF_IS_BACK) { |
| 538 | /* FIXME: this is temporary, for outgoing connections we need |
| 539 | * to immediately allocate a stream until the code is modified |
| 540 | * so that the caller calls ->attach(). For now the outgoing cs |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 541 | * is stored as conn->ctx by the caller. |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 542 | */ |
| 543 | struct h2s *h2s; |
| 544 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 545 | h2s = h2c_bck_stream_new(h2c, conn->ctx, sess); |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 546 | if (!h2s) |
| 547 | goto fail_stream; |
| 548 | } |
| 549 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 550 | conn->ctx = h2c; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 551 | |
Willy Tarreau | 0f38358 | 2018-10-03 14:22:21 +0200 | [diff] [blame] | 552 | /* prepare to read something */ |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 553 | h2c_restart_reading(h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 554 | return 0; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 555 | fail_stream: |
| 556 | hpack_dht_free(h2c->ddht); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 557 | fail: |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 558 | if (t) |
| 559 | task_free(t); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 560 | if (h2c->wait_event.task) |
| 561 | tasklet_free(h2c->wait_event.task); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 562 | pool_free(pool_head_h2c, h2c); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 563 | fail_no_h2c: |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 564 | return -1; |
| 565 | } |
| 566 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 567 | /* returns the next allocatable outgoing stream ID for the H2 connection, or |
| 568 | * -1 if no more is allocatable. |
| 569 | */ |
| 570 | static inline int32_t h2c_get_next_sid(const struct h2c *h2c) |
| 571 | { |
| 572 | int32_t id = (h2c->max_id + 1) | 1; |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 573 | |
| 574 | if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid)) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 575 | id = -1; |
| 576 | return id; |
| 577 | } |
| 578 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 579 | /* returns the stream associated with id <id> or NULL if not found */ |
| 580 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 581 | { |
| 582 | struct eb32_node *node; |
| 583 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 584 | if (id == 0) |
| 585 | return (struct h2s *)h2_closed_stream; |
| 586 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 587 | if (id > h2c->max_id) |
| 588 | return (struct h2s *)h2_idle_stream; |
| 589 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 590 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 591 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 592 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 593 | |
| 594 | return container_of(node, struct h2s, by_id); |
| 595 | } |
| 596 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 597 | /* release function for a connection. This one should be called to free all |
| 598 | * resources allocated to the mux. |
| 599 | */ |
| 600 | static void h2_release(struct connection *conn) |
| 601 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 602 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 603 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 604 | if (h2c) { |
| 605 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 606 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 607 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 608 | LIST_DEL(&h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 609 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 610 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 611 | h2_release_buf(h2c, &h2c->dbuf); |
| 612 | h2_release_buf(h2c, &h2c->mbuf); |
| 613 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 614 | if (h2c->task) { |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 615 | h2c->task->context = NULL; |
| 616 | task_wakeup(h2c->task, TASK_WOKEN_OTHER); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 617 | h2c->task = NULL; |
| 618 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 619 | if (h2c->wait_event.task) |
| 620 | tasklet_free(h2c->wait_event.task); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 621 | if (h2c->wait_event.events != 0) |
| 622 | conn->xprt->unsubscribe(conn, h2c->wait_event.events, |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 623 | &h2c->wait_event); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 624 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 625 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | conn->mux = NULL; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 629 | conn->ctx = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 630 | |
| 631 | conn_stop_tracking(conn); |
| 632 | conn_full_close(conn); |
| 633 | if (conn->destroy_cb) |
| 634 | conn->destroy_cb(conn); |
| 635 | conn_free(conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 639 | /******************************************************/ |
| 640 | /* functions below are for the H2 protocol processing */ |
| 641 | /******************************************************/ |
| 642 | |
| 643 | /* 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] | 644 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 645 | { |
| 646 | return h2s ? h2s->id : 0; |
| 647 | } |
| 648 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 649 | /* 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] | 650 | 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] | 651 | { |
| 652 | if (h2c->msi < 0) |
| 653 | return 0; |
| 654 | |
| 655 | if (h2c->msi == h2s_id(h2s)) |
| 656 | return 0; |
| 657 | |
| 658 | return 1; |
| 659 | } |
| 660 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 661 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 662 | 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] | 663 | { |
| 664 | h2c->errcode = err; |
| 665 | h2c->st0 = H2_CS_ERROR; |
| 666 | } |
| 667 | |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 668 | /* marks an error on the stream. It may also update an already closed stream |
| 669 | * (e.g. to report an error after an RST was received). |
| 670 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 671 | 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] | 672 | { |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 673 | if (h2s->id && h2s->st != H2_SS_ERROR) { |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 674 | h2s->errcode = err; |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 675 | if (h2s->st < H2_SS_ERROR) |
| 676 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 677 | if (h2s->cs) |
| 678 | cs_set_error(h2s->cs); |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 682 | /* attempt to notify the data layer of recv availability */ |
| 683 | static void __maybe_unused h2s_notify_recv(struct h2s *h2s) |
| 684 | { |
| 685 | struct wait_event *sw; |
| 686 | |
| 687 | if (h2s->recv_wait) { |
| 688 | sw = h2s->recv_wait; |
| 689 | sw->events &= ~SUB_RETRY_RECV; |
| 690 | tasklet_wakeup(sw->task); |
| 691 | h2s->recv_wait = NULL; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | /* attempt to notify the data layer of send availability */ |
| 696 | static void __maybe_unused h2s_notify_send(struct h2s *h2s) |
| 697 | { |
| 698 | struct wait_event *sw; |
| 699 | |
| 700 | if (h2s->send_wait) { |
| 701 | sw = h2s->send_wait; |
| 702 | sw->events &= ~SUB_RETRY_SEND; |
| 703 | tasklet_wakeup(sw->task); |
| 704 | h2s->send_wait = NULL; |
Willy Tarreau | 645b33d | 2018-12-20 15:35:57 +0100 | [diff] [blame] | 705 | LIST_DEL(&h2s->list); |
| 706 | LIST_INIT(&h2s->list); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 710 | /* alerts the data layer, trying to wake it up by all means, following |
| 711 | * this sequence : |
| 712 | * - if the h2s' data layer is subscribed to recv, then it's woken up for recv |
| 713 | * - if its subscribed to send, then it's woken up for send |
| 714 | * - if it was subscribed to neither, its ->wake() callback is called |
| 715 | * It is safe to call this function with a closed stream which doesn't have a |
| 716 | * conn_stream anymore. |
| 717 | */ |
| 718 | static void __maybe_unused h2s_alert(struct h2s *h2s) |
| 719 | { |
| 720 | if (h2s->recv_wait || h2s->send_wait) { |
| 721 | h2s_notify_recv(h2s); |
| 722 | h2s_notify_send(h2s); |
| 723 | } |
| 724 | else if (h2s->cs && h2s->cs->data_cb->wake != NULL) |
| 725 | h2s->cs->data_cb->wake(h2s->cs); |
| 726 | } |
| 727 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 728 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 729 | 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] | 730 | { |
| 731 | uint8_t *out = frame; |
| 732 | |
| 733 | *out = len >> 16; |
| 734 | write_n16(out + 1, len); |
| 735 | } |
| 736 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 737 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 738 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 739 | * 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] | 740 | * available in the buffer's input prior to calling this function. The buffer |
| 741 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 742 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 743 | 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] | 744 | const struct buffer *b, int o) |
| 745 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 746 | 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] | 747 | } |
| 748 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 749 | 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] | 750 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 751 | 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] | 752 | } |
| 753 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 754 | 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] | 755 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 756 | 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] | 757 | } |
| 758 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 759 | 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] | 760 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 761 | 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] | 762 | } |
| 763 | |
| 764 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 765 | /* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>. |
| 766 | * The algorithm is not obvious. It turns out that H2 headers are neither |
| 767 | * aligned nor do they use regular sizes. And to add to the trouble, the buffer |
| 768 | * 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] | 769 | * |
| 770 | * b0 b1 b2 b3 b4 b5..b8 |
| 771 | * +----------+---------+--------+----+----+----------------------+ |
| 772 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 773 | * +----------+---------+--------+----+----+----------------------+ |
| 774 | * |
| 775 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 776 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 777 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 778 | * 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] | 779 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 780 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 781 | */ |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 782 | 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] | 783 | { |
| 784 | uint64_t w; |
| 785 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 786 | if (b_data(b) < o + 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 787 | return 0; |
| 788 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 789 | w = h2_get_n64(b, o + 1); |
| 790 | h->len = *(uint8_t*)b_peek(b, o) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 791 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 792 | h->ff = w >> 32; |
| 793 | h->ft = w >> 40; |
| 794 | h->len += w >> 48; |
| 795 | return 1; |
| 796 | } |
| 797 | |
| 798 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 799 | * h2_peek_frame_hdr() above. |
| 800 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 801 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 802 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 803 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 807 | 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] | 808 | { |
| 809 | int ret; |
| 810 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 811 | ret = h2_peek_frame_hdr(b, 0, h); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 812 | if (ret > 0) |
| 813 | h2_skip_frame_hdr(b); |
| 814 | return ret; |
| 815 | } |
| 816 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 817 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 818 | * its connection if the stream was not yet closed. Please use this exclusively |
| 819 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 820 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 821 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 822 | { |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 823 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 824 | h2s->h2c->nb_streams--; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 825 | if (!h2s->id) |
| 826 | h2s->h2c->nb_reserved--; |
| 827 | } |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 828 | h2s->st = H2_SS_CLOSED; |
| 829 | } |
| 830 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 831 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 832 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 833 | { |
| 834 | h2s_close(h2s); |
| 835 | eb32_delete(&h2s->by_id); |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 836 | if (b_size(&h2s->rxbuf)) { |
| 837 | b_free(&h2s->rxbuf); |
| 838 | offer_buffers(NULL, tasks_run_queue); |
| 839 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 840 | if (h2s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 841 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 842 | if (h2s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 843 | h2s->recv_wait->events &= ~SUB_RETRY_RECV; |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 844 | /* There's no need to explicitly call unsubscribe here, the only |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 845 | * reference left would be in the h2c send_list/fctl_list, and if |
| 846 | * we're in it, we're getting out anyway |
| 847 | */ |
| 848 | LIST_DEL(&h2s->list); |
| 849 | LIST_INIT(&h2s->list); |
| 850 | tasklet_free(h2s->wait_event.task); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 851 | pool_free(pool_head_h2s, h2s); |
| 852 | } |
| 853 | |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 854 | /* allocates a new stream <id> for connection <h2c> and adds it into h2c's |
| 855 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 856 | * causes of errors can be any failed memory allocation. The caller is |
| 857 | * responsible for checking if the connection may support an extra stream |
| 858 | * prior to calling this function. |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 859 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 860 | static struct h2s *h2s_new(struct h2c *h2c, int id) |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 861 | { |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 862 | struct h2s *h2s; |
| 863 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 864 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 865 | if (!h2s) |
| 866 | goto out; |
| 867 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 868 | h2s->wait_event.task = tasklet_new(); |
| 869 | if (!h2s->wait_event.task) { |
| 870 | pool_free(pool_head_h2s, h2s); |
| 871 | goto out; |
| 872 | } |
| 873 | h2s->send_wait = NULL; |
| 874 | h2s->recv_wait = NULL; |
| 875 | h2s->wait_event.task->process = h2_deferred_shut; |
| 876 | h2s->wait_event.task->context = h2s; |
| 877 | h2s->wait_event.handle = NULL; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 878 | h2s->wait_event.events = 0; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 879 | LIST_INIT(&h2s->list); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 880 | h2s->h2c = h2c; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 881 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 882 | h2s->mws = h2c->miw; |
| 883 | h2s->flags = H2_SF_NONE; |
| 884 | h2s->errcode = H2_ERR_NO_ERROR; |
| 885 | h2s->st = H2_SS_IDLE; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 886 | h2s->status = 0; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 887 | h2s->body_len = 0; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 888 | h2s->rxbuf = BUF_NULL; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 889 | |
| 890 | if (h2c->flags & H2_CF_IS_BACK) { |
| 891 | h1m_init_req(&h2s->h1m); |
| 892 | h2s->h1m.err_pos = -1; // don't care about errors on the request path |
| 893 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 894 | } else { |
| 895 | h1m_init_res(&h2s->h1m); |
| 896 | h2s->h1m.err_pos = -1; // don't care about errors on the response path |
| 897 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 898 | } |
| 899 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 900 | h2s->by_id.key = h2s->id = id; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 901 | if (id > 0) |
| 902 | h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 903 | else |
| 904 | h2c->nb_reserved++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 905 | |
| 906 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 907 | h2c->nb_streams++; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 908 | h2c->stream_cnt++; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 909 | |
| 910 | return h2s; |
| 911 | |
| 912 | out_free_h2s: |
| 913 | pool_free(pool_head_h2s, h2s); |
| 914 | out: |
| 915 | return NULL; |
| 916 | } |
| 917 | |
| 918 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 919 | * case of memory allocation error. |
| 920 | */ |
| 921 | static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id) |
| 922 | { |
| 923 | struct session *sess = h2c->conn->owner; |
| 924 | struct conn_stream *cs; |
| 925 | struct h2s *h2s; |
| 926 | |
| 927 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 928 | goto out; |
| 929 | |
| 930 | h2s = h2s_new(h2c, id); |
| 931 | if (!h2s) |
| 932 | goto out; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 933 | |
| 934 | cs = cs_new(h2c->conn); |
| 935 | if (!cs) |
| 936 | goto out_close; |
| 937 | |
Olivier Houchard | 746fb77 | 2018-12-15 19:42:00 +0100 | [diff] [blame] | 938 | cs->flags |= CS_FL_NOT_FIRST; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 939 | h2s->cs = cs; |
| 940 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 941 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 942 | |
| 943 | if (stream_create_from_cs(cs) < 0) |
| 944 | goto out_free_cs; |
| 945 | |
Willy Tarreau | 590a051 | 2018-09-05 11:56:48 +0200 | [diff] [blame] | 946 | /* We want the accept date presented to the next stream to be the one |
| 947 | * we have now, the handshake time to be null (since the next stream |
| 948 | * is not delayed by a handshake), and the idle time to count since |
| 949 | * right now. |
| 950 | */ |
| 951 | sess->accept_date = date; |
| 952 | sess->tv_accept = now; |
| 953 | sess->t_handshake = 0; |
| 954 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 955 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 956 | if (h2_frt_has_too_many_cs(h2c)) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 957 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 958 | return h2s; |
| 959 | |
| 960 | out_free_cs: |
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 | cs_free(cs); |
| 963 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 964 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 965 | out: |
Willy Tarreau | 45efc07 | 2018-10-03 18:27:52 +0200 | [diff] [blame] | 966 | sess_log(sess); |
| 967 | return NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 968 | } |
| 969 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 970 | /* allocates a new stream associated to conn_stream <cs> on the h2c connection |
| 971 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 972 | * possible stream ID was reached. |
| 973 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 974 | 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] | 975 | { |
| 976 | struct h2s *h2s = NULL; |
| 977 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 978 | if (h2c->nb_streams >= h2c->streams_limit) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 979 | goto out; |
| 980 | |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 981 | if (h2_streams_left(h2c) < 1) |
| 982 | goto out; |
| 983 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 984 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 985 | h2s = h2s_new(h2c, 0); |
| 986 | if (!h2s) |
| 987 | goto out; |
| 988 | |
| 989 | h2s->cs = cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 990 | h2s->sess = sess; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 991 | cs->ctx = h2s; |
| 992 | h2c->nb_cs++; |
| 993 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 994 | out: |
| 995 | return h2s; |
| 996 | } |
| 997 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 998 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 999 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 1000 | * the various settings codes. |
| 1001 | */ |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1002 | static int h2c_send_settings(struct h2c *h2c) |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1003 | { |
| 1004 | struct buffer *res; |
| 1005 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1006 | struct buffer buf; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1007 | int mfs; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1008 | int ret; |
| 1009 | |
| 1010 | if (h2c_mux_busy(h2c, NULL)) { |
| 1011 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1015 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1016 | if (!res) { |
| 1017 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1018 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
| 1022 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 1023 | chunk_memcpy(&buf, |
| 1024 | "\x00\x00\x00" /* length : 0 for now */ |
| 1025 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 1026 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 1027 | 9); |
| 1028 | |
Willy Tarreau | 0bbad6b | 2019-02-26 16:01:52 +0100 | [diff] [blame] | 1029 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1030 | /* send settings_enable_push=0 */ |
| 1031 | chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6); |
| 1032 | } |
| 1033 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1034 | if (h2_settings_header_table_size != 4096) { |
| 1035 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 1036 | |
| 1037 | write_n32(str + 2, h2_settings_header_table_size); |
| 1038 | chunk_memcat(&buf, str, 6); |
| 1039 | } |
| 1040 | |
| 1041 | if (h2_settings_initial_window_size != 65535) { |
| 1042 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 1043 | |
| 1044 | write_n32(str + 2, h2_settings_initial_window_size); |
| 1045 | chunk_memcat(&buf, str, 6); |
| 1046 | } |
| 1047 | |
| 1048 | if (h2_settings_max_concurrent_streams != 0) { |
| 1049 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 1050 | |
| 1051 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 1052 | * the protocol, so never send this value! |
| 1053 | */ |
| 1054 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 1055 | chunk_memcat(&buf, str, 6); |
| 1056 | } |
| 1057 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1058 | mfs = h2_settings_max_frame_size; |
| 1059 | if (mfs > global.tune.bufsize) |
| 1060 | mfs = global.tune.bufsize; |
| 1061 | |
| 1062 | if (!mfs) |
| 1063 | mfs = global.tune.bufsize; |
| 1064 | |
| 1065 | if (mfs != 16384) { |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1066 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 1067 | |
| 1068 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 1069 | * match bufsize - rewrite size, but at the moment it seems |
| 1070 | * that clients don't take care of it. |
| 1071 | */ |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1072 | write_n32(str + 2, mfs); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1073 | chunk_memcat(&buf, str, 6); |
| 1074 | } |
| 1075 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1076 | h2_set_frame_size(buf.area, buf.data - 9); |
| 1077 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1078 | if (unlikely(ret <= 0)) { |
| 1079 | if (!ret) { |
| 1080 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1081 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1082 | return 0; |
| 1083 | } |
| 1084 | else { |
| 1085 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1086 | return 0; |
| 1087 | } |
| 1088 | } |
| 1089 | return ret; |
| 1090 | } |
| 1091 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1092 | /* Try to receive a connection preface, then upon success try to send our |
| 1093 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1094 | * missing data. It may return an error in h2c. |
| 1095 | */ |
| 1096 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 1097 | { |
| 1098 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1099 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1100 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1101 | 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] | 1102 | |
| 1103 | if (unlikely(ret1 <= 0)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1104 | if (ret1 < 0) |
| 1105 | sess_log(h2c->conn->owner); |
| 1106 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1107 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 1108 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1112 | ret2 = h2c_send_settings(h2c); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1113 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1114 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1115 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1116 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1117 | } |
| 1118 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1119 | /* Try to send a connection preface, then upon success try to send our |
| 1120 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1121 | * missing data. It may return an error in h2c. |
| 1122 | */ |
| 1123 | static int h2c_bck_send_preface(struct h2c *h2c) |
| 1124 | { |
| 1125 | struct buffer *res; |
| 1126 | |
| 1127 | if (h2c_mux_busy(h2c, NULL)) { |
| 1128 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
| 1132 | res = h2_get_buf(h2c, &h2c->mbuf); |
| 1133 | if (!res) { |
| 1134 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1135 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | if (!b_data(res)) { |
| 1140 | /* preface not yet sent */ |
| 1141 | b_istput(res, ist(H2_CONN_PREFACE)); |
| 1142 | } |
| 1143 | |
| 1144 | return h2c_send_settings(h2c); |
| 1145 | } |
| 1146 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1147 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 1148 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 1149 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 1150 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 1151 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 1152 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 1153 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 1154 | * case so that it is safe to use h2c_error() to report such errors. |
| 1155 | */ |
| 1156 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 1157 | { |
| 1158 | struct buffer *res; |
| 1159 | char str[17]; |
| 1160 | int ret; |
| 1161 | |
| 1162 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 1163 | return 1; // claim that it worked |
| 1164 | |
| 1165 | if (h2c_mux_busy(h2c, h2s)) { |
| 1166 | if (h2s) |
| 1167 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1168 | else |
| 1169 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1173 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1174 | if (!res) { |
| 1175 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1176 | if (h2s) |
| 1177 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1178 | else |
| 1179 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
| 1183 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 1184 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 1185 | |
| 1186 | if (h2c->last_sid < 0) |
| 1187 | h2c->last_sid = h2c->max_id; |
| 1188 | |
| 1189 | write_n32(str + 9, h2c->last_sid); |
| 1190 | write_n32(str + 13, h2c->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1191 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1192 | if (unlikely(ret <= 0)) { |
| 1193 | if (!ret) { |
| 1194 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1195 | if (h2s) |
| 1196 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1197 | else |
| 1198 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1199 | return 0; |
| 1200 | } |
| 1201 | else { |
| 1202 | /* we cannot report this error using GOAWAY, so we mark |
| 1203 | * it and claim a success. |
| 1204 | */ |
| 1205 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1206 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 1207 | return 1; |
| 1208 | } |
| 1209 | } |
| 1210 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 1211 | return ret; |
| 1212 | } |
| 1213 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1214 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 1215 | * during mux operations. This stream must be valid and cannot be closed |
| 1216 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 1217 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 1218 | * not yet. |
| 1219 | * |
| 1220 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1221 | * to write the message, it subscribes the stream to future notifications. |
| 1222 | */ |
| 1223 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1224 | { |
| 1225 | struct buffer *res; |
| 1226 | char str[13]; |
| 1227 | int ret; |
| 1228 | |
| 1229 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 1230 | return 1; |
| 1231 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1232 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1233 | * RST_STREAM in response to a RST_STREAM frame. |
| 1234 | */ |
| 1235 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1236 | ret = 1; |
| 1237 | goto ignore; |
| 1238 | } |
| 1239 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1240 | if (h2c_mux_busy(h2c, h2s)) { |
| 1241 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1245 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1246 | if (!res) { |
| 1247 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1248 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | /* len: 4, type: 3, flags: none */ |
| 1253 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1254 | write_n32(str + 5, h2s->id); |
| 1255 | write_n32(str + 9, h2s->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1256 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1257 | |
| 1258 | if (unlikely(ret <= 0)) { |
| 1259 | if (!ret) { |
| 1260 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1261 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1262 | return 0; |
| 1263 | } |
| 1264 | else { |
| 1265 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1266 | return 0; |
| 1267 | } |
| 1268 | } |
| 1269 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1270 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1271 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1272 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1273 | return ret; |
| 1274 | } |
| 1275 | |
| 1276 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 1277 | * 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] | 1278 | * error code, even if the stream is one of the dummy ones, and will update |
| 1279 | * h2s->st to H2_SS_CLOSED if it was not yet. |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1280 | * |
| 1281 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1282 | * 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] | 1283 | * 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] | 1284 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1285 | */ |
| 1286 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1287 | { |
| 1288 | struct buffer *res; |
| 1289 | char str[13]; |
| 1290 | int ret; |
| 1291 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1292 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1293 | * RST_STREAM in response to a RST_STREAM frame. |
| 1294 | */ |
| 1295 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1296 | ret = 1; |
| 1297 | goto ignore; |
| 1298 | } |
| 1299 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1300 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1301 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1302 | return 0; |
| 1303 | } |
| 1304 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1305 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1306 | if (!res) { |
| 1307 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1308 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1309 | return 0; |
| 1310 | } |
| 1311 | |
| 1312 | /* len: 4, type: 3, flags: none */ |
| 1313 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1314 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1315 | write_n32(str + 5, h2c->dsi); |
Willy Tarreau | e6888ff | 2018-12-23 18:26:26 +0100 | [diff] [blame] | 1316 | write_n32(str + 9, h2s->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1317 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1318 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1319 | if (unlikely(ret <= 0)) { |
| 1320 | if (!ret) { |
| 1321 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1322 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1323 | return 0; |
| 1324 | } |
| 1325 | else { |
| 1326 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1327 | return 0; |
| 1328 | } |
| 1329 | } |
| 1330 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1331 | ignore: |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1332 | if (h2s->id) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1333 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1334 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1335 | } |
| 1336 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1337 | return ret; |
| 1338 | } |
| 1339 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1340 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 1341 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 1342 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 1343 | * on success or zero if nothing was done. In case of lack of room to write the |
| 1344 | * message, it subscribes the requesting stream to future notifications. |
| 1345 | */ |
| 1346 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 1347 | { |
| 1348 | struct h2c *h2c = h2s->h2c; |
| 1349 | struct buffer *res; |
| 1350 | char str[9]; |
| 1351 | int ret; |
| 1352 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1353 | 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] | 1354 | return 1; |
| 1355 | |
| 1356 | if (h2c_mux_busy(h2c, h2s)) { |
| 1357 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1358 | return 0; |
| 1359 | } |
| 1360 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1361 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1362 | if (!res) { |
| 1363 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1364 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1365 | return 0; |
| 1366 | } |
| 1367 | |
| 1368 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1369 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1370 | write_n32(str + 5, h2s->id); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1371 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1372 | if (likely(ret > 0)) { |
| 1373 | h2s->flags |= H2_SF_ES_SENT; |
| 1374 | } |
| 1375 | else if (!ret) { |
| 1376 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1377 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1378 | return 0; |
| 1379 | } |
| 1380 | else { |
| 1381 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1382 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1383 | } |
| 1384 | return ret; |
| 1385 | } |
| 1386 | |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1387 | /* wake the streams attached to the connection, whose id is greater than <last>, |
| 1388 | * and assign their conn_stream the CS_FL_* flags <flags> in addition to |
Willy Tarreau | 2c096c3 | 2018-09-12 09:45:54 +0200 | [diff] [blame] | 1389 | * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection. |
| 1390 | * The stream's state is automatically updated accordingly. |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1391 | */ |
| 1392 | static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags) |
| 1393 | { |
| 1394 | struct eb32_node *node; |
| 1395 | struct h2s *h2s; |
| 1396 | |
| 1397 | if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR) |
Willy Tarreau | a851935 | 2018-12-18 16:44:28 +0100 | [diff] [blame] | 1398 | flags |= CS_FL_ERR_PENDING; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1399 | |
| 1400 | if (conn_xprt_read0_pending(h2c->conn)) |
Willy Tarreau | 2c096c3 | 2018-09-12 09:45:54 +0200 | [diff] [blame] | 1401 | flags |= CS_FL_REOS; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1402 | |
| 1403 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1404 | while (node) { |
| 1405 | h2s = container_of(node, struct h2s, by_id); |
| 1406 | if (h2s->id <= last) |
| 1407 | break; |
| 1408 | node = eb32_next(node); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1409 | |
| 1410 | if (!h2s->cs) { |
| 1411 | /* this stream was already orphaned */ |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 1412 | h2s_destroy(h2s); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1413 | continue; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1414 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1415 | |
| 1416 | h2s->cs->flags |= flags; |
Willy Tarreau | a851935 | 2018-12-18 16:44:28 +0100 | [diff] [blame] | 1417 | if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS)) |
| 1418 | h2s->cs->flags |= CS_FL_ERROR; |
| 1419 | |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 1420 | h2s_alert(h2s); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1421 | |
Willy Tarreau | a851935 | 2018-12-18 16:44:28 +0100 | [diff] [blame] | 1422 | if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR) |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1423 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | 2c096c3 | 2018-09-12 09:45:54 +0200 | [diff] [blame] | 1424 | else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN) |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1425 | h2s->st = H2_SS_HREM; |
Willy Tarreau | 2c096c3 | 2018-09-12 09:45:54 +0200 | [diff] [blame] | 1426 | else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1427 | h2s_close(h2s); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1431 | /* Increase all streams' outgoing window size by the difference passed in |
| 1432 | * argument. This is needed upon receipt of the settings frame if the initial |
| 1433 | * window size is different. The difference may be negative and the resulting |
| 1434 | * window size as well, for the time it takes to receive some window updates. |
| 1435 | */ |
| 1436 | static void h2c_update_all_ws(struct h2c *h2c, int diff) |
| 1437 | { |
| 1438 | struct h2s *h2s; |
| 1439 | struct eb32_node *node; |
| 1440 | |
| 1441 | if (!diff) |
| 1442 | return; |
| 1443 | |
| 1444 | node = eb32_first(&h2c->streams_by_id); |
| 1445 | while (node) { |
| 1446 | h2s = container_of(node, struct h2s, by_id); |
| 1447 | h2s->mws += diff; |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1448 | |
| 1449 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1450 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
| 1451 | if (h2s->send_wait) |
| 1452 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 1453 | |
| 1454 | } |
| 1455 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1456 | node = eb32_next(node); |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1461 | * 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] | 1462 | * return an error in h2c. The caller must have already verified frame length |
| 1463 | * and stream ID validity. Described in RFC7540#6.5. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1464 | */ |
| 1465 | static int h2c_handle_settings(struct h2c *h2c) |
| 1466 | { |
| 1467 | unsigned int offset; |
| 1468 | int error; |
| 1469 | |
| 1470 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1471 | if (h2c->dfl) { |
| 1472 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1473 | goto fail; |
| 1474 | } |
| 1475 | return 1; |
| 1476 | } |
| 1477 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1478 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1479 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1480 | return 0; |
| 1481 | |
| 1482 | /* parse the frame */ |
| 1483 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1484 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1485 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1486 | |
| 1487 | switch (type) { |
| 1488 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1489 | /* we need to update all existing streams with the |
| 1490 | * difference from the previous iws. |
| 1491 | */ |
| 1492 | if (arg < 0) { // RFC7540#6.5.2 |
| 1493 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1494 | goto fail; |
| 1495 | } |
| 1496 | h2c_update_all_ws(h2c, arg - h2c->miw); |
| 1497 | h2c->miw = arg; |
| 1498 | break; |
| 1499 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1500 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1501 | error = H2_ERR_PROTOCOL_ERROR; |
| 1502 | goto fail; |
| 1503 | } |
| 1504 | h2c->mfs = arg; |
| 1505 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1506 | case H2_SETTINGS_ENABLE_PUSH: |
| 1507 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1508 | error = H2_ERR_PROTOCOL_ERROR; |
| 1509 | goto fail; |
| 1510 | } |
| 1511 | break; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 1512 | case H2_SETTINGS_MAX_CONCURRENT_STREAMS: |
| 1513 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1514 | /* the limit is only for the backend; for the frontend it is our limit */ |
| 1515 | if ((unsigned int)arg > h2_settings_max_concurrent_streams) |
| 1516 | arg = h2_settings_max_concurrent_streams; |
| 1517 | h2c->streams_limit = arg; |
| 1518 | } |
| 1519 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | /* need to ACK this frame now */ |
| 1524 | h2c->st0 = H2_CS_FRAME_A; |
| 1525 | return 1; |
| 1526 | fail: |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1527 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1528 | h2c_error(h2c, error); |
| 1529 | return 0; |
| 1530 | } |
| 1531 | |
| 1532 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1533 | * success or one of the h2_status values. |
| 1534 | */ |
| 1535 | static int h2c_ack_settings(struct h2c *h2c) |
| 1536 | { |
| 1537 | struct buffer *res; |
| 1538 | char str[9]; |
| 1539 | int ret = -1; |
| 1540 | |
| 1541 | if (h2c_mux_busy(h2c, NULL)) { |
| 1542 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1543 | return 0; |
| 1544 | } |
| 1545 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1546 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1547 | if (!res) { |
| 1548 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1549 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1550 | return 0; |
| 1551 | } |
| 1552 | |
| 1553 | memcpy(str, |
| 1554 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1555 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1556 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1557 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1558 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1559 | if (unlikely(ret <= 0)) { |
| 1560 | if (!ret) { |
| 1561 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1562 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1563 | return 0; |
| 1564 | } |
| 1565 | else { |
| 1566 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1567 | return 0; |
| 1568 | } |
| 1569 | } |
| 1570 | return ret; |
| 1571 | } |
| 1572 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1573 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1574 | * 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] | 1575 | * missing data. The caller must have already verified frame length |
| 1576 | * and stream ID validity. |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1577 | */ |
| 1578 | static int h2c_handle_ping(struct h2c *h2c) |
| 1579 | { |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1580 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1581 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1582 | h2c->st0 = H2_CS_FRAME_A; |
| 1583 | return 1; |
| 1584 | } |
| 1585 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1586 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1587 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1588 | * error in h2c. |
| 1589 | */ |
| 1590 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1591 | { |
| 1592 | struct buffer *res; |
| 1593 | char str[13]; |
| 1594 | int ret = -1; |
| 1595 | |
| 1596 | if (h2c_mux_busy(h2c, NULL)) { |
| 1597 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1601 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1602 | if (!res) { |
| 1603 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1604 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1605 | return 0; |
| 1606 | } |
| 1607 | |
| 1608 | /* length: 4, type: 8, flags: none */ |
| 1609 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1610 | write_n32(str + 5, sid); |
| 1611 | write_n32(str + 9, increment); |
| 1612 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1613 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1614 | |
| 1615 | if (unlikely(ret <= 0)) { |
| 1616 | if (!ret) { |
| 1617 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1618 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1619 | return 0; |
| 1620 | } |
| 1621 | else { |
| 1622 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1623 | return 0; |
| 1624 | } |
| 1625 | } |
| 1626 | return ret; |
| 1627 | } |
| 1628 | |
| 1629 | /* try to send pending window update for the connection. It's safe to call it |
| 1630 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1631 | * failure. It may return an error in h2c. |
| 1632 | */ |
| 1633 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1634 | { |
| 1635 | int ret = 1; |
| 1636 | |
| 1637 | if (h2c->rcvd_c <= 0) |
| 1638 | return 1; |
| 1639 | |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 1640 | if (!(h2c->flags & H2_CF_WINDOW_OPENED)) { |
| 1641 | /* increase the advertised connection window to 2G on |
| 1642 | * first update. |
| 1643 | */ |
| 1644 | h2c->flags |= H2_CF_WINDOW_OPENED; |
| 1645 | h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT; |
| 1646 | } |
| 1647 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1648 | /* send WU for the connection */ |
| 1649 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1650 | if (ret > 0) |
| 1651 | h2c->rcvd_c = 0; |
| 1652 | |
| 1653 | return ret; |
| 1654 | } |
| 1655 | |
| 1656 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1657 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1658 | * room or failure. It may return an error in h2c. |
| 1659 | */ |
| 1660 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1661 | { |
| 1662 | int ret = 1; |
| 1663 | |
| 1664 | if (h2c->rcvd_s <= 0) |
| 1665 | return 1; |
| 1666 | |
| 1667 | /* send WU for the stream */ |
| 1668 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1669 | if (ret > 0) |
| 1670 | h2c->rcvd_s = 0; |
| 1671 | |
| 1672 | return ret; |
| 1673 | } |
| 1674 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1675 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1676 | * success, 0 on missing data or one of the h2_status values. |
| 1677 | */ |
| 1678 | static int h2c_ack_ping(struct h2c *h2c) |
| 1679 | { |
| 1680 | struct buffer *res; |
| 1681 | char str[17]; |
| 1682 | int ret = -1; |
| 1683 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1684 | if (b_data(&h2c->dbuf) < 8) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1685 | return 0; |
| 1686 | |
| 1687 | if (h2c_mux_busy(h2c, NULL)) { |
| 1688 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1689 | return 0; |
| 1690 | } |
| 1691 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1692 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1693 | if (!res) { |
| 1694 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1695 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1696 | return 0; |
| 1697 | } |
| 1698 | |
| 1699 | memcpy(str, |
| 1700 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1701 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1702 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1703 | |
| 1704 | /* copy the original payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1705 | h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1706 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1707 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1708 | if (unlikely(ret <= 0)) { |
| 1709 | if (!ret) { |
| 1710 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1711 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1712 | return 0; |
| 1713 | } |
| 1714 | else { |
| 1715 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1716 | return 0; |
| 1717 | } |
| 1718 | } |
| 1719 | return ret; |
| 1720 | } |
| 1721 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1722 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1723 | * 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] | 1724 | * h2c or h2s. The caller must have already verified frame length and stream ID |
| 1725 | * validity. Described in RFC7540#6.9. |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1726 | */ |
| 1727 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1728 | { |
| 1729 | int32_t inc; |
| 1730 | int error; |
| 1731 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1732 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1733 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1734 | return 0; |
| 1735 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1736 | inc = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1737 | |
| 1738 | if (h2c->dsi != 0) { |
| 1739 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1740 | |
| 1741 | /* it's not an error to receive WU on a closed stream */ |
| 1742 | if (h2s->st == H2_SS_CLOSED) |
| 1743 | return 1; |
| 1744 | |
| 1745 | if (!inc) { |
| 1746 | error = H2_ERR_PROTOCOL_ERROR; |
| 1747 | goto strm_err; |
| 1748 | } |
| 1749 | |
| 1750 | if (h2s->mws >= 0 && h2s->mws + inc < 0) { |
| 1751 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1752 | goto strm_err; |
| 1753 | } |
| 1754 | |
| 1755 | h2s->mws += inc; |
| 1756 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1757 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 1758 | if (h2s->send_wait) |
| 1759 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 1760 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1761 | } |
| 1762 | } |
| 1763 | else { |
| 1764 | /* connection window update */ |
| 1765 | if (!inc) { |
| 1766 | error = H2_ERR_PROTOCOL_ERROR; |
| 1767 | goto conn_err; |
| 1768 | } |
| 1769 | |
| 1770 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1771 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1772 | goto conn_err; |
| 1773 | } |
| 1774 | |
| 1775 | h2c->mws += inc; |
| 1776 | } |
| 1777 | |
| 1778 | return 1; |
| 1779 | |
| 1780 | conn_err: |
| 1781 | h2c_error(h2c, error); |
| 1782 | return 0; |
| 1783 | |
| 1784 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 1785 | h2s_error(h2s, error); |
| 1786 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1787 | return 0; |
| 1788 | } |
| 1789 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1790 | /* 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] | 1791 | * the last ID. Returns > 0 on success or zero on missing data. The caller must |
| 1792 | * have already verified frame length and stream ID validity. Described in |
| 1793 | * RFC7540#6.8. |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1794 | */ |
| 1795 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1796 | { |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1797 | int last; |
| 1798 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1799 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1800 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1801 | return 0; |
| 1802 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1803 | last = h2_get_n32(&h2c->dbuf, 0); |
| 1804 | h2c->errcode = h2_get_n32(&h2c->dbuf, 4); |
Olivier Houchard | 9117780 | 2018-12-19 14:49:39 +0100 | [diff] [blame] | 1805 | h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1806 | if (h2c->last_sid < 0) |
| 1807 | h2c->last_sid = last; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1808 | return 1; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1809 | } |
| 1810 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1811 | /* 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] | 1812 | * invalid. Returns > 0 on success or zero on missing data. It may return an |
| 1813 | * error in h2c. The caller must have already verified frame length and stream |
| 1814 | * ID validity. Described in RFC7540#6.3. |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1815 | */ |
| 1816 | static int h2c_handle_priority(struct h2c *h2c) |
| 1817 | { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1818 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1819 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1820 | return 0; |
| 1821 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1822 | if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1823 | /* 7540#5.3 : can't depend on itself */ |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1824 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1825 | return 0; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1826 | } |
| 1827 | return 1; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1828 | } |
| 1829 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1830 | /* 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] | 1831 | * Returns > 0 on success or zero on missing data. The caller must have already |
| 1832 | * verified frame length and stream ID validity. Described in RFC7540#6.4. |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1833 | */ |
| 1834 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1835 | { |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1836 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1837 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1838 | return 0; |
| 1839 | |
| 1840 | /* late RST, already handled */ |
| 1841 | if (h2s->st == H2_SS_CLOSED) |
| 1842 | return 1; |
| 1843 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1844 | h2s->errcode = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1845 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1846 | |
| 1847 | if (h2s->cs) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 1848 | cs_set_error(h2s->cs); |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 1849 | h2s_alert(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | h2s->flags |= H2_SF_RST_RCVD; |
| 1853 | return 1; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1854 | } |
| 1855 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1856 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 1857 | * It may return an error in h2c or h2s. The caller must consider that the |
| 1858 | * return value is the new h2s in case one was allocated (most common case). |
| 1859 | * Described in RFC7540#6.2. Most of the |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1860 | * errors here are reported as connection errors since it's impossible to |
| 1861 | * recover from such errors after the compression context has been altered. |
| 1862 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1863 | 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] | 1864 | { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1865 | struct buffer rxbuf = BUF_NULL; |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1866 | unsigned long long body_len = 0; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1867 | uint32_t flags = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1868 | int error; |
| 1869 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1870 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1871 | return NULL; // empty buffer |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1872 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1873 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1874 | return NULL; // incomplete frame |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1875 | |
| 1876 | /* now either the frame is complete or the buffer is complete */ |
| 1877 | if (h2s->st != H2_SS_IDLE) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1878 | /* The stream exists/existed, this must be a trailers frame */ |
| 1879 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1880 | if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0) |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1881 | goto out; |
| 1882 | goto done; |
| 1883 | } |
Willy Tarreau | 1f03550 | 2019-01-30 11:44:07 +0100 | [diff] [blame] | 1884 | /* the connection was already killed by an RST, let's consume |
| 1885 | * the data and send another RST. |
| 1886 | */ |
| 1887 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
| 1888 | h2s = (struct h2s*)h2_error_stream; |
| 1889 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1890 | } |
| 1891 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 1892 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 1893 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1894 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1895 | goto conn_err; |
| 1896 | } |
Willy Tarreau | 415b1ee | 2019-01-02 13:59:43 +0100 | [diff] [blame] | 1897 | else if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 1898 | goto out; // IDLE but too many cs still present |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1899 | |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1900 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1901 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1902 | /* unrecoverable error ? */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1903 | if (h2c->st0 >= H2_CS_ERROR) |
| 1904 | goto out; |
| 1905 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1906 | if (error <= 0) { |
| 1907 | if (error == 0) |
| 1908 | goto out; // missing data |
| 1909 | |
| 1910 | /* Failed to decode this stream (e.g. too large request) |
| 1911 | * but the HPACK decompressor is still synchronized. |
| 1912 | */ |
| 1913 | h2s = (struct h2s*)h2_error_stream; |
| 1914 | goto send_rst; |
| 1915 | } |
| 1916 | |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1917 | /* 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] | 1918 | * positively from h2c_frt_stream_new(), the stream will report the error, |
| 1919 | * 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] | 1920 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 1921 | h2s = h2c_frt_stream_new(h2c, h2c->dsi); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1922 | if (!h2s) { |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 1923 | h2s = (struct h2s*)h2_refused_stream; |
| 1924 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | h2s->st = H2_SS_OPEN; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1928 | h2s->rxbuf = rxbuf; |
| 1929 | h2s->flags |= flags; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 1930 | h2s->body_len = body_len; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1931 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1932 | done: |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1933 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1934 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1935 | |
| 1936 | if (h2s->flags & H2_SF_ES_RCVD) { |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 1937 | if (h2s->st == H2_SS_OPEN) |
| 1938 | h2s->st = H2_SS_HREM; |
| 1939 | else |
| 1940 | h2s_close(h2s); |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 1941 | if (h2s->cs) |
| 1942 | h2s->cs->flags |= CS_FL_REOS; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1943 | } |
| 1944 | |
Willy Tarreau | 3a429f0 | 2019-01-03 11:41:50 +0100 | [diff] [blame] | 1945 | /* update the max stream ID if the request is being processed */ |
| 1946 | if (h2s->id > h2c->max_id) |
| 1947 | h2c->max_id = h2s->id; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1948 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1949 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1950 | |
| 1951 | conn_err: |
| 1952 | h2c_error(h2c, error); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1953 | goto out; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1954 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1955 | out: |
| 1956 | h2_release_buf(h2c, &rxbuf); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1957 | return NULL; |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 1958 | |
| 1959 | send_rst: |
| 1960 | /* make the demux send an RST for the current stream. We may only |
| 1961 | * do this if we're certain that the HEADERS frame was properly |
| 1962 | * decompressed so that the HPACK decoder is still kept up to date. |
| 1963 | */ |
| 1964 | h2_release_buf(h2c, &rxbuf); |
| 1965 | h2c->st0 = H2_CS_FRAME_E; |
| 1966 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1967 | } |
| 1968 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 1969 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 1970 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 1971 | * errors here are reported as connection errors since it's impossible to |
| 1972 | * recover from such errors after the compression context has been altered. |
| 1973 | */ |
| 1974 | static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 1975 | { |
| 1976 | int error; |
| 1977 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 1978 | if (!b_size(&h2c->dbuf)) |
| 1979 | return NULL; // empty buffer |
| 1980 | |
| 1981 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
| 1982 | return NULL; // incomplete frame |
| 1983 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 1984 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 1985 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1986 | /* unrecoverable error ? */ |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 1987 | if (h2c->st0 >= H2_CS_ERROR) |
| 1988 | return NULL; |
| 1989 | |
Willy Tarreau | 08bb1d6 | 2019-01-30 16:55:48 +0100 | [diff] [blame] | 1990 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 1991 | /* RFC7540#5.1 */ |
| 1992 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 1993 | h2c->st0 = H2_CS_FRAME_E; |
| 1994 | return NULL; |
| 1995 | } |
| 1996 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1997 | if (error <= 0) { |
| 1998 | if (error == 0) |
| 1999 | return NULL; // missing data |
| 2000 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2001 | /* stream error : send RST_STREAM */ |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2002 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2003 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2004 | return NULL; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2005 | } |
| 2006 | |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2007 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 2008 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2009 | if (h2s->cs) |
| 2010 | h2s->cs->flags |= CS_FL_REOS; |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2011 | } |
| 2012 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2013 | 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] | 2014 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2015 | else if (h2s->cs && h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN) |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2016 | h2s->st = H2_SS_HREM; |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2017 | else if ((!h2s || h2s->cs->flags & CS_FL_REOS) && h2s->st == H2_SS_HLOC) |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2018 | h2s_close(h2s); |
| 2019 | |
| 2020 | return h2s; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2021 | } |
| 2022 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2023 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 2024 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 2025 | */ |
| 2026 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 2027 | { |
| 2028 | int error; |
| 2029 | |
| 2030 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 2031 | * to signal an end of stream (with the ES flag). |
| 2032 | */ |
| 2033 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2034 | if (!b_size(&h2c->dbuf) && h2c->dfl) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2035 | return 0; // empty buffer |
| 2036 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2037 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2038 | return 0; // incomplete frame |
| 2039 | |
| 2040 | /* now either the frame is complete or the buffer is complete */ |
| 2041 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2042 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2043 | /* RFC7540#6.1 */ |
| 2044 | error = H2_ERR_STREAM_CLOSED; |
| 2045 | goto strm_err; |
| 2046 | } |
| 2047 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2048 | if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) { |
| 2049 | /* RFC7540#8.1.2 */ |
| 2050 | error = H2_ERR_PROTOCOL_ERROR; |
| 2051 | goto strm_err; |
| 2052 | } |
| 2053 | |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 2054 | if (!h2_frt_transfer_data(h2s)) |
| 2055 | return 0; |
| 2056 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2057 | /* call the upper layers to process the frame, then let the upper layer |
| 2058 | * notify the stream about any change. |
| 2059 | */ |
| 2060 | if (!h2s->cs) { |
| 2061 | error = H2_ERR_STREAM_CLOSED; |
| 2062 | goto strm_err; |
| 2063 | } |
| 2064 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 2065 | if (h2c->st0 >= H2_CS_ERROR) |
| 2066 | return 0; |
| 2067 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2068 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2069 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2070 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | /* check for completion : the callee will change this to FRAME_A or |
| 2074 | * FRAME_H once done. |
| 2075 | */ |
| 2076 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2077 | return 0; |
| 2078 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2079 | /* last frame */ |
| 2080 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2081 | if (h2s->st == H2_SS_OPEN) |
| 2082 | h2s->st = H2_SS_HREM; |
| 2083 | else |
| 2084 | h2s_close(h2s); |
| 2085 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2086 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2087 | if (h2s->cs) |
| 2088 | h2s->cs->flags |= CS_FL_REOS; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2089 | |
| 2090 | if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) { |
| 2091 | /* RFC7540#8.1.2 */ |
| 2092 | error = H2_ERR_PROTOCOL_ERROR; |
| 2093 | goto strm_err; |
| 2094 | } |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2095 | } |
| 2096 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2097 | return 1; |
| 2098 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2099 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 2100 | h2s_error(h2s, error); |
| 2101 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2102 | return 0; |
| 2103 | } |
| 2104 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2105 | /* process Rx frames to be demultiplexed */ |
| 2106 | static void h2_process_demux(struct h2c *h2c) |
| 2107 | { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2108 | struct h2s *h2s = NULL, *tmp_h2s; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2109 | struct h2_fh hdr; |
| 2110 | unsigned int padlen = 0; |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 2111 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2112 | if (h2c->st0 >= H2_CS_ERROR) |
| 2113 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2114 | |
| 2115 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2116 | if (h2c->st0 == H2_CS_PREFACE) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2117 | if (h2c->flags & H2_CF_IS_BACK) |
| 2118 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2119 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 2120 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2121 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2122 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2123 | sess_log(h2c->conn->owner); |
| 2124 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2125 | goto fail; |
| 2126 | } |
| 2127 | |
| 2128 | h2c->max_id = 0; |
| 2129 | h2c->st0 = H2_CS_SETTINGS1; |
| 2130 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2131 | |
| 2132 | if (h2c->st0 == H2_CS_SETTINGS1) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2133 | /* ensure that what is pending is a valid SETTINGS frame |
| 2134 | * without an ACK. |
| 2135 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2136 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2137 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2138 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2139 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2140 | sess_log(h2c->conn->owner); |
| 2141 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2142 | goto fail; |
| 2143 | } |
| 2144 | |
| 2145 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 2146 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2147 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2148 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2149 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2150 | goto fail; |
| 2151 | } |
| 2152 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2153 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2154 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2155 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2156 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2157 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2158 | goto fail; |
| 2159 | } |
| 2160 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2161 | /* that's OK, switch to FRAME_P to process it. This is |
| 2162 | * a SETTINGS frame whose header has already been |
| 2163 | * deleted above. |
| 2164 | */ |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2165 | padlen = 0; |
| 2166 | goto new_frame; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2167 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2168 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2169 | |
| 2170 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2171 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2172 | int ret = 0; |
| 2173 | |
| 2174 | if (h2c->st0 >= H2_CS_ERROR) |
| 2175 | break; |
| 2176 | |
| 2177 | if (h2c->st0 == H2_CS_FRAME_H) { |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 2178 | if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2179 | break; |
| 2180 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2181 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2182 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2183 | if (!h2c->nb_streams) { |
| 2184 | /* only log if no other stream can report the error */ |
| 2185 | sess_log(h2c->conn->owner); |
| 2186 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2187 | break; |
| 2188 | } |
| 2189 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2190 | if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) { |
| 2191 | /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA), |
| 2192 | * we read the pad length and drop it from the remaining |
| 2193 | * payload (one byte + the 9 remaining ones = 10 total |
| 2194 | * removed), so we have a frame payload starting after the |
| 2195 | * pad len. Flow controlled frames (DATA) also count the |
| 2196 | * padlen in the flow control, so it must be adjusted. |
| 2197 | */ |
| 2198 | if (hdr.len < 1) { |
| 2199 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2200 | sess_log(h2c->conn->owner); |
| 2201 | goto fail; |
| 2202 | } |
| 2203 | hdr.len--; |
| 2204 | |
| 2205 | if (b_data(&h2c->dbuf) < 10) |
| 2206 | break; // missing padlen |
| 2207 | |
| 2208 | padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9); |
| 2209 | |
| 2210 | if (padlen > hdr.len) { |
| 2211 | /* RFC7540#6.1 : pad length = length of |
| 2212 | * frame payload or greater => error. |
| 2213 | */ |
| 2214 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2215 | sess_log(h2c->conn->owner); |
| 2216 | goto fail; |
| 2217 | } |
| 2218 | |
| 2219 | if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) { |
| 2220 | h2c->rcvd_c++; |
| 2221 | h2c->rcvd_s++; |
| 2222 | } |
| 2223 | b_del(&h2c->dbuf, 1); |
| 2224 | } |
| 2225 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2226 | |
| 2227 | new_frame: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2228 | h2c->dfl = hdr.len; |
| 2229 | h2c->dsi = hdr.sid; |
| 2230 | h2c->dft = hdr.ft; |
| 2231 | h2c->dff = hdr.ff; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2232 | h2c->dpl = padlen; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2233 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2234 | |
| 2235 | /* check for minimum basic frame format validity */ |
| 2236 | ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize); |
| 2237 | if (ret != H2_ERR_NO_ERROR) { |
| 2238 | h2c_error(h2c, ret); |
| 2239 | sess_log(h2c->conn->owner); |
| 2240 | goto fail; |
| 2241 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2245 | tmp_h2s = h2c_st_by_id(h2c, h2c->dsi); |
| 2246 | |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2247 | if (tmp_h2s != h2s && h2s && h2s->cs && |
| 2248 | (b_data(&h2s->rxbuf) || |
| 2249 | (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2250 | /* we may have to signal the upper layers */ |
| 2251 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2252 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2253 | } |
| 2254 | h2s = tmp_h2s; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2255 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 2256 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2257 | goto strm_err; |
| 2258 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2259 | if (h2s->st == H2_SS_IDLE && |
| 2260 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 2261 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 2262 | * this state MUST be treated as a connection error |
| 2263 | */ |
| 2264 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2265 | if (!h2c->nb_streams) { |
| 2266 | /* only log if no other stream can report the error */ |
| 2267 | sess_log(h2c->conn->owner); |
| 2268 | } |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2269 | break; |
| 2270 | } |
| 2271 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2272 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 2273 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 2274 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2275 | * this state MUST be treated as a stream error. |
| 2276 | * 6.2, 6.6 and 6.10 further mandate that HEADERS/ |
| 2277 | * PUSH_PROMISE/CONTINUATION cause connection errors. |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2278 | */ |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2279 | if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) |
| 2280 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2281 | else |
| 2282 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2283 | goto strm_err; |
| 2284 | } |
| 2285 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2286 | /* Below the management of frames received in closed state is a |
| 2287 | * bit hackish because the spec makes strong differences between |
| 2288 | * streams closed by receiving RST, sending RST, and seeing ES |
| 2289 | * in both directions. In addition to this, the creation of a |
| 2290 | * new stream reusing the identifier of a closed one will be |
| 2291 | * detected here. Given that we cannot keep track of all closed |
| 2292 | * streams forever, we consider that unknown closed streams were |
| 2293 | * closed on RST received, which allows us to respond with an |
| 2294 | * RST without breaking the connection (eg: to abort a transfer). |
| 2295 | * Some frames have to be silently ignored as well. |
| 2296 | */ |
| 2297 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
Willy Tarreau | 3ad5d31 | 2019-01-29 18:33:26 +0100 | [diff] [blame] | 2298 | 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] | 2299 | /* #5.1.1: The identifier of a newly |
| 2300 | * established stream MUST be numerically |
| 2301 | * greater than all streams that the initiating |
| 2302 | * endpoint has opened or reserved. This |
| 2303 | * governs streams that are opened using a |
| 2304 | * HEADERS frame and streams that are reserved |
| 2305 | * using PUSH_PROMISE. An endpoint that |
| 2306 | * receives an unexpected stream identifier |
| 2307 | * MUST respond with a connection error. |
| 2308 | */ |
| 2309 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2310 | goto strm_err; |
| 2311 | } |
| 2312 | |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2313 | 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] | 2314 | /* RFC7540#5.1:closed: an endpoint that |
| 2315 | * receives any frame other than PRIORITY after |
| 2316 | * receiving a RST_STREAM MUST treat that as a |
| 2317 | * stream error of type STREAM_CLOSED. |
| 2318 | * |
| 2319 | * Note that old streams fall into this category |
| 2320 | * and will lead to an RST being sent. |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2321 | * |
| 2322 | * However, we cannot generalize this to all frame types. Those |
| 2323 | * carrying compression state must still be processed before |
| 2324 | * being dropped or we'll desynchronize the decoder. This can |
| 2325 | * happen with request trailers received after sending an |
| 2326 | * RST_STREAM, or with header/trailers responses received after |
| 2327 | * sending RST_STREAM (aborted stream). |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2328 | */ |
| 2329 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2330 | h2c->st0 = H2_CS_FRAME_E; |
| 2331 | goto strm_err; |
| 2332 | } |
| 2333 | |
| 2334 | /* RFC7540#5.1:closed: if this state is reached as a |
| 2335 | * result of sending a RST_STREAM frame, the peer that |
| 2336 | * receives the RST_STREAM might have already sent |
| 2337 | * frames on the stream that cannot be withdrawn. An |
| 2338 | * endpoint MUST ignore frames that it receives on |
| 2339 | * closed streams after it has sent a RST_STREAM |
| 2340 | * frame. An endpoint MAY choose to limit the period |
| 2341 | * over which it ignores frames and treat frames that |
| 2342 | * arrive after this time as being in error. |
| 2343 | */ |
Willy Tarreau | 24ff1f8 | 2019-01-30 19:20:09 +0100 | [diff] [blame] | 2344 | if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2345 | /* RFC7540#5.1:closed: any frame other than |
| 2346 | * PRIO/WU/RST in this state MUST be treated as |
| 2347 | * a connection error |
| 2348 | */ |
| 2349 | if (h2c->dft != H2_FT_RST_STREAM && |
| 2350 | h2c->dft != H2_FT_PRIORITY && |
| 2351 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 2352 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2353 | goto strm_err; |
| 2354 | } |
| 2355 | } |
| 2356 | } |
| 2357 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2358 | #if 0 |
| 2359 | // problem below: it is not possible to completely ignore such |
| 2360 | // streams as we need to maintain the compression state as well |
| 2361 | // and for this we need to completely process these frames (eg: |
| 2362 | // HEADERS frames) as well as counting DATA frames to emit |
| 2363 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 2364 | // This is a typical case of layer violation where the |
| 2365 | // transported contents are critical to the connection's |
| 2366 | // validity and must be ignored at the same time :-( |
| 2367 | |
| 2368 | /* graceful shutdown, ignore streams whose ID is higher than |
| 2369 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 2370 | */ |
| 2371 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2372 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2373 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2374 | h2c->dfl -= ret; |
| 2375 | ret = h2c->dfl == 0; |
| 2376 | goto strm_err; |
| 2377 | } |
| 2378 | #endif |
| 2379 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2380 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 2381 | case H2_FT_SETTINGS: |
| 2382 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2383 | ret = h2c_handle_settings(h2c); |
| 2384 | |
| 2385 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2386 | ret = h2c_ack_settings(h2c); |
| 2387 | break; |
| 2388 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 2389 | case H2_FT_PING: |
| 2390 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2391 | ret = h2c_handle_ping(h2c); |
| 2392 | |
| 2393 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2394 | ret = h2c_ack_ping(h2c); |
| 2395 | break; |
| 2396 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 2397 | case H2_FT_WINDOW_UPDATE: |
| 2398 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2399 | ret = h2c_handle_window_update(h2c, h2s); |
| 2400 | break; |
| 2401 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2402 | case H2_FT_CONTINUATION: |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2403 | /* RFC7540#6.10: CONTINUATION may only be preceeded by |
| 2404 | * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These |
| 2405 | * frames' parsers consume all following CONTINUATION |
| 2406 | * frames so this one is out of sequence. |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2407 | */ |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2408 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2409 | sess_log(h2c->conn->owner); |
| 2410 | goto fail; |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2411 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2412 | case H2_FT_HEADERS: |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2413 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2414 | if (h2c->flags & H2_CF_IS_BACK) |
| 2415 | tmp_h2s = h2c_bck_handle_headers(h2c, h2s); |
| 2416 | else |
| 2417 | tmp_h2s = h2c_frt_handle_headers(h2c, h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2418 | if (tmp_h2s) { |
| 2419 | h2s = tmp_h2s; |
| 2420 | ret = 1; |
| 2421 | } |
| 2422 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2423 | break; |
| 2424 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2425 | case H2_FT_DATA: |
| 2426 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2427 | ret = h2c_frt_handle_data(h2c, h2s); |
| 2428 | |
| 2429 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2430 | ret = h2c_send_strm_wu(h2c); |
| 2431 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2432 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 2433 | case H2_FT_PRIORITY: |
| 2434 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2435 | ret = h2c_handle_priority(h2c); |
| 2436 | break; |
| 2437 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2438 | case H2_FT_RST_STREAM: |
| 2439 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2440 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 2441 | break; |
| 2442 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 2443 | case H2_FT_GOAWAY: |
| 2444 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2445 | ret = h2c_handle_goaway(h2c); |
| 2446 | break; |
| 2447 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2448 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2449 | default: |
| 2450 | /* drop frames that we ignore. They may be larger than |
| 2451 | * the buffer so we drain all of their contents until |
| 2452 | * we reach the end. |
| 2453 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2454 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2455 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2456 | h2c->dfl -= ret; |
| 2457 | ret = h2c->dfl == 0; |
| 2458 | } |
| 2459 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2460 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2461 | /* We may have to send an RST if not done yet */ |
| 2462 | if (h2s->st == H2_SS_ERROR) |
| 2463 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2464 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2465 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2466 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2467 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2468 | /* error or missing data condition met above ? */ |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2469 | if (ret <= 0) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2470 | break; |
| 2471 | |
| 2472 | if (h2c->st0 != H2_CS_FRAME_H) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2473 | b_del(&h2c->dbuf, h2c->dfl); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2474 | h2c->st0 = H2_CS_FRAME_H; |
| 2475 | } |
| 2476 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2477 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2478 | if (h2c->rcvd_c > 0 && |
| 2479 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2480 | h2c_send_conn_wu(h2c); |
| 2481 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2482 | fail: |
| 2483 | /* we can go here on missing data, blocked response or error */ |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2484 | if (h2s && h2s->cs && |
| 2485 | (b_data(&h2s->rxbuf) || |
| 2486 | (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2487 | /* we may have to signal the upper layers */ |
| 2488 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2489 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2490 | } |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2491 | |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 2492 | h2c_restart_reading(h2c); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2496 | * the end. |
| 2497 | */ |
| 2498 | static int h2_process_mux(struct h2c *h2c) |
| 2499 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2500 | struct h2s *h2s, *h2s_back; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2501 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2502 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2503 | if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) { |
| 2504 | if (unlikely(h2c_bck_send_preface(h2c) <= 0)) { |
| 2505 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2506 | if (h2c->st0 == H2_CS_ERROR) { |
| 2507 | h2c->st0 = H2_CS_ERROR2; |
| 2508 | sess_log(h2c->conn->owner); |
| 2509 | } |
| 2510 | goto fail; |
| 2511 | } |
| 2512 | h2c->st0 = H2_CS_SETTINGS1; |
| 2513 | } |
| 2514 | /* need to wait for the other side */ |
Willy Tarreau | 75a930a | 2018-12-12 08:03:58 +0100 | [diff] [blame] | 2515 | if (h2c->st0 < H2_CS_FRAME_H) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2516 | return 1; |
| 2517 | } |
| 2518 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2519 | /* start by sending possibly pending window updates */ |
| 2520 | if (h2c->rcvd_c > 0 && |
| 2521 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2522 | h2c_send_conn_wu(h2c) < 0) |
| 2523 | goto fail; |
| 2524 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2525 | /* First we always process the flow control list because the streams |
| 2526 | * waiting there were already elected for immediate emission but were |
| 2527 | * blocked just on this. |
| 2528 | */ |
| 2529 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2530 | list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2531 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2532 | h2c->st0 >= H2_CS_ERROR) |
| 2533 | break; |
| 2534 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2535 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2536 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2537 | h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2538 | tasklet_wakeup(h2s->send_wait->task); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2539 | LIST_DEL(&h2s->list); |
| 2540 | LIST_INIT(&h2s->list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 2541 | LIST_ADDQ(&h2c->sending_list, &h2s->list); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2542 | } |
| 2543 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2544 | list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2545 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2546 | break; |
| 2547 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2548 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2549 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2550 | h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2551 | tasklet_wakeup(h2s->send_wait->task); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2552 | LIST_DEL(&h2s->list); |
| 2553 | LIST_INIT(&h2s->list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 2554 | LIST_ADDQ(&h2c->sending_list, &h2s->list); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2555 | } |
| 2556 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2557 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2558 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2559 | if (h2c->st0 == H2_CS_ERROR) { |
| 2560 | if (h2c->max_id >= 0) { |
| 2561 | h2c_send_goaway_error(h2c, NULL); |
| 2562 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2563 | return 0; |
| 2564 | } |
| 2565 | |
| 2566 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2567 | } |
| 2568 | return 1; |
| 2569 | } |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2570 | return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2571 | } |
| 2572 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2573 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2574 | /* Attempt to read data, and subscribe if none available. |
| 2575 | * The function returns 1 if data has been received, otherwise zero. |
| 2576 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2577 | static int h2_recv(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2578 | { |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2579 | struct connection *conn = h2c->conn; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2580 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2581 | int max; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2582 | size_t ret; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2583 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2584 | if (h2c->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2585 | return (b_data(&h2c->dbuf)); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2586 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2587 | if (!h2_recv_allowed(h2c)) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2588 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2589 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2590 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2591 | if (!buf) { |
| 2592 | h2c->flags |= H2_CF_DEM_DALLOC; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2593 | return 0; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2594 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2595 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2596 | do { |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 2597 | b_realign_if_empty(buf); |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2598 | if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
| 2599 | /* HTX in use : try to pre-align the buffer like the |
| 2600 | * rxbufs will be to optimize memory copies. We'll make |
| 2601 | * sure that the frame header lands at the end of the |
| 2602 | * HTX block to alias it upon recv. We cannot use the |
| 2603 | * head because rcv_buf() will realign the buffer if |
| 2604 | * it's empty. Thus we cheat and pretend we already |
| 2605 | * have a few bytes there. |
| 2606 | */ |
| 2607 | max = buf_room_for_htx_data(buf) + 9; |
Willy Tarreau | c0960d1 | 2018-12-14 10:59:15 +0100 | [diff] [blame] | 2608 | buf->head = sizeof(struct htx) - 9; |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2609 | } |
| 2610 | else |
| 2611 | max = b_room(buf); |
| 2612 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2613 | if (max) |
| 2614 | ret = conn->xprt->rcv_buf(conn, buf, max, 0); |
| 2615 | else |
| 2616 | ret = 0; |
| 2617 | } while (ret > 0); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2618 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2619 | if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size)) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2620 | conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event); |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2621 | |
Olivier Houchard | a1411e6 | 2018-08-17 18:42:48 +0200 | [diff] [blame] | 2622 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2623 | h2_release_buf(h2c, &h2c->dbuf); |
Olivier Houchard | 4667773 | 2018-11-29 17:06:17 +0100 | [diff] [blame] | 2624 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2625 | } |
| 2626 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2627 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2628 | h2c->flags |= H2_CF_DEM_DFULL; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2629 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2630 | } |
| 2631 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2632 | /* Try to send data if possible. |
| 2633 | * The function returns 1 if data have been sent, otherwise zero. |
| 2634 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2635 | static int h2_send(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2636 | { |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2637 | struct connection *conn = h2c->conn; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2638 | int done; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2639 | int sent = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2640 | |
| 2641 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 2642 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2643 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2644 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2645 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2646 | /* a handshake was requested */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2647 | goto schedule; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2648 | } |
| 2649 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2650 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2651 | * pending streams into the existing buffer until it's reportedly full |
| 2652 | * or the end of send requests is reached. Then it tries to send this |
| 2653 | * buffer's contents out, marks it not full if at least one byte could |
| 2654 | * be sent, and tries again. |
| 2655 | * |
| 2656 | * The snd_buf() function normally takes a "flags" argument which may |
| 2657 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2658 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2659 | * connection is streaming lots of data (used to increase TLS record |
| 2660 | * size at the expense of latency). The former can be sent any time |
| 2661 | * there's a buffer full flag, as it indicates at least one stream |
| 2662 | * attempted to send and failed so there are pending data. An |
| 2663 | * alternative would be to set it as long as there's an active stream |
| 2664 | * but that would be problematic for ACKs until we have an absolute |
| 2665 | * guarantee that all waiters have at least one byte to send. The |
| 2666 | * latter should possibly not be set for now. |
| 2667 | */ |
| 2668 | |
| 2669 | done = 0; |
| 2670 | while (!done) { |
| 2671 | unsigned int flags = 0; |
| 2672 | |
| 2673 | /* fill as much as we can into the current buffer */ |
| 2674 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2675 | done = h2_process_mux(h2c); |
| 2676 | |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2677 | if (h2c->flags & H2_CF_MUX_MALLOC) |
| 2678 | break; |
| 2679 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2680 | if (conn->flags & CO_FL_ERROR) |
| 2681 | break; |
| 2682 | |
| 2683 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2684 | flags |= CO_SFL_MSG_MORE; |
| 2685 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2686 | if (b_data(&h2c->mbuf)) { |
| 2687 | 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] | 2688 | if (!ret) |
| 2689 | break; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2690 | sent = 1; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2691 | b_del(&h2c->mbuf, ret); |
| 2692 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2693 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2694 | |
| 2695 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2696 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2697 | } |
| 2698 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2699 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2700 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2701 | b_reset(&h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2702 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2703 | /* We're not full anymore, so we can wake any task that are waiting |
| 2704 | * for us. |
| 2705 | */ |
| 2706 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 2707 | while (!LIST_ISEMPTY(&h2c->send_list)) { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2708 | struct h2s *h2s = LIST_ELEM(h2c->send_list.n, |
| 2709 | struct h2s *, list); |
| 2710 | LIST_DEL(&h2s->list); |
| 2711 | LIST_INIT(&h2s->list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 2712 | LIST_ADDQ(&h2c->sending_list, &h2s->list); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2713 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2714 | h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2715 | tasklet_wakeup(h2s->send_wait->task); |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 2716 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2717 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2718 | /* We're done, no more to send */ |
| 2719 | if (!b_data(&h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2720 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2721 | schedule: |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2722 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
| 2723 | conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event); |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2724 | return sent; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2728 | { |
| 2729 | struct h2c *h2c = ctx; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2730 | int ret = 0; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2731 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2732 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2733 | ret = h2_send(h2c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2734 | if (!(h2c->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2735 | ret |= h2_recv(h2c); |
Willy Tarreau | cef5c8e | 2018-12-18 10:29:54 +0100 | [diff] [blame] | 2736 | if (ret || b_data(&h2c->dbuf)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2737 | h2_process(h2c); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2738 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2739 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2740 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2741 | /* callback called on any event by the connection handler. |
| 2742 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2743 | * destruction of the connection (which normally doesn not happen in h2). |
| 2744 | */ |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2745 | static int h2_process(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2746 | { |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2747 | struct connection *conn = h2c->conn; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2748 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2749 | if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2750 | h2_process_demux(h2c); |
| 2751 | |
| 2752 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2753 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2754 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2755 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2756 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 2757 | } |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2758 | h2_send(h2c); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2759 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 2760 | if (unlikely(h2c->proxy->state == PR_STSTOPPED)) { |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2761 | /* frontend is stopping, reload likely in progress, let's try |
| 2762 | * to announce a graceful shutdown if not yet done. We don't |
| 2763 | * care if it fails, it will be tried again later. |
| 2764 | */ |
| 2765 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 2766 | if (h2c->last_sid < 0) |
| 2767 | h2c->last_sid = (1U << 31) - 1; |
| 2768 | h2c_send_goaway_error(h2c, NULL); |
| 2769 | } |
| 2770 | } |
| 2771 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2772 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2773 | * If we received early data, and the handshake is done, wake |
| 2774 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2775 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2776 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 2777 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 2778 | struct eb32_node *node; |
| 2779 | struct h2s *h2s; |
| 2780 | |
| 2781 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 2782 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 2783 | |
| 2784 | while (node) { |
| 2785 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | fde287c | 2018-12-19 18:33:16 +0100 | [diff] [blame] | 2786 | if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2787 | h2s_notify_recv(h2s); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2788 | node = eb32_next(node); |
| 2789 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2790 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2791 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 2792 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 2793 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 2794 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 2795 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 2796 | h2_wake_some_streams(h2c, 0, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2797 | |
| 2798 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2799 | /* no more stream, kill the connection now */ |
| 2800 | h2_release(conn); |
| 2801 | return -1; |
| 2802 | } |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2803 | } |
| 2804 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2805 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2806 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2807 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2808 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 2809 | h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 2810 | (h2c->st0 != H2_CS_ERROR && |
| 2811 | !b_data(&h2c->mbuf) && |
| 2812 | (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && |
| 2813 | ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list)))) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2814 | h2_release_buf(h2c, &h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2815 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2816 | if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2817 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2818 | 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] | 2819 | task_queue(h2c->task); |
| 2820 | } |
| 2821 | else |
| 2822 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2823 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2824 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2825 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2826 | return 0; |
| 2827 | } |
| 2828 | |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2829 | static int h2_wake(struct connection *conn) |
| 2830 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2831 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2832 | |
| 2833 | return (h2_process(h2c)); |
| 2834 | } |
| 2835 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2836 | /* Connection timeout management. The principle is that if there's no receipt |
| 2837 | * nor sending for a certain amount of time, the connection is closed. If the |
| 2838 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 2839 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 2840 | * GOAWAY frame. |
| 2841 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2842 | 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] | 2843 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2844 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2845 | int expired = tick_is_expired(t->expire, now_ms); |
| 2846 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2847 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2848 | return t; |
| 2849 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2850 | task_delete(t); |
| 2851 | task_free(t); |
| 2852 | |
| 2853 | if (!h2c) { |
| 2854 | /* resources were already deleted */ |
| 2855 | return NULL; |
| 2856 | } |
| 2857 | |
| 2858 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2859 | h2c_error(h2c, H2_ERR_NO_ERROR); |
| 2860 | h2_wake_some_streams(h2c, 0, 0); |
| 2861 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2862 | if (b_data(&h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2863 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 2864 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2865 | } |
| 2866 | |
| 2867 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2868 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2869 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 2870 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2871 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2872 | if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) { |
| 2873 | 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] | 2874 | if (ret > 0) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2875 | b_del(&h2c->mbuf, ret); |
| 2876 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2877 | } |
| 2878 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2879 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2880 | /* either we can release everything now or it will be done later once |
| 2881 | * the last stream closes. |
| 2882 | */ |
| 2883 | if (eb_is_empty(&h2c->streams_by_id)) |
| 2884 | h2_release(h2c->conn); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2885 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2886 | return NULL; |
| 2887 | } |
| 2888 | |
| 2889 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2890 | /*******************************************/ |
| 2891 | /* functions below are used by the streams */ |
| 2892 | /*******************************************/ |
| 2893 | |
| 2894 | /* |
| 2895 | * Attach a new stream to a connection |
| 2896 | * (Used for outgoing connections) |
| 2897 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2898 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2899 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2900 | struct conn_stream *cs; |
| 2901 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2902 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2903 | |
| 2904 | cs = cs_new(conn); |
| 2905 | if (!cs) |
| 2906 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2907 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2908 | if (!h2s) { |
| 2909 | cs_free(cs); |
| 2910 | return NULL; |
| 2911 | } |
| 2912 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2913 | } |
| 2914 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 2915 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 2916 | * We have to scan because we may have some orphan streams. It might be |
| 2917 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 2918 | * orphans. |
| 2919 | */ |
| 2920 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 2921 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2922 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 2923 | struct h2s *h2s; |
| 2924 | struct eb32_node *node; |
| 2925 | |
| 2926 | node = eb32_first(&h2c->streams_by_id); |
| 2927 | while (node) { |
| 2928 | h2s = container_of(node, struct h2s, by_id); |
| 2929 | if (h2s->cs) |
| 2930 | return h2s->cs; |
| 2931 | node = eb32_next(node); |
| 2932 | } |
| 2933 | return NULL; |
| 2934 | } |
| 2935 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2936 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 2937 | * Destroy the mux and the associated connection, if it is no longer used |
| 2938 | */ |
| 2939 | static void h2_destroy(struct connection *conn) |
| 2940 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2941 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 2942 | |
| 2943 | if (eb_is_empty(&h2c->streams_by_id)) |
| 2944 | h2_release(h2c->conn); |
| 2945 | } |
| 2946 | |
| 2947 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2948 | * Detach the stream from the connection and possibly release the connection. |
| 2949 | */ |
| 2950 | static void h2_detach(struct conn_stream *cs) |
| 2951 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2952 | struct h2s *h2s = cs->ctx; |
| 2953 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2954 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2955 | |
| 2956 | cs->ctx = NULL; |
| 2957 | if (!h2s) |
| 2958 | return; |
| 2959 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2960 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2961 | h2c = h2s->h2c; |
| 2962 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 2963 | h2c->nb_cs--; |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 2964 | if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY && |
| 2965 | !h2_frt_has_too_many_cs(h2c)) { |
| 2966 | /* frontend connection was blocking new streams creation */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 2967 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 2968 | h2c_restart_reading(h2c); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 2969 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2970 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2971 | /* this stream may be blocked waiting for some data to leave (possibly |
| 2972 | * an ES or RST frame), so orphan it in this case. |
| 2973 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 2974 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 2975 | (h2c->st0 < H2_CS_ERROR) && |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 2976 | (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2977 | return; |
| 2978 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 2979 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 2980 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 2981 | /* unblock the connection if it was blocked on this |
| 2982 | * stream. |
| 2983 | */ |
| 2984 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 2985 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 2986 | h2c_restart_reading(h2c); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 2987 | } |
| 2988 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 2989 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2990 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2991 | if (h2c->flags & H2_CF_IS_BACK && |
| 2992 | (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2993 | if (!(h2c->conn->flags & |
| 2994 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 2995 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2996 | h2c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 2997 | if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) { |
| 2998 | h2c->conn->owner = NULL; |
| 2999 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3000 | if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn)) |
| 3001 | /* The server doesn't want it, let's kill the connection right away */ |
| 3002 | h2c->conn->mux->destroy(h2c->conn); |
| 3003 | return; |
| 3004 | } |
| 3005 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3006 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3007 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3008 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 3009 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 3010 | return; |
| 3011 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3012 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3013 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3014 | h2c->conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 3015 | if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3016 | struct server *srv = objt_server(h2c->conn->target); |
| 3017 | |
| 3018 | if (srv) { |
| 3019 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 3020 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 3021 | else |
| 3022 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 3023 | } |
| 3024 | |
| 3025 | } |
| 3026 | } |
| 3027 | } |
| 3028 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3029 | /* We don't want to close right now unless we're removing the |
| 3030 | * last stream, and either the connection is in error, or it |
| 3031 | * reached the ID already specified in a GOAWAY frame received |
| 3032 | * or sent (as seen by last_sid >= 0). |
| 3033 | */ |
| 3034 | if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */ |
| 3035 | ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */ |
Willy Tarreau | 42d55b9 | 2018-06-13 14:24:56 +0200 | [diff] [blame] | 3036 | (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */ |
Olivier Houchard | 93c8852 | 2018-11-30 15:39:16 +0100 | [diff] [blame] | 3037 | (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3038 | (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */ |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3039 | (conn_xprt_read0_pending(h2c->conn) || |
| 3040 | (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) { |
| 3041 | /* no more stream will come, kill it now */ |
| 3042 | h2_release(h2c->conn); |
| 3043 | } |
| 3044 | else if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3045 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3046 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3047 | task_queue(h2c->task); |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 3048 | } |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3049 | else |
| 3050 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3051 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3052 | } |
| 3053 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3054 | static void h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3055 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3056 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3057 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3058 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 3059 | 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] | 3060 | return; |
| 3061 | |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3062 | /* a connstream may require us to immediately kill the whole connection |
| 3063 | * for example because of a "tcp-request content reject" rule that is |
| 3064 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3065 | * close the connection. |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3066 | */ |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3067 | if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) && |
| 3068 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3069 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3070 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3071 | } |
| 3072 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3073 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3074 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3075 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3076 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3077 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3078 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3079 | h2s_close(h2s); |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3080 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3081 | return; |
| 3082 | add_to_list: |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3083 | if (LIST_ISEMPTY(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3084 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3085 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3086 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3087 | h2s->send_wait = sw; |
| 3088 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3089 | h2s->send_wait = sw; |
| 3090 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3091 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3092 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3093 | /* Let the handler know we want shutr */ |
| 3094 | sw->handle = (void *)((long)sw->handle | 1); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3095 | } |
| 3096 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3097 | static void h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3098 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3099 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3100 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3101 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 3102 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3103 | return; |
| 3104 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3105 | if (h2s->flags & H2_SF_HEADERS_SENT) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3106 | /* we can cleanly close using an empty data frame only after headers */ |
| 3107 | |
| 3108 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 3109 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3110 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3111 | |
| 3112 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3113 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3114 | else |
| 3115 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3116 | } else { |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3117 | /* a connstream may require us to immediately kill the whole connection |
| 3118 | * for example because of a "tcp-request content reject" rule that is |
| 3119 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3120 | * close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3121 | */ |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3122 | if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) && |
| 3123 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3124 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3125 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3126 | } |
| 3127 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3128 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3129 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3130 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3131 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3132 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3133 | } |
| 3134 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3135 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3136 | tasklet_wakeup(h2c->wait_event.task); |
| 3137 | return; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3138 | |
| 3139 | add_to_list: |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3140 | if (LIST_ISEMPTY(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3141 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3142 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3143 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3144 | h2s->send_wait = sw; |
| 3145 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3146 | h2s->send_wait = sw; |
| 3147 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3148 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3149 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3150 | /* let the handler know we want to shutw */ |
| 3151 | sw->handle = (void *)((long)(sw->handle) | 2); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3155 | { |
| 3156 | struct h2s *h2s = ctx; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3157 | long reason = (long)h2s->wait_event.handle; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3158 | |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3159 | if (h2s->send_wait) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3160 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3161 | h2s->send_wait = NULL; |
| 3162 | LIST_DEL(&h2s->list); |
| 3163 | LIST_INIT(&h2s->list); |
| 3164 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3165 | if (reason & 2) |
| 3166 | h2_do_shutw(h2s); |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3167 | if (reason & 1) |
| 3168 | h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3169 | |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3170 | if (h2s->st == H2_SS_CLOSED && |
Olivier Houchard | ffda58b | 2018-12-16 01:29:11 +0100 | [diff] [blame] | 3171 | !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs) |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3172 | h2s_destroy(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3173 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3174 | } |
| 3175 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3176 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3177 | { |
| 3178 | struct h2s *h2s = cs->ctx; |
| 3179 | |
| 3180 | if (!mode) |
| 3181 | return; |
| 3182 | |
| 3183 | h2_do_shutr(h2s); |
| 3184 | } |
| 3185 | |
| 3186 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3187 | { |
| 3188 | struct h2s *h2s = cs->ctx; |
| 3189 | |
| 3190 | h2_do_shutw(h2s); |
| 3191 | } |
| 3192 | |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3193 | /* 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] | 3194 | * HTX request or response depending on the connection's side. Returns a |
| 3195 | * positive value on success, a negative value on failure, or 0 if it couldn't |
| 3196 | * proceed. May report connection errors in h2c->errcode if the frame is |
| 3197 | * non-decodable and the connection unrecoverable. In absence of connection |
| 3198 | * 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] | 3199 | * |
| 3200 | * The function may fold CONTINUATION frames into the initial HEADERS frame |
| 3201 | * by removing padding and next frame header, then moving the CONTINUATION |
| 3202 | * frame's payload and adjusting h2c->dfl to match the new aggregated frame, |
| 3203 | * leaving a hole between the main frame and the beginning of the next one. |
| 3204 | * The possibly remaining incomplete or next frame at the end may be moved |
| 3205 | * if the aggregated frame is not deleted, in order to fill the hole. Wrapped |
| 3206 | * HEADERS frames are unwrapped into a temporary buffer before decoding. |
| 3207 | * |
| 3208 | * A buffer at the beginning of processing may look like this : |
| 3209 | * |
| 3210 | * ,---.---------.-----.--------------.--------------.------.---. |
| 3211 | * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///| |
| 3212 | * `---^---------^-----^--------------^--------------^------^---' |
| 3213 | * | | <-----> | | |
| 3214 | * area | dpl | wrap |
| 3215 | * |<--------------> | |
| 3216 | * | dfl | |
| 3217 | * |<-------------------------------------------------->| |
| 3218 | * head data |
| 3219 | * |
| 3220 | * Padding is automatically overwritten when folding, participating to the |
| 3221 | * hole size after dfl : |
| 3222 | * |
| 3223 | * ,---.------------------------.-----.--------------.------.---. |
| 3224 | * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///| |
| 3225 | * `---^------------------------^-----^--------------^------^---' |
| 3226 | * | | <-----> | | |
| 3227 | * area | hole | wrap |
| 3228 | * |<-----------------------> | |
| 3229 | * | dfl | |
| 3230 | * |<-------------------------------------------------->| |
| 3231 | * head data |
| 3232 | * |
| 3233 | * Please note that the HEADERS frame is always deprived from its PADLEN byte |
| 3234 | * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY |
| 3235 | * bit. |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3236 | * |
| 3237 | * The <flags> field must point to either the stream's flags or to a copy of it |
| 3238 | * so that the function can update the following flags : |
| 3239 | * - H2_SF_DATA_CLEN when content-length is seen |
| 3240 | * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion |
| 3241 | * - H2_SF_HEADERS_RCVD once the frame is successfully decoded |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3242 | * |
| 3243 | * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to |
| 3244 | * decoding, in order to detect if we're dealing with a headers or a trailers |
| 3245 | * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3246 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3247 | 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] | 3248 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3249 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3250 | struct buffer *tmp = get_trash_chunk(); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3251 | struct http_hdr list[MAX_HTTP_HDR * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3252 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3253 | unsigned int msgf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3254 | struct htx *htx = NULL; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3255 | int flen; // header frame len |
| 3256 | int hole = 0; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3257 | int ret = 0; |
| 3258 | int outlen; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3259 | int wrap; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3260 | int try = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3261 | |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3262 | next_frame: |
| 3263 | if (b_data(&h2c->dbuf) - hole < h2c->dfl) |
| 3264 | goto leave; // incomplete input frame |
| 3265 | |
| 3266 | /* No END_HEADERS means there's one or more CONTINUATION frames. In |
| 3267 | * this case, we'll try to paste it immediately after the initial |
| 3268 | * HEADERS frame payload and kill any possible padding. The initial |
| 3269 | * frame's length will be increased to represent the concatenation |
| 3270 | * of the two frames. The next frame is read from position <tlen> |
| 3271 | * and written at position <flen> (minus padding if some is present). |
| 3272 | */ |
| 3273 | if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) { |
| 3274 | struct h2_fh hdr; |
| 3275 | int clen; // CONTINUATION frame's payload length |
| 3276 | |
| 3277 | if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) { |
| 3278 | /* no more data, the buffer may be full, either due to |
| 3279 | * too large a frame or because of too large a hole that |
| 3280 | * we're going to compact at the end. |
| 3281 | */ |
| 3282 | goto leave; |
| 3283 | } |
| 3284 | |
| 3285 | if (hdr.ft != H2_FT_CONTINUATION) { |
| 3286 | /* RFC7540#6.10: frame of unexpected type */ |
| 3287 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3288 | goto fail; |
| 3289 | } |
| 3290 | |
| 3291 | if (hdr.sid != h2c->dsi) { |
| 3292 | /* RFC7540#6.10: frame of different stream */ |
| 3293 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3294 | goto fail; |
| 3295 | } |
| 3296 | |
| 3297 | if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { |
| 3298 | /* RFC7540#4.2: invalid frame length */ |
| 3299 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3300 | goto fail; |
| 3301 | } |
| 3302 | |
| 3303 | /* detect when we must stop aggragating frames */ |
| 3304 | h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS; |
| 3305 | |
| 3306 | /* Take as much as we can of the CONTINUATION frame's payload */ |
| 3307 | clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9); |
| 3308 | if (clen > hdr.len) |
| 3309 | clen = hdr.len; |
| 3310 | |
| 3311 | /* Move the frame's payload over the padding, hole and frame |
| 3312 | * header. At least one of hole or dpl is null (see diagrams |
| 3313 | * above). The hole moves after the new aggragated frame. |
| 3314 | */ |
| 3315 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9)); |
| 3316 | h2c->dfl += clen - h2c->dpl; |
| 3317 | hole += h2c->dpl + 9; |
| 3318 | h2c->dpl = 0; |
| 3319 | goto next_frame; |
| 3320 | } |
| 3321 | |
| 3322 | flen = h2c->dfl - h2c->dpl; |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3323 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3324 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3325 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3326 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3327 | copy = alloc_trash_chunk(); |
| 3328 | if (!copy) { |
| 3329 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3330 | goto fail; |
| 3331 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3332 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3333 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3334 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3335 | } |
| 3336 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3337 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3338 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3339 | if (read_n32(hdrs) == h2c->dsi) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3340 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3341 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3342 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3343 | } |
| 3344 | |
Willy Tarreau | a01f45e | 2018-12-31 07:41:24 +0100 | [diff] [blame] | 3345 | if (flen < 5) { |
| 3346 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3347 | goto fail; |
| 3348 | } |
| 3349 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3350 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3351 | flen -= 5; |
| 3352 | } |
| 3353 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3354 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3355 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3356 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3357 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3358 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3359 | /* we can't retry a failed decompression operation so we must be very |
| 3360 | * careful not to take any risks. In practice the output buffer is |
| 3361 | * always empty except maybe for trailers, in which case we simply have |
| 3362 | * to wait for the upper layer to finish consuming what is available. |
| 3363 | */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3364 | |
| 3365 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3366 | htx = htx_from_buf(rxbuf); |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3367 | if (!htx_is_empty(htx)) { |
| 3368 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3369 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3370 | } |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3371 | } else { |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3372 | if (b_data(rxbuf)) { |
| 3373 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3374 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3375 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3376 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3377 | rxbuf->head = 0; |
| 3378 | try = b_size(rxbuf); |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3379 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3380 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3381 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3382 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3383 | sizeof(list)/sizeof(list[0]), tmp); |
| 3384 | if (outlen < 0) { |
| 3385 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3386 | goto fail; |
| 3387 | } |
| 3388 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3389 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3390 | * the parser's state to commit these changes and allow us to later |
| 3391 | * fail solely on the stream if needed. |
| 3392 | */ |
| 3393 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3394 | h2c->dfl = hole = 0; |
| 3395 | h2c->st0 = H2_CS_FRAME_H; |
| 3396 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3397 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3398 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3399 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3400 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3401 | goto trailers; |
| 3402 | |
| 3403 | /* This is the first HEADERS frame so it's a headers block */ |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3404 | if (htx) { |
| 3405 | /* HTX mode */ |
| 3406 | if (h2c->flags & H2_CF_IS_BACK) |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3407 | outlen = h2_make_htx_response(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3408 | else |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3409 | outlen = h2_make_htx_request(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3410 | } else { |
| 3411 | /* HTTP/1 mode */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3412 | 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] | 3413 | if (outlen > 0) |
| 3414 | b_add(rxbuf, outlen); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3415 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3416 | |
| 3417 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3418 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3419 | goto fail; |
| 3420 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3421 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3422 | if (msgf & H2_MSGF_BODY) { |
| 3423 | /* a payload is present */ |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3424 | if (msgf & H2_MSGF_BODY_CL) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3425 | *flags |= H2_SF_DATA_CLEN; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3426 | if (htx) |
| 3427 | htx->extra = *body_len; |
| 3428 | } |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3429 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3430 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3431 | } |
| 3432 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3433 | done: |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3434 | /* indicate that a HEADERS frame was received for this stream, except |
| 3435 | * for 1xx responses. For 1xx responses, another HEADERS frame is |
| 3436 | * expected. |
| 3437 | */ |
| 3438 | if (!(msgf & H2_MSGF_RSP_1XX)) |
| 3439 | *flags |= H2_SF_HEADERS_RCVD; |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3440 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3441 | 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] | 3442 | /* Mark the end of message, either using EOM in HTX or with the |
| 3443 | * trailing CRLF after the end of trailers. Note that DATA_CHNK |
| 3444 | * is not set during headers with END_STREAM. |
| 3445 | */ |
| 3446 | if (htx) { |
| 3447 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 3448 | goto fail; |
| 3449 | } |
| 3450 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3451 | if (!b_putblk(rxbuf, "\r\n", 2)) |
| 3452 | goto fail; |
| 3453 | } |
| 3454 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3455 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3456 | /* success */ |
| 3457 | ret = 1; |
| 3458 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3459 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3460 | /* 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] | 3461 | * move the remaining data over it. |
| 3462 | */ |
| 3463 | if (hole) { |
| 3464 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3465 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3466 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3467 | b_sub(&h2c->dbuf, hole); |
| 3468 | } |
| 3469 | |
| 3470 | if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) { |
| 3471 | /* too large frames */ |
| 3472 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3473 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3474 | } |
| 3475 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3476 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3477 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3478 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3479 | return ret; |
| 3480 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3481 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3482 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3483 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3484 | |
| 3485 | trailers: |
| 3486 | /* This is the last HEADERS frame hence a trailer */ |
| 3487 | |
| 3488 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3489 | /* It's a trailer but it's missing ES flag */ |
| 3490 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3491 | goto fail; |
| 3492 | } |
| 3493 | |
| 3494 | /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD |
| 3495 | * block, and when using chunks we must send the 0 CRLF marker. For |
| 3496 | * other modes, the trailers are silently dropped. |
| 3497 | */ |
| 3498 | if (htx) { |
| 3499 | if (!htx_add_endof(htx, HTX_BLK_EOD)) |
| 3500 | goto fail; |
Willy Tarreau | 5255f28 | 2019-01-03 18:41:05 +0100 | [diff] [blame] | 3501 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3502 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3503 | } |
| 3504 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3505 | /* Legacy mode with chunked encoding : we must finalize the |
| 3506 | * data block message emit the trailing CRLF */ |
| 3507 | if (!b_putblk(rxbuf, "0\r\n", 3)) |
| 3508 | goto fail; |
Willy Tarreau | e2b05cc | 2019-01-03 16:18:34 +0100 | [diff] [blame] | 3509 | |
| 3510 | outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try); |
| 3511 | if (outlen > 0) |
| 3512 | b_add(rxbuf, outlen); |
| 3513 | else |
| 3514 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3518 | } |
| 3519 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3520 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 3521 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 3522 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 3523 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 3524 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 3525 | * 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] | 3526 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3527 | * send the current frame, 0 if it couldn't complete, in which case |
| 3528 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3529 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3530 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3531 | * have checked the frame header and ensured that the frame was complete or the |
| 3532 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3533 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3534 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3535 | { |
| 3536 | struct h2c *h2c = h2s->h2c; |
| 3537 | int block1, block2; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3538 | unsigned int flen = 0; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3539 | unsigned int chklen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3540 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3541 | struct buffer *csbuf; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3542 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3543 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3544 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3545 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3546 | if (!csbuf) { |
| 3547 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3548 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3549 | } |
| 3550 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3551 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3552 | flen = h2c->dfl - h2c->dpl; |
Olivier Houchard | 2f30883 | 2018-12-19 15:53:53 +0100 | [diff] [blame] | 3553 | if (h2c->proxy->options2 & PR_O2_USE_HTX) |
| 3554 | htx = htx_from_buf(csbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3555 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3556 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3557 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3558 | if (flen > b_data(&h2c->dbuf)) { |
| 3559 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3560 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3561 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3562 | } |
| 3563 | |
Willy Tarreau | a9b7796 | 2019-01-31 07:23:00 +0100 | [diff] [blame] | 3564 | if (htx) { |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3565 | block1 = htx_free_data_space(htx); |
| 3566 | if (!block1) { |
| 3567 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3568 | goto fail; |
| 3569 | } |
| 3570 | if (flen > block1) |
| 3571 | flen = block1; |
| 3572 | |
| 3573 | /* here, flen is the max we can copy into the output buffer */ |
| 3574 | block1 = b_contig_data(&h2c->dbuf, 0); |
| 3575 | if (flen > block1) |
| 3576 | flen = block1; |
| 3577 | |
| 3578 | if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) { |
| 3579 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3580 | goto fail; |
| 3581 | } |
| 3582 | |
| 3583 | b_del(&h2c->dbuf, flen); |
| 3584 | h2c->dfl -= flen; |
| 3585 | h2c->rcvd_c += flen; |
| 3586 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3587 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3588 | if (h2s->flags & H2_SF_DATA_CLEN) { |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3589 | h2s->body_len -= flen; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3590 | htx->extra = h2s->body_len; |
| 3591 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3592 | goto try_again; |
| 3593 | } |
| 3594 | else if (unlikely(b_space_wraps(csbuf))) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3595 | /* it doesn't fit and the buffer is fragmented, |
| 3596 | * so let's defragment it and try again. |
| 3597 | */ |
| 3598 | b_slow_realign(csbuf, trash.area, 0); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3599 | } |
| 3600 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3601 | /* chunked-encoding requires more room */ |
| 3602 | if (h2s->flags & H2_SF_DATA_CHNK) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3603 | chklen = MIN(flen, b_room(csbuf)); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3604 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 3605 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 3606 | (chklen < 1048576) ? 4 : 8; |
| 3607 | chklen += 4; // CRLF, CRLF |
| 3608 | } |
| 3609 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3610 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3611 | if (flen + chklen > b_room(csbuf)) { |
| 3612 | if (chklen >= b_room(csbuf)) { |
| 3613 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3614 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3615 | } |
| 3616 | flen = b_room(csbuf) - chklen; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3617 | } |
| 3618 | |
| 3619 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3620 | /* emit the chunk size */ |
| 3621 | unsigned int chksz = flen; |
| 3622 | char str[10]; |
| 3623 | char *beg; |
| 3624 | |
| 3625 | beg = str + sizeof(str); |
| 3626 | *--beg = '\n'; |
| 3627 | *--beg = '\r'; |
| 3628 | do { |
| 3629 | *--beg = hextab[chksz & 0xF]; |
| 3630 | } while (chksz >>= 4); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3631 | b_putblk(csbuf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3632 | } |
| 3633 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3634 | /* Block1 is the length of the first block before the buffer wraps, |
| 3635 | * block2 is the optional second block to reach the end of the frame. |
| 3636 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3637 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3638 | if (block1 > flen) |
| 3639 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3640 | block2 = flen - block1; |
| 3641 | |
| 3642 | if (block1) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3643 | b_putblk(csbuf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3644 | |
| 3645 | if (block2) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3646 | b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3647 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3648 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3649 | /* emit the CRLF */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3650 | b_putblk(csbuf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3651 | } |
| 3652 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3653 | /* now mark the input data as consumed (will be deleted from the buffer |
| 3654 | * by the caller when seeing FRAME_A after sending the window update). |
| 3655 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3656 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3657 | h2c->dfl -= flen; |
| 3658 | h2c->rcvd_c += flen; |
| 3659 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 3660 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3661 | if (h2s->flags & H2_SF_DATA_CLEN) |
| 3662 | h2s->body_len -= flen; |
| 3663 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3664 | if (h2c->dfl > h2c->dpl) { |
| 3665 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3666 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3667 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3668 | } |
| 3669 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3670 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3671 | /* here we're done with the frame, all the payload (except padding) was |
| 3672 | * transferred. |
| 3673 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3674 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3675 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 3676 | if (htx) { |
| 3677 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3678 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3679 | goto fail; |
| 3680 | } |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3681 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3682 | else if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3683 | /* emit the trailing 0 CRLF CRLF */ |
| 3684 | if (b_room(csbuf) < 5) { |
| 3685 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3686 | goto fail; |
| 3687 | } |
| 3688 | chklen += 5; |
| 3689 | b_putblk(csbuf, "0\r\n\r\n", 5); |
| 3690 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3691 | } |
| 3692 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 3693 | h2c->rcvd_c += h2c->dpl; |
| 3694 | h2c->rcvd_s += h2c->dpl; |
| 3695 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3696 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
| 3697 | |
Willy Tarreau | 39d6850 | 2018-03-02 12:26:37 +0100 | [diff] [blame] | 3698 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3699 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 3700 | if (h2s->cs) |
| 3701 | h2s->cs->flags |= CS_FL_REOS; |
Willy Tarreau | 39d6850 | 2018-03-02 12:26:37 +0100 | [diff] [blame] | 3702 | } |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3703 | if (htx) |
| 3704 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3705 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3706 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3707 | if (htx) |
| 3708 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3709 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3710 | } |
| 3711 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3712 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 3713 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 3714 | * number of bytes sent. The caller must check the stream's status to detect |
| 3715 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3716 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3717 | 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] | 3718 | { |
| 3719 | struct http_hdr list[MAX_HTTP_HDR]; |
| 3720 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3721 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3722 | struct buffer outbuf; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3723 | union h1_sl sl; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3724 | int es_now = 0; |
| 3725 | int ret = 0; |
| 3726 | int hdr; |
| 3727 | |
| 3728 | if (h2c_mux_busy(h2c, h2s)) { |
| 3729 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3730 | return 0; |
| 3731 | } |
| 3732 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3733 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3734 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3735 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3736 | return 0; |
| 3737 | } |
| 3738 | |
| 3739 | /* First, try to parse the H1 response and index it into <list>. |
| 3740 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 3741 | * block does not wrap and we can safely read it this way without |
| 3742 | * having to realign the buffer. |
| 3743 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3744 | 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] | 3745 | list, sizeof(list)/sizeof(list[0]), h1m, &sl); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3746 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 3747 | /* incomplete or invalid response, this is abnormal coming from |
| 3748 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 3749 | * so that won't be fixed, raise an error now. |
| 3750 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3751 | * FIXME: we should instead add the ability to only return a |
| 3752 | * 502 bad gateway. But in theory this is not supposed to |
| 3753 | * happen. |
| 3754 | */ |
| 3755 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3756 | ret = 0; |
| 3757 | goto end; |
| 3758 | } |
| 3759 | |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3760 | h2s->status = sl.st.status; |
Willy Tarreau | db72da0 | 2018-09-13 11:52:20 +0200 | [diff] [blame] | 3761 | |
| 3762 | /* certain statuses have no body or an empty one, regardless of |
| 3763 | * what the headers say. |
| 3764 | */ |
| 3765 | if (sl.st.status >= 100 && sl.st.status < 200) { |
| 3766 | h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK); |
| 3767 | h1m->curr_len = h1m->body_len = 0; |
| 3768 | } |
| 3769 | else if (sl.st.status == 204 || sl.st.status == 304) { |
| 3770 | /* no contents, claim c-len is present and set to zero */ |
| 3771 | h1m->flags &= ~H1_MF_CHNK; |
| 3772 | h1m->flags |= H1_MF_CLEN; |
| 3773 | h1m->curr_len = h1m->body_len = 0; |
| 3774 | } |
| 3775 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3776 | chunk_reset(&outbuf); |
| 3777 | |
| 3778 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3779 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3780 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3781 | outbuf.data = 0; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3782 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3783 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3784 | break; |
| 3785 | realign_again: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3786 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3787 | } |
| 3788 | |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3789 | if (outbuf.size < 9) |
| 3790 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3791 | |
| 3792 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3793 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 3794 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3795 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3796 | |
| 3797 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3798 | if (unlikely(list[0].v.len != 3)) { |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 3799 | /* this is an unparsable response */ |
| 3800 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3801 | ret = 0; |
| 3802 | goto end; |
| 3803 | } |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3804 | |
| 3805 | if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3806 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3807 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3808 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3809 | } |
| 3810 | |
| 3811 | /* encode all headers, stop at empty name */ |
| 3812 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 3813 | /* these ones do not exist in H2 and must be dropped. */ |
| 3814 | if (isteq(list[hdr].n, ist("connection")) || |
| 3815 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 3816 | isteq(list[hdr].n, ist("keep-alive")) || |
| 3817 | isteq(list[hdr].n, ist("upgrade")) || |
| 3818 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3819 | continue; |
| 3820 | |
| 3821 | if (isteq(list[hdr].n, ist(""))) |
| 3822 | break; // end |
| 3823 | |
| 3824 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 3825 | /* output full */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3826 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3827 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3828 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3829 | } |
| 3830 | } |
| 3831 | |
| 3832 | /* we may need to add END_STREAM */ |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 3833 | 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] | 3834 | es_now = 1; |
| 3835 | |
| 3836 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3837 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3838 | |
| 3839 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3840 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3841 | |
| 3842 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3843 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3844 | |
| 3845 | /* commit the H2 response */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3846 | b_add(&h2c->mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3847 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3848 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3849 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3850 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3851 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3852 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3853 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3854 | h2s->flags |= H2_SF_ES_SENT; |
| 3855 | if (h2s->st == H2_SS_OPEN) |
| 3856 | h2s->st = H2_SS_HLOC; |
| 3857 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3858 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3859 | } |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3860 | else if (h2s->status >= 100 && h2s->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3861 | /* 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] | 3862 | h1m_init_res(h1m); |
Willy Tarreau | 9b8cd1f | 2018-09-12 09:24:38 +0200 | [diff] [blame] | 3863 | 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] | 3864 | h2s->h1m.flags |= H1_MF_TOLOWER; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3865 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 3866 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 3867 | |
| 3868 | /* 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] | 3869 | |
| 3870 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 3871 | //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] | 3872 | return ret; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3873 | full: |
| 3874 | h1m_init_res(h1m); |
| 3875 | h1m->err_pos = -1; // don't care about errors on the response path |
| 3876 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3877 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3878 | ret = 0; |
| 3879 | goto end; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3880 | } |
| 3881 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3882 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 3883 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 3884 | * the number of bytes sent. The caller must check the stream's status to |
| 3885 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3886 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3887 | 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] | 3888 | { |
| 3889 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3890 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3891 | struct buffer outbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3892 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 3893 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3894 | int es_now = 0; |
| 3895 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3896 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3897 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3898 | |
| 3899 | if (h2c_mux_busy(h2c, h2s)) { |
| 3900 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3901 | goto end; |
| 3902 | } |
| 3903 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3904 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3905 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3906 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3907 | goto end; |
| 3908 | } |
| 3909 | |
| 3910 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3911 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3912 | goto end; |
| 3913 | |
| 3914 | chunk_reset(&outbuf); |
| 3915 | |
| 3916 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3917 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3918 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3919 | outbuf.data = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3920 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3921 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3922 | break; |
| 3923 | realign_again: |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 3924 | /* If there are pending data in the output buffer, and we have |
| 3925 | * less than 1/4 of the mbuf's size and everything fits, we'll |
| 3926 | * still perform a copy anyway. Otherwise we'll pretend the mbuf |
| 3927 | * is full and wait, to save some slow realign calls. |
| 3928 | */ |
| 3929 | if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) { |
| 3930 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3931 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3932 | goto end; |
| 3933 | } |
| 3934 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3935 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3936 | } |
| 3937 | |
| 3938 | if (outbuf.size < 9) { |
| 3939 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3940 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3941 | goto end; |
| 3942 | } |
| 3943 | |
| 3944 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3945 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 3946 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3947 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3948 | |
| 3949 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 3950 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3951 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 3952 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3953 | break; |
| 3954 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3955 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3956 | if ((long long)size > h1m->curr_len) |
| 3957 | size = h1m->curr_len; |
| 3958 | break; |
| 3959 | default: /* te:chunked : parse chunks */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3960 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 3961 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3962 | if (!ret) |
| 3963 | goto end; |
| 3964 | |
| 3965 | if (ret < 0) { |
| 3966 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 3967 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3968 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3969 | goto end; |
| 3970 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3971 | max -= ret; |
| 3972 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3973 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3974 | h1m->state = H1_MSG_CHUNK_SIZE; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3975 | } |
| 3976 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3977 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3978 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 3979 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3980 | if (!ret) |
| 3981 | goto end; |
| 3982 | |
| 3983 | if (ret < 0) { |
| 3984 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 3985 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3986 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3987 | goto end; |
| 3988 | } |
| 3989 | |
| 3990 | size = chunk; |
| 3991 | h1m->curr_len = chunk; |
| 3992 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3993 | max -= ret; |
| 3994 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3995 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3996 | h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3997 | if (!size) |
| 3998 | goto send_empty; |
| 3999 | } |
| 4000 | |
| 4001 | /* in MSG_DATA state, continue below */ |
| 4002 | size = h1m->curr_len; |
| 4003 | break; |
| 4004 | } |
| 4005 | |
| 4006 | /* we have in <size> the exact number of bytes we need to copy from |
| 4007 | * the H1 buffer. We need to check this against the connection's and |
| 4008 | * the stream's send windows, and to ensure that this fits in the max |
| 4009 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4010 | * the frame header). The connection's flow control is applied last so |
| 4011 | * that we can use a separate list of streams which are immediately |
| 4012 | * unblocked on window opening. Note: we don't implement padding. |
| 4013 | */ |
| 4014 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4015 | if (size > max) |
| 4016 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4017 | |
| 4018 | if (size > h2s->mws) |
| 4019 | size = h2s->mws; |
| 4020 | |
| 4021 | if (size <= 0) { |
| 4022 | h2s->flags |= H2_SF_BLK_SFCTL; |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 4023 | if (h2s->send_wait) { |
| 4024 | LIST_DEL(&h2s->list); |
| 4025 | LIST_INIT(&h2s->list); |
| 4026 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4027 | goto end; |
| 4028 | } |
| 4029 | |
| 4030 | if (h2c->mfs && size > h2c->mfs) |
| 4031 | size = h2c->mfs; |
| 4032 | |
| 4033 | if (size + 9 > outbuf.size) { |
| 4034 | /* we have an opportunity for enlarging the too small |
| 4035 | * available space, let's try. |
| 4036 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4037 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4038 | goto realign_again; |
| 4039 | size = outbuf.size - 9; |
| 4040 | } |
| 4041 | |
| 4042 | if (size <= 0) { |
| 4043 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4044 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4045 | goto end; |
| 4046 | } |
| 4047 | |
| 4048 | if (size > h2c->mws) |
| 4049 | size = h2c->mws; |
| 4050 | |
| 4051 | if (size <= 0) { |
| 4052 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4053 | goto end; |
| 4054 | } |
| 4055 | |
| 4056 | /* copy whatever we can */ |
| 4057 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4058 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4059 | if (ret == 1) |
| 4060 | len2 = 0; |
| 4061 | |
| 4062 | if (!ret || len1 + len2 < size) { |
| 4063 | /* FIXME: must normally never happen */ |
| 4064 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4065 | goto end; |
| 4066 | } |
| 4067 | |
| 4068 | /* limit len1/len2 to size */ |
| 4069 | if (len1 + len2 > size) { |
| 4070 | int sub = len1 + len2 - size; |
| 4071 | |
| 4072 | if (len2 > sub) |
| 4073 | len2 -= sub; |
| 4074 | else { |
| 4075 | sub -= len2; |
| 4076 | len2 = 0; |
| 4077 | len1 -= sub; |
| 4078 | } |
| 4079 | } |
| 4080 | |
| 4081 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4082 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4083 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4084 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4085 | |
| 4086 | send_empty: |
| 4087 | /* we may need to add END_STREAM */ |
| 4088 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 4089 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4090 | * |
| 4091 | * FIXME: what we do here is not correct because we send end_stream |
| 4092 | * before knowing if we'll have to send a HEADERS frame for the |
| 4093 | * trailers. More importantly we're not consuming the trailing CRLF |
| 4094 | * after the end of trailers, so it will be left to the caller to |
| 4095 | * eat it. The right way to do it would be to measure trailers here |
| 4096 | * and to send ES only if there are no trailers. |
| 4097 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4098 | */ |
| 4099 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4100 | !h1m->curr_len || h1m->state >= H1_MSG_DONE) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4101 | es_now = 1; |
| 4102 | |
| 4103 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4104 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4105 | |
| 4106 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4107 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4108 | |
| 4109 | /* commit the H2 response */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4110 | b_add(&h2c->mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4111 | |
| 4112 | /* consume incoming H1 response */ |
| 4113 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4114 | max -= size; |
| 4115 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4116 | total += size; |
| 4117 | h1m->curr_len -= size; |
| 4118 | h2s->mws -= size; |
| 4119 | h2c->mws -= size; |
| 4120 | |
| 4121 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4122 | h1m->state = H1_MSG_CHUNK_CRLF; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4123 | goto new_frame; |
| 4124 | } |
| 4125 | } |
| 4126 | |
| 4127 | if (es_now) { |
| 4128 | if (h2s->st == H2_SS_OPEN) |
| 4129 | h2s->st = H2_SS_HLOC; |
| 4130 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4131 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4132 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4133 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 4134 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4135 | total += max; |
| 4136 | ofs += max; |
| 4137 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4138 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4139 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4140 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4141 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4142 | h2s->flags |= H2_SF_ES_SENT; |
| 4143 | } |
| 4144 | |
| 4145 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4146 | 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] | 4147 | return total; |
| 4148 | } |
| 4149 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4150 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 4151 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4152 | * must check the stream's status to detect any error which might have happened |
| 4153 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4154 | * from the message. The htx message is assumed to be valid since produced from |
| 4155 | * the internal code, hence it contains a start line, an optional series of |
| 4156 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4157 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4158 | */ |
| 4159 | static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
| 4160 | { |
| 4161 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4162 | struct h2c *h2c = h2s->h2c; |
| 4163 | struct htx_blk *blk; |
| 4164 | struct htx_blk *blk_end; |
| 4165 | struct buffer outbuf; |
| 4166 | struct htx_sl *sl; |
| 4167 | enum htx_blk_type type; |
| 4168 | int es_now = 0; |
| 4169 | int ret = 0; |
| 4170 | int hdr; |
| 4171 | int idx; |
| 4172 | |
| 4173 | if (h2c_mux_busy(h2c, h2s)) { |
| 4174 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4175 | return 0; |
| 4176 | } |
| 4177 | |
| 4178 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4179 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4180 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4181 | return 0; |
| 4182 | } |
| 4183 | |
| 4184 | /* determine the first block which must not be deleted, blk_end may |
| 4185 | * be NULL if all blocks have to be deleted. |
| 4186 | */ |
| 4187 | idx = htx_get_head(htx); |
| 4188 | blk_end = NULL; |
| 4189 | while (idx != -1) { |
| 4190 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4191 | idx = htx_get_next(htx, idx); |
| 4192 | if (type == HTX_BLK_EOH) { |
| 4193 | if (idx != -1) |
| 4194 | blk_end = htx_get_blk(htx, idx); |
| 4195 | break; |
| 4196 | } |
| 4197 | } |
| 4198 | |
| 4199 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4200 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4201 | ALREADY_CHECKED(sl); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4202 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4203 | if (h2s->status < 100 || h2s->status > 999) |
| 4204 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4205 | |
| 4206 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4207 | hdr = 0; |
| 4208 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4209 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4210 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4211 | blk = htx_get_blk(htx, idx); |
| 4212 | type = htx_get_blk_type(blk); |
| 4213 | |
| 4214 | if (type == HTX_BLK_UNUSED) |
| 4215 | continue; |
| 4216 | |
| 4217 | if (type != HTX_BLK_HDR) |
| 4218 | break; |
| 4219 | |
| 4220 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4221 | goto fail; |
| 4222 | |
| 4223 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4224 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4225 | hdr++; |
| 4226 | } |
| 4227 | |
| 4228 | /* marker for end of headers */ |
| 4229 | list[hdr].n = ist(""); |
| 4230 | |
| 4231 | if (h2s->status == 204 || h2s->status == 304) { |
| 4232 | /* no contents, claim c-len is present and set to zero */ |
| 4233 | es_now = 1; |
| 4234 | } |
| 4235 | |
| 4236 | chunk_reset(&outbuf); |
| 4237 | |
| 4238 | while (1) { |
| 4239 | outbuf.area = b_tail(&h2c->mbuf); |
| 4240 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4241 | outbuf.data = 0; |
| 4242 | |
| 4243 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4244 | break; |
| 4245 | realign_again: |
| 4246 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4247 | } |
| 4248 | |
| 4249 | if (outbuf.size < 9) |
| 4250 | goto full; |
| 4251 | |
| 4252 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4253 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4254 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4255 | outbuf.data = 9; |
| 4256 | |
| 4257 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4258 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4259 | if (b_space_wraps(&h2c->mbuf)) |
| 4260 | goto realign_again; |
| 4261 | goto full; |
| 4262 | } |
| 4263 | |
| 4264 | /* encode all headers, stop at empty name */ |
| 4265 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4266 | /* these ones do not exist in H2 and must be dropped. */ |
| 4267 | if (isteq(list[hdr].n, ist("connection")) || |
| 4268 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4269 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4270 | isteq(list[hdr].n, ist("upgrade")) || |
| 4271 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4272 | continue; |
| 4273 | |
| 4274 | if (isteq(list[hdr].n, ist(""))) |
| 4275 | break; // end |
| 4276 | |
| 4277 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4278 | /* output full */ |
| 4279 | if (b_space_wraps(&h2c->mbuf)) |
| 4280 | goto realign_again; |
| 4281 | goto full; |
| 4282 | } |
| 4283 | } |
| 4284 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4285 | /* we may need to add END_STREAM except for 1xx responses. |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4286 | * FIXME: we should also set it when we know for sure that the |
| 4287 | * content-length is zero as well as on 204/304 |
| 4288 | */ |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4289 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM && |
| 4290 | (h2s->status >= 200 || h2s->status == 101)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4291 | es_now = 1; |
| 4292 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4293 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4294 | es_now = 1; |
| 4295 | |
| 4296 | /* update the frame's size */ |
| 4297 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4298 | |
| 4299 | if (es_now) |
| 4300 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4301 | |
| 4302 | /* commit the H2 response */ |
| 4303 | b_add(&h2c->mbuf, outbuf.data); |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4304 | |
| 4305 | /* indicates the HEADERS frame was sent, except for 1xx responses. For |
| 4306 | * 1xx responses, another HEADERS frame is expected. |
| 4307 | */ |
| 4308 | if (h2s->status >= 200 || h2s->status == 101) |
| 4309 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4310 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4311 | if (es_now) { |
| 4312 | h2s->flags |= H2_SF_ES_SENT; |
| 4313 | if (h2s->st == H2_SS_OPEN) |
| 4314 | h2s->st = H2_SS_HLOC; |
| 4315 | else |
| 4316 | h2s_close(h2s); |
| 4317 | } |
| 4318 | |
| 4319 | /* OK we could properly deliver the response */ |
| 4320 | |
| 4321 | /* remove all header blocks including the EOH and compute the |
| 4322 | * corresponding size. |
| 4323 | * |
| 4324 | * FIXME: We should remove everything when es_now is set. |
| 4325 | */ |
| 4326 | ret = 0; |
| 4327 | idx = htx_get_head(htx); |
| 4328 | blk = htx_get_blk(htx, idx); |
| 4329 | while (blk != blk_end) { |
| 4330 | ret += htx_get_blksz(blk); |
| 4331 | blk = htx_remove_blk(htx, blk); |
| 4332 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4333 | |
| 4334 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4335 | htx_remove_blk(htx, blk_end); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4336 | end: |
| 4337 | return ret; |
| 4338 | full: |
| 4339 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4340 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4341 | ret = 0; |
| 4342 | goto end; |
| 4343 | fail: |
| 4344 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4345 | * list etc go here (unrecoverable errors). |
| 4346 | */ |
| 4347 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4348 | ret = 0; |
| 4349 | goto end; |
| 4350 | } |
| 4351 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4352 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4353 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4354 | * must check the stream's status to detect any error which might have happened |
| 4355 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4356 | * from the message. The htx message is assumed to be valid since produced from |
| 4357 | * the internal code, hence it contains a start line, an optional series of |
| 4358 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4359 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4360 | */ |
| 4361 | static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
| 4362 | { |
| 4363 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4364 | struct h2c *h2c = h2s->h2c; |
| 4365 | struct htx_blk *blk; |
| 4366 | struct htx_blk *blk_end; |
| 4367 | struct buffer outbuf; |
| 4368 | struct htx_sl *sl; |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4369 | struct ist meth, path, auth; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4370 | enum htx_blk_type type; |
| 4371 | int es_now = 0; |
| 4372 | int ret = 0; |
| 4373 | int hdr; |
| 4374 | int idx; |
| 4375 | |
| 4376 | if (h2c_mux_busy(h2c, h2s)) { |
| 4377 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4378 | return 0; |
| 4379 | } |
| 4380 | |
| 4381 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4382 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4383 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4384 | return 0; |
| 4385 | } |
| 4386 | |
| 4387 | /* determine the first block which must not be deleted, blk_end may |
| 4388 | * be NULL if all blocks have to be deleted. |
| 4389 | */ |
| 4390 | idx = htx_get_head(htx); |
| 4391 | blk_end = NULL; |
| 4392 | while (idx != -1) { |
| 4393 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4394 | idx = htx_get_next(htx, idx); |
| 4395 | if (type == HTX_BLK_EOH) { |
| 4396 | if (idx != -1) |
| 4397 | blk_end = htx_get_blk(htx, idx); |
| 4398 | break; |
| 4399 | } |
| 4400 | } |
| 4401 | |
| 4402 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4403 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4404 | ALREADY_CHECKED(sl); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4405 | meth = htx_sl_req_meth(sl); |
| 4406 | path = htx_sl_req_uri(sl); |
| 4407 | |
| 4408 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4409 | hdr = 0; |
| 4410 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4411 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4412 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4413 | blk = htx_get_blk(htx, idx); |
| 4414 | type = htx_get_blk_type(blk); |
| 4415 | |
| 4416 | if (type == HTX_BLK_UNUSED) |
| 4417 | continue; |
| 4418 | |
| 4419 | if (type != HTX_BLK_HDR) |
| 4420 | break; |
| 4421 | |
| 4422 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4423 | goto fail; |
| 4424 | |
| 4425 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4426 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4427 | hdr++; |
| 4428 | } |
| 4429 | |
| 4430 | /* marker for end of headers */ |
| 4431 | list[hdr].n = ist(""); |
| 4432 | |
| 4433 | chunk_reset(&outbuf); |
| 4434 | |
| 4435 | while (1) { |
| 4436 | outbuf.area = b_tail(&h2c->mbuf); |
| 4437 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4438 | outbuf.data = 0; |
| 4439 | |
| 4440 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4441 | break; |
| 4442 | realign_again: |
| 4443 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4444 | } |
| 4445 | |
| 4446 | if (outbuf.size < 9) |
| 4447 | goto full; |
| 4448 | |
| 4449 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4450 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4451 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4452 | outbuf.data = 9; |
| 4453 | |
| 4454 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4455 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4456 | if (b_space_wraps(&h2c->mbuf)) |
| 4457 | goto realign_again; |
| 4458 | goto full; |
| 4459 | } |
| 4460 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4461 | /* RFC7540 #8.3: the CONNECT method must have : |
| 4462 | * - :authority set to the URI part (host:port) |
| 4463 | * - :method set to CONNECT |
| 4464 | * - :scheme and :path omitted |
| 4465 | */ |
| 4466 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 4467 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
| 4468 | if (!hpack_encode_scheme(&outbuf, ist("https"))) { |
| 4469 | /* output full */ |
| 4470 | if (b_space_wraps(&h2c->mbuf)) |
| 4471 | goto realign_again; |
| 4472 | goto full; |
| 4473 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4474 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4475 | /* encode the path, which necessarily is the second one */ |
| 4476 | if (!hpack_encode_path(&outbuf, path)) { |
| 4477 | /* output full */ |
| 4478 | if (b_space_wraps(&h2c->mbuf)) |
| 4479 | goto realign_again; |
| 4480 | goto full; |
| 4481 | } |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4482 | |
| 4483 | /* look for the Host header and place it in :authority */ |
| 4484 | auth = ist2(NULL, 0); |
| 4485 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4486 | if (isteq(list[hdr].n, ist(""))) |
| 4487 | break; // end |
| 4488 | |
| 4489 | if (isteq(list[hdr].n, ist("host"))) { |
| 4490 | auth = list[hdr].v; |
| 4491 | break; |
| 4492 | } |
| 4493 | } |
| 4494 | } |
| 4495 | else { |
| 4496 | /* for CONNECT, :authority is taken from the path */ |
| 4497 | auth = path; |
| 4498 | } |
| 4499 | |
| 4500 | if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) { |
| 4501 | /* output full */ |
| 4502 | if (b_space_wraps(&h2c->mbuf)) |
| 4503 | goto realign_again; |
| 4504 | goto full; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4505 | } |
| 4506 | |
| 4507 | /* encode all headers, stop at empty name */ |
| 4508 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4509 | /* these ones do not exist in H2 and must be dropped. */ |
| 4510 | if (isteq(list[hdr].n, ist("connection")) || |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4511 | isteq(list[hdr].n, ist("host")) || |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4512 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4513 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4514 | isteq(list[hdr].n, ist("upgrade")) || |
| 4515 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4516 | continue; |
| 4517 | |
| 4518 | if (isteq(list[hdr].n, ist(""))) |
| 4519 | break; // end |
| 4520 | |
| 4521 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4522 | /* output full */ |
| 4523 | if (b_space_wraps(&h2c->mbuf)) |
| 4524 | goto realign_again; |
| 4525 | goto full; |
| 4526 | } |
| 4527 | } |
| 4528 | |
| 4529 | /* we may need to add END_STREAM if we have no body : |
| 4530 | * - request already closed, or : |
| 4531 | * - no transfer-encoding, and : |
| 4532 | * - no content-length or content-length:0 |
| 4533 | * Fixme: this doesn't take into account CONNECT requests. |
| 4534 | */ |
| 4535 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4536 | es_now = 1; |
| 4537 | |
| 4538 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4539 | es_now = 1; |
| 4540 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4541 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4542 | es_now = 1; |
| 4543 | |
| 4544 | /* update the frame's size */ |
| 4545 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4546 | |
| 4547 | if (es_now) |
| 4548 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4549 | |
| 4550 | /* commit the H2 response */ |
| 4551 | b_add(&h2c->mbuf, outbuf.data); |
| 4552 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4553 | h2s->st = H2_SS_OPEN; |
| 4554 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4555 | if (es_now) { |
| 4556 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4557 | h2s->flags |= H2_SF_ES_SENT; |
| 4558 | h2s->st = H2_SS_HLOC; |
| 4559 | } |
| 4560 | |
| 4561 | /* remove all header blocks including the EOH and compute the |
| 4562 | * corresponding size. |
| 4563 | * |
| 4564 | * FIXME: We should remove everything when es_now is set. |
| 4565 | */ |
| 4566 | ret = 0; |
| 4567 | idx = htx_get_head(htx); |
| 4568 | blk = htx_get_blk(htx, idx); |
| 4569 | while (blk != blk_end) { |
| 4570 | ret += htx_get_blksz(blk); |
| 4571 | blk = htx_remove_blk(htx, blk); |
| 4572 | } |
| 4573 | |
| 4574 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4575 | htx_remove_blk(htx, blk_end); |
| 4576 | |
| 4577 | end: |
| 4578 | return ret; |
| 4579 | full: |
| 4580 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4581 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4582 | ret = 0; |
| 4583 | goto end; |
| 4584 | fail: |
| 4585 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4586 | * list etc go here (unrecoverable errors). |
| 4587 | */ |
| 4588 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4589 | ret = 0; |
| 4590 | goto end; |
| 4591 | } |
| 4592 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4593 | /* 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] | 4594 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 4595 | * caller must check the stream's status to detect any error which might have |
| 4596 | * happened subsequently to a successful send. Returns the number of data bytes |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4597 | * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte. |
| 4598 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4599 | 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] | 4600 | { |
| 4601 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4602 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4603 | struct buffer outbuf; |
| 4604 | size_t total = 0; |
| 4605 | int es_now = 0; |
| 4606 | int bsize; /* htx block size */ |
| 4607 | int fsize; /* h2 frame size */ |
| 4608 | struct htx_blk *blk; |
| 4609 | enum htx_blk_type type; |
| 4610 | int idx; |
| 4611 | |
| 4612 | if (h2c_mux_busy(h2c, h2s)) { |
| 4613 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4614 | goto end; |
| 4615 | } |
| 4616 | |
| 4617 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4618 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4619 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4620 | goto end; |
| 4621 | } |
| 4622 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4623 | htx = htx_from_buf(buf); |
| 4624 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4625 | /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However, |
| 4626 | * while looping, we can meet an HTX_BLK_EOM block that we'll leave to |
| 4627 | * the caller to handle. |
| 4628 | */ |
| 4629 | |
| 4630 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4631 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4632 | goto end; |
| 4633 | |
| 4634 | idx = htx_get_head(htx); |
| 4635 | blk = htx_get_blk(htx, idx); |
| 4636 | type = htx_get_blk_type(blk); // DATA or EOD or EOM |
| 4637 | bsize = htx_get_blksz(blk); |
| 4638 | fsize = bsize; |
| 4639 | |
| 4640 | if (type == HTX_BLK_EOD) { |
| 4641 | /* if we have an EOD, we're dealing with chunked data. We may |
| 4642 | * have a set of trailers after us that the caller will want to |
| 4643 | * deal with. Let's simply remove the EOD and return. |
| 4644 | */ |
| 4645 | htx_remove_blk(htx, blk); |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4646 | total++; // EOD counts as one byte |
| 4647 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4648 | goto end; |
| 4649 | } |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 4650 | else if (type == HTX_BLK_EOM) { |
| 4651 | if (h2s->flags & H2_SF_ES_SENT) { |
| 4652 | /* ES already sent */ |
| 4653 | htx_remove_blk(htx, blk); |
| 4654 | total++; // EOM counts as one byte |
| 4655 | count--; |
| 4656 | goto end; |
| 4657 | } |
| 4658 | } |
| 4659 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4660 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4661 | |
| 4662 | /* Perform some optimizations to reduce the number of buffer copies. |
| 4663 | * First, if the mux's buffer is empty and the htx area contains |
| 4664 | * exactly one data block of the same size as the requested count, and |
| 4665 | * this count fits within the frame size, the stream's window size, and |
| 4666 | * the connection's window size, then it's possible to simply swap the |
| 4667 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 4668 | * length to match the entire DATA HTX block in the middle. In this |
| 4669 | * case we perform a true zero-copy operation from end-to-end. This is |
| 4670 | * the situation that happens all the time with large files. Second, if |
| 4671 | * this is not possible, but the mux's output buffer is empty, we still |
| 4672 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 4673 | * making the intermediary buffer's area point to the output buffer's |
| 4674 | * area. In this case we want to skip the HTX header to make sure that |
| 4675 | * copies remain aligned and that this operation remains possible all |
| 4676 | * the time. This goes for headers, data blocks and any data extracted |
| 4677 | * from the HTX blocks. |
| 4678 | */ |
| 4679 | if (unlikely(fsize == count && |
| 4680 | htx->used == 1 && type == HTX_BLK_DATA && |
| 4681 | fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
| 4682 | void *old_area = h2c->mbuf.area; |
| 4683 | |
| 4684 | if (b_data(&h2c->mbuf)) { |
| 4685 | /* too bad there are data left there. If we have less |
| 4686 | * than 1/4 of the mbuf's size and everything fits, |
| 4687 | * we'll perform a copy anyway. Otherwise we'll pretend |
| 4688 | * the mbuf is full and wait. |
| 4689 | */ |
| 4690 | if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf)) |
| 4691 | goto copy; |
| 4692 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4693 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4694 | goto end; |
| 4695 | } |
| 4696 | |
| 4697 | /* map an H2 frame to the HTX block so that we can put the |
| 4698 | * frame header there. |
| 4699 | */ |
| 4700 | h2c->mbuf.area = buf->area; |
Olivier Houchard | 84cca66 | 2018-12-14 16:28:08 +0100 | [diff] [blame] | 4701 | h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4702 | h2c->mbuf.data = fsize + 9; |
| 4703 | outbuf.area = b_head(&h2c->mbuf); |
| 4704 | |
| 4705 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 4706 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4707 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4708 | h2_set_frame_size(outbuf.area, fsize); |
| 4709 | |
| 4710 | /* update windows */ |
| 4711 | h2s->mws -= fsize; |
| 4712 | h2c->mws -= fsize; |
| 4713 | |
| 4714 | /* and exchange with our old area */ |
| 4715 | buf->area = old_area; |
| 4716 | buf->data = buf->head = 0; |
| 4717 | total += fsize; |
| 4718 | goto end; |
| 4719 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4720 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4721 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4722 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 4723 | |
| 4724 | while (1) { |
| 4725 | outbuf.area = b_tail(&h2c->mbuf); |
| 4726 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4727 | outbuf.data = 0; |
| 4728 | |
| 4729 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4730 | break; |
| 4731 | realign_again: |
| 4732 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4733 | } |
| 4734 | |
| 4735 | if (outbuf.size < 9) { |
| 4736 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4737 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4738 | goto end; |
| 4739 | } |
| 4740 | |
| 4741 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 4742 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4743 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4744 | outbuf.data = 9; |
| 4745 | |
| 4746 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 4747 | * the HTX buffer. We need to check this against the connection's and |
| 4748 | * the stream's send windows, and to ensure that this fits in the max |
| 4749 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4750 | * the frame header). The connection's flow control is applied last so |
| 4751 | * that we can use a separate list of streams which are immediately |
| 4752 | * unblocked on window opening. Note: we don't implement padding. |
| 4753 | */ |
| 4754 | |
| 4755 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 4756 | * empty frame, thus we force it to zero here. |
| 4757 | */ |
| 4758 | if (type == HTX_BLK_EOM) |
| 4759 | bsize = fsize = 0; |
| 4760 | |
| 4761 | if (!fsize) |
| 4762 | goto send_empty; |
| 4763 | |
| 4764 | if (h2s->mws <= 0) { |
| 4765 | h2s->flags |= H2_SF_BLK_SFCTL; |
| 4766 | if (h2s->send_wait) { |
| 4767 | LIST_DEL(&h2s->list); |
| 4768 | LIST_INIT(&h2s->list); |
| 4769 | } |
| 4770 | goto end; |
| 4771 | } |
| 4772 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4773 | if (fsize > count) |
| 4774 | fsize = count; |
| 4775 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4776 | if (fsize > h2s->mws) |
| 4777 | fsize = h2s->mws; // >0 |
| 4778 | |
| 4779 | if (h2c->mfs && fsize > h2c->mfs) |
| 4780 | fsize = h2c->mfs; // >0 |
| 4781 | |
| 4782 | if (fsize + 9 > outbuf.size) { |
| 4783 | /* we have an opportunity for enlarging the too small |
| 4784 | * available space, let's try. |
| 4785 | * FIXME: is this really interesting to do? Maybe we'll |
| 4786 | * spend lots of time realigning instead of using two |
| 4787 | * frames. |
| 4788 | */ |
| 4789 | if (b_space_wraps(&h2c->mbuf)) |
| 4790 | goto realign_again; |
| 4791 | fsize = outbuf.size - 9; |
| 4792 | |
| 4793 | if (fsize <= 0) { |
| 4794 | /* no need to send an empty frame here */ |
| 4795 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4796 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4797 | goto end; |
| 4798 | } |
| 4799 | } |
| 4800 | |
| 4801 | if (h2c->mws <= 0) { |
| 4802 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4803 | goto end; |
| 4804 | } |
| 4805 | |
| 4806 | if (fsize > h2c->mws) |
| 4807 | fsize = h2c->mws; |
| 4808 | |
| 4809 | /* now let's copy this this into the output buffer */ |
| 4810 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 4811 | h2s->mws -= fsize; |
| 4812 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4813 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4814 | |
| 4815 | send_empty: |
| 4816 | /* update the frame's size */ |
| 4817 | h2_set_frame_size(outbuf.area, fsize); |
| 4818 | |
| 4819 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 4820 | * meeting EOM. We should optimize this later. |
| 4821 | */ |
| 4822 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4823 | total++; // EOM counts as one byte |
| 4824 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4825 | es_now = 1; |
| 4826 | } |
| 4827 | |
| 4828 | if (es_now) |
| 4829 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 4830 | |
| 4831 | /* commit the H2 response */ |
| 4832 | b_add(&h2c->mbuf, fsize + 9); |
| 4833 | |
| 4834 | /* consume incoming HTX block, including EOM */ |
| 4835 | total += fsize; |
| 4836 | if (fsize == bsize) { |
| 4837 | htx_remove_blk(htx, blk); |
| 4838 | if (fsize) |
| 4839 | goto new_frame; |
| 4840 | } else { |
| 4841 | /* we've truncated this block */ |
| 4842 | htx_cut_data_blk(htx, blk, fsize); |
| 4843 | } |
| 4844 | |
| 4845 | if (es_now) { |
| 4846 | if (h2s->st == H2_SS_OPEN) |
| 4847 | h2s->st = H2_SS_HLOC; |
| 4848 | else |
| 4849 | h2s_close(h2s); |
| 4850 | |
| 4851 | h2s->flags |= H2_SF_ES_SENT; |
| 4852 | } |
| 4853 | |
| 4854 | end: |
| 4855 | return total; |
| 4856 | } |
| 4857 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4858 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 4859 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 4860 | * processed. The caller must check the stream's status to detect any error |
| 4861 | * which might have happened subsequently to a successful send. The htx blocks |
| 4862 | * are automatically removed from the message. The htx message is assumed to be |
| 4863 | * valid since produced from the internal code. Processing stops when meeting |
| 4864 | * the EOM, which is also removed. All trailers are processed at once and sent |
| 4865 | * as a single frame. The ES flag is always set. |
| 4866 | */ |
| 4867 | static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx) |
| 4868 | { |
| 4869 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4870 | struct h2c *h2c = h2s->h2c; |
| 4871 | struct htx_blk *blk; |
| 4872 | struct htx_blk *blk_end; |
| 4873 | struct buffer outbuf; |
| 4874 | struct h1m h1m; |
| 4875 | enum htx_blk_type type; |
| 4876 | uint32_t size; |
| 4877 | int ret = 0; |
| 4878 | int hdr; |
| 4879 | int idx; |
| 4880 | void *start; |
| 4881 | |
| 4882 | if (h2c_mux_busy(h2c, h2s)) { |
| 4883 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4884 | goto end; |
| 4885 | } |
| 4886 | |
| 4887 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4888 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4889 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4890 | goto end; |
| 4891 | } |
| 4892 | |
| 4893 | /* The principle is that we parse each and every trailers block using |
| 4894 | * the H1 headers parser, and append it to the list. We don't proceed |
| 4895 | * until EOM is met. blk_end will point to the EOM block. |
| 4896 | */ |
| 4897 | hdr = 0; |
| 4898 | memset(list, 0, sizeof(list)); |
| 4899 | blk_end = NULL; |
| 4900 | |
| 4901 | for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) { |
| 4902 | blk = htx_get_blk(htx, idx); |
| 4903 | type = htx_get_blk_type(blk); |
| 4904 | |
| 4905 | if (type == HTX_BLK_UNUSED) |
| 4906 | continue; |
| 4907 | |
| 4908 | if (type != HTX_BLK_TLR) { |
| 4909 | if (type == HTX_BLK_EOM) |
| 4910 | blk_end = blk; |
| 4911 | break; |
| 4912 | } |
| 4913 | |
| 4914 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4915 | goto fail; |
| 4916 | |
| 4917 | size = htx_get_blksz(blk); |
| 4918 | start = htx_get_blk_ptr(htx, blk); |
| 4919 | |
| 4920 | h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER; |
| 4921 | h1m.err_pos = 0; |
| 4922 | ret = h1_headers_to_hdr_list(start, start + size, |
| 4923 | list + hdr, sizeof(list)/sizeof(list[0]) - hdr, |
| 4924 | &h1m, NULL); |
| 4925 | if (ret < 0) |
| 4926 | goto fail; |
| 4927 | |
| 4928 | /* ret == 0 if an incomplete trailers block was found (missing |
| 4929 | * empty line), or > 0 if it was found. We have to continue on |
| 4930 | * incomplete messages because the trailers block might be |
| 4931 | * incomplete. |
| 4932 | */ |
| 4933 | |
| 4934 | /* search the new end */ |
| 4935 | while (hdr <= sizeof(list)/sizeof(list[0])) { |
| 4936 | if (!list[hdr].n.len) |
| 4937 | break; |
| 4938 | hdr++; |
| 4939 | } |
| 4940 | } |
| 4941 | |
| 4942 | if (!blk_end) |
| 4943 | goto end; // end not found yet |
| 4944 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4945 | chunk_reset(&outbuf); |
| 4946 | |
| 4947 | while (1) { |
| 4948 | outbuf.area = b_tail(&h2c->mbuf); |
| 4949 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4950 | outbuf.data = 0; |
| 4951 | |
| 4952 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4953 | break; |
| 4954 | realign_again: |
| 4955 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4956 | } |
| 4957 | |
| 4958 | if (outbuf.size < 9) |
| 4959 | goto full; |
| 4960 | |
| 4961 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 4962 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 4963 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4964 | outbuf.data = 9; |
| 4965 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4966 | /* encode all headers */ |
| 4967 | for (idx = 0; idx < hdr; idx++) { |
| 4968 | /* these ones do not exist in H2 or must not appear in |
| 4969 | * trailers and must be dropped. |
| 4970 | */ |
| 4971 | if (isteq(list[idx].n, ist("host")) || |
| 4972 | isteq(list[idx].n, ist("content-length")) || |
| 4973 | isteq(list[idx].n, ist("connection")) || |
| 4974 | isteq(list[idx].n, ist("proxy-connection")) || |
| 4975 | isteq(list[idx].n, ist("keep-alive")) || |
| 4976 | isteq(list[idx].n, ist("upgrade")) || |
| 4977 | isteq(list[idx].n, ist("te")) || |
| 4978 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 4979 | continue; |
| 4980 | |
| 4981 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 4982 | /* output full */ |
| 4983 | if (b_space_wraps(&h2c->mbuf)) |
| 4984 | goto realign_again; |
| 4985 | goto full; |
| 4986 | } |
| 4987 | } |
| 4988 | |
Willy Tarreau | 67b8cae | 2019-02-21 18:16:35 +0100 | [diff] [blame] | 4989 | if (!hdr) { |
| 4990 | /* here we have a problem, we've received an empty trailers |
| 4991 | * block followed by an EOM. Because of this we can't send a |
| 4992 | * HEADERS frame, so we have to cheat and instead send an empty |
| 4993 | * DATA frame conveying the ES flag. |
| 4994 | */ |
| 4995 | outbuf.area[3] = H2_FT_DATA; |
| 4996 | outbuf.area[4] = H2_F_DATA_END_STREAM; |
| 4997 | } |
| 4998 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4999 | /* update the frame's size */ |
| 5000 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 5001 | |
| 5002 | /* commit the H2 response */ |
| 5003 | b_add(&h2c->mbuf, outbuf.data); |
| 5004 | h2s->flags |= H2_SF_ES_SENT; |
| 5005 | |
| 5006 | if (h2s->st == H2_SS_OPEN) |
| 5007 | h2s->st = H2_SS_HLOC; |
| 5008 | else |
| 5009 | h2s_close(h2s); |
| 5010 | |
| 5011 | /* OK we could properly deliver the response */ |
| 5012 | done: |
| 5013 | /* remove all header blocks including EOM and compute the corresponding size. */ |
| 5014 | ret = 0; |
| 5015 | idx = htx_get_head(htx); |
| 5016 | blk = htx_get_blk(htx, idx); |
| 5017 | while (blk != blk_end) { |
| 5018 | ret += htx_get_blksz(blk); |
| 5019 | blk = htx_remove_blk(htx, blk); |
| 5020 | } |
| 5021 | blk = htx_remove_blk(htx, blk); |
| 5022 | end: |
| 5023 | return ret; |
| 5024 | full: |
| 5025 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5026 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5027 | ret = 0; |
| 5028 | goto end; |
| 5029 | fail: |
| 5030 | /* unparsable HTX messages, too large ones to be produced in the local |
| 5031 | * list etc go here (unrecoverable errors). |
| 5032 | */ |
| 5033 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5034 | ret = 0; |
| 5035 | goto end; |
| 5036 | } |
| 5037 | |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5038 | /* Called from the upper layer, to subscribe to events, such as being able to send */ |
| 5039 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 5040 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5041 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5042 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5043 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5044 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5045 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5046 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5047 | if (!(sw->events & SUB_RETRY_RECV)) { |
| 5048 | sw->events |= SUB_RETRY_RECV; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 5049 | sw->handle = h2s; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5050 | h2s->recv_wait = sw; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5051 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5052 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5053 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5054 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5055 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5056 | if (!(sw->events & SUB_RETRY_SEND)) { |
| 5057 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 5058 | sw->handle = h2s; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5059 | h2s->send_wait = sw; |
| 5060 | if (!(h2s->flags & H2_SF_BLK_SFCTL)) { |
| 5061 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 5062 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 5063 | else |
| 5064 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 5065 | } |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 5066 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5067 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5068 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5069 | if (event_type != 0) |
| 5070 | return -1; |
| 5071 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5072 | |
| 5073 | |
| 5074 | } |
| 5075 | |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5076 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 5077 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5078 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5079 | struct h2s *h2s = cs->ctx; |
| 5080 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5081 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5082 | sw = param; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5083 | if (h2s->recv_wait == sw) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5084 | sw->events &= ~SUB_RETRY_RECV; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5085 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5086 | } |
| 5087 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5088 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5089 | sw = param; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5090 | if (h2s->send_wait == sw) { |
| 5091 | LIST_DEL(&h2s->list); |
| 5092 | LIST_INIT(&h2s->list); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5093 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5094 | h2s->send_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5095 | } |
| 5096 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5097 | if (event_type & SUB_CALL_UNSUBSCRIBE) { |
| 5098 | sw = param; |
| 5099 | if (h2s->send_wait == sw) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5100 | sw->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5101 | h2s->send_wait = NULL; |
Olivier Houchard | f29cd5c | 2018-12-20 11:56:28 +0100 | [diff] [blame] | 5102 | LIST_DEL(&h2s->list); |
| 5103 | LIST_INIT(&h2s->list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5104 | } |
| 5105 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5106 | return 0; |
| 5107 | } |
| 5108 | |
| 5109 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5110 | /* Called from the upper layer, to receive data */ |
| 5111 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 5112 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5113 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5114 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5115 | struct htx *h2s_htx = NULL; |
| 5116 | struct htx *buf_htx = NULL; |
| 5117 | struct htx_ret htx_ret; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5118 | size_t ret = 0; |
| 5119 | |
| 5120 | /* transfer possibly pending data to the upper layer */ |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5121 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
| 5122 | /* in HTX mode we ignore the count argument */ |
| 5123 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5124 | if (htx_is_empty(h2s_htx)) { |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5125 | /* Here htx_to_buf() will set buffer data to 0 because |
| 5126 | * the HTX is empty. |
| 5127 | */ |
| 5128 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5129 | goto end; |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5130 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5131 | |
| 5132 | buf_htx = htx_from_buf(buf); |
Christopher Faulet | a413e95 | 2019-01-21 11:49:37 +0100 | [diff] [blame] | 5133 | count = htx_free_data_space(buf_htx); |
| 5134 | if (flags & CO_RFL_KEEP_RSV) { |
| 5135 | if (count <= global.tune.maxrewrite) |
| 5136 | goto end; |
| 5137 | count -= global.tune.maxrewrite; |
| 5138 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5139 | |
Willy Tarreau | 0c22fa7 | 2018-12-04 15:21:35 +0100 | [diff] [blame] | 5140 | 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] | 5141 | |
| 5142 | if (h2s_htx->flags & HTX_FL_PARSING_ERROR) |
| 5143 | buf_htx->flags |= HTX_FL_PARSING_ERROR; |
| 5144 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 5145 | 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] | 5146 | htx_to_buf(buf_htx, buf); |
| 5147 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5148 | ret = htx_ret.ret; |
| 5149 | } |
| 5150 | else { |
| 5151 | ret = b_xfer(buf, &h2s->rxbuf, count); |
| 5152 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5153 | |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5154 | end: |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5155 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5156 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5157 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5158 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5159 | if (cs->flags & CS_FL_REOS) |
| 5160 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5161 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5162 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5163 | if (b_size(&h2s->rxbuf)) { |
| 5164 | b_free(&h2s->rxbuf); |
| 5165 | offer_buffers(NULL, tasks_run_queue); |
| 5166 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5167 | } |
| 5168 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5169 | if (ret && h2c->dsi == h2s->id) { |
| 5170 | /* demux is blocking on this stream's buffer */ |
| 5171 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 872e2fa | 2019-01-03 08:27:41 +0100 | [diff] [blame] | 5172 | h2c_restart_reading(h2c); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5173 | } |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5174 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5175 | return ret; |
| 5176 | } |
| 5177 | |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5178 | static void h2_stop_senders(struct h2c *h2c) |
| 5179 | { |
| 5180 | struct h2s *h2s, *h2s_back; |
| 5181 | |
| 5182 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) { |
| 5183 | /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */ |
| 5184 | if (h2c->msi == h2s_id(h2s)) |
| 5185 | continue; |
| 5186 | LIST_DEL(&h2s->list); |
| 5187 | LIST_INIT(&h2s->list); |
| 5188 | task_remove_from_task_list((struct task *)h2s->send_wait->task); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5189 | h2s->send_wait->events |= SUB_RETRY_SEND; |
| 5190 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5191 | LIST_ADD(&h2c->send_list, &h2s->list); |
| 5192 | } |
| 5193 | } |
| 5194 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5195 | /* Called from the upper layer, to send data */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 5196 | 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] | 5197 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5198 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5199 | size_t orig_count = count; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 5200 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5201 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5202 | struct htx *htx; |
| 5203 | struct htx_blk *blk; |
| 5204 | enum htx_blk_type btype; |
| 5205 | uint32_t bsize; |
| 5206 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5207 | |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5208 | if (h2s->send_wait) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5209 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5210 | h2s->send_wait = NULL; |
| 5211 | LIST_DEL(&h2s->list); |
| 5212 | LIST_INIT(&h2s->list); |
| 5213 | } |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 5214 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 5215 | return 0; |
| 5216 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5217 | /* htx will be enough to decide if we're using HTX or legacy */ |
| 5218 | htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL; |
| 5219 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5220 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 5221 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 5222 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5223 | if (h2s->id == 0) { |
| 5224 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 5225 | |
| 5226 | if (id < 0) { |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5227 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5228 | return 0; |
| 5229 | } |
| 5230 | |
| 5231 | eb32_delete(&h2s->by_id); |
| 5232 | h2s->by_id.key = h2s->id = id; |
| 5233 | h2s->h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 5234 | h2s->h2c->nb_reserved--; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5235 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 5236 | } |
| 5237 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5238 | if (htx) { |
Willy Tarreau | c14999b | 2018-12-06 14:09:09 +0100 | [diff] [blame] | 5239 | while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) && |
| 5240 | count && !htx_is_empty(htx)) { |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5241 | idx = htx_get_head(htx); |
| 5242 | blk = htx_get_blk(htx, idx); |
| 5243 | btype = htx_get_blk_type(blk); |
| 5244 | bsize = htx_get_blksz(blk); |
| 5245 | |
| 5246 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5247 | case HTX_BLK_REQ_SL: |
| 5248 | /* start-line before headers */ |
| 5249 | ret = h2s_htx_bck_make_req_headers(h2s, htx); |
| 5250 | if (ret > 0) { |
| 5251 | total += ret; |
| 5252 | count -= ret; |
| 5253 | if (ret < bsize) |
| 5254 | goto done; |
| 5255 | } |
| 5256 | break; |
| 5257 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 5258 | case HTX_BLK_RES_SL: |
| 5259 | /* start-line before headers */ |
| 5260 | ret = h2s_htx_frt_make_resp_headers(h2s, htx); |
| 5261 | if (ret > 0) { |
| 5262 | total += ret; |
| 5263 | count -= ret; |
| 5264 | if (ret < bsize) |
| 5265 | goto done; |
| 5266 | } |
| 5267 | break; |
| 5268 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5269 | case HTX_BLK_DATA: |
| 5270 | case HTX_BLK_EOD: |
| 5271 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5272 | /* all these cause the emission of a DATA frame (possibly empty). |
| 5273 | * This EOM necessarily is one before trailers, as the EOM following |
| 5274 | * trailers would have been consumed by the trailers parser. |
| 5275 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5276 | ret = h2s_htx_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5277 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5278 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5279 | total += ret; |
| 5280 | count -= ret; |
| 5281 | if (ret < bsize) |
| 5282 | goto done; |
| 5283 | } |
| 5284 | break; |
| 5285 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5286 | case HTX_BLK_TLR: |
| 5287 | /* This is the first trailers block, all the subsequent ones AND |
| 5288 | * the EOM will be swallowed by the parser. |
| 5289 | */ |
| 5290 | ret = h2s_htx_make_trailers(h2s, htx); |
| 5291 | if (ret > 0) { |
| 5292 | total += ret; |
| 5293 | count -= ret; |
| 5294 | if (ret < bsize) |
| 5295 | goto done; |
| 5296 | } |
| 5297 | break; |
| 5298 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5299 | default: |
| 5300 | htx_remove_blk(htx, blk); |
| 5301 | total += bsize; |
| 5302 | count -= bsize; |
| 5303 | break; |
| 5304 | } |
| 5305 | } |
| 5306 | goto done; |
| 5307 | } |
| 5308 | |
| 5309 | /* legacy transfer mode */ |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5310 | while (h2s->h1m.state < H1_MSG_DONE && count) { |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 5311 | if (h2s->h1m.state <= H1_MSG_LAST_LF) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5312 | if (h2s->h2c->flags & H2_CF_IS_BACK) |
| 5313 | ret = -1; |
| 5314 | else |
| 5315 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5316 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5317 | else if (h2s->h1m.state < H1_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5318 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5319 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5320 | else if (h2s->h1m.state == H1_MSG_TRAILERS) { |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5321 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5322 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5323 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5324 | if (unlikely((int)ret <= 0)) { |
| 5325 | if ((int)ret < 0) |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5326 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5327 | break; |
| 5328 | } |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 5329 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5330 | total += count; |
| 5331 | count = 0; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5332 | h2s->h1m.state = H1_MSG_DONE; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5333 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 5334 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5335 | else { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5336 | cs_set_error(cs); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5337 | break; |
| 5338 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5339 | |
| 5340 | total += ret; |
| 5341 | count -= ret; |
| 5342 | |
| 5343 | if (h2s->st >= H2_SS_ERROR) |
| 5344 | break; |
| 5345 | |
| 5346 | if (h2s->flags & H2_SF_BLK_ANY) |
| 5347 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5348 | } |
| 5349 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5350 | done: |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5351 | if (h2s->st >= H2_SS_ERROR) { |
| 5352 | /* trim any possibly pending data after we close (extra CR-LF, |
| 5353 | * unprocessed trailers, abnormal extra data, ...) |
| 5354 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5355 | total += count; |
| 5356 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5357 | } |
| 5358 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5359 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 5360 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5361 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 5362 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 5363 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5364 | } |
| 5365 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5366 | if (htx) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5367 | htx_to_buf(htx, buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5368 | } else { |
| 5369 | b_del(buf, total); |
| 5370 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5371 | |
| 5372 | /* The mux is full, cancel the pending tasks */ |
| 5373 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 5374 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 5375 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5376 | |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5377 | /* If we're running HTX, and we read the whole buffer, then pretend |
| 5378 | * we read exactly what the caller specified, as with HTX the caller |
| 5379 | * will always give the buffer size, instead of the amount of data |
| 5380 | * available. |
| 5381 | */ |
| 5382 | if (htx && !b_data(buf)) |
| 5383 | total = orig_count; |
| 5384 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5385 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5386 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5387 | tasklet_wakeup(h2s->h2c->wait_event.task); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5388 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5389 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 5390 | /* If we're waiting for flow control, and we got a shutr on the |
| 5391 | * connection, we will never be unlocked, so add an error on |
| 5392 | * the conn_stream. |
| 5393 | */ |
| 5394 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 5395 | !b_data(&h2s->h2c->dbuf) && |
| 5396 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 5397 | if (cs->flags & CS_FL_EOS) |
| 5398 | cs->flags |= CS_FL_ERROR; |
| 5399 | else |
| 5400 | cs->flags |= CS_FL_ERR_PENDING; |
| 5401 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5402 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5403 | } |
| 5404 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5405 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5406 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5407 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5408 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5409 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5410 | struct eb32_node *node; |
| 5411 | int fctl_cnt = 0; |
| 5412 | int send_cnt = 0; |
| 5413 | int tree_cnt = 0; |
| 5414 | int orph_cnt = 0; |
| 5415 | |
| 5416 | if (!h2c) |
| 5417 | return; |
| 5418 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5419 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5420 | fctl_cnt++; |
| 5421 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5422 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5423 | send_cnt++; |
| 5424 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5425 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5426 | node = eb32_first(&h2c->streams_by_id); |
| 5427 | while (node) { |
| 5428 | h2s = container_of(node, struct h2s, by_id); |
| 5429 | tree_cnt++; |
| 5430 | if (!h2s->cs) |
| 5431 | orph_cnt++; |
| 5432 | node = eb32_next(node); |
| 5433 | } |
| 5434 | |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5435 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5436 | " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d" |
| 5437 | " .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] | 5438 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5439 | 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] | 5440 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5441 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5442 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5443 | h2c->msi, |
| 5444 | (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf), |
| 5445 | (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf)); |
| 5446 | |
| 5447 | if (h2s) { |
| 5448 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5449 | h2s, h2s->id, h2s->flags, |
| 5450 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5451 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5452 | h2s->cs); |
| 5453 | if (h2s->cs) |
| 5454 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5455 | h2s->cs->flags, h2s->cs->data); |
| 5456 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5457 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5458 | |
| 5459 | /*******************************************************/ |
| 5460 | /* functions below are dedicated to the config parsers */ |
| 5461 | /*******************************************************/ |
| 5462 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5463 | /* config parser for global "tune.h2.header-table-size" */ |
| 5464 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5465 | struct proxy *defpx, const char *file, int line, |
| 5466 | char **err) |
| 5467 | { |
| 5468 | if (too_many_args(1, args, err, NULL)) |
| 5469 | return -1; |
| 5470 | |
| 5471 | h2_settings_header_table_size = atoi(args[1]); |
| 5472 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5473 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5474 | return -1; |
| 5475 | } |
| 5476 | return 0; |
| 5477 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5478 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5479 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5480 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5481 | struct proxy *defpx, const char *file, int line, |
| 5482 | char **err) |
| 5483 | { |
| 5484 | if (too_many_args(1, args, err, NULL)) |
| 5485 | return -1; |
| 5486 | |
| 5487 | h2_settings_initial_window_size = atoi(args[1]); |
| 5488 | if (h2_settings_initial_window_size < 0) { |
| 5489 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5490 | return -1; |
| 5491 | } |
| 5492 | return 0; |
| 5493 | } |
| 5494 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5495 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5496 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5497 | struct proxy *defpx, const char *file, int line, |
| 5498 | char **err) |
| 5499 | { |
| 5500 | if (too_many_args(1, args, err, NULL)) |
| 5501 | return -1; |
| 5502 | |
| 5503 | h2_settings_max_concurrent_streams = atoi(args[1]); |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 5504 | if ((int)h2_settings_max_concurrent_streams < 0) { |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5505 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5506 | return -1; |
| 5507 | } |
| 5508 | return 0; |
| 5509 | } |
| 5510 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5511 | /* config parser for global "tune.h2.max-frame-size" */ |
| 5512 | static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx, |
| 5513 | struct proxy *defpx, const char *file, int line, |
| 5514 | char **err) |
| 5515 | { |
| 5516 | if (too_many_args(1, args, err, NULL)) |
| 5517 | return -1; |
| 5518 | |
| 5519 | h2_settings_max_frame_size = atoi(args[1]); |
| 5520 | if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) { |
| 5521 | memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]); |
| 5522 | return -1; |
| 5523 | } |
| 5524 | return 0; |
| 5525 | } |
| 5526 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5527 | |
| 5528 | /****************************************/ |
| 5529 | /* MUX initialization and instanciation */ |
| 5530 | /***************************************/ |
| 5531 | |
| 5532 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5533 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5534 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5535 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5536 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5537 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5538 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5539 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5540 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5541 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5542 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5543 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5544 | .avail_streams = h2_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 5545 | .used_streams = h2_used_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5546 | .shutr = h2_shutr, |
| 5547 | .shutw = h2_shutw, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5548 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 5549 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5550 | .name = "H2", |
| 5551 | }; |
| 5552 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5553 | /* PROTO selection : this mux registers PROTO token "h2" */ |
| 5554 | static struct mux_proto_list mux_proto_h2 = |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5555 | { .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] | 5556 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5557 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 5558 | |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5559 | static struct mux_proto_list mux_proto_h2_htx = |
| 5560 | { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops }; |
| 5561 | |
| 5562 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx); |
| 5563 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5564 | /* config keyword parsers */ |
| 5565 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5566 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5567 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5568 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5569 | { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5570 | { 0, NULL, NULL } |
| 5571 | }}; |
| 5572 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5573 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |