Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP/2 mux-demux for connections |
| 3 | * |
| 4 | * Copyright 2017 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <common/cfgparse.h> |
| 14 | #include <common/config.h> |
Willy Tarreau | afba57a | 2018-12-11 13:44:24 +0100 | [diff] [blame] | 15 | #include <common/h1.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 16 | #include <common/h2.h> |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 17 | #include <common/hpack-dec.h> |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 18 | #include <common/hpack-enc.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 19 | #include <common/hpack-tbl.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 20 | #include <common/htx.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 21 | #include <common/initcall.h> |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 22 | #include <common/net_helper.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 23 | #include <proto/connection.h> |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 24 | #include <proto/http_htx.h> |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 25 | #include <proto/session.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 26 | #include <proto/stream.h> |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 27 | #include <proto/stream_interface.h> |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 28 | #include <types/session.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 29 | #include <eb32tree.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 30 | |
| 31 | |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 32 | /* dummy streams returned for closed, error, refused, idle and states */ |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 33 | static const struct h2s *h2_closed_stream; |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 34 | static const struct h2s *h2_error_stream; |
Willy Tarreau | 8d0d58b | 2018-12-23 18:29:12 +0100 | [diff] [blame] | 35 | static const struct h2s *h2_refused_stream; |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 36 | static const struct h2s *h2_idle_stream; |
| 37 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 38 | /* Connection flags (32 bit), in h2c->flags */ |
| 39 | #define H2_CF_NONE 0x00000000 |
| 40 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 41 | /* Flags indicating why writing to the mux is blocked. */ |
| 42 | #define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer |
| 43 | #define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full |
| 44 | #define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above |
| 45 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 46 | /* Flags indicating why writing to the demux is blocked. |
| 47 | * The first two ones directly affect the ability for the mux to receive data |
| 48 | * from the connection. The other ones affect the mux's ability to demux |
| 49 | * received data. |
| 50 | */ |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 51 | #define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer |
| 52 | #define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 53 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 54 | #define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy |
| 55 | #define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer |
| 56 | #define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer |
| 57 | #define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 58 | #define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave |
| 59 | #define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 60 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 61 | /* other flags */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 62 | #define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent |
| 63 | #define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent |
| 64 | #define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake |
Willy Tarreau | b3fb56d | 2018-10-03 13:56:38 +0200 | [diff] [blame] | 65 | #define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 66 | #define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 67 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 68 | /* H2 connection state, in h2c->st0 */ |
| 69 | enum h2_cs { |
| 70 | H2_CS_PREFACE, // init done, waiting for connection preface |
| 71 | H2_CS_SETTINGS1, // preface OK, waiting for first settings frame |
| 72 | H2_CS_FRAME_H, // first settings frame ok, waiting for frame header |
| 73 | H2_CS_FRAME_P, // frame header OK, waiting for frame payload |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 74 | H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame |
| 75 | H2_CS_FRAME_E, // frame payload OK, trying to send RST frame |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 76 | H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP |
| 77 | H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP |
| 78 | H2_CS_ENTRIES // must be last |
| 79 | } __attribute__((packed)); |
| 80 | |
| 81 | /* H2 connection descriptor */ |
| 82 | struct h2c { |
| 83 | struct connection *conn; |
| 84 | |
| 85 | enum h2_cs st0; /* mux state */ |
| 86 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 87 | |
| 88 | /* 16 bit hole here */ |
| 89 | uint32_t flags; /* connection flags: H2_CF_* */ |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 90 | uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 91 | int32_t max_id; /* highest ID known on this connection, <0 before preface */ |
| 92 | uint32_t rcvd_c; /* newly received data to ACK for the connection */ |
| 93 | uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */ |
| 94 | |
| 95 | /* states for the demux direction */ |
| 96 | struct hpack_dht *ddht; /* demux dynamic header table */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 97 | struct buffer dbuf; /* demux buffer */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 98 | |
| 99 | int32_t dsi; /* demux stream ID (<0 = idle) */ |
| 100 | int32_t dfl; /* demux frame length (if dsi >= 0) */ |
| 101 | int8_t dft; /* demux frame type (if dsi >= 0) */ |
| 102 | int8_t dff; /* demux frame flags (if dsi >= 0) */ |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 103 | uint8_t dpl; /* demux pad length (part of dfl), init to 0 */ |
| 104 | /* 8 bit hole here */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 105 | int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */ |
| 106 | |
| 107 | /* states for the mux direction */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 108 | struct buffer mbuf; /* mux buffer */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 109 | int32_t msi; /* mux stream ID (<0 = idle) */ |
| 110 | int32_t mfl; /* mux frame length (if dsi >= 0) */ |
| 111 | int8_t mft; /* mux frame type (if dsi >= 0) */ |
| 112 | int8_t mff; /* mux frame flags (if dsi >= 0) */ |
| 113 | /* 16 bit hole here */ |
| 114 | int32_t miw; /* mux initial window size for all new streams */ |
| 115 | int32_t mws; /* mux window size. Can be negative. */ |
| 116 | int32_t mfs; /* mux's max frame size */ |
| 117 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 118 | int timeout; /* idle timeout duration in ticks */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 119 | int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */ |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 120 | unsigned int nb_streams; /* number of streams in the tree */ |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 121 | unsigned int nb_cs; /* number of attached conn_streams */ |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 122 | unsigned int nb_reserved; /* number of reserved streams */ |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 123 | unsigned int stream_cnt; /* total number of streams seen */ |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 124 | struct proxy *proxy; /* the proxy this connection was created for */ |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 125 | struct task *task; /* timeout management task */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 126 | struct eb_root streams_by_id; /* all active streams by their ID */ |
| 127 | struct list send_list; /* list of blocked streams requesting to send */ |
| 128 | struct list fctl_list; /* list of streams blocked by connection's fctl */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 129 | struct list sending_list; /* list of h2s scheduled to send data */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 130 | struct buffer_wait buf_wait; /* wait list for buffer allocations */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 131 | struct wait_event wait_event; /* To be used if we're waiting for I/Os */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 132 | }; |
| 133 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 134 | /* H2 stream state, in h2s->st */ |
| 135 | enum h2_ss { |
| 136 | H2_SS_IDLE = 0, // idle |
| 137 | H2_SS_RLOC, // reserved(local) |
| 138 | H2_SS_RREM, // reserved(remote) |
| 139 | H2_SS_OPEN, // open |
| 140 | H2_SS_HREM, // half-closed(remote) |
| 141 | H2_SS_HLOC, // half-closed(local) |
Willy Tarreau | 96060ba | 2017-10-16 18:34:34 +0200 | [diff] [blame] | 142 | H2_SS_ERROR, // an error needs to be sent using RST_STREAM |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 143 | H2_SS_CLOSED, // closed |
| 144 | H2_SS_ENTRIES // must be last |
| 145 | } __attribute__((packed)); |
| 146 | |
| 147 | /* HTTP/2 stream flags (32 bit), in h2s->flags */ |
| 148 | #define H2_SF_NONE 0x00000000 |
| 149 | #define H2_SF_ES_RCVD 0x00000001 |
| 150 | #define H2_SF_ES_SENT 0x00000002 |
| 151 | |
| 152 | #define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM |
| 153 | #define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM |
| 154 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 155 | /* stream flags indicating the reason the stream is blocked */ |
| 156 | #define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient) |
| 157 | #define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux |
| 158 | #define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl |
| 159 | #define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl |
| 160 | #define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above |
| 161 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 162 | /* stream flags indicating how data is supposed to be sent */ |
| 163 | #define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length |
| 164 | #define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding |
| 165 | |
| 166 | /* step we're currently in when sending chunks. This is needed because we may |
| 167 | * have to transfer chunks as large as a full buffer so there's no room left |
| 168 | * for size nor crlf around. |
| 169 | */ |
| 170 | #define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size |
| 171 | #define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data |
| 172 | #define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data |
| 173 | |
| 174 | #define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size |
| 175 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 176 | #define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 177 | #define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 178 | |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 179 | #define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream |
| 180 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 181 | /* H2 stream descriptor, describing the stream as it appears in the H2C, and as |
| 182 | * it is being processed in the internal HTTP representation (H1 for now). |
| 183 | */ |
| 184 | struct h2s { |
| 185 | struct conn_stream *cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 186 | struct session *sess; |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 187 | struct h2c *h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 188 | struct h1m h1m; /* request or response parser state for H1 */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 189 | struct eb32_node by_id; /* place in h2c's streams_by_id */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 190 | int32_t id; /* stream ID */ |
| 191 | uint32_t flags; /* H2_SF_* */ |
| 192 | int mws; /* mux window size for this stream */ |
| 193 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 194 | enum h2_ss st; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 195 | uint16_t status; /* HTTP response status */ |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 196 | unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */ |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 197 | struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 198 | struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */ |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 199 | struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */ |
| 200 | struct wait_event *send_wait; /* send wait_event the conn_stream associated is waiting on (via h2_subscribe) */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 201 | struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 202 | struct list sending_list; /* To be used when adding in h2c->sending_list */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 203 | }; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 204 | |
Willy Tarreau | c640514 | 2017-09-21 20:23:50 +0200 | [diff] [blame] | 205 | /* descriptor for an h2 frame header */ |
| 206 | struct h2_fh { |
| 207 | uint32_t len; /* length, host order, 24 bits */ |
| 208 | uint32_t sid; /* stream id, host order, 31 bits */ |
| 209 | uint8_t ft; /* frame type */ |
| 210 | uint8_t ff; /* frame flags */ |
| 211 | }; |
| 212 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 213 | /* the h2c connection pool */ |
| 214 | DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c)); |
| 215 | |
| 216 | /* the h2s stream pool */ |
| 217 | DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s)); |
| 218 | |
Willy Tarreau | dc57236 | 2018-12-12 08:08:05 +0100 | [diff] [blame] | 219 | /* The default connection window size is 65535, it may only be enlarged using |
| 220 | * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1, |
| 221 | * we'll pretend we already received the difference between the two to send |
| 222 | * an equivalent window update to enlarge it to 2G-1. |
| 223 | */ |
| 224 | #define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535) |
| 225 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 226 | /* a few settings from the global section */ |
| 227 | static int h2_settings_header_table_size = 4096; /* initial value */ |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 228 | static int h2_settings_initial_window_size = 65535; /* initial value */ |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 229 | static unsigned int h2_settings_max_concurrent_streams = 100; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 230 | static int h2_settings_max_frame_size = 0; /* unset */ |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 231 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 232 | /* a dmumy closed stream */ |
| 233 | static const struct h2s *h2_closed_stream = &(const struct h2s){ |
| 234 | .cs = NULL, |
| 235 | .h2c = NULL, |
| 236 | .st = H2_SS_CLOSED, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 237 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 238 | .flags = H2_SF_RST_RCVD, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 239 | .id = 0, |
| 240 | }; |
| 241 | |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 242 | /* a dmumy closed stream returning a PROTOCOL_ERROR error */ |
| 243 | static const struct h2s *h2_error_stream = &(const struct h2s){ |
| 244 | .cs = NULL, |
| 245 | .h2c = NULL, |
| 246 | .st = H2_SS_CLOSED, |
| 247 | .errcode = H2_ERR_PROTOCOL_ERROR, |
| 248 | .flags = 0, |
| 249 | .id = 0, |
| 250 | }; |
| 251 | |
Willy Tarreau | 8d0d58b | 2018-12-23 18:29:12 +0100 | [diff] [blame] | 252 | /* a dmumy closed stream returning a REFUSED_STREAM error */ |
| 253 | static const struct h2s *h2_refused_stream = &(const struct h2s){ |
| 254 | .cs = NULL, |
| 255 | .h2c = NULL, |
| 256 | .st = H2_SS_CLOSED, |
| 257 | .errcode = H2_ERR_REFUSED_STREAM, |
| 258 | .flags = 0, |
| 259 | .id = 0, |
| 260 | }; |
| 261 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 262 | /* and a dummy idle stream for use with any unannounced stream */ |
| 263 | static const struct h2s *h2_idle_stream = &(const struct h2s){ |
| 264 | .cs = NULL, |
| 265 | .h2c = NULL, |
| 266 | .st = H2_SS_IDLE, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 267 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 268 | .id = 0, |
| 269 | }; |
| 270 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 271 | static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state); |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 272 | static int h2_send(struct h2c *h2c); |
| 273 | static int h2_recv(struct h2c *h2c); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 274 | static int h2_process(struct h2c *h2c); |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 275 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state); |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 276 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id); |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 277 | static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len); |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 278 | static int h2_frt_transfer_data(struct h2s *h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 279 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state); |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 280 | static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess); |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 281 | static void h2s_alert(struct h2s *h2s); |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 282 | |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 283 | static __inline int |
| 284 | h2c_is_dead(struct h2c *h2c) |
| 285 | { |
| 286 | if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */ |
| 287 | ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */ |
| 288 | (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */ |
| 289 | (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */ |
| 290 | (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */ |
| 291 | (conn_xprt_read0_pending(h2c->conn) || |
| 292 | (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) |
| 293 | return 1; |
| 294 | |
| 295 | return 0; |
| 296 | |
| 297 | } |
| 298 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 299 | /*****************************************************/ |
| 300 | /* functions below are for dynamic buffer management */ |
| 301 | /*****************************************************/ |
| 302 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 303 | /* indicates whether or not the we may call the h2_recv() function to attempt |
| 304 | * to receive data into the buffer and/or demux pending data. The condition is |
| 305 | * a bit complex due to some API limits for now. The rules are the following : |
| 306 | * - if an error or a shutdown was detected on the connection and the buffer |
| 307 | * is empty, we must not attempt to receive |
| 308 | * - if the demux buf failed to be allocated, we must not try to receive and |
| 309 | * we know there is nothing pending |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 310 | * - if no flag indicates a blocking condition, we may attempt to receive, |
| 311 | * regardless of whether the demux buffer is full or not, so that only |
| 312 | * de demux part decides whether or not to block. This is needed because |
| 313 | * the connection API indeed prevents us from re-enabling receipt that is |
| 314 | * already enabled in a polled state, so we must always immediately stop |
| 315 | * as soon as the demux can't proceed so as never to hit an end of read |
| 316 | * with data pending in the buffers. |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 317 | * - otherwise must may not attempt |
| 318 | */ |
| 319 | static inline int h2_recv_allowed(const struct h2c *h2c) |
| 320 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 321 | if (b_data(&h2c->dbuf) == 0 && |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 322 | (h2c->st0 >= H2_CS_ERROR || |
| 323 | h2c->conn->flags & CO_FL_ERROR || |
| 324 | conn_xprt_read0_pending(h2c->conn))) |
| 325 | return 0; |
| 326 | |
| 327 | if (!(h2c->flags & H2_CF_DEM_DALLOC) && |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 328 | !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 329 | return 1; |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 334 | /* restarts reading on the connection if it was not enabled */ |
| 335 | static inline void h2c_restart_reading(const struct h2c *h2c) |
| 336 | { |
| 337 | if (!h2_recv_allowed(h2c)) |
| 338 | return; |
Willy Tarreau | 872e2fa | 2019-01-03 08:27:41 +0100 | [diff] [blame] | 339 | if (!b_data(&h2c->dbuf) && (h2c->wait_event.events & SUB_RETRY_RECV)) |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 340 | return; |
| 341 | tasklet_wakeup(h2c->wait_event.task); |
| 342 | } |
| 343 | |
| 344 | |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 345 | /* returns true if the front connection has too many conn_streams attached */ |
| 346 | static inline int h2_frt_has_too_many_cs(const struct h2c *h2c) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 347 | { |
Willy Tarreau | a875466 | 2018-12-23 20:43:58 +0100 | [diff] [blame] | 348 | return h2c->nb_cs > h2_settings_max_concurrent_streams; |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 349 | } |
| 350 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 351 | /* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c |
| 352 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 353 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 354 | * impossible to wake up and we prefer to be woken up later. |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 355 | */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 356 | static int h2_buf_available(void *target) |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 357 | { |
| 358 | struct h2c *h2c = target; |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 359 | struct h2s *h2s; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 360 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 361 | if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) { |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 362 | h2c->flags &= ~H2_CF_DEM_DALLOC; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 363 | h2c_restart_reading(h2c); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 364 | return 1; |
| 365 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 366 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 367 | if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) { |
| 368 | h2c->flags &= ~H2_CF_MUX_MALLOC; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 369 | |
| 370 | if (h2c->flags & H2_CF_DEM_MROOM) { |
| 371 | h2c->flags &= ~H2_CF_DEM_MROOM; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 372 | h2c_restart_reading(h2c); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 373 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 374 | return 1; |
| 375 | } |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 376 | |
| 377 | if ((h2c->flags & H2_CF_DEM_SALLOC) && |
| 378 | (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs && |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 379 | b_alloc_margin(&h2s->rxbuf, 0)) { |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 380 | h2c->flags &= ~H2_CF_DEM_SALLOC; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 381 | h2c_restart_reading(h2c); |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 382 | return 1; |
| 383 | } |
| 384 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 385 | return 0; |
| 386 | } |
| 387 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 388 | static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 389 | { |
| 390 | struct buffer *buf = NULL; |
| 391 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 392 | if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) && |
| 393 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 394 | h2c->buf_wait.target = h2c; |
| 395 | h2c->buf_wait.wakeup_cb = h2_buf_available; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 396 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 397 | LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 398 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 399 | __conn_xprt_stop_recv(h2c->conn); |
| 400 | } |
| 401 | return buf; |
| 402 | } |
| 403 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 404 | static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 405 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 406 | if (bptr->size) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 407 | b_free(bptr); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 408 | offer_buffers(h2c->buf_wait.target, tasks_run_queue); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 412 | /* returns the number of allocatable outgoing streams for the connection taking |
| 413 | * the last_sid and the reserved ones into account. |
| 414 | */ |
| 415 | static inline int h2_streams_left(const struct h2c *h2c) |
| 416 | { |
| 417 | int ret; |
| 418 | |
| 419 | /* consider the number of outgoing streams we're allowed to create before |
| 420 | * reaching the last GOAWAY frame seen. max_id is the last assigned id, |
| 421 | * nb_reserved is the number of streams which don't yet have an ID. |
| 422 | */ |
| 423 | ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF; |
| 424 | ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1; |
| 425 | if (ret < 0) |
| 426 | ret = 0; |
| 427 | return ret; |
| 428 | } |
| 429 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 430 | /* returns the number of streams in use on a connection to figure if it's |
| 431 | * idle or not. We check nb_cs and not nb_streams as the caller will want |
| 432 | * to know if it was the last one after a detach(). |
| 433 | */ |
| 434 | static int h2_used_streams(struct connection *conn) |
| 435 | { |
| 436 | struct h2c *h2c = conn->ctx; |
| 437 | |
| 438 | return h2c->nb_cs; |
| 439 | } |
| 440 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 441 | /* returns the number of concurrent streams available on the connection */ |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 442 | static int h2_avail_streams(struct connection *conn) |
| 443 | { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 444 | struct server *srv = objt_server(conn->target); |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 445 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 446 | int ret1, ret2; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 447 | |
Willy Tarreau | 6afec46 | 2019-01-28 06:40:19 +0100 | [diff] [blame] | 448 | /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional |
| 449 | * streams on the connection. |
| 450 | */ |
| 451 | if (h2c->last_sid >= 0) |
| 452 | return 0; |
| 453 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 454 | /* note: may be negative if a SETTINGS frame changes the limit */ |
| 455 | ret1 = h2c->streams_limit - h2c->nb_streams; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 456 | |
| 457 | /* we must also consider the limit imposed by stream IDs */ |
| 458 | ret2 = h2_streams_left(h2c); |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 459 | ret1 = MIN(ret1, ret2); |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 460 | if (ret1 > 0 && srv && srv->max_reuse >= 0) { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 461 | ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0; |
| 462 | ret1 = MIN(ret1, ret2); |
| 463 | } |
| 464 | return ret1; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 465 | } |
| 466 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 467 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 468 | /*****************************************************************/ |
| 469 | /* functions below are dedicated to the mux setup and management */ |
| 470 | /*****************************************************************/ |
| 471 | |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 472 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 473 | * is already initialized before installing the mux, so we detect incoming |
| 474 | * connections from the fact that the context is still NULL. Returns < 0 on |
| 475 | * error. |
| 476 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 477 | static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess) |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 478 | { |
| 479 | struct h2c *h2c; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 480 | struct task *t = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 481 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 482 | h2c = pool_alloc(pool_head_h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 483 | if (!h2c) |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 484 | goto fail_no_h2c; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 485 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 486 | if (conn->ctx) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 487 | h2c->flags = H2_CF_IS_BACK; |
| 488 | h2c->shut_timeout = h2c->timeout = prx->timeout.server; |
| 489 | if (tick_isset(prx->timeout.serverfin)) |
| 490 | h2c->shut_timeout = prx->timeout.serverfin; |
| 491 | } else { |
| 492 | h2c->flags = H2_CF_NONE; |
| 493 | h2c->shut_timeout = h2c->timeout = prx->timeout.client; |
| 494 | if (tick_isset(prx->timeout.clientfin)) |
| 495 | h2c->shut_timeout = prx->timeout.clientfin; |
| 496 | } |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 497 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 498 | h2c->proxy = prx; |
Willy Tarreau | 3340029 | 2017-11-05 11:23:40 +0100 | [diff] [blame] | 499 | h2c->task = NULL; |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 500 | if (tick_isset(h2c->timeout)) { |
| 501 | t = task_new(tid_bit); |
| 502 | if (!t) |
| 503 | goto fail; |
| 504 | |
| 505 | h2c->task = t; |
| 506 | t->process = h2_timeout_task; |
| 507 | t->context = h2c; |
| 508 | t->expire = tick_add(now_ms, h2c->timeout); |
| 509 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 510 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 511 | h2c->wait_event.task = tasklet_new(); |
| 512 | if (!h2c->wait_event.task) |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 513 | goto fail; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 514 | h2c->wait_event.task->process = h2_io_cb; |
| 515 | h2c->wait_event.task->context = h2c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 516 | h2c->wait_event.events = 0; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 517 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 518 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 519 | if (!h2c->ddht) |
| 520 | goto fail; |
| 521 | |
| 522 | /* Initialise the context. */ |
| 523 | h2c->st0 = H2_CS_PREFACE; |
| 524 | h2c->conn = conn; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 525 | h2c->streams_limit = h2_settings_max_concurrent_streams; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 526 | h2c->max_id = -1; |
| 527 | h2c->errcode = H2_ERR_NO_ERROR; |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 528 | h2c->rcvd_c = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 529 | h2c->rcvd_s = 0; |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 530 | h2c->nb_streams = 0; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 531 | h2c->nb_cs = 0; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 532 | h2c->nb_reserved = 0; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 533 | h2c->stream_cnt = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 534 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 535 | h2c->dbuf = BUF_NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 536 | h2c->dsi = -1; |
| 537 | h2c->msi = -1; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 538 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 539 | h2c->last_sid = -1; |
| 540 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 541 | h2c->mbuf = BUF_NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 542 | h2c->miw = 65535; /* mux initial window size */ |
| 543 | h2c->mws = 65535; /* mux window size */ |
| 544 | h2c->mfs = 16384; /* initial max frame size */ |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 545 | h2c->streams_by_id = EB_ROOT; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 546 | LIST_INIT(&h2c->send_list); |
| 547 | LIST_INIT(&h2c->fctl_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 548 | LIST_INIT(&h2c->sending_list); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 549 | LIST_INIT(&h2c->buf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 550 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 551 | if (t) |
| 552 | task_queue(t); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 553 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 554 | if (h2c->flags & H2_CF_IS_BACK) { |
| 555 | /* FIXME: this is temporary, for outgoing connections we need |
| 556 | * to immediately allocate a stream until the code is modified |
| 557 | * so that the caller calls ->attach(). For now the outgoing cs |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 558 | * is stored as conn->ctx by the caller. |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 559 | */ |
| 560 | struct h2s *h2s; |
| 561 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 562 | h2s = h2c_bck_stream_new(h2c, conn->ctx, sess); |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 563 | if (!h2s) |
| 564 | goto fail_stream; |
| 565 | } |
| 566 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 567 | conn->ctx = h2c; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 568 | |
Willy Tarreau | 0f38358 | 2018-10-03 14:22:21 +0200 | [diff] [blame] | 569 | /* prepare to read something */ |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 570 | h2c_restart_reading(h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 571 | return 0; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 572 | fail_stream: |
| 573 | hpack_dht_free(h2c->ddht); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 574 | fail: |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 575 | if (t) |
| 576 | task_free(t); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 577 | if (h2c->wait_event.task) |
| 578 | tasklet_free(h2c->wait_event.task); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 579 | pool_free(pool_head_h2c, h2c); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 580 | fail_no_h2c: |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 581 | return -1; |
| 582 | } |
| 583 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 584 | /* returns the next allocatable outgoing stream ID for the H2 connection, or |
| 585 | * -1 if no more is allocatable. |
| 586 | */ |
| 587 | static inline int32_t h2c_get_next_sid(const struct h2c *h2c) |
| 588 | { |
| 589 | int32_t id = (h2c->max_id + 1) | 1; |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 590 | |
| 591 | if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid)) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 592 | id = -1; |
| 593 | return id; |
| 594 | } |
| 595 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 596 | /* returns the stream associated with id <id> or NULL if not found */ |
| 597 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 598 | { |
| 599 | struct eb32_node *node; |
| 600 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 601 | if (id == 0) |
| 602 | return (struct h2s *)h2_closed_stream; |
| 603 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 604 | if (id > h2c->max_id) |
| 605 | return (struct h2s *)h2_idle_stream; |
| 606 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 607 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 608 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 609 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 610 | |
| 611 | return container_of(node, struct h2s, by_id); |
| 612 | } |
| 613 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 614 | /* release function for a connection. This one should be called to free all |
| 615 | * resources allocated to the mux. |
| 616 | */ |
| 617 | static void h2_release(struct connection *conn) |
| 618 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 619 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 620 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 621 | if (h2c) { |
| 622 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 623 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 624 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 625 | LIST_DEL(&h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 626 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 627 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 628 | h2_release_buf(h2c, &h2c->dbuf); |
| 629 | h2_release_buf(h2c, &h2c->mbuf); |
| 630 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 631 | if (h2c->task) { |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 632 | h2c->task->context = NULL; |
| 633 | task_wakeup(h2c->task, TASK_WOKEN_OTHER); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 634 | h2c->task = NULL; |
| 635 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 636 | if (h2c->wait_event.task) |
| 637 | tasklet_free(h2c->wait_event.task); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 638 | if (h2c->wait_event.events != 0) |
| 639 | conn->xprt->unsubscribe(conn, h2c->wait_event.events, |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 640 | &h2c->wait_event); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 641 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 642 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | conn->mux = NULL; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 646 | conn->ctx = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 647 | |
| 648 | conn_stop_tracking(conn); |
| 649 | conn_full_close(conn); |
| 650 | if (conn->destroy_cb) |
| 651 | conn->destroy_cb(conn); |
| 652 | conn_free(conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 656 | /******************************************************/ |
| 657 | /* functions below are for the H2 protocol processing */ |
| 658 | /******************************************************/ |
| 659 | |
| 660 | /* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 661 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 662 | { |
| 663 | return h2s ? h2s->id : 0; |
| 664 | } |
| 665 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 666 | /* returns true of the mux is currently busy as seen from stream <h2s> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 667 | static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s) |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 668 | { |
| 669 | if (h2c->msi < 0) |
| 670 | return 0; |
| 671 | |
| 672 | if (h2c->msi == h2s_id(h2s)) |
| 673 | return 0; |
| 674 | |
| 675 | return 1; |
| 676 | } |
| 677 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 678 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 679 | static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err) |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 680 | { |
| 681 | h2c->errcode = err; |
| 682 | h2c->st0 = H2_CS_ERROR; |
| 683 | } |
| 684 | |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 685 | /* marks an error on the stream. It may also update an already closed stream |
| 686 | * (e.g. to report an error after an RST was received). |
| 687 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 688 | static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err) |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 689 | { |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 690 | if (h2s->id && h2s->st != H2_SS_ERROR) { |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 691 | h2s->errcode = err; |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 692 | if (h2s->st < H2_SS_ERROR) |
| 693 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 694 | if (h2s->cs) |
| 695 | cs_set_error(h2s->cs); |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 699 | /* attempt to notify the data layer of recv availability */ |
| 700 | static void __maybe_unused h2s_notify_recv(struct h2s *h2s) |
| 701 | { |
| 702 | struct wait_event *sw; |
| 703 | |
| 704 | if (h2s->recv_wait) { |
| 705 | sw = h2s->recv_wait; |
| 706 | sw->events &= ~SUB_RETRY_RECV; |
| 707 | tasklet_wakeup(sw->task); |
| 708 | h2s->recv_wait = NULL; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | /* attempt to notify the data layer of send availability */ |
| 713 | static void __maybe_unused h2s_notify_send(struct h2s *h2s) |
| 714 | { |
| 715 | struct wait_event *sw; |
| 716 | |
Olivier Houchard | fd1e96d | 2019-03-25 14:04:25 +0100 | [diff] [blame] | 717 | if (h2s->send_wait && !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) { |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 718 | sw = h2s->send_wait; |
| 719 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fd1e96d | 2019-03-25 14:04:25 +0100 | [diff] [blame] | 720 | sw->events |= SUB_CALL_UNSUBSCRIBE; |
| 721 | LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 722 | tasklet_wakeup(sw->task); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 726 | /* alerts the data layer, trying to wake it up by all means, following |
| 727 | * this sequence : |
| 728 | * - if the h2s' data layer is subscribed to recv, then it's woken up for recv |
| 729 | * - if its subscribed to send, then it's woken up for send |
| 730 | * - if it was subscribed to neither, its ->wake() callback is called |
| 731 | * It is safe to call this function with a closed stream which doesn't have a |
| 732 | * conn_stream anymore. |
| 733 | */ |
| 734 | static void __maybe_unused h2s_alert(struct h2s *h2s) |
| 735 | { |
| 736 | if (h2s->recv_wait || h2s->send_wait) { |
| 737 | h2s_notify_recv(h2s); |
| 738 | h2s_notify_send(h2s); |
| 739 | } |
| 740 | else if (h2s->cs && h2s->cs->data_cb->wake != NULL) |
| 741 | h2s->cs->data_cb->wake(h2s->cs); |
| 742 | } |
| 743 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 744 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 745 | static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len) |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 746 | { |
| 747 | uint8_t *out = frame; |
| 748 | |
| 749 | *out = len >> 16; |
| 750 | write_n16(out + 1, len); |
| 751 | } |
| 752 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 753 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 754 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 755 | * the caller's responsibility to verify that there are at least <bytes> bytes |
Willy Tarreau | 9c7f2d1 | 2018-06-15 11:51:32 +0200 | [diff] [blame] | 756 | * available in the buffer's input prior to calling this function. The buffer |
| 757 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 758 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 759 | static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes, |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 760 | const struct buffer *b, int o) |
| 761 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 762 | readv_bytes(dst, bytes, b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b)); |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 763 | } |
| 764 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 765 | static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o) |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 766 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 767 | return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b)); |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 768 | } |
| 769 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 770 | static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o) |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 771 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 772 | return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b)); |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 773 | } |
| 774 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 775 | static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o) |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 776 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 777 | return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b)); |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 781 | /* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>. |
| 782 | * The algorithm is not obvious. It turns out that H2 headers are neither |
| 783 | * aligned nor do they use regular sizes. And to add to the trouble, the buffer |
| 784 | * may wrap so each byte read must be checked. The header is formed like this : |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 785 | * |
| 786 | * b0 b1 b2 b3 b4 b5..b8 |
| 787 | * +----------+---------+--------+----+----+----------------------+ |
| 788 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 789 | * +----------+---------+--------+----+----+----------------------+ |
| 790 | * |
| 791 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 792 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 793 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 794 | * the word, and only one extra read is needed to fetch len[16:23]. |
Willy Tarreau | 9c7f2d1 | 2018-06-15 11:51:32 +0200 | [diff] [blame] | 795 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 796 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 797 | */ |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 798 | static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 799 | { |
| 800 | uint64_t w; |
| 801 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 802 | if (b_data(b) < o + 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 803 | return 0; |
| 804 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 805 | w = h2_get_n64(b, o + 1); |
| 806 | h->len = *(uint8_t*)b_peek(b, o) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 807 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 808 | h->ff = w >> 32; |
| 809 | h->ft = w >> 40; |
| 810 | h->len += w >> 48; |
| 811 | return 1; |
| 812 | } |
| 813 | |
| 814 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 815 | * h2_peek_frame_hdr() above. |
| 816 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 817 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 818 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 819 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 823 | static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 824 | { |
| 825 | int ret; |
| 826 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 827 | ret = h2_peek_frame_hdr(b, 0, h); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 828 | if (ret > 0) |
| 829 | h2_skip_frame_hdr(b); |
| 830 | return ret; |
| 831 | } |
| 832 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 833 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 834 | * its connection if the stream was not yet closed. Please use this exclusively |
| 835 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 836 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 837 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 838 | { |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 839 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 840 | h2s->h2c->nb_streams--; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 841 | if (!h2s->id) |
| 842 | h2s->h2c->nb_reserved--; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 843 | if (h2s->cs) |
| 844 | h2s->cs->flags |= CS_FL_REOS; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 845 | } |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 846 | h2s->st = H2_SS_CLOSED; |
| 847 | } |
| 848 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 849 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 850 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 851 | { |
| 852 | h2s_close(h2s); |
| 853 | eb32_delete(&h2s->by_id); |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 854 | if (b_size(&h2s->rxbuf)) { |
| 855 | b_free(&h2s->rxbuf); |
| 856 | offer_buffers(NULL, tasks_run_queue); |
| 857 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 858 | if (h2s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 859 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 860 | if (h2s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 861 | h2s->recv_wait->events &= ~SUB_RETRY_RECV; |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 862 | /* There's no need to explicitly call unsubscribe here, the only |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 863 | * reference left would be in the h2c send_list/fctl_list, and if |
| 864 | * we're in it, we're getting out anyway |
| 865 | */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 866 | LIST_DEL_INIT(&h2s->list); |
| 867 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 868 | tasklet_free(h2s->wait_event.task); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 869 | pool_free(pool_head_h2s, h2s); |
| 870 | } |
| 871 | |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 872 | /* allocates a new stream <id> for connection <h2c> and adds it into h2c's |
| 873 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 874 | * causes of errors can be any failed memory allocation. The caller is |
| 875 | * responsible for checking if the connection may support an extra stream |
| 876 | * prior to calling this function. |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 877 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 878 | static struct h2s *h2s_new(struct h2c *h2c, int id) |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 879 | { |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 880 | struct h2s *h2s; |
| 881 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 882 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 883 | if (!h2s) |
| 884 | goto out; |
| 885 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 886 | h2s->wait_event.task = tasklet_new(); |
| 887 | if (!h2s->wait_event.task) { |
| 888 | pool_free(pool_head_h2s, h2s); |
| 889 | goto out; |
| 890 | } |
| 891 | h2s->send_wait = NULL; |
| 892 | h2s->recv_wait = NULL; |
| 893 | h2s->wait_event.task->process = h2_deferred_shut; |
| 894 | h2s->wait_event.task->context = h2s; |
| 895 | h2s->wait_event.handle = NULL; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 896 | h2s->wait_event.events = 0; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 897 | LIST_INIT(&h2s->list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 898 | LIST_INIT(&h2s->sending_list); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 899 | h2s->h2c = h2c; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 900 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 901 | h2s->mws = h2c->miw; |
| 902 | h2s->flags = H2_SF_NONE; |
| 903 | h2s->errcode = H2_ERR_NO_ERROR; |
| 904 | h2s->st = H2_SS_IDLE; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 905 | h2s->status = 0; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 906 | h2s->body_len = 0; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 907 | h2s->rxbuf = BUF_NULL; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 908 | |
| 909 | if (h2c->flags & H2_CF_IS_BACK) { |
| 910 | h1m_init_req(&h2s->h1m); |
| 911 | h2s->h1m.err_pos = -1; // don't care about errors on the request path |
| 912 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 913 | } else { |
| 914 | h1m_init_res(&h2s->h1m); |
| 915 | h2s->h1m.err_pos = -1; // don't care about errors on the response path |
| 916 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 917 | } |
| 918 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 919 | h2s->by_id.key = h2s->id = id; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 920 | if (id > 0) |
| 921 | h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 922 | else |
| 923 | h2c->nb_reserved++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 924 | |
| 925 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 926 | h2c->nb_streams++; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 927 | h2c->stream_cnt++; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 928 | |
| 929 | return h2s; |
| 930 | |
| 931 | out_free_h2s: |
| 932 | pool_free(pool_head_h2s, h2s); |
| 933 | out: |
| 934 | return NULL; |
| 935 | } |
| 936 | |
| 937 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 938 | * case of memory allocation error. |
| 939 | */ |
| 940 | static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id) |
| 941 | { |
| 942 | struct session *sess = h2c->conn->owner; |
| 943 | struct conn_stream *cs; |
| 944 | struct h2s *h2s; |
| 945 | |
| 946 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 947 | goto out; |
| 948 | |
| 949 | h2s = h2s_new(h2c, id); |
| 950 | if (!h2s) |
| 951 | goto out; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 952 | |
| 953 | cs = cs_new(h2c->conn); |
| 954 | if (!cs) |
| 955 | goto out_close; |
| 956 | |
Olivier Houchard | 746fb77 | 2018-12-15 19:42:00 +0100 | [diff] [blame] | 957 | cs->flags |= CS_FL_NOT_FIRST; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 958 | h2s->cs = cs; |
| 959 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 960 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 961 | |
| 962 | if (stream_create_from_cs(cs) < 0) |
| 963 | goto out_free_cs; |
| 964 | |
Willy Tarreau | 590a051 | 2018-09-05 11:56:48 +0200 | [diff] [blame] | 965 | /* We want the accept date presented to the next stream to be the one |
| 966 | * we have now, the handshake time to be null (since the next stream |
| 967 | * is not delayed by a handshake), and the idle time to count since |
| 968 | * right now. |
| 969 | */ |
| 970 | sess->accept_date = date; |
| 971 | sess->tv_accept = now; |
| 972 | sess->t_handshake = 0; |
| 973 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 974 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 975 | if (h2_frt_has_too_many_cs(h2c)) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 976 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 977 | return h2s; |
| 978 | |
| 979 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 980 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 981 | cs_free(cs); |
| 982 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 983 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 984 | out: |
Willy Tarreau | 45efc07 | 2018-10-03 18:27:52 +0200 | [diff] [blame] | 985 | sess_log(sess); |
| 986 | return NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 987 | } |
| 988 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 989 | /* allocates a new stream associated to conn_stream <cs> on the h2c connection |
| 990 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 991 | * possible stream ID was reached. |
| 992 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 993 | 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] | 994 | { |
| 995 | struct h2s *h2s = NULL; |
| 996 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 997 | if (h2c->nb_streams >= h2c->streams_limit) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 998 | goto out; |
| 999 | |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 1000 | if (h2_streams_left(h2c) < 1) |
| 1001 | goto out; |
| 1002 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1003 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 1004 | h2s = h2s_new(h2c, 0); |
| 1005 | if (!h2s) |
| 1006 | goto out; |
| 1007 | |
| 1008 | h2s->cs = cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1009 | h2s->sess = sess; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1010 | cs->ctx = h2s; |
| 1011 | h2c->nb_cs++; |
| 1012 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1013 | out: |
| 1014 | return h2s; |
| 1015 | } |
| 1016 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1017 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 1018 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 1019 | * the various settings codes. |
| 1020 | */ |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1021 | static int h2c_send_settings(struct h2c *h2c) |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1022 | { |
| 1023 | struct buffer *res; |
| 1024 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1025 | struct buffer buf; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1026 | int mfs; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1027 | int ret; |
| 1028 | |
| 1029 | if (h2c_mux_busy(h2c, NULL)) { |
| 1030 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1034 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1035 | if (!res) { |
| 1036 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1037 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
| 1041 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 1042 | chunk_memcpy(&buf, |
| 1043 | "\x00\x00\x00" /* length : 0 for now */ |
| 1044 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 1045 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 1046 | 9); |
| 1047 | |
Willy Tarreau | 0bbad6b | 2019-02-26 16:01:52 +0100 | [diff] [blame] | 1048 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1049 | /* send settings_enable_push=0 */ |
| 1050 | chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6); |
| 1051 | } |
| 1052 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1053 | if (h2_settings_header_table_size != 4096) { |
| 1054 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 1055 | |
| 1056 | write_n32(str + 2, h2_settings_header_table_size); |
| 1057 | chunk_memcat(&buf, str, 6); |
| 1058 | } |
| 1059 | |
| 1060 | if (h2_settings_initial_window_size != 65535) { |
| 1061 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 1062 | |
| 1063 | write_n32(str + 2, h2_settings_initial_window_size); |
| 1064 | chunk_memcat(&buf, str, 6); |
| 1065 | } |
| 1066 | |
| 1067 | if (h2_settings_max_concurrent_streams != 0) { |
| 1068 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 1069 | |
| 1070 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 1071 | * the protocol, so never send this value! |
| 1072 | */ |
| 1073 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 1074 | chunk_memcat(&buf, str, 6); |
| 1075 | } |
| 1076 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1077 | mfs = h2_settings_max_frame_size; |
| 1078 | if (mfs > global.tune.bufsize) |
| 1079 | mfs = global.tune.bufsize; |
| 1080 | |
| 1081 | if (!mfs) |
| 1082 | mfs = global.tune.bufsize; |
| 1083 | |
| 1084 | if (mfs != 16384) { |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1085 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 1086 | |
| 1087 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 1088 | * match bufsize - rewrite size, but at the moment it seems |
| 1089 | * that clients don't take care of it. |
| 1090 | */ |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1091 | write_n32(str + 2, mfs); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1092 | chunk_memcat(&buf, str, 6); |
| 1093 | } |
| 1094 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1095 | h2_set_frame_size(buf.area, buf.data - 9); |
| 1096 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1097 | if (unlikely(ret <= 0)) { |
| 1098 | if (!ret) { |
| 1099 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1100 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1101 | return 0; |
| 1102 | } |
| 1103 | else { |
| 1104 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1105 | return 0; |
| 1106 | } |
| 1107 | } |
| 1108 | return ret; |
| 1109 | } |
| 1110 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1111 | /* Try to receive a connection preface, then upon success try to send our |
| 1112 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1113 | * missing data. It may return an error in h2c. |
| 1114 | */ |
| 1115 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 1116 | { |
| 1117 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1118 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1119 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1120 | 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] | 1121 | |
| 1122 | if (unlikely(ret1 <= 0)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1123 | if (ret1 < 0) |
| 1124 | sess_log(h2c->conn->owner); |
| 1125 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1126 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 1127 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1131 | ret2 = h2c_send_settings(h2c); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1132 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1133 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1134 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1135 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1136 | } |
| 1137 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1138 | /* Try to send a connection preface, then upon success try to send our |
| 1139 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1140 | * missing data. It may return an error in h2c. |
| 1141 | */ |
| 1142 | static int h2c_bck_send_preface(struct h2c *h2c) |
| 1143 | { |
| 1144 | struct buffer *res; |
| 1145 | |
| 1146 | if (h2c_mux_busy(h2c, NULL)) { |
| 1147 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1148 | return 0; |
| 1149 | } |
| 1150 | |
| 1151 | res = h2_get_buf(h2c, &h2c->mbuf); |
| 1152 | if (!res) { |
| 1153 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1154 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1155 | return 0; |
| 1156 | } |
| 1157 | |
| 1158 | if (!b_data(res)) { |
| 1159 | /* preface not yet sent */ |
| 1160 | b_istput(res, ist(H2_CONN_PREFACE)); |
| 1161 | } |
| 1162 | |
| 1163 | return h2c_send_settings(h2c); |
| 1164 | } |
| 1165 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1166 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 1167 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 1168 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 1169 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 1170 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 1171 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 1172 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 1173 | * case so that it is safe to use h2c_error() to report such errors. |
| 1174 | */ |
| 1175 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 1176 | { |
| 1177 | struct buffer *res; |
| 1178 | char str[17]; |
| 1179 | int ret; |
| 1180 | |
| 1181 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 1182 | return 1; // claim that it worked |
| 1183 | |
| 1184 | if (h2c_mux_busy(h2c, h2s)) { |
| 1185 | if (h2s) |
| 1186 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1187 | else |
| 1188 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1192 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1193 | if (!res) { |
| 1194 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1195 | if (h2s) |
| 1196 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1197 | else |
| 1198 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1199 | return 0; |
| 1200 | } |
| 1201 | |
| 1202 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 1203 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 1204 | |
| 1205 | if (h2c->last_sid < 0) |
| 1206 | h2c->last_sid = h2c->max_id; |
| 1207 | |
| 1208 | write_n32(str + 9, h2c->last_sid); |
| 1209 | write_n32(str + 13, h2c->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1210 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1211 | if (unlikely(ret <= 0)) { |
| 1212 | if (!ret) { |
| 1213 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1214 | if (h2s) |
| 1215 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1216 | else |
| 1217 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1218 | return 0; |
| 1219 | } |
| 1220 | else { |
| 1221 | /* we cannot report this error using GOAWAY, so we mark |
| 1222 | * it and claim a success. |
| 1223 | */ |
| 1224 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1225 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 1226 | return 1; |
| 1227 | } |
| 1228 | } |
| 1229 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 1230 | return ret; |
| 1231 | } |
| 1232 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1233 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 1234 | * during mux operations. This stream must be valid and cannot be closed |
| 1235 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 1236 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 1237 | * not yet. |
| 1238 | * |
| 1239 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1240 | * to write the message, it subscribes the stream to future notifications. |
| 1241 | */ |
| 1242 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1243 | { |
| 1244 | struct buffer *res; |
| 1245 | char str[13]; |
| 1246 | int ret; |
| 1247 | |
| 1248 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 1249 | return 1; |
| 1250 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1251 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1252 | * RST_STREAM in response to a RST_STREAM frame. |
| 1253 | */ |
| 1254 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1255 | ret = 1; |
| 1256 | goto ignore; |
| 1257 | } |
| 1258 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1259 | if (h2c_mux_busy(h2c, h2s)) { |
| 1260 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1261 | return 0; |
| 1262 | } |
| 1263 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1264 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1265 | if (!res) { |
| 1266 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1267 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
| 1271 | /* len: 4, type: 3, flags: none */ |
| 1272 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1273 | write_n32(str + 5, h2s->id); |
| 1274 | write_n32(str + 9, h2s->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1275 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1276 | |
| 1277 | if (unlikely(ret <= 0)) { |
| 1278 | if (!ret) { |
| 1279 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1280 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1281 | return 0; |
| 1282 | } |
| 1283 | else { |
| 1284 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1285 | return 0; |
| 1286 | } |
| 1287 | } |
| 1288 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1289 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1290 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1291 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1292 | return ret; |
| 1293 | } |
| 1294 | |
| 1295 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 1296 | * 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] | 1297 | * error code, even if the stream is one of the dummy ones, and will update |
| 1298 | * h2s->st to H2_SS_CLOSED if it was not yet. |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1299 | * |
| 1300 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1301 | * 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] | 1302 | * 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] | 1303 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1304 | */ |
| 1305 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1306 | { |
| 1307 | struct buffer *res; |
| 1308 | char str[13]; |
| 1309 | int ret; |
| 1310 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1311 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1312 | * RST_STREAM in response to a RST_STREAM frame. |
| 1313 | */ |
| 1314 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1315 | ret = 1; |
| 1316 | goto ignore; |
| 1317 | } |
| 1318 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1319 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1320 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1321 | return 0; |
| 1322 | } |
| 1323 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1324 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1325 | if (!res) { |
| 1326 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1327 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | /* len: 4, type: 3, flags: none */ |
| 1332 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1333 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1334 | write_n32(str + 5, h2c->dsi); |
Willy Tarreau | e6888ff | 2018-12-23 18:26:26 +0100 | [diff] [blame] | 1335 | write_n32(str + 9, h2s->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1336 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1337 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1338 | if (unlikely(ret <= 0)) { |
| 1339 | if (!ret) { |
| 1340 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1341 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1342 | return 0; |
| 1343 | } |
| 1344 | else { |
| 1345 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1346 | return 0; |
| 1347 | } |
| 1348 | } |
| 1349 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1350 | ignore: |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1351 | if (h2s->id) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1352 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1353 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1354 | } |
| 1355 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1356 | return ret; |
| 1357 | } |
| 1358 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1359 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 1360 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 1361 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 1362 | * on success or zero if nothing was done. In case of lack of room to write the |
| 1363 | * message, it subscribes the requesting stream to future notifications. |
| 1364 | */ |
| 1365 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 1366 | { |
| 1367 | struct h2c *h2c = h2s->h2c; |
| 1368 | struct buffer *res; |
| 1369 | char str[9]; |
| 1370 | int ret; |
| 1371 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1372 | 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] | 1373 | return 1; |
| 1374 | |
| 1375 | if (h2c_mux_busy(h2c, h2s)) { |
| 1376 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1377 | return 0; |
| 1378 | } |
| 1379 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1380 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1381 | if (!res) { |
| 1382 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1383 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1384 | return 0; |
| 1385 | } |
| 1386 | |
| 1387 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1388 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1389 | write_n32(str + 5, h2s->id); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1390 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1391 | if (likely(ret > 0)) { |
| 1392 | h2s->flags |= H2_SF_ES_SENT; |
| 1393 | } |
| 1394 | else if (!ret) { |
| 1395 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1396 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1397 | return 0; |
| 1398 | } |
| 1399 | else { |
| 1400 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1401 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1402 | } |
| 1403 | return ret; |
| 1404 | } |
| 1405 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1406 | /* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags> |
| 1407 | * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close |
| 1408 | * connection. The stream's state is automatically updated accordingly. If the |
| 1409 | * stream is orphaned, it is destroyed. |
| 1410 | */ |
| 1411 | static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags) |
| 1412 | { |
| 1413 | if (!h2s->cs) { |
| 1414 | /* this stream was already orphaned */ |
| 1415 | h2s_destroy(h2s); |
| 1416 | return; |
| 1417 | } |
| 1418 | |
| 1419 | h2s->cs->flags |= flags; |
| 1420 | if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS)) |
| 1421 | h2s->cs->flags |= CS_FL_ERROR; |
| 1422 | |
| 1423 | h2s_alert(h2s); |
| 1424 | |
| 1425 | if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR) |
| 1426 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 1427 | else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN) |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1428 | h2s->st = H2_SS_HREM; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 1429 | else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC) |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1430 | h2s_close(h2s); |
| 1431 | } |
| 1432 | |
| 1433 | /* wake the streams attached to the connection, whose id is greater than <last> |
| 1434 | * or unassigned. |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1435 | */ |
| 1436 | static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags) |
| 1437 | { |
| 1438 | struct eb32_node *node; |
| 1439 | struct h2s *h2s; |
| 1440 | |
| 1441 | if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR) |
Willy Tarreau | a851935 | 2018-12-18 16:44:28 +0100 | [diff] [blame] | 1442 | flags |= CS_FL_ERR_PENDING; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1443 | |
| 1444 | if (conn_xprt_read0_pending(h2c->conn)) |
Christopher Faulet | 87a8f35 | 2019-03-22 14:51:36 +0100 | [diff] [blame] | 1445 | flags |= CS_FL_REOS; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1446 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1447 | /* Wake all streams with ID > last */ |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1448 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1449 | while (node) { |
| 1450 | h2s = container_of(node, struct h2s, by_id); |
| 1451 | if (h2s->id <= last) |
| 1452 | break; |
| 1453 | node = eb32_next(node); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1454 | h2s_wake_one_stream(h2s, flags); |
| 1455 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1456 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1457 | /* Wake all streams with unassigned ID (ID == 0) */ |
| 1458 | node = eb32_lookup(&h2c->streams_by_id, 0); |
| 1459 | while (node) { |
| 1460 | h2s = container_of(node, struct h2s, by_id); |
| 1461 | if (h2s->id > 0) |
| 1462 | break; |
| 1463 | node = eb32_next(node); |
| 1464 | h2s_wake_one_stream(h2s, flags); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1465 | } |
| 1466 | } |
| 1467 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1468 | /* Increase all streams' outgoing window size by the difference passed in |
| 1469 | * argument. This is needed upon receipt of the settings frame if the initial |
| 1470 | * window size is different. The difference may be negative and the resulting |
| 1471 | * window size as well, for the time it takes to receive some window updates. |
| 1472 | */ |
| 1473 | static void h2c_update_all_ws(struct h2c *h2c, int diff) |
| 1474 | { |
| 1475 | struct h2s *h2s; |
| 1476 | struct eb32_node *node; |
| 1477 | |
| 1478 | if (!diff) |
| 1479 | return; |
| 1480 | |
| 1481 | node = eb32_first(&h2c->streams_by_id); |
| 1482 | while (node) { |
| 1483 | h2s = container_of(node, struct h2s, by_id); |
| 1484 | h2s->mws += diff; |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1485 | |
| 1486 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1487 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
| 1488 | if (h2s->send_wait) |
| 1489 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 1490 | |
| 1491 | } |
| 1492 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1493 | node = eb32_next(node); |
| 1494 | } |
| 1495 | } |
| 1496 | |
| 1497 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1498 | * 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] | 1499 | * return an error in h2c. The caller must have already verified frame length |
| 1500 | * and stream ID validity. Described in RFC7540#6.5. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1501 | */ |
| 1502 | static int h2c_handle_settings(struct h2c *h2c) |
| 1503 | { |
| 1504 | unsigned int offset; |
| 1505 | int error; |
| 1506 | |
| 1507 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1508 | if (h2c->dfl) { |
| 1509 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1510 | goto fail; |
| 1511 | } |
| 1512 | return 1; |
| 1513 | } |
| 1514 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1515 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1516 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1517 | return 0; |
| 1518 | |
| 1519 | /* parse the frame */ |
| 1520 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1521 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1522 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1523 | |
| 1524 | switch (type) { |
| 1525 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1526 | /* we need to update all existing streams with the |
| 1527 | * difference from the previous iws. |
| 1528 | */ |
| 1529 | if (arg < 0) { // RFC7540#6.5.2 |
| 1530 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1531 | goto fail; |
| 1532 | } |
| 1533 | h2c_update_all_ws(h2c, arg - h2c->miw); |
| 1534 | h2c->miw = arg; |
| 1535 | break; |
| 1536 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1537 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1538 | error = H2_ERR_PROTOCOL_ERROR; |
| 1539 | goto fail; |
| 1540 | } |
| 1541 | h2c->mfs = arg; |
| 1542 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1543 | case H2_SETTINGS_ENABLE_PUSH: |
| 1544 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1545 | error = H2_ERR_PROTOCOL_ERROR; |
| 1546 | goto fail; |
| 1547 | } |
| 1548 | break; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 1549 | case H2_SETTINGS_MAX_CONCURRENT_STREAMS: |
| 1550 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1551 | /* the limit is only for the backend; for the frontend it is our limit */ |
| 1552 | if ((unsigned int)arg > h2_settings_max_concurrent_streams) |
| 1553 | arg = h2_settings_max_concurrent_streams; |
| 1554 | h2c->streams_limit = arg; |
| 1555 | } |
| 1556 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | /* need to ACK this frame now */ |
| 1561 | h2c->st0 = H2_CS_FRAME_A; |
| 1562 | return 1; |
| 1563 | fail: |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1564 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1565 | h2c_error(h2c, error); |
| 1566 | return 0; |
| 1567 | } |
| 1568 | |
| 1569 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1570 | * success or one of the h2_status values. |
| 1571 | */ |
| 1572 | static int h2c_ack_settings(struct h2c *h2c) |
| 1573 | { |
| 1574 | struct buffer *res; |
| 1575 | char str[9]; |
| 1576 | int ret = -1; |
| 1577 | |
| 1578 | if (h2c_mux_busy(h2c, NULL)) { |
| 1579 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1580 | return 0; |
| 1581 | } |
| 1582 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1583 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1584 | if (!res) { |
| 1585 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1586 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1587 | return 0; |
| 1588 | } |
| 1589 | |
| 1590 | memcpy(str, |
| 1591 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1592 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1593 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1594 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1595 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1596 | if (unlikely(ret <= 0)) { |
| 1597 | if (!ret) { |
| 1598 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1599 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1600 | return 0; |
| 1601 | } |
| 1602 | else { |
| 1603 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1604 | return 0; |
| 1605 | } |
| 1606 | } |
| 1607 | return ret; |
| 1608 | } |
| 1609 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1610 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1611 | * 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] | 1612 | * missing data. The caller must have already verified frame length |
| 1613 | * and stream ID validity. |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1614 | */ |
| 1615 | static int h2c_handle_ping(struct h2c *h2c) |
| 1616 | { |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1617 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1618 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1619 | h2c->st0 = H2_CS_FRAME_A; |
| 1620 | return 1; |
| 1621 | } |
| 1622 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1623 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1624 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1625 | * error in h2c. |
| 1626 | */ |
| 1627 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1628 | { |
| 1629 | struct buffer *res; |
| 1630 | char str[13]; |
| 1631 | int ret = -1; |
| 1632 | |
| 1633 | if (h2c_mux_busy(h2c, NULL)) { |
| 1634 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1635 | return 0; |
| 1636 | } |
| 1637 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1638 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1639 | if (!res) { |
| 1640 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1641 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1642 | return 0; |
| 1643 | } |
| 1644 | |
| 1645 | /* length: 4, type: 8, flags: none */ |
| 1646 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1647 | write_n32(str + 5, sid); |
| 1648 | write_n32(str + 9, increment); |
| 1649 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1650 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1651 | |
| 1652 | if (unlikely(ret <= 0)) { |
| 1653 | if (!ret) { |
| 1654 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1655 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1656 | return 0; |
| 1657 | } |
| 1658 | else { |
| 1659 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1660 | return 0; |
| 1661 | } |
| 1662 | } |
| 1663 | return ret; |
| 1664 | } |
| 1665 | |
| 1666 | /* try to send pending window update for the connection. It's safe to call it |
| 1667 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1668 | * failure. It may return an error in h2c. |
| 1669 | */ |
| 1670 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1671 | { |
| 1672 | int ret = 1; |
| 1673 | |
| 1674 | if (h2c->rcvd_c <= 0) |
| 1675 | return 1; |
| 1676 | |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 1677 | if (!(h2c->flags & H2_CF_WINDOW_OPENED)) { |
| 1678 | /* increase the advertised connection window to 2G on |
| 1679 | * first update. |
| 1680 | */ |
| 1681 | h2c->flags |= H2_CF_WINDOW_OPENED; |
| 1682 | h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT; |
| 1683 | } |
| 1684 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1685 | /* send WU for the connection */ |
| 1686 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1687 | if (ret > 0) |
| 1688 | h2c->rcvd_c = 0; |
| 1689 | |
| 1690 | return ret; |
| 1691 | } |
| 1692 | |
| 1693 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1694 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1695 | * room or failure. It may return an error in h2c. |
| 1696 | */ |
| 1697 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1698 | { |
| 1699 | int ret = 1; |
| 1700 | |
| 1701 | if (h2c->rcvd_s <= 0) |
| 1702 | return 1; |
| 1703 | |
| 1704 | /* send WU for the stream */ |
| 1705 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1706 | if (ret > 0) |
| 1707 | h2c->rcvd_s = 0; |
| 1708 | |
| 1709 | return ret; |
| 1710 | } |
| 1711 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1712 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1713 | * success, 0 on missing data or one of the h2_status values. |
| 1714 | */ |
| 1715 | static int h2c_ack_ping(struct h2c *h2c) |
| 1716 | { |
| 1717 | struct buffer *res; |
| 1718 | char str[17]; |
| 1719 | int ret = -1; |
| 1720 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1721 | if (b_data(&h2c->dbuf) < 8) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1722 | return 0; |
| 1723 | |
| 1724 | if (h2c_mux_busy(h2c, NULL)) { |
| 1725 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1726 | return 0; |
| 1727 | } |
| 1728 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1729 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1730 | if (!res) { |
| 1731 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1732 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1733 | return 0; |
| 1734 | } |
| 1735 | |
| 1736 | memcpy(str, |
| 1737 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1738 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1739 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1740 | |
| 1741 | /* copy the original payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1742 | h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1743 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1744 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1745 | if (unlikely(ret <= 0)) { |
| 1746 | if (!ret) { |
| 1747 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1748 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1749 | return 0; |
| 1750 | } |
| 1751 | else { |
| 1752 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1753 | return 0; |
| 1754 | } |
| 1755 | } |
| 1756 | return ret; |
| 1757 | } |
| 1758 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1759 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1760 | * 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] | 1761 | * h2c or h2s. The caller must have already verified frame length and stream ID |
| 1762 | * validity. Described in RFC7540#6.9. |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1763 | */ |
| 1764 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1765 | { |
| 1766 | int32_t inc; |
| 1767 | int error; |
| 1768 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1769 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1770 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1771 | return 0; |
| 1772 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1773 | inc = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1774 | |
| 1775 | if (h2c->dsi != 0) { |
| 1776 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1777 | |
| 1778 | /* it's not an error to receive WU on a closed stream */ |
| 1779 | if (h2s->st == H2_SS_CLOSED) |
| 1780 | return 1; |
| 1781 | |
| 1782 | if (!inc) { |
| 1783 | error = H2_ERR_PROTOCOL_ERROR; |
| 1784 | goto strm_err; |
| 1785 | } |
| 1786 | |
| 1787 | if (h2s->mws >= 0 && h2s->mws + inc < 0) { |
| 1788 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1789 | goto strm_err; |
| 1790 | } |
| 1791 | |
| 1792 | h2s->mws += inc; |
| 1793 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1794 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 1795 | if (h2s->send_wait) |
| 1796 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 1797 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1798 | } |
| 1799 | } |
| 1800 | else { |
| 1801 | /* connection window update */ |
| 1802 | if (!inc) { |
| 1803 | error = H2_ERR_PROTOCOL_ERROR; |
| 1804 | goto conn_err; |
| 1805 | } |
| 1806 | |
| 1807 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1808 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1809 | goto conn_err; |
| 1810 | } |
| 1811 | |
| 1812 | h2c->mws += inc; |
| 1813 | } |
| 1814 | |
| 1815 | return 1; |
| 1816 | |
| 1817 | conn_err: |
| 1818 | h2c_error(h2c, error); |
| 1819 | return 0; |
| 1820 | |
| 1821 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 1822 | h2s_error(h2s, error); |
| 1823 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1824 | return 0; |
| 1825 | } |
| 1826 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1827 | /* 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] | 1828 | * the last ID. Returns > 0 on success or zero on missing data. The caller must |
| 1829 | * have already verified frame length and stream ID validity. Described in |
| 1830 | * RFC7540#6.8. |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1831 | */ |
| 1832 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1833 | { |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1834 | int last; |
| 1835 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [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 | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1838 | return 0; |
| 1839 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1840 | last = h2_get_n32(&h2c->dbuf, 0); |
| 1841 | h2c->errcode = h2_get_n32(&h2c->dbuf, 4); |
Olivier Houchard | 9117780 | 2018-12-19 14:49:39 +0100 | [diff] [blame] | 1842 | h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1843 | if (h2c->last_sid < 0) |
| 1844 | h2c->last_sid = last; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1845 | return 1; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1846 | } |
| 1847 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1848 | /* 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] | 1849 | * invalid. Returns > 0 on success or zero on missing data. It may return an |
| 1850 | * error in h2c. The caller must have already verified frame length and stream |
| 1851 | * ID validity. Described in RFC7540#6.3. |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1852 | */ |
| 1853 | static int h2c_handle_priority(struct h2c *h2c) |
| 1854 | { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1855 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1856 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1857 | return 0; |
| 1858 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1859 | if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1860 | /* 7540#5.3 : can't depend on itself */ |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1861 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1862 | return 0; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1863 | } |
| 1864 | return 1; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1865 | } |
| 1866 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1867 | /* 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] | 1868 | * Returns > 0 on success or zero on missing data. The caller must have already |
| 1869 | * verified frame length and stream ID validity. Described in RFC7540#6.4. |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1870 | */ |
| 1871 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1872 | { |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1873 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1874 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1875 | return 0; |
| 1876 | |
| 1877 | /* late RST, already handled */ |
| 1878 | if (h2s->st == H2_SS_CLOSED) |
| 1879 | return 1; |
| 1880 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1881 | h2s->errcode = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1882 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1883 | |
| 1884 | if (h2s->cs) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 1885 | cs_set_error(h2s->cs); |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 1886 | h2s_alert(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | h2s->flags |= H2_SF_RST_RCVD; |
| 1890 | return 1; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1891 | } |
| 1892 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1893 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 1894 | * It may return an error in h2c or h2s. The caller must consider that the |
| 1895 | * return value is the new h2s in case one was allocated (most common case). |
| 1896 | * Described in RFC7540#6.2. Most of the |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1897 | * errors here are reported as connection errors since it's impossible to |
| 1898 | * recover from such errors after the compression context has been altered. |
| 1899 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1900 | 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] | 1901 | { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1902 | struct buffer rxbuf = BUF_NULL; |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1903 | unsigned long long body_len = 0; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1904 | uint32_t flags = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1905 | int error; |
| 1906 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1907 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1908 | return NULL; // empty buffer |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1909 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1910 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1911 | return NULL; // incomplete frame |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1912 | |
| 1913 | /* now either the frame is complete or the buffer is complete */ |
| 1914 | if (h2s->st != H2_SS_IDLE) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1915 | /* The stream exists/existed, this must be a trailers frame */ |
| 1916 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1917 | if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0) |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1918 | goto out; |
| 1919 | goto done; |
| 1920 | } |
Willy Tarreau | 1f03550 | 2019-01-30 11:44:07 +0100 | [diff] [blame] | 1921 | /* the connection was already killed by an RST, let's consume |
| 1922 | * the data and send another RST. |
| 1923 | */ |
| 1924 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
| 1925 | h2s = (struct h2s*)h2_error_stream; |
| 1926 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1927 | } |
| 1928 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 1929 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 1930 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1931 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1932 | goto conn_err; |
| 1933 | } |
Willy Tarreau | 415b1ee | 2019-01-02 13:59:43 +0100 | [diff] [blame] | 1934 | else if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 1935 | goto out; // IDLE but too many cs still present |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1936 | |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1937 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1938 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1939 | /* unrecoverable error ? */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1940 | if (h2c->st0 >= H2_CS_ERROR) |
| 1941 | goto out; |
| 1942 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1943 | if (error <= 0) { |
| 1944 | if (error == 0) |
| 1945 | goto out; // missing data |
| 1946 | |
| 1947 | /* Failed to decode this stream (e.g. too large request) |
| 1948 | * but the HPACK decompressor is still synchronized. |
| 1949 | */ |
| 1950 | h2s = (struct h2s*)h2_error_stream; |
| 1951 | goto send_rst; |
| 1952 | } |
| 1953 | |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1954 | /* 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] | 1955 | * positively from h2c_frt_stream_new(), the stream will report the error, |
| 1956 | * 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] | 1957 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 1958 | h2s = h2c_frt_stream_new(h2c, h2c->dsi); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1959 | if (!h2s) { |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 1960 | h2s = (struct h2s*)h2_refused_stream; |
| 1961 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | h2s->st = H2_SS_OPEN; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1965 | h2s->rxbuf = rxbuf; |
| 1966 | h2s->flags |= flags; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 1967 | h2s->body_len = body_len; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1968 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1969 | done: |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1970 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1971 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1972 | |
| 1973 | if (h2s->flags & H2_SF_ES_RCVD) { |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 1974 | if (h2s->cs) |
| 1975 | h2s->cs->flags |= CS_FL_EOI; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 1976 | if (h2s->st == H2_SS_OPEN) |
| 1977 | h2s->st = H2_SS_HREM; |
| 1978 | else |
| 1979 | h2s_close(h2s); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1980 | } |
| 1981 | |
Willy Tarreau | 3a429f0 | 2019-01-03 11:41:50 +0100 | [diff] [blame] | 1982 | /* update the max stream ID if the request is being processed */ |
| 1983 | if (h2s->id > h2c->max_id) |
| 1984 | h2c->max_id = h2s->id; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1985 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1986 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1987 | |
| 1988 | conn_err: |
| 1989 | h2c_error(h2c, error); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1990 | goto out; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1991 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1992 | out: |
| 1993 | h2_release_buf(h2c, &rxbuf); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1994 | return NULL; |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 1995 | |
| 1996 | send_rst: |
| 1997 | /* make the demux send an RST for the current stream. We may only |
| 1998 | * do this if we're certain that the HEADERS frame was properly |
| 1999 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2000 | */ |
| 2001 | h2_release_buf(h2c, &rxbuf); |
| 2002 | h2c->st0 = H2_CS_FRAME_E; |
| 2003 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2004 | } |
| 2005 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2006 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 2007 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 2008 | * errors here are reported as connection errors since it's impossible to |
| 2009 | * recover from such errors after the compression context has been altered. |
| 2010 | */ |
| 2011 | static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 2012 | { |
| 2013 | int error; |
| 2014 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2015 | if (!b_size(&h2c->dbuf)) |
| 2016 | return NULL; // empty buffer |
| 2017 | |
| 2018 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
| 2019 | return NULL; // incomplete frame |
| 2020 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2021 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2022 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2023 | /* unrecoverable error ? */ |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2024 | if (h2c->st0 >= H2_CS_ERROR) |
| 2025 | return NULL; |
| 2026 | |
Willy Tarreau | 08bb1d6 | 2019-01-30 16:55:48 +0100 | [diff] [blame] | 2027 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2028 | /* RFC7540#5.1 */ |
| 2029 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2030 | h2c->st0 = H2_CS_FRAME_E; |
| 2031 | return NULL; |
| 2032 | } |
| 2033 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2034 | if (error <= 0) { |
| 2035 | if (error == 0) |
| 2036 | return NULL; // missing data |
| 2037 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2038 | /* stream error : send RST_STREAM */ |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2039 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2040 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2041 | return NULL; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2042 | } |
| 2043 | |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2044 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 2045 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2046 | if (h2s->cs) |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2047 | h2s->cs->flags |= CS_FL_EOI; |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2048 | } |
| 2049 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2050 | 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] | 2051 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2052 | else if (h2s->cs && (h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN) |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2053 | h2s->st = H2_SS_HREM; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2054 | else if ((!h2s || h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC) |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2055 | h2s_close(h2s); |
| 2056 | |
| 2057 | return h2s; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2058 | } |
| 2059 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2060 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 2061 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 2062 | */ |
| 2063 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 2064 | { |
| 2065 | int error; |
| 2066 | |
| 2067 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 2068 | * to signal an end of stream (with the ES flag). |
| 2069 | */ |
| 2070 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2071 | if (!b_size(&h2c->dbuf) && h2c->dfl) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2072 | return 0; // empty buffer |
| 2073 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2074 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2075 | return 0; // incomplete frame |
| 2076 | |
| 2077 | /* now either the frame is complete or the buffer is complete */ |
| 2078 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2079 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2080 | /* RFC7540#6.1 */ |
| 2081 | error = H2_ERR_STREAM_CLOSED; |
| 2082 | goto strm_err; |
| 2083 | } |
| 2084 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2085 | if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) { |
| 2086 | /* RFC7540#8.1.2 */ |
| 2087 | error = H2_ERR_PROTOCOL_ERROR; |
| 2088 | goto strm_err; |
| 2089 | } |
| 2090 | |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 2091 | if (!h2_frt_transfer_data(h2s)) |
| 2092 | return 0; |
| 2093 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2094 | /* call the upper layers to process the frame, then let the upper layer |
| 2095 | * notify the stream about any change. |
| 2096 | */ |
| 2097 | if (!h2s->cs) { |
| 2098 | error = H2_ERR_STREAM_CLOSED; |
| 2099 | goto strm_err; |
| 2100 | } |
| 2101 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 2102 | if (h2c->st0 >= H2_CS_ERROR) |
| 2103 | return 0; |
| 2104 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2105 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2106 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2107 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | /* check for completion : the callee will change this to FRAME_A or |
| 2111 | * FRAME_H once done. |
| 2112 | */ |
| 2113 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2114 | return 0; |
| 2115 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2116 | /* last frame */ |
| 2117 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2118 | if (h2s->cs) |
| 2119 | h2s->cs->flags |= CS_FL_EOI; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2120 | if (h2s->st == H2_SS_OPEN) |
| 2121 | h2s->st = H2_SS_HREM; |
| 2122 | else |
| 2123 | h2s_close(h2s); |
| 2124 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2125 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2126 | |
| 2127 | if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) { |
| 2128 | /* RFC7540#8.1.2 */ |
| 2129 | error = H2_ERR_PROTOCOL_ERROR; |
| 2130 | goto strm_err; |
| 2131 | } |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2132 | } |
| 2133 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2134 | return 1; |
| 2135 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2136 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 2137 | h2s_error(h2s, error); |
| 2138 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2139 | return 0; |
| 2140 | } |
| 2141 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2142 | /* process Rx frames to be demultiplexed */ |
| 2143 | static void h2_process_demux(struct h2c *h2c) |
| 2144 | { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2145 | struct h2s *h2s = NULL, *tmp_h2s; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2146 | struct h2_fh hdr; |
| 2147 | unsigned int padlen = 0; |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 2148 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2149 | if (h2c->st0 >= H2_CS_ERROR) |
| 2150 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2151 | |
| 2152 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2153 | if (h2c->st0 == H2_CS_PREFACE) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2154 | if (h2c->flags & H2_CF_IS_BACK) |
| 2155 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2156 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 2157 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2158 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2159 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2160 | sess_log(h2c->conn->owner); |
| 2161 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2162 | goto fail; |
| 2163 | } |
| 2164 | |
| 2165 | h2c->max_id = 0; |
| 2166 | h2c->st0 = H2_CS_SETTINGS1; |
| 2167 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2168 | |
| 2169 | if (h2c->st0 == H2_CS_SETTINGS1) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2170 | /* ensure that what is pending is a valid SETTINGS frame |
| 2171 | * without an ACK. |
| 2172 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2173 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2174 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2175 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2176 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2177 | sess_log(h2c->conn->owner); |
| 2178 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2179 | goto fail; |
| 2180 | } |
| 2181 | |
| 2182 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 2183 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2184 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2185 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2186 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2187 | goto fail; |
| 2188 | } |
| 2189 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2190 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2191 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2192 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2193 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2194 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2195 | goto fail; |
| 2196 | } |
| 2197 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2198 | /* that's OK, switch to FRAME_P to process it. This is |
| 2199 | * a SETTINGS frame whose header has already been |
| 2200 | * deleted above. |
| 2201 | */ |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2202 | padlen = 0; |
| 2203 | goto new_frame; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2204 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2205 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2206 | |
| 2207 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2208 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2209 | int ret = 0; |
| 2210 | |
| 2211 | if (h2c->st0 >= H2_CS_ERROR) |
| 2212 | break; |
| 2213 | |
| 2214 | if (h2c->st0 == H2_CS_FRAME_H) { |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 2215 | if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2216 | break; |
| 2217 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2218 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2219 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2220 | if (!h2c->nb_streams) { |
| 2221 | /* only log if no other stream can report the error */ |
| 2222 | sess_log(h2c->conn->owner); |
| 2223 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2224 | break; |
| 2225 | } |
| 2226 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2227 | if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) { |
| 2228 | /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA), |
| 2229 | * we read the pad length and drop it from the remaining |
| 2230 | * payload (one byte + the 9 remaining ones = 10 total |
| 2231 | * removed), so we have a frame payload starting after the |
| 2232 | * pad len. Flow controlled frames (DATA) also count the |
| 2233 | * padlen in the flow control, so it must be adjusted. |
| 2234 | */ |
| 2235 | if (hdr.len < 1) { |
| 2236 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2237 | sess_log(h2c->conn->owner); |
| 2238 | goto fail; |
| 2239 | } |
| 2240 | hdr.len--; |
| 2241 | |
| 2242 | if (b_data(&h2c->dbuf) < 10) |
| 2243 | break; // missing padlen |
| 2244 | |
| 2245 | padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9); |
| 2246 | |
| 2247 | if (padlen > hdr.len) { |
| 2248 | /* RFC7540#6.1 : pad length = length of |
| 2249 | * frame payload or greater => error. |
| 2250 | */ |
| 2251 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2252 | sess_log(h2c->conn->owner); |
| 2253 | goto fail; |
| 2254 | } |
| 2255 | |
| 2256 | if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) { |
| 2257 | h2c->rcvd_c++; |
| 2258 | h2c->rcvd_s++; |
| 2259 | } |
| 2260 | b_del(&h2c->dbuf, 1); |
| 2261 | } |
| 2262 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2263 | |
| 2264 | new_frame: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2265 | h2c->dfl = hdr.len; |
| 2266 | h2c->dsi = hdr.sid; |
| 2267 | h2c->dft = hdr.ft; |
| 2268 | h2c->dff = hdr.ff; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2269 | h2c->dpl = padlen; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2270 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2271 | |
| 2272 | /* check for minimum basic frame format validity */ |
| 2273 | ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize); |
| 2274 | if (ret != H2_ERR_NO_ERROR) { |
| 2275 | h2c_error(h2c, ret); |
| 2276 | sess_log(h2c->conn->owner); |
| 2277 | goto fail; |
| 2278 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2282 | tmp_h2s = h2c_st_by_id(h2c, h2c->dsi); |
| 2283 | |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2284 | if (tmp_h2s != h2s && h2s && h2s->cs && |
| 2285 | (b_data(&h2s->rxbuf) || |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2286 | (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS|CS_FL_EOI)))) { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2287 | /* we may have to signal the upper layers */ |
| 2288 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2289 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2290 | } |
| 2291 | h2s = tmp_h2s; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2292 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 2293 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2294 | goto strm_err; |
| 2295 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2296 | if (h2s->st == H2_SS_IDLE && |
| 2297 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 2298 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 2299 | * this state MUST be treated as a connection error |
| 2300 | */ |
| 2301 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2302 | if (!h2c->nb_streams) { |
| 2303 | /* only log if no other stream can report the error */ |
| 2304 | sess_log(h2c->conn->owner); |
| 2305 | } |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2306 | break; |
| 2307 | } |
| 2308 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2309 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 2310 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 2311 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2312 | * this state MUST be treated as a stream error. |
| 2313 | * 6.2, 6.6 and 6.10 further mandate that HEADERS/ |
| 2314 | * PUSH_PROMISE/CONTINUATION cause connection errors. |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2315 | */ |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2316 | if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) |
| 2317 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2318 | else |
| 2319 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2320 | goto strm_err; |
| 2321 | } |
| 2322 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2323 | /* Below the management of frames received in closed state is a |
| 2324 | * bit hackish because the spec makes strong differences between |
| 2325 | * streams closed by receiving RST, sending RST, and seeing ES |
| 2326 | * in both directions. In addition to this, the creation of a |
| 2327 | * new stream reusing the identifier of a closed one will be |
| 2328 | * detected here. Given that we cannot keep track of all closed |
| 2329 | * streams forever, we consider that unknown closed streams were |
| 2330 | * closed on RST received, which allows us to respond with an |
| 2331 | * RST without breaking the connection (eg: to abort a transfer). |
| 2332 | * Some frames have to be silently ignored as well. |
| 2333 | */ |
| 2334 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
Willy Tarreau | 3ad5d31 | 2019-01-29 18:33:26 +0100 | [diff] [blame] | 2335 | 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] | 2336 | /* #5.1.1: The identifier of a newly |
| 2337 | * established stream MUST be numerically |
| 2338 | * greater than all streams that the initiating |
| 2339 | * endpoint has opened or reserved. This |
| 2340 | * governs streams that are opened using a |
| 2341 | * HEADERS frame and streams that are reserved |
| 2342 | * using PUSH_PROMISE. An endpoint that |
| 2343 | * receives an unexpected stream identifier |
| 2344 | * MUST respond with a connection error. |
| 2345 | */ |
| 2346 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2347 | goto strm_err; |
| 2348 | } |
| 2349 | |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2350 | 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] | 2351 | /* RFC7540#5.1:closed: an endpoint that |
| 2352 | * receives any frame other than PRIORITY after |
| 2353 | * receiving a RST_STREAM MUST treat that as a |
| 2354 | * stream error of type STREAM_CLOSED. |
| 2355 | * |
| 2356 | * Note that old streams fall into this category |
| 2357 | * and will lead to an RST being sent. |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2358 | * |
| 2359 | * However, we cannot generalize this to all frame types. Those |
| 2360 | * carrying compression state must still be processed before |
| 2361 | * being dropped or we'll desynchronize the decoder. This can |
| 2362 | * happen with request trailers received after sending an |
| 2363 | * RST_STREAM, or with header/trailers responses received after |
| 2364 | * sending RST_STREAM (aborted stream). |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2365 | */ |
| 2366 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2367 | h2c->st0 = H2_CS_FRAME_E; |
| 2368 | goto strm_err; |
| 2369 | } |
| 2370 | |
| 2371 | /* RFC7540#5.1:closed: if this state is reached as a |
| 2372 | * result of sending a RST_STREAM frame, the peer that |
| 2373 | * receives the RST_STREAM might have already sent |
| 2374 | * frames on the stream that cannot be withdrawn. An |
| 2375 | * endpoint MUST ignore frames that it receives on |
| 2376 | * closed streams after it has sent a RST_STREAM |
| 2377 | * frame. An endpoint MAY choose to limit the period |
| 2378 | * over which it ignores frames and treat frames that |
| 2379 | * arrive after this time as being in error. |
| 2380 | */ |
Willy Tarreau | 24ff1f8 | 2019-01-30 19:20:09 +0100 | [diff] [blame] | 2381 | if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2382 | /* RFC7540#5.1:closed: any frame other than |
| 2383 | * PRIO/WU/RST in this state MUST be treated as |
| 2384 | * a connection error |
| 2385 | */ |
| 2386 | if (h2c->dft != H2_FT_RST_STREAM && |
| 2387 | h2c->dft != H2_FT_PRIORITY && |
| 2388 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 2389 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2390 | goto strm_err; |
| 2391 | } |
| 2392 | } |
| 2393 | } |
| 2394 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2395 | #if 0 |
| 2396 | // problem below: it is not possible to completely ignore such |
| 2397 | // streams as we need to maintain the compression state as well |
| 2398 | // and for this we need to completely process these frames (eg: |
| 2399 | // HEADERS frames) as well as counting DATA frames to emit |
| 2400 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 2401 | // This is a typical case of layer violation where the |
| 2402 | // transported contents are critical to the connection's |
| 2403 | // validity and must be ignored at the same time :-( |
| 2404 | |
| 2405 | /* graceful shutdown, ignore streams whose ID is higher than |
| 2406 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 2407 | */ |
| 2408 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2409 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2410 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2411 | h2c->dfl -= ret; |
| 2412 | ret = h2c->dfl == 0; |
| 2413 | goto strm_err; |
| 2414 | } |
| 2415 | #endif |
| 2416 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2417 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 2418 | case H2_FT_SETTINGS: |
| 2419 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2420 | ret = h2c_handle_settings(h2c); |
| 2421 | |
| 2422 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2423 | ret = h2c_ack_settings(h2c); |
| 2424 | break; |
| 2425 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 2426 | case H2_FT_PING: |
| 2427 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2428 | ret = h2c_handle_ping(h2c); |
| 2429 | |
| 2430 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2431 | ret = h2c_ack_ping(h2c); |
| 2432 | break; |
| 2433 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 2434 | case H2_FT_WINDOW_UPDATE: |
| 2435 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2436 | ret = h2c_handle_window_update(h2c, h2s); |
| 2437 | break; |
| 2438 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2439 | case H2_FT_CONTINUATION: |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2440 | /* RFC7540#6.10: CONTINUATION may only be preceeded by |
| 2441 | * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These |
| 2442 | * frames' parsers consume all following CONTINUATION |
| 2443 | * frames so this one is out of sequence. |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2444 | */ |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2445 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2446 | sess_log(h2c->conn->owner); |
| 2447 | goto fail; |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2448 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2449 | case H2_FT_HEADERS: |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2450 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2451 | if (h2c->flags & H2_CF_IS_BACK) |
| 2452 | tmp_h2s = h2c_bck_handle_headers(h2c, h2s); |
| 2453 | else |
| 2454 | tmp_h2s = h2c_frt_handle_headers(h2c, h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2455 | if (tmp_h2s) { |
| 2456 | h2s = tmp_h2s; |
| 2457 | ret = 1; |
| 2458 | } |
| 2459 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2460 | break; |
| 2461 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2462 | case H2_FT_DATA: |
| 2463 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2464 | ret = h2c_frt_handle_data(h2c, h2s); |
| 2465 | |
| 2466 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2467 | ret = h2c_send_strm_wu(h2c); |
| 2468 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2469 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 2470 | case H2_FT_PRIORITY: |
| 2471 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2472 | ret = h2c_handle_priority(h2c); |
| 2473 | break; |
| 2474 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2475 | case H2_FT_RST_STREAM: |
| 2476 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2477 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 2478 | break; |
| 2479 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 2480 | case H2_FT_GOAWAY: |
| 2481 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2482 | ret = h2c_handle_goaway(h2c); |
| 2483 | break; |
| 2484 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2485 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2486 | default: |
| 2487 | /* drop frames that we ignore. They may be larger than |
| 2488 | * the buffer so we drain all of their contents until |
| 2489 | * we reach the end. |
| 2490 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2491 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2492 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2493 | h2c->dfl -= ret; |
| 2494 | ret = h2c->dfl == 0; |
| 2495 | } |
| 2496 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2497 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2498 | /* We may have to send an RST if not done yet */ |
| 2499 | if (h2s->st == H2_SS_ERROR) |
| 2500 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2501 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2502 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2503 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2504 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2505 | /* error or missing data condition met above ? */ |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2506 | if (ret <= 0) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2507 | break; |
| 2508 | |
| 2509 | if (h2c->st0 != H2_CS_FRAME_H) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2510 | b_del(&h2c->dbuf, h2c->dfl); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2511 | h2c->st0 = H2_CS_FRAME_H; |
| 2512 | } |
| 2513 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2514 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2515 | if (h2c->rcvd_c > 0 && |
| 2516 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2517 | h2c_send_conn_wu(h2c); |
| 2518 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2519 | fail: |
| 2520 | /* we can go here on missing data, blocked response or error */ |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2521 | if (h2s && h2s->cs && |
| 2522 | (b_data(&h2s->rxbuf) || |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2523 | (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS|CS_FL_EOI)))) { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2524 | /* we may have to signal the upper layers */ |
| 2525 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2526 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2527 | } |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2528 | |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 2529 | h2c_restart_reading(h2c); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2533 | * the end. |
| 2534 | */ |
| 2535 | static int h2_process_mux(struct h2c *h2c) |
| 2536 | { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2537 | struct h2s *h2s; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2538 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2539 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2540 | if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) { |
| 2541 | if (unlikely(h2c_bck_send_preface(h2c) <= 0)) { |
| 2542 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2543 | if (h2c->st0 == H2_CS_ERROR) { |
| 2544 | h2c->st0 = H2_CS_ERROR2; |
| 2545 | sess_log(h2c->conn->owner); |
| 2546 | } |
| 2547 | goto fail; |
| 2548 | } |
| 2549 | h2c->st0 = H2_CS_SETTINGS1; |
| 2550 | } |
| 2551 | /* need to wait for the other side */ |
Willy Tarreau | 75a930a | 2018-12-12 08:03:58 +0100 | [diff] [blame] | 2552 | if (h2c->st0 < H2_CS_FRAME_H) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2553 | return 1; |
| 2554 | } |
| 2555 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2556 | /* start by sending possibly pending window updates */ |
| 2557 | if (h2c->rcvd_c > 0 && |
| 2558 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2559 | h2c_send_conn_wu(h2c) < 0) |
| 2560 | goto fail; |
| 2561 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2562 | /* First we always process the flow control list because the streams |
| 2563 | * waiting there were already elected for immediate emission but were |
| 2564 | * blocked just on this. |
| 2565 | */ |
| 2566 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2567 | list_for_each_entry(h2s, &h2c->fctl_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2568 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2569 | h2c->st0 >= H2_CS_ERROR) |
| 2570 | break; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2571 | if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE) |
| 2572 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2573 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2574 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2575 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2576 | h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2577 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2578 | tasklet_wakeup(h2s->send_wait->task); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2579 | } |
| 2580 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2581 | list_for_each_entry(h2s, &h2c->send_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2582 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2583 | break; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2584 | if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE) |
| 2585 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2586 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2587 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2588 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2589 | h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2590 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2591 | tasklet_wakeup(h2s->send_wait->task); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2592 | } |
| 2593 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2594 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2595 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2596 | if (h2c->st0 == H2_CS_ERROR) { |
| 2597 | if (h2c->max_id >= 0) { |
| 2598 | h2c_send_goaway_error(h2c, NULL); |
| 2599 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2600 | return 0; |
| 2601 | } |
| 2602 | |
| 2603 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2604 | } |
| 2605 | return 1; |
| 2606 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2607 | return (1); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2608 | } |
| 2609 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2610 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2611 | /* Attempt to read data, and subscribe if none available. |
| 2612 | * The function returns 1 if data has been received, otherwise zero. |
| 2613 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2614 | static int h2_recv(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2615 | { |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2616 | struct connection *conn = h2c->conn; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2617 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2618 | int max; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2619 | size_t ret; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2620 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2621 | if (h2c->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2622 | return (b_data(&h2c->dbuf)); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2623 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2624 | if (!h2_recv_allowed(h2c)) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2625 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2626 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2627 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2628 | if (!buf) { |
| 2629 | h2c->flags |= H2_CF_DEM_DALLOC; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2630 | return 0; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2631 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2632 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2633 | do { |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 2634 | b_realign_if_empty(buf); |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2635 | if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
| 2636 | /* HTX in use : try to pre-align the buffer like the |
| 2637 | * rxbufs will be to optimize memory copies. We'll make |
| 2638 | * sure that the frame header lands at the end of the |
| 2639 | * HTX block to alias it upon recv. We cannot use the |
| 2640 | * head because rcv_buf() will realign the buffer if |
| 2641 | * it's empty. Thus we cheat and pretend we already |
| 2642 | * have a few bytes there. |
| 2643 | */ |
| 2644 | max = buf_room_for_htx_data(buf) + 9; |
Willy Tarreau | c0960d1 | 2018-12-14 10:59:15 +0100 | [diff] [blame] | 2645 | buf->head = sizeof(struct htx) - 9; |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2646 | } |
| 2647 | else |
| 2648 | max = b_room(buf); |
| 2649 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2650 | if (max) |
| 2651 | ret = conn->xprt->rcv_buf(conn, buf, max, 0); |
| 2652 | else |
| 2653 | ret = 0; |
| 2654 | } while (ret > 0); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2655 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2656 | if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size)) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2657 | conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event); |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2658 | |
Olivier Houchard | a1411e6 | 2018-08-17 18:42:48 +0200 | [diff] [blame] | 2659 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2660 | h2_release_buf(h2c, &h2c->dbuf); |
Olivier Houchard | 4667773 | 2018-11-29 17:06:17 +0100 | [diff] [blame] | 2661 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2662 | } |
| 2663 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2664 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2665 | h2c->flags |= H2_CF_DEM_DFULL; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2666 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2667 | } |
| 2668 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2669 | /* Try to send data if possible. |
| 2670 | * The function returns 1 if data have been sent, otherwise zero. |
| 2671 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2672 | static int h2_send(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2673 | { |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2674 | struct connection *conn = h2c->conn; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2675 | int done; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2676 | int sent = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2677 | |
| 2678 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 2679 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2680 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2681 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2682 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2683 | /* a handshake was requested */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2684 | goto schedule; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2685 | } |
| 2686 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2687 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2688 | * pending streams into the existing buffer until it's reportedly full |
| 2689 | * or the end of send requests is reached. Then it tries to send this |
| 2690 | * buffer's contents out, marks it not full if at least one byte could |
| 2691 | * be sent, and tries again. |
| 2692 | * |
| 2693 | * The snd_buf() function normally takes a "flags" argument which may |
| 2694 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2695 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2696 | * connection is streaming lots of data (used to increase TLS record |
| 2697 | * size at the expense of latency). The former can be sent any time |
| 2698 | * there's a buffer full flag, as it indicates at least one stream |
| 2699 | * attempted to send and failed so there are pending data. An |
| 2700 | * alternative would be to set it as long as there's an active stream |
| 2701 | * but that would be problematic for ACKs until we have an absolute |
| 2702 | * guarantee that all waiters have at least one byte to send. The |
| 2703 | * latter should possibly not be set for now. |
| 2704 | */ |
| 2705 | |
| 2706 | done = 0; |
| 2707 | while (!done) { |
| 2708 | unsigned int flags = 0; |
| 2709 | |
| 2710 | /* fill as much as we can into the current buffer */ |
| 2711 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2712 | done = h2_process_mux(h2c); |
| 2713 | |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2714 | if (h2c->flags & H2_CF_MUX_MALLOC) |
| 2715 | break; |
| 2716 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2717 | if (conn->flags & CO_FL_ERROR) |
| 2718 | break; |
| 2719 | |
| 2720 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2721 | flags |= CO_SFL_MSG_MORE; |
| 2722 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2723 | if (b_data(&h2c->mbuf)) { |
| 2724 | 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] | 2725 | if (!ret) |
| 2726 | break; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2727 | sent = 1; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2728 | b_del(&h2c->mbuf, ret); |
| 2729 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2730 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2731 | |
| 2732 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2733 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2734 | } |
| 2735 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2736 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2737 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2738 | b_reset(&h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2739 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2740 | /* We're not full anymore, so we can wake any task that are waiting |
| 2741 | * for us. |
| 2742 | */ |
| 2743 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2744 | struct h2s *h2s; |
| 2745 | |
| 2746 | list_for_each_entry(h2s, &h2c->send_list, list) { |
| 2747 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2748 | break; |
| 2749 | if (h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE) |
| 2750 | continue; |
| 2751 | |
| 2752 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2753 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2754 | h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2755 | tasklet_wakeup(h2s->send_wait->task); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2756 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 2757 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2758 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2759 | /* We're done, no more to send */ |
| 2760 | if (!b_data(&h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2761 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2762 | schedule: |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2763 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
| 2764 | conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event); |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2765 | return sent; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2766 | } |
| 2767 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 2768 | /* this is the tasklet referenced in h2c->wait_event.task */ |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2769 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2770 | { |
| 2771 | struct h2c *h2c = ctx; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2772 | int ret = 0; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2773 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2774 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2775 | ret = h2_send(h2c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2776 | if (!(h2c->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2777 | ret |= h2_recv(h2c); |
Willy Tarreau | cef5c8e | 2018-12-18 10:29:54 +0100 | [diff] [blame] | 2778 | if (ret || b_data(&h2c->dbuf)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2779 | h2_process(h2c); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2780 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2781 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2782 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2783 | /* callback called on any event by the connection handler. |
| 2784 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2785 | * destruction of the connection (which normally doesn not happen in h2). |
| 2786 | */ |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2787 | static int h2_process(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2788 | { |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2789 | struct connection *conn = h2c->conn; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2790 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2791 | if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2792 | h2_process_demux(h2c); |
| 2793 | |
| 2794 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2795 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2796 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2797 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2798 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 2799 | } |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2800 | h2_send(h2c); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2801 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 2802 | if (unlikely(h2c->proxy->state == PR_STSTOPPED)) { |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2803 | /* frontend is stopping, reload likely in progress, let's try |
| 2804 | * to announce a graceful shutdown if not yet done. We don't |
| 2805 | * care if it fails, it will be tried again later. |
| 2806 | */ |
| 2807 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 2808 | if (h2c->last_sid < 0) |
| 2809 | h2c->last_sid = (1U << 31) - 1; |
| 2810 | h2c_send_goaway_error(h2c, NULL); |
| 2811 | } |
| 2812 | } |
| 2813 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2814 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2815 | * If we received early data, and the handshake is done, wake |
| 2816 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2817 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2818 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 2819 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 2820 | struct eb32_node *node; |
| 2821 | struct h2s *h2s; |
| 2822 | |
| 2823 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 2824 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 2825 | |
| 2826 | while (node) { |
| 2827 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | fde287c | 2018-12-19 18:33:16 +0100 | [diff] [blame] | 2828 | if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2829 | h2s_notify_recv(h2s); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2830 | node = eb32_next(node); |
| 2831 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2832 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2833 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 2834 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 2835 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 2836 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 2837 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 2838 | h2_wake_some_streams(h2c, 0, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2839 | |
| 2840 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2841 | /* no more stream, kill the connection now */ |
| 2842 | h2_release(conn); |
| 2843 | return -1; |
| 2844 | } |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2845 | } |
| 2846 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2847 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2848 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2849 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2850 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 2851 | h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 2852 | (h2c->st0 != H2_CS_ERROR && |
| 2853 | !b_data(&h2c->mbuf) && |
| 2854 | (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && |
| 2855 | ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list)))) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2856 | h2_release_buf(h2c, &h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2857 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2858 | if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2859 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2860 | 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] | 2861 | task_queue(h2c->task); |
| 2862 | } |
| 2863 | else |
| 2864 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2865 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2866 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2867 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2868 | return 0; |
| 2869 | } |
| 2870 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 2871 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2872 | static int h2_wake(struct connection *conn) |
| 2873 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2874 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2875 | |
| 2876 | return (h2_process(h2c)); |
| 2877 | } |
| 2878 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2879 | /* Connection timeout management. The principle is that if there's no receipt |
| 2880 | * nor sending for a certain amount of time, the connection is closed. If the |
| 2881 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 2882 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 2883 | * GOAWAY frame. |
| 2884 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2885 | 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] | 2886 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2887 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2888 | int expired = tick_is_expired(t->expire, now_ms); |
| 2889 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2890 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2891 | return t; |
| 2892 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2893 | task_delete(t); |
| 2894 | task_free(t); |
| 2895 | |
| 2896 | if (!h2c) { |
| 2897 | /* resources were already deleted */ |
| 2898 | return NULL; |
| 2899 | } |
| 2900 | |
| 2901 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2902 | h2c_error(h2c, H2_ERR_NO_ERROR); |
| 2903 | h2_wake_some_streams(h2c, 0, 0); |
| 2904 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2905 | if (b_data(&h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2906 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 2907 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2908 | } |
| 2909 | |
| 2910 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2911 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2912 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 2913 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2914 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2915 | if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) { |
| 2916 | 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] | 2917 | if (ret > 0) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2918 | b_del(&h2c->mbuf, ret); |
| 2919 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2920 | } |
| 2921 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2922 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2923 | /* either we can release everything now or it will be done later once |
| 2924 | * the last stream closes. |
| 2925 | */ |
| 2926 | if (eb_is_empty(&h2c->streams_by_id)) |
| 2927 | h2_release(h2c->conn); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2928 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2929 | return NULL; |
| 2930 | } |
| 2931 | |
| 2932 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2933 | /*******************************************/ |
| 2934 | /* functions below are used by the streams */ |
| 2935 | /*******************************************/ |
| 2936 | |
| 2937 | /* |
| 2938 | * Attach a new stream to a connection |
| 2939 | * (Used for outgoing connections) |
| 2940 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2941 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2942 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2943 | struct conn_stream *cs; |
| 2944 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2945 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2946 | |
| 2947 | cs = cs_new(conn); |
| 2948 | if (!cs) |
| 2949 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2950 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2951 | if (!h2s) { |
| 2952 | cs_free(cs); |
| 2953 | return NULL; |
| 2954 | } |
| 2955 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2956 | } |
| 2957 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 2958 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 2959 | * We have to scan because we may have some orphan streams. It might be |
| 2960 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 2961 | * orphans. |
| 2962 | */ |
| 2963 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 2964 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2965 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 2966 | struct h2s *h2s; |
| 2967 | struct eb32_node *node; |
| 2968 | |
| 2969 | node = eb32_first(&h2c->streams_by_id); |
| 2970 | while (node) { |
| 2971 | h2s = container_of(node, struct h2s, by_id); |
| 2972 | if (h2s->cs) |
| 2973 | return h2s->cs; |
| 2974 | node = eb32_next(node); |
| 2975 | } |
| 2976 | return NULL; |
| 2977 | } |
| 2978 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2979 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 2980 | * Destroy the mux and the associated connection, if it is no longer used |
| 2981 | */ |
| 2982 | static void h2_destroy(struct connection *conn) |
| 2983 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2984 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 2985 | |
| 2986 | if (eb_is_empty(&h2c->streams_by_id)) |
| 2987 | h2_release(h2c->conn); |
| 2988 | } |
| 2989 | |
| 2990 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2991 | * Detach the stream from the connection and possibly release the connection. |
| 2992 | */ |
| 2993 | static void h2_detach(struct conn_stream *cs) |
| 2994 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2995 | struct h2s *h2s = cs->ctx; |
| 2996 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2997 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2998 | |
| 2999 | cs->ctx = NULL; |
| 3000 | if (!h2s) |
| 3001 | return; |
| 3002 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3003 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3004 | h2c = h2s->h2c; |
| 3005 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 3006 | h2c->nb_cs--; |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 3007 | if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY && |
| 3008 | !h2_frt_has_too_many_cs(h2c)) { |
| 3009 | /* frontend connection was blocking new streams creation */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3010 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 3011 | h2c_restart_reading(h2c); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3012 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3013 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3014 | /* this stream may be blocked waiting for some data to leave (possibly |
| 3015 | * an ES or RST frame), so orphan it in this case. |
| 3016 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 3017 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 3018 | (h2c->st0 < H2_CS_ERROR) && |
Olivier Houchard | 16ff261 | 2019-03-21 15:48:46 +0100 | [diff] [blame] | 3019 | (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) && (h2s->send_wait || h2s->recv_wait)) |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3020 | return; |
| 3021 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3022 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 3023 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 3024 | /* unblock the connection if it was blocked on this |
| 3025 | * stream. |
| 3026 | */ |
| 3027 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 3028 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 3029 | h2c_restart_reading(h2c); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3030 | } |
| 3031 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 3032 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3033 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3034 | if (h2c->flags & H2_CF_IS_BACK && |
| 3035 | (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3036 | if (!(h2c->conn->flags & |
| 3037 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 3038 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3039 | h2c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3040 | if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) { |
| 3041 | h2c->conn->owner = NULL; |
| 3042 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3043 | if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn)) |
| 3044 | /* The server doesn't want it, let's kill the connection right away */ |
| 3045 | h2c->conn->mux->destroy(h2c->conn); |
| 3046 | return; |
| 3047 | } |
| 3048 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3049 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3050 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3051 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 3052 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 3053 | return; |
| 3054 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3055 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3056 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3057 | h2c->conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 3058 | if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3059 | struct server *srv = objt_server(h2c->conn->target); |
| 3060 | |
| 3061 | if (srv) { |
| 3062 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 3063 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 3064 | else |
| 3065 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 3066 | } |
| 3067 | |
| 3068 | } |
| 3069 | } |
| 3070 | } |
| 3071 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3072 | /* We don't want to close right now unless we're removing the |
| 3073 | * last stream, and either the connection is in error, or it |
| 3074 | * reached the ID already specified in a GOAWAY frame received |
| 3075 | * or sent (as seen by last_sid >= 0). |
| 3076 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3077 | if (h2c_is_dead(h2c)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3078 | /* no more stream will come, kill it now */ |
| 3079 | h2_release(h2c->conn); |
| 3080 | } |
| 3081 | else if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3082 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3083 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3084 | task_queue(h2c->task); |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 3085 | } |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3086 | else |
| 3087 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3088 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3089 | } |
| 3090 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3091 | /* Performs a synchronous or asynchronous shutr(). |
| 3092 | * FIXME: guess what the return code tries to indicate! |
| 3093 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3094 | static int h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3095 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3096 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3097 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3098 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 3099 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3100 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3101 | |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3102 | /* a connstream may require us to immediately kill the whole connection |
| 3103 | * for example because of a "tcp-request content reject" rule that is |
| 3104 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3105 | * close the connection. |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3106 | */ |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3107 | if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) && |
| 3108 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3109 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3110 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3111 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3112 | else if (!(h2s->flags & H2_SF_HEADERS_SENT)) { |
| 3113 | /* Nothing was never sent for this stream, so reset with |
| 3114 | * REFUSED_STREAM error to let the client retry the |
| 3115 | * request. |
| 3116 | */ |
| 3117 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3118 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3119 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3120 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3121 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3122 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3123 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3124 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3125 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3126 | h2s_close(h2s); |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3127 | |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3128 | return 0; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3129 | add_to_list: |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3130 | if (LIST_ISEMPTY(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3131 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3132 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3133 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3134 | h2s->send_wait = sw; |
| 3135 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3136 | h2s->send_wait = sw; |
| 3137 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3138 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3139 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3140 | /* Let the handler know we want shutr */ |
| 3141 | sw->handle = (void *)((long)sw->handle | 1); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3142 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3143 | } |
| 3144 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3145 | /* Performs a synchronous or asynchronous shutw(). |
| 3146 | * FIXME: guess what the return code tries to indicate! |
| 3147 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3148 | static int h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3149 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3150 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3151 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3152 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 3153 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3154 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3155 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3156 | if (h2s->flags & H2_SF_HEADERS_SENT) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3157 | /* we can cleanly close using an empty data frame only after headers */ |
| 3158 | |
| 3159 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 3160 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3161 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3162 | |
| 3163 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3164 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3165 | else |
| 3166 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3167 | } else { |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3168 | /* a connstream may require us to immediately kill the whole connection |
| 3169 | * for example because of a "tcp-request content reject" rule that is |
| 3170 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3171 | * close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3172 | */ |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3173 | if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) && |
| 3174 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3175 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3176 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3177 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3178 | else { |
| 3179 | /* Nothing was never sent for this stream, so reset with |
| 3180 | * REFUSED_STREAM error to let the client retry the |
| 3181 | * request. |
| 3182 | */ |
| 3183 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3184 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3185 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3186 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3187 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3188 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3189 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3190 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3191 | } |
| 3192 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3193 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3194 | tasklet_wakeup(h2c->wait_event.task); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3195 | return 0; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3196 | |
| 3197 | add_to_list: |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3198 | if (LIST_ISEMPTY(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3199 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3200 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3201 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3202 | h2s->send_wait = sw; |
| 3203 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3204 | h2s->send_wait = sw; |
| 3205 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3206 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3207 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3208 | /* let the handler know we want to shutw */ |
| 3209 | sw->handle = (void *)((long)(sw->handle) | 2); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3210 | return 1; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3211 | } |
| 3212 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3213 | /* This is the tasklet referenced in h2s->wait_event.task, it is used for |
| 3214 | * deferred shutdowns when the h2_detach() was done but the mux buffer was full |
| 3215 | * and prevented the last frame from being emitted. |
| 3216 | */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3217 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3218 | { |
| 3219 | struct h2s *h2s = ctx; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3220 | long reason = (long)h2s->wait_event.handle; |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3221 | int ret = 0; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3222 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3223 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3224 | if (h2s->send_wait) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3225 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3226 | h2s->send_wait->events |= SUB_RETRY_SEND; |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3227 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3228 | if (reason & 2) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3229 | ret |= h2_do_shutw(h2s); |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3230 | if (reason & 1) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3231 | ret |= h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3232 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3233 | if (!ret) { |
| 3234 | /* We're done trying to send, remove ourself from the send_list */ |
| 3235 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 3236 | h2s->send_wait = NULL; |
| 3237 | LIST_DEL_INIT(&h2s->list); |
| 3238 | } |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3239 | /* We're no longer trying to send anything, let's destroy the h2s */ |
Olivier Houchard | 01d4cb5 | 2019-03-25 13:25:02 +0100 | [diff] [blame] | 3240 | if (!ret && (h2s->cs == NULL)) { |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3241 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3242 | h2s_destroy(h2s); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3243 | |
| 3244 | if (h2c_is_dead(h2c)) |
| 3245 | h2_release(h2c->conn); |
| 3246 | } |
| 3247 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3248 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3249 | } |
| 3250 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3251 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3252 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3253 | { |
| 3254 | struct h2s *h2s = cs->ctx; |
| 3255 | |
| 3256 | if (!mode) |
| 3257 | return; |
| 3258 | |
| 3259 | h2_do_shutr(h2s); |
| 3260 | } |
| 3261 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3262 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3263 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3264 | { |
| 3265 | struct h2s *h2s = cs->ctx; |
| 3266 | |
| 3267 | h2_do_shutw(h2s); |
| 3268 | } |
| 3269 | |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3270 | /* 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] | 3271 | * HTX request or response depending on the connection's side. Returns a |
| 3272 | * positive value on success, a negative value on failure, or 0 if it couldn't |
| 3273 | * proceed. May report connection errors in h2c->errcode if the frame is |
| 3274 | * non-decodable and the connection unrecoverable. In absence of connection |
| 3275 | * 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] | 3276 | * |
| 3277 | * The function may fold CONTINUATION frames into the initial HEADERS frame |
| 3278 | * by removing padding and next frame header, then moving the CONTINUATION |
| 3279 | * frame's payload and adjusting h2c->dfl to match the new aggregated frame, |
| 3280 | * leaving a hole between the main frame and the beginning of the next one. |
| 3281 | * The possibly remaining incomplete or next frame at the end may be moved |
| 3282 | * if the aggregated frame is not deleted, in order to fill the hole. Wrapped |
| 3283 | * HEADERS frames are unwrapped into a temporary buffer before decoding. |
| 3284 | * |
| 3285 | * A buffer at the beginning of processing may look like this : |
| 3286 | * |
| 3287 | * ,---.---------.-----.--------------.--------------.------.---. |
| 3288 | * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///| |
| 3289 | * `---^---------^-----^--------------^--------------^------^---' |
| 3290 | * | | <-----> | | |
| 3291 | * area | dpl | wrap |
| 3292 | * |<--------------> | |
| 3293 | * | dfl | |
| 3294 | * |<-------------------------------------------------->| |
| 3295 | * head data |
| 3296 | * |
| 3297 | * Padding is automatically overwritten when folding, participating to the |
| 3298 | * hole size after dfl : |
| 3299 | * |
| 3300 | * ,---.------------------------.-----.--------------.------.---. |
| 3301 | * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///| |
| 3302 | * `---^------------------------^-----^--------------^------^---' |
| 3303 | * | | <-----> | | |
| 3304 | * area | hole | wrap |
| 3305 | * |<-----------------------> | |
| 3306 | * | dfl | |
| 3307 | * |<-------------------------------------------------->| |
| 3308 | * head data |
| 3309 | * |
| 3310 | * Please note that the HEADERS frame is always deprived from its PADLEN byte |
| 3311 | * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY |
| 3312 | * bit. |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3313 | * |
| 3314 | * The <flags> field must point to either the stream's flags or to a copy of it |
| 3315 | * so that the function can update the following flags : |
| 3316 | * - H2_SF_DATA_CLEN when content-length is seen |
| 3317 | * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion |
| 3318 | * - H2_SF_HEADERS_RCVD once the frame is successfully decoded |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3319 | * |
| 3320 | * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to |
| 3321 | * decoding, in order to detect if we're dealing with a headers or a trailers |
| 3322 | * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3323 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3324 | 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] | 3325 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3326 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3327 | struct buffer *tmp = get_trash_chunk(); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3328 | struct http_hdr list[MAX_HTTP_HDR * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3329 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3330 | unsigned int msgf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3331 | struct htx *htx = NULL; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3332 | int flen; // header frame len |
| 3333 | int hole = 0; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3334 | int ret = 0; |
| 3335 | int outlen; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3336 | int wrap; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3337 | int try = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3338 | |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3339 | next_frame: |
| 3340 | if (b_data(&h2c->dbuf) - hole < h2c->dfl) |
| 3341 | goto leave; // incomplete input frame |
| 3342 | |
| 3343 | /* No END_HEADERS means there's one or more CONTINUATION frames. In |
| 3344 | * this case, we'll try to paste it immediately after the initial |
| 3345 | * HEADERS frame payload and kill any possible padding. The initial |
| 3346 | * frame's length will be increased to represent the concatenation |
| 3347 | * of the two frames. The next frame is read from position <tlen> |
| 3348 | * and written at position <flen> (minus padding if some is present). |
| 3349 | */ |
| 3350 | if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) { |
| 3351 | struct h2_fh hdr; |
| 3352 | int clen; // CONTINUATION frame's payload length |
| 3353 | |
| 3354 | if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) { |
| 3355 | /* no more data, the buffer may be full, either due to |
| 3356 | * too large a frame or because of too large a hole that |
| 3357 | * we're going to compact at the end. |
| 3358 | */ |
| 3359 | goto leave; |
| 3360 | } |
| 3361 | |
| 3362 | if (hdr.ft != H2_FT_CONTINUATION) { |
| 3363 | /* RFC7540#6.10: frame of unexpected type */ |
| 3364 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3365 | goto fail; |
| 3366 | } |
| 3367 | |
| 3368 | if (hdr.sid != h2c->dsi) { |
| 3369 | /* RFC7540#6.10: frame of different stream */ |
| 3370 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3371 | goto fail; |
| 3372 | } |
| 3373 | |
| 3374 | if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { |
| 3375 | /* RFC7540#4.2: invalid frame length */ |
| 3376 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3377 | goto fail; |
| 3378 | } |
| 3379 | |
| 3380 | /* detect when we must stop aggragating frames */ |
| 3381 | h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS; |
| 3382 | |
| 3383 | /* Take as much as we can of the CONTINUATION frame's payload */ |
| 3384 | clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9); |
| 3385 | if (clen > hdr.len) |
| 3386 | clen = hdr.len; |
| 3387 | |
| 3388 | /* Move the frame's payload over the padding, hole and frame |
| 3389 | * header. At least one of hole or dpl is null (see diagrams |
| 3390 | * above). The hole moves after the new aggragated frame. |
| 3391 | */ |
| 3392 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9)); |
| 3393 | h2c->dfl += clen - h2c->dpl; |
| 3394 | hole += h2c->dpl + 9; |
| 3395 | h2c->dpl = 0; |
| 3396 | goto next_frame; |
| 3397 | } |
| 3398 | |
| 3399 | flen = h2c->dfl - h2c->dpl; |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3400 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3401 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3402 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3403 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3404 | copy = alloc_trash_chunk(); |
| 3405 | if (!copy) { |
| 3406 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3407 | goto fail; |
| 3408 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3409 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3410 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3411 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3412 | } |
| 3413 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3414 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3415 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3416 | if (read_n32(hdrs) == h2c->dsi) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3417 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3418 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3419 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3420 | } |
| 3421 | |
Willy Tarreau | a01f45e | 2018-12-31 07:41:24 +0100 | [diff] [blame] | 3422 | if (flen < 5) { |
| 3423 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3424 | goto fail; |
| 3425 | } |
| 3426 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3427 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3428 | flen -= 5; |
| 3429 | } |
| 3430 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3431 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3432 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3433 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3434 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3435 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3436 | /* we can't retry a failed decompression operation so we must be very |
| 3437 | * careful not to take any risks. In practice the output buffer is |
| 3438 | * always empty except maybe for trailers, in which case we simply have |
| 3439 | * to wait for the upper layer to finish consuming what is available. |
| 3440 | */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3441 | |
| 3442 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3443 | htx = htx_from_buf(rxbuf); |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3444 | if (!htx_is_empty(htx)) { |
| 3445 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3446 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3447 | } |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3448 | } else { |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3449 | if (b_data(rxbuf)) { |
| 3450 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3451 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3452 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3453 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3454 | rxbuf->head = 0; |
| 3455 | try = b_size(rxbuf); |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3456 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3457 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3458 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3459 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3460 | sizeof(list)/sizeof(list[0]), tmp); |
| 3461 | if (outlen < 0) { |
| 3462 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3463 | goto fail; |
| 3464 | } |
| 3465 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3466 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3467 | * the parser's state to commit these changes and allow us to later |
| 3468 | * fail solely on the stream if needed. |
| 3469 | */ |
| 3470 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3471 | h2c->dfl = hole = 0; |
| 3472 | h2c->st0 = H2_CS_FRAME_H; |
| 3473 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3474 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3475 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3476 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3477 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3478 | goto trailers; |
| 3479 | |
| 3480 | /* This is the first HEADERS frame so it's a headers block */ |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3481 | if (htx) { |
| 3482 | /* HTX mode */ |
| 3483 | if (h2c->flags & H2_CF_IS_BACK) |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3484 | outlen = h2_make_htx_response(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3485 | else |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3486 | outlen = h2_make_htx_request(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3487 | } else { |
| 3488 | /* HTTP/1 mode */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3489 | 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] | 3490 | if (outlen > 0) |
| 3491 | b_add(rxbuf, outlen); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3492 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3493 | |
| 3494 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3495 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3496 | goto fail; |
| 3497 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3498 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3499 | if (msgf & H2_MSGF_BODY) { |
| 3500 | /* a payload is present */ |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3501 | if (msgf & H2_MSGF_BODY_CL) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3502 | *flags |= H2_SF_DATA_CLEN; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3503 | if (htx) |
| 3504 | htx->extra = *body_len; |
| 3505 | } |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3506 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3507 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3508 | } |
| 3509 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3510 | done: |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3511 | /* indicate that a HEADERS frame was received for this stream, except |
| 3512 | * for 1xx responses. For 1xx responses, another HEADERS frame is |
| 3513 | * expected. |
| 3514 | */ |
| 3515 | if (!(msgf & H2_MSGF_RSP_1XX)) |
| 3516 | *flags |= H2_SF_HEADERS_RCVD; |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3517 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3518 | 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] | 3519 | /* Mark the end of message, either using EOM in HTX or with the |
| 3520 | * trailing CRLF after the end of trailers. Note that DATA_CHNK |
| 3521 | * is not set during headers with END_STREAM. |
| 3522 | */ |
| 3523 | if (htx) { |
| 3524 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 3525 | goto fail; |
| 3526 | } |
| 3527 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3528 | if (!b_putblk(rxbuf, "\r\n", 2)) |
| 3529 | goto fail; |
| 3530 | } |
| 3531 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3532 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3533 | /* success */ |
| 3534 | ret = 1; |
| 3535 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3536 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3537 | /* 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] | 3538 | * move the remaining data over it. |
| 3539 | */ |
| 3540 | if (hole) { |
| 3541 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3542 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3543 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3544 | b_sub(&h2c->dbuf, hole); |
| 3545 | } |
| 3546 | |
| 3547 | if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) { |
| 3548 | /* too large frames */ |
| 3549 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3550 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3551 | } |
| 3552 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3553 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3554 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3555 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3556 | return ret; |
| 3557 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3558 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3559 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3560 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3561 | |
| 3562 | trailers: |
| 3563 | /* This is the last HEADERS frame hence a trailer */ |
| 3564 | |
| 3565 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3566 | /* It's a trailer but it's missing ES flag */ |
| 3567 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3568 | goto fail; |
| 3569 | } |
| 3570 | |
| 3571 | /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD |
| 3572 | * block, and when using chunks we must send the 0 CRLF marker. For |
| 3573 | * other modes, the trailers are silently dropped. |
| 3574 | */ |
| 3575 | if (htx) { |
| 3576 | if (!htx_add_endof(htx, HTX_BLK_EOD)) |
| 3577 | goto fail; |
Willy Tarreau | 5255f28 | 2019-01-03 18:41:05 +0100 | [diff] [blame] | 3578 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3579 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3580 | } |
| 3581 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3582 | /* Legacy mode with chunked encoding : we must finalize the |
| 3583 | * data block message emit the trailing CRLF */ |
| 3584 | if (!b_putblk(rxbuf, "0\r\n", 3)) |
| 3585 | goto fail; |
Willy Tarreau | e2b05cc | 2019-01-03 16:18:34 +0100 | [diff] [blame] | 3586 | |
| 3587 | outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try); |
| 3588 | if (outlen > 0) |
| 3589 | b_add(rxbuf, outlen); |
| 3590 | else |
| 3591 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3592 | } |
| 3593 | |
| 3594 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3595 | } |
| 3596 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3597 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 3598 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 3599 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 3600 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 3601 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 3602 | * 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] | 3603 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3604 | * send the current frame, 0 if it couldn't complete, in which case |
| 3605 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3606 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3607 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3608 | * have checked the frame header and ensured that the frame was complete or the |
| 3609 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3610 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3611 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3612 | { |
| 3613 | struct h2c *h2c = h2s->h2c; |
| 3614 | int block1, block2; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3615 | unsigned int flen = 0; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3616 | unsigned int chklen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3617 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3618 | struct buffer *csbuf; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3619 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3620 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3621 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3622 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3623 | if (!csbuf) { |
| 3624 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3625 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3626 | } |
| 3627 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3628 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3629 | flen = h2c->dfl - h2c->dpl; |
Olivier Houchard | 2f30883 | 2018-12-19 15:53:53 +0100 | [diff] [blame] | 3630 | if (h2c->proxy->options2 & PR_O2_USE_HTX) |
| 3631 | htx = htx_from_buf(csbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3632 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3633 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3634 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3635 | if (flen > b_data(&h2c->dbuf)) { |
| 3636 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3637 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3638 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3639 | } |
| 3640 | |
Willy Tarreau | a9b7796 | 2019-01-31 07:23:00 +0100 | [diff] [blame] | 3641 | if (htx) { |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3642 | block1 = htx_free_data_space(htx); |
| 3643 | if (!block1) { |
| 3644 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3645 | goto fail; |
| 3646 | } |
| 3647 | if (flen > block1) |
| 3648 | flen = block1; |
| 3649 | |
| 3650 | /* here, flen is the max we can copy into the output buffer */ |
| 3651 | block1 = b_contig_data(&h2c->dbuf, 0); |
| 3652 | if (flen > block1) |
| 3653 | flen = block1; |
| 3654 | |
| 3655 | if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) { |
| 3656 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3657 | goto fail; |
| 3658 | } |
| 3659 | |
| 3660 | b_del(&h2c->dbuf, flen); |
| 3661 | h2c->dfl -= flen; |
| 3662 | h2c->rcvd_c += flen; |
| 3663 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3664 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3665 | if (h2s->flags & H2_SF_DATA_CLEN) { |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3666 | h2s->body_len -= flen; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3667 | htx->extra = h2s->body_len; |
| 3668 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3669 | goto try_again; |
| 3670 | } |
| 3671 | else if (unlikely(b_space_wraps(csbuf))) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3672 | /* it doesn't fit and the buffer is fragmented, |
| 3673 | * so let's defragment it and try again. |
| 3674 | */ |
| 3675 | b_slow_realign(csbuf, trash.area, 0); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3676 | } |
| 3677 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3678 | /* chunked-encoding requires more room */ |
| 3679 | if (h2s->flags & H2_SF_DATA_CHNK) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3680 | chklen = MIN(flen, b_room(csbuf)); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3681 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 3682 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 3683 | (chklen < 1048576) ? 4 : 8; |
| 3684 | chklen += 4; // CRLF, CRLF |
| 3685 | } |
| 3686 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3687 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3688 | if (flen + chklen > b_room(csbuf)) { |
| 3689 | if (chklen >= b_room(csbuf)) { |
| 3690 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3691 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3692 | } |
| 3693 | flen = b_room(csbuf) - chklen; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3694 | } |
| 3695 | |
| 3696 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3697 | /* emit the chunk size */ |
| 3698 | unsigned int chksz = flen; |
| 3699 | char str[10]; |
| 3700 | char *beg; |
| 3701 | |
| 3702 | beg = str + sizeof(str); |
| 3703 | *--beg = '\n'; |
| 3704 | *--beg = '\r'; |
| 3705 | do { |
| 3706 | *--beg = hextab[chksz & 0xF]; |
| 3707 | } while (chksz >>= 4); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3708 | b_putblk(csbuf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3709 | } |
| 3710 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3711 | /* Block1 is the length of the first block before the buffer wraps, |
| 3712 | * block2 is the optional second block to reach the end of the frame. |
| 3713 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3714 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3715 | if (block1 > flen) |
| 3716 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3717 | block2 = flen - block1; |
| 3718 | |
| 3719 | if (block1) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3720 | b_putblk(csbuf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3721 | |
| 3722 | if (block2) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3723 | b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3724 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3725 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3726 | /* emit the CRLF */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3727 | b_putblk(csbuf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3728 | } |
| 3729 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3730 | /* now mark the input data as consumed (will be deleted from the buffer |
| 3731 | * by the caller when seeing FRAME_A after sending the window update). |
| 3732 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3733 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3734 | h2c->dfl -= flen; |
| 3735 | h2c->rcvd_c += flen; |
| 3736 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 3737 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3738 | if (h2s->flags & H2_SF_DATA_CLEN) |
| 3739 | h2s->body_len -= flen; |
| 3740 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3741 | if (h2c->dfl > h2c->dpl) { |
| 3742 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3743 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3744 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3745 | } |
| 3746 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3747 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3748 | /* here we're done with the frame, all the payload (except padding) was |
| 3749 | * transferred. |
| 3750 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3751 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3752 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 3753 | if (htx) { |
| 3754 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3755 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3756 | goto fail; |
| 3757 | } |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3758 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3759 | else if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3760 | /* emit the trailing 0 CRLF CRLF */ |
| 3761 | if (b_room(csbuf) < 5) { |
| 3762 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3763 | goto fail; |
| 3764 | } |
| 3765 | chklen += 5; |
| 3766 | b_putblk(csbuf, "0\r\n\r\n", 5); |
| 3767 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3768 | } |
| 3769 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 3770 | h2c->rcvd_c += h2c->dpl; |
| 3771 | h2c->rcvd_s += h2c->dpl; |
| 3772 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3773 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3774 | if (htx) |
| 3775 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3776 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3777 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3778 | if (htx) |
| 3779 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3780 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3781 | } |
| 3782 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3783 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 3784 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 3785 | * number of bytes sent. The caller must check the stream's status to detect |
| 3786 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3787 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3788 | 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] | 3789 | { |
| 3790 | struct http_hdr list[MAX_HTTP_HDR]; |
| 3791 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3792 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3793 | struct buffer outbuf; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3794 | union h1_sl sl; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3795 | int es_now = 0; |
| 3796 | int ret = 0; |
| 3797 | int hdr; |
| 3798 | |
| 3799 | if (h2c_mux_busy(h2c, h2s)) { |
| 3800 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3801 | return 0; |
| 3802 | } |
| 3803 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3804 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3805 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3806 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3807 | return 0; |
| 3808 | } |
| 3809 | |
| 3810 | /* First, try to parse the H1 response and index it into <list>. |
| 3811 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 3812 | * block does not wrap and we can safely read it this way without |
| 3813 | * having to realign the buffer. |
| 3814 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3815 | 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] | 3816 | list, sizeof(list)/sizeof(list[0]), h1m, &sl); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3817 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 3818 | /* incomplete or invalid response, this is abnormal coming from |
| 3819 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 3820 | * so that won't be fixed, raise an error now. |
| 3821 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3822 | * FIXME: we should instead add the ability to only return a |
| 3823 | * 502 bad gateway. But in theory this is not supposed to |
| 3824 | * happen. |
| 3825 | */ |
| 3826 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3827 | ret = 0; |
| 3828 | goto end; |
| 3829 | } |
| 3830 | |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3831 | h2s->status = sl.st.status; |
Willy Tarreau | db72da0 | 2018-09-13 11:52:20 +0200 | [diff] [blame] | 3832 | |
| 3833 | /* certain statuses have no body or an empty one, regardless of |
| 3834 | * what the headers say. |
| 3835 | */ |
| 3836 | if (sl.st.status >= 100 && sl.st.status < 200) { |
| 3837 | h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK); |
| 3838 | h1m->curr_len = h1m->body_len = 0; |
| 3839 | } |
| 3840 | else if (sl.st.status == 204 || sl.st.status == 304) { |
| 3841 | /* no contents, claim c-len is present and set to zero */ |
| 3842 | h1m->flags &= ~H1_MF_CHNK; |
| 3843 | h1m->flags |= H1_MF_CLEN; |
| 3844 | h1m->curr_len = h1m->body_len = 0; |
| 3845 | } |
| 3846 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3847 | chunk_reset(&outbuf); |
| 3848 | |
| 3849 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3850 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3851 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3852 | outbuf.data = 0; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3853 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3854 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3855 | break; |
| 3856 | realign_again: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3857 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3858 | } |
| 3859 | |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3860 | if (outbuf.size < 9) |
| 3861 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3862 | |
| 3863 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3864 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 3865 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3866 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3867 | |
| 3868 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3869 | if (unlikely(list[0].v.len != 3)) { |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 3870 | /* this is an unparsable response */ |
| 3871 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3872 | ret = 0; |
| 3873 | goto end; |
| 3874 | } |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3875 | |
| 3876 | if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3877 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3878 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3879 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3880 | } |
| 3881 | |
| 3882 | /* encode all headers, stop at empty name */ |
| 3883 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 3884 | /* these ones do not exist in H2 and must be dropped. */ |
| 3885 | if (isteq(list[hdr].n, ist("connection")) || |
| 3886 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 3887 | isteq(list[hdr].n, ist("keep-alive")) || |
| 3888 | isteq(list[hdr].n, ist("upgrade")) || |
| 3889 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3890 | continue; |
| 3891 | |
| 3892 | if (isteq(list[hdr].n, ist(""))) |
| 3893 | break; // end |
| 3894 | |
| 3895 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 3896 | /* output full */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3897 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3898 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3899 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3900 | } |
| 3901 | } |
| 3902 | |
| 3903 | /* we may need to add END_STREAM */ |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 3904 | 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] | 3905 | es_now = 1; |
| 3906 | |
| 3907 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3908 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3909 | |
| 3910 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3911 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3912 | |
| 3913 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3914 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3915 | |
| 3916 | /* commit the H2 response */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3917 | b_add(&h2c->mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3918 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3919 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3920 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3921 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3922 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3923 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3924 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3925 | h2s->flags |= H2_SF_ES_SENT; |
| 3926 | if (h2s->st == H2_SS_OPEN) |
| 3927 | h2s->st = H2_SS_HLOC; |
| 3928 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3929 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3930 | } |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3931 | else if (h2s->status >= 100 && h2s->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3932 | /* 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] | 3933 | h1m_init_res(h1m); |
Willy Tarreau | 9b8cd1f | 2018-09-12 09:24:38 +0200 | [diff] [blame] | 3934 | 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] | 3935 | h2s->h1m.flags |= H1_MF_TOLOWER; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3936 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 3937 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 3938 | |
| 3939 | /* 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] | 3940 | |
| 3941 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 3942 | //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] | 3943 | return ret; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3944 | full: |
| 3945 | h1m_init_res(h1m); |
| 3946 | h1m->err_pos = -1; // don't care about errors on the response path |
| 3947 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3948 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3949 | ret = 0; |
| 3950 | goto end; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3951 | } |
| 3952 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3953 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 3954 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 3955 | * the number of bytes sent. The caller must check the stream's status to |
| 3956 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3957 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3958 | 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] | 3959 | { |
| 3960 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3961 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3962 | struct buffer outbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3963 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 3964 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3965 | int es_now = 0; |
| 3966 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3967 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3968 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3969 | |
| 3970 | if (h2c_mux_busy(h2c, h2s)) { |
| 3971 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3972 | goto end; |
| 3973 | } |
| 3974 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3975 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3976 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3977 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3978 | goto end; |
| 3979 | } |
| 3980 | |
| 3981 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3982 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3983 | goto end; |
| 3984 | |
| 3985 | chunk_reset(&outbuf); |
| 3986 | |
| 3987 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3988 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3989 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3990 | outbuf.data = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3991 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3992 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3993 | break; |
| 3994 | realign_again: |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 3995 | /* If there are pending data in the output buffer, and we have |
| 3996 | * less than 1/4 of the mbuf's size and everything fits, we'll |
| 3997 | * still perform a copy anyway. Otherwise we'll pretend the mbuf |
| 3998 | * is full and wait, to save some slow realign calls. |
| 3999 | */ |
| 4000 | if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) { |
| 4001 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4002 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4003 | goto end; |
| 4004 | } |
| 4005 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4006 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4007 | } |
| 4008 | |
| 4009 | if (outbuf.size < 9) { |
| 4010 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4011 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4012 | goto end; |
| 4013 | } |
| 4014 | |
| 4015 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4016 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4017 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4018 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4019 | |
| 4020 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 4021 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4022 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 4023 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4024 | break; |
| 4025 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4026 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4027 | if ((long long)size > h1m->curr_len) |
| 4028 | size = h1m->curr_len; |
| 4029 | break; |
| 4030 | default: /* te:chunked : parse chunks */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4031 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 4032 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4033 | if (!ret) |
| 4034 | goto end; |
| 4035 | |
| 4036 | if (ret < 0) { |
| 4037 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4038 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4039 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4040 | goto end; |
| 4041 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4042 | max -= ret; |
| 4043 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4044 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4045 | h1m->state = H1_MSG_CHUNK_SIZE; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4046 | } |
| 4047 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4048 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4049 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 4050 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4051 | if (!ret) |
| 4052 | goto end; |
| 4053 | |
| 4054 | if (ret < 0) { |
| 4055 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4056 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4057 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4058 | goto end; |
| 4059 | } |
| 4060 | |
| 4061 | size = chunk; |
| 4062 | h1m->curr_len = chunk; |
| 4063 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4064 | max -= ret; |
| 4065 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4066 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4067 | h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4068 | if (!size) |
| 4069 | goto send_empty; |
| 4070 | } |
| 4071 | |
| 4072 | /* in MSG_DATA state, continue below */ |
| 4073 | size = h1m->curr_len; |
| 4074 | break; |
| 4075 | } |
| 4076 | |
| 4077 | /* we have in <size> the exact number of bytes we need to copy from |
| 4078 | * the H1 buffer. We need to check this against the connection's and |
| 4079 | * the stream's send windows, and to ensure that this fits in the max |
| 4080 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4081 | * the frame header). The connection's flow control is applied last so |
| 4082 | * that we can use a separate list of streams which are immediately |
| 4083 | * unblocked on window opening. Note: we don't implement padding. |
| 4084 | */ |
| 4085 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4086 | if (size > max) |
| 4087 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4088 | |
| 4089 | if (size > h2s->mws) |
| 4090 | size = h2s->mws; |
| 4091 | |
| 4092 | if (size <= 0) { |
| 4093 | h2s->flags |= H2_SF_BLK_SFCTL; |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 4094 | if (h2s->send_wait) { |
| 4095 | LIST_DEL(&h2s->list); |
| 4096 | LIST_INIT(&h2s->list); |
| 4097 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4098 | goto end; |
| 4099 | } |
| 4100 | |
| 4101 | if (h2c->mfs && size > h2c->mfs) |
| 4102 | size = h2c->mfs; |
| 4103 | |
| 4104 | if (size + 9 > outbuf.size) { |
| 4105 | /* we have an opportunity for enlarging the too small |
| 4106 | * available space, let's try. |
| 4107 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4108 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4109 | goto realign_again; |
| 4110 | size = outbuf.size - 9; |
| 4111 | } |
| 4112 | |
| 4113 | if (size <= 0) { |
| 4114 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4115 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4116 | goto end; |
| 4117 | } |
| 4118 | |
| 4119 | if (size > h2c->mws) |
| 4120 | size = h2c->mws; |
| 4121 | |
| 4122 | if (size <= 0) { |
| 4123 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4124 | goto end; |
| 4125 | } |
| 4126 | |
| 4127 | /* copy whatever we can */ |
| 4128 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4129 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4130 | if (ret == 1) |
| 4131 | len2 = 0; |
| 4132 | |
| 4133 | if (!ret || len1 + len2 < size) { |
| 4134 | /* FIXME: must normally never happen */ |
| 4135 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4136 | goto end; |
| 4137 | } |
| 4138 | |
| 4139 | /* limit len1/len2 to size */ |
| 4140 | if (len1 + len2 > size) { |
| 4141 | int sub = len1 + len2 - size; |
| 4142 | |
| 4143 | if (len2 > sub) |
| 4144 | len2 -= sub; |
| 4145 | else { |
| 4146 | sub -= len2; |
| 4147 | len2 = 0; |
| 4148 | len1 -= sub; |
| 4149 | } |
| 4150 | } |
| 4151 | |
| 4152 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4153 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4154 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4155 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4156 | |
| 4157 | send_empty: |
| 4158 | /* we may need to add END_STREAM */ |
| 4159 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 4160 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4161 | * |
| 4162 | * FIXME: what we do here is not correct because we send end_stream |
| 4163 | * before knowing if we'll have to send a HEADERS frame for the |
| 4164 | * trailers. More importantly we're not consuming the trailing CRLF |
| 4165 | * after the end of trailers, so it will be left to the caller to |
| 4166 | * eat it. The right way to do it would be to measure trailers here |
| 4167 | * and to send ES only if there are no trailers. |
| 4168 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4169 | */ |
| 4170 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4171 | !h1m->curr_len || h1m->state >= H1_MSG_DONE) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4172 | es_now = 1; |
| 4173 | |
| 4174 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4175 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4176 | |
| 4177 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4178 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4179 | |
| 4180 | /* commit the H2 response */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4181 | b_add(&h2c->mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4182 | |
| 4183 | /* consume incoming H1 response */ |
| 4184 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4185 | max -= size; |
| 4186 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4187 | total += size; |
| 4188 | h1m->curr_len -= size; |
| 4189 | h2s->mws -= size; |
| 4190 | h2c->mws -= size; |
| 4191 | |
| 4192 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4193 | h1m->state = H1_MSG_CHUNK_CRLF; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4194 | goto new_frame; |
| 4195 | } |
| 4196 | } |
| 4197 | |
| 4198 | if (es_now) { |
| 4199 | if (h2s->st == H2_SS_OPEN) |
| 4200 | h2s->st = H2_SS_HLOC; |
| 4201 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4202 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4203 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4204 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 4205 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4206 | total += max; |
| 4207 | ofs += max; |
| 4208 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4209 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4210 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4211 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4212 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4213 | h2s->flags |= H2_SF_ES_SENT; |
| 4214 | } |
| 4215 | |
| 4216 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4217 | 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] | 4218 | return total; |
| 4219 | } |
| 4220 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4221 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 4222 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4223 | * must check the stream's status to detect any error which might have happened |
| 4224 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4225 | * from the message. The htx message is assumed to be valid since produced from |
| 4226 | * the internal code, hence it contains a start line, an optional series of |
| 4227 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4228 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4229 | */ |
| 4230 | static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
| 4231 | { |
| 4232 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4233 | struct h2c *h2c = h2s->h2c; |
| 4234 | struct htx_blk *blk; |
| 4235 | struct htx_blk *blk_end; |
| 4236 | struct buffer outbuf; |
| 4237 | struct htx_sl *sl; |
| 4238 | enum htx_blk_type type; |
| 4239 | int es_now = 0; |
| 4240 | int ret = 0; |
| 4241 | int hdr; |
| 4242 | int idx; |
| 4243 | |
| 4244 | if (h2c_mux_busy(h2c, h2s)) { |
| 4245 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4246 | return 0; |
| 4247 | } |
| 4248 | |
| 4249 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4250 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4251 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4252 | return 0; |
| 4253 | } |
| 4254 | |
| 4255 | /* determine the first block which must not be deleted, blk_end may |
| 4256 | * be NULL if all blocks have to be deleted. |
| 4257 | */ |
| 4258 | idx = htx_get_head(htx); |
| 4259 | blk_end = NULL; |
| 4260 | while (idx != -1) { |
| 4261 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4262 | idx = htx_get_next(htx, idx); |
| 4263 | if (type == HTX_BLK_EOH) { |
| 4264 | if (idx != -1) |
| 4265 | blk_end = htx_get_blk(htx, idx); |
| 4266 | break; |
| 4267 | } |
| 4268 | } |
| 4269 | |
| 4270 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4271 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4272 | ALREADY_CHECKED(sl); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4273 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4274 | if (h2s->status < 100 || h2s->status > 999) |
| 4275 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4276 | |
| 4277 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4278 | hdr = 0; |
| 4279 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4280 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4281 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4282 | blk = htx_get_blk(htx, idx); |
| 4283 | type = htx_get_blk_type(blk); |
| 4284 | |
| 4285 | if (type == HTX_BLK_UNUSED) |
| 4286 | continue; |
| 4287 | |
| 4288 | if (type != HTX_BLK_HDR) |
| 4289 | break; |
| 4290 | |
| 4291 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4292 | goto fail; |
| 4293 | |
| 4294 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4295 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4296 | hdr++; |
| 4297 | } |
| 4298 | |
| 4299 | /* marker for end of headers */ |
| 4300 | list[hdr].n = ist(""); |
| 4301 | |
| 4302 | if (h2s->status == 204 || h2s->status == 304) { |
| 4303 | /* no contents, claim c-len is present and set to zero */ |
| 4304 | es_now = 1; |
| 4305 | } |
| 4306 | |
| 4307 | chunk_reset(&outbuf); |
| 4308 | |
| 4309 | while (1) { |
| 4310 | outbuf.area = b_tail(&h2c->mbuf); |
| 4311 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4312 | outbuf.data = 0; |
| 4313 | |
| 4314 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4315 | break; |
| 4316 | realign_again: |
| 4317 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4318 | } |
| 4319 | |
| 4320 | if (outbuf.size < 9) |
| 4321 | goto full; |
| 4322 | |
| 4323 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4324 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4325 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4326 | outbuf.data = 9; |
| 4327 | |
| 4328 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4329 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4330 | if (b_space_wraps(&h2c->mbuf)) |
| 4331 | goto realign_again; |
| 4332 | goto full; |
| 4333 | } |
| 4334 | |
| 4335 | /* encode all headers, stop at empty name */ |
| 4336 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4337 | /* these ones do not exist in H2 and must be dropped. */ |
| 4338 | if (isteq(list[hdr].n, ist("connection")) || |
| 4339 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4340 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4341 | isteq(list[hdr].n, ist("upgrade")) || |
| 4342 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4343 | continue; |
| 4344 | |
| 4345 | if (isteq(list[hdr].n, ist(""))) |
| 4346 | break; // end |
| 4347 | |
| 4348 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4349 | /* output full */ |
| 4350 | if (b_space_wraps(&h2c->mbuf)) |
| 4351 | goto realign_again; |
| 4352 | goto full; |
| 4353 | } |
| 4354 | } |
| 4355 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4356 | /* we may need to add END_STREAM except for 1xx responses. |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4357 | * FIXME: we should also set it when we know for sure that the |
| 4358 | * content-length is zero as well as on 204/304 |
| 4359 | */ |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4360 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM && |
| 4361 | (h2s->status >= 200 || h2s->status == 101)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4362 | es_now = 1; |
| 4363 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4364 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4365 | es_now = 1; |
| 4366 | |
| 4367 | /* update the frame's size */ |
| 4368 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4369 | |
| 4370 | if (es_now) |
| 4371 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4372 | |
| 4373 | /* commit the H2 response */ |
| 4374 | b_add(&h2c->mbuf, outbuf.data); |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4375 | |
| 4376 | /* indicates the HEADERS frame was sent, except for 1xx responses. For |
| 4377 | * 1xx responses, another HEADERS frame is expected. |
| 4378 | */ |
| 4379 | if (h2s->status >= 200 || h2s->status == 101) |
| 4380 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4381 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4382 | if (es_now) { |
| 4383 | h2s->flags |= H2_SF_ES_SENT; |
| 4384 | if (h2s->st == H2_SS_OPEN) |
| 4385 | h2s->st = H2_SS_HLOC; |
| 4386 | else |
| 4387 | h2s_close(h2s); |
| 4388 | } |
| 4389 | |
| 4390 | /* OK we could properly deliver the response */ |
| 4391 | |
| 4392 | /* remove all header blocks including the EOH and compute the |
| 4393 | * corresponding size. |
| 4394 | * |
| 4395 | * FIXME: We should remove everything when es_now is set. |
| 4396 | */ |
| 4397 | ret = 0; |
| 4398 | idx = htx_get_head(htx); |
| 4399 | blk = htx_get_blk(htx, idx); |
| 4400 | while (blk != blk_end) { |
| 4401 | ret += htx_get_blksz(blk); |
| 4402 | blk = htx_remove_blk(htx, blk); |
| 4403 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4404 | |
| 4405 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4406 | htx_remove_blk(htx, blk_end); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4407 | end: |
| 4408 | return ret; |
| 4409 | full: |
| 4410 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4411 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4412 | ret = 0; |
| 4413 | goto end; |
| 4414 | fail: |
| 4415 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4416 | * list etc go here (unrecoverable errors). |
| 4417 | */ |
| 4418 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4419 | ret = 0; |
| 4420 | goto end; |
| 4421 | } |
| 4422 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4423 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4424 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4425 | * must check the stream's status to detect any error which might have happened |
| 4426 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4427 | * from the message. The htx message is assumed to be valid since produced from |
| 4428 | * the internal code, hence it contains a start line, an optional series of |
| 4429 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4430 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4431 | */ |
| 4432 | static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
| 4433 | { |
| 4434 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4435 | struct h2c *h2c = h2s->h2c; |
| 4436 | struct htx_blk *blk; |
| 4437 | struct htx_blk *blk_end; |
| 4438 | struct buffer outbuf; |
| 4439 | struct htx_sl *sl; |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4440 | struct ist meth, path, auth; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4441 | enum htx_blk_type type; |
| 4442 | int es_now = 0; |
| 4443 | int ret = 0; |
| 4444 | int hdr; |
| 4445 | int idx; |
| 4446 | |
| 4447 | if (h2c_mux_busy(h2c, h2s)) { |
| 4448 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4449 | return 0; |
| 4450 | } |
| 4451 | |
| 4452 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4453 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4454 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4455 | return 0; |
| 4456 | } |
| 4457 | |
| 4458 | /* determine the first block which must not be deleted, blk_end may |
| 4459 | * be NULL if all blocks have to be deleted. |
| 4460 | */ |
| 4461 | idx = htx_get_head(htx); |
| 4462 | blk_end = NULL; |
| 4463 | while (idx != -1) { |
| 4464 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4465 | idx = htx_get_next(htx, idx); |
| 4466 | if (type == HTX_BLK_EOH) { |
| 4467 | if (idx != -1) |
| 4468 | blk_end = htx_get_blk(htx, idx); |
| 4469 | break; |
| 4470 | } |
| 4471 | } |
| 4472 | |
| 4473 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4474 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4475 | ALREADY_CHECKED(sl); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4476 | meth = htx_sl_req_meth(sl); |
| 4477 | path = htx_sl_req_uri(sl); |
| 4478 | |
| 4479 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4480 | hdr = 0; |
| 4481 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4482 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4483 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4484 | blk = htx_get_blk(htx, idx); |
| 4485 | type = htx_get_blk_type(blk); |
| 4486 | |
| 4487 | if (type == HTX_BLK_UNUSED) |
| 4488 | continue; |
| 4489 | |
| 4490 | if (type != HTX_BLK_HDR) |
| 4491 | break; |
| 4492 | |
| 4493 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4494 | goto fail; |
| 4495 | |
| 4496 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4497 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4498 | hdr++; |
| 4499 | } |
| 4500 | |
| 4501 | /* marker for end of headers */ |
| 4502 | list[hdr].n = ist(""); |
| 4503 | |
| 4504 | chunk_reset(&outbuf); |
| 4505 | |
| 4506 | while (1) { |
| 4507 | outbuf.area = b_tail(&h2c->mbuf); |
| 4508 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4509 | outbuf.data = 0; |
| 4510 | |
| 4511 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4512 | break; |
| 4513 | realign_again: |
| 4514 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4515 | } |
| 4516 | |
| 4517 | if (outbuf.size < 9) |
| 4518 | goto full; |
| 4519 | |
| 4520 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4521 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4522 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4523 | outbuf.data = 9; |
| 4524 | |
| 4525 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4526 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4527 | if (b_space_wraps(&h2c->mbuf)) |
| 4528 | goto realign_again; |
| 4529 | goto full; |
| 4530 | } |
| 4531 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4532 | /* RFC7540 #8.3: the CONNECT method must have : |
| 4533 | * - :authority set to the URI part (host:port) |
| 4534 | * - :method set to CONNECT |
| 4535 | * - :scheme and :path omitted |
| 4536 | */ |
| 4537 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 4538 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
| 4539 | if (!hpack_encode_scheme(&outbuf, ist("https"))) { |
| 4540 | /* output full */ |
| 4541 | if (b_space_wraps(&h2c->mbuf)) |
| 4542 | goto realign_again; |
| 4543 | goto full; |
| 4544 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4545 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4546 | /* encode the path, which necessarily is the second one */ |
| 4547 | if (!hpack_encode_path(&outbuf, path)) { |
| 4548 | /* output full */ |
| 4549 | if (b_space_wraps(&h2c->mbuf)) |
| 4550 | goto realign_again; |
| 4551 | goto full; |
| 4552 | } |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4553 | |
| 4554 | /* look for the Host header and place it in :authority */ |
| 4555 | auth = ist2(NULL, 0); |
| 4556 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4557 | if (isteq(list[hdr].n, ist(""))) |
| 4558 | break; // end |
| 4559 | |
| 4560 | if (isteq(list[hdr].n, ist("host"))) { |
| 4561 | auth = list[hdr].v; |
| 4562 | break; |
| 4563 | } |
| 4564 | } |
| 4565 | } |
| 4566 | else { |
| 4567 | /* for CONNECT, :authority is taken from the path */ |
| 4568 | auth = path; |
| 4569 | } |
| 4570 | |
| 4571 | if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) { |
| 4572 | /* output full */ |
| 4573 | if (b_space_wraps(&h2c->mbuf)) |
| 4574 | goto realign_again; |
| 4575 | goto full; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4576 | } |
| 4577 | |
| 4578 | /* encode all headers, stop at empty name */ |
| 4579 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4580 | /* these ones do not exist in H2 and must be dropped. */ |
| 4581 | if (isteq(list[hdr].n, ist("connection")) || |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4582 | isteq(list[hdr].n, ist("host")) || |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4583 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4584 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4585 | isteq(list[hdr].n, ist("upgrade")) || |
| 4586 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4587 | continue; |
| 4588 | |
| 4589 | if (isteq(list[hdr].n, ist(""))) |
| 4590 | break; // end |
| 4591 | |
| 4592 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4593 | /* output full */ |
| 4594 | if (b_space_wraps(&h2c->mbuf)) |
| 4595 | goto realign_again; |
| 4596 | goto full; |
| 4597 | } |
| 4598 | } |
| 4599 | |
| 4600 | /* we may need to add END_STREAM if we have no body : |
| 4601 | * - request already closed, or : |
| 4602 | * - no transfer-encoding, and : |
| 4603 | * - no content-length or content-length:0 |
| 4604 | * Fixme: this doesn't take into account CONNECT requests. |
| 4605 | */ |
| 4606 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4607 | es_now = 1; |
| 4608 | |
| 4609 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4610 | es_now = 1; |
| 4611 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4612 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4613 | es_now = 1; |
| 4614 | |
| 4615 | /* update the frame's size */ |
| 4616 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4617 | |
| 4618 | if (es_now) |
| 4619 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4620 | |
| 4621 | /* commit the H2 response */ |
| 4622 | b_add(&h2c->mbuf, outbuf.data); |
| 4623 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4624 | h2s->st = H2_SS_OPEN; |
| 4625 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4626 | if (es_now) { |
| 4627 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4628 | h2s->flags |= H2_SF_ES_SENT; |
| 4629 | h2s->st = H2_SS_HLOC; |
| 4630 | } |
| 4631 | |
| 4632 | /* remove all header blocks including the EOH and compute the |
| 4633 | * corresponding size. |
| 4634 | * |
| 4635 | * FIXME: We should remove everything when es_now is set. |
| 4636 | */ |
| 4637 | ret = 0; |
| 4638 | idx = htx_get_head(htx); |
| 4639 | blk = htx_get_blk(htx, idx); |
| 4640 | while (blk != blk_end) { |
| 4641 | ret += htx_get_blksz(blk); |
| 4642 | blk = htx_remove_blk(htx, blk); |
| 4643 | } |
| 4644 | |
| 4645 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4646 | htx_remove_blk(htx, blk_end); |
| 4647 | |
| 4648 | end: |
| 4649 | return ret; |
| 4650 | full: |
| 4651 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4652 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4653 | ret = 0; |
| 4654 | goto end; |
| 4655 | fail: |
| 4656 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4657 | * list etc go here (unrecoverable errors). |
| 4658 | */ |
| 4659 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4660 | ret = 0; |
| 4661 | goto end; |
| 4662 | } |
| 4663 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4664 | /* 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] | 4665 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 4666 | * caller must check the stream's status to detect any error which might have |
| 4667 | * happened subsequently to a successful send. Returns the number of data bytes |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4668 | * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte. |
| 4669 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4670 | 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] | 4671 | { |
| 4672 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4673 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4674 | struct buffer outbuf; |
| 4675 | size_t total = 0; |
| 4676 | int es_now = 0; |
| 4677 | int bsize; /* htx block size */ |
| 4678 | int fsize; /* h2 frame size */ |
| 4679 | struct htx_blk *blk; |
| 4680 | enum htx_blk_type type; |
| 4681 | int idx; |
| 4682 | |
| 4683 | if (h2c_mux_busy(h2c, h2s)) { |
| 4684 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4685 | goto end; |
| 4686 | } |
| 4687 | |
| 4688 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4689 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4690 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4691 | goto end; |
| 4692 | } |
| 4693 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4694 | htx = htx_from_buf(buf); |
| 4695 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4696 | /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However, |
| 4697 | * while looping, we can meet an HTX_BLK_EOM block that we'll leave to |
| 4698 | * the caller to handle. |
| 4699 | */ |
| 4700 | |
| 4701 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4702 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4703 | goto end; |
| 4704 | |
| 4705 | idx = htx_get_head(htx); |
| 4706 | blk = htx_get_blk(htx, idx); |
| 4707 | type = htx_get_blk_type(blk); // DATA or EOD or EOM |
| 4708 | bsize = htx_get_blksz(blk); |
| 4709 | fsize = bsize; |
| 4710 | |
| 4711 | if (type == HTX_BLK_EOD) { |
| 4712 | /* if we have an EOD, we're dealing with chunked data. We may |
| 4713 | * have a set of trailers after us that the caller will want to |
| 4714 | * deal with. Let's simply remove the EOD and return. |
| 4715 | */ |
| 4716 | htx_remove_blk(htx, blk); |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4717 | total++; // EOD counts as one byte |
| 4718 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4719 | goto end; |
| 4720 | } |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 4721 | else if (type == HTX_BLK_EOM) { |
| 4722 | if (h2s->flags & H2_SF_ES_SENT) { |
| 4723 | /* ES already sent */ |
| 4724 | htx_remove_blk(htx, blk); |
| 4725 | total++; // EOM counts as one byte |
| 4726 | count--; |
| 4727 | goto end; |
| 4728 | } |
| 4729 | } |
| 4730 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4731 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4732 | |
| 4733 | /* Perform some optimizations to reduce the number of buffer copies. |
| 4734 | * First, if the mux's buffer is empty and the htx area contains |
| 4735 | * exactly one data block of the same size as the requested count, and |
| 4736 | * this count fits within the frame size, the stream's window size, and |
| 4737 | * the connection's window size, then it's possible to simply swap the |
| 4738 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 4739 | * length to match the entire DATA HTX block in the middle. In this |
| 4740 | * case we perform a true zero-copy operation from end-to-end. This is |
| 4741 | * the situation that happens all the time with large files. Second, if |
| 4742 | * this is not possible, but the mux's output buffer is empty, we still |
| 4743 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 4744 | * making the intermediary buffer's area point to the output buffer's |
| 4745 | * area. In this case we want to skip the HTX header to make sure that |
| 4746 | * copies remain aligned and that this operation remains possible all |
| 4747 | * the time. This goes for headers, data blocks and any data extracted |
| 4748 | * from the HTX blocks. |
| 4749 | */ |
| 4750 | if (unlikely(fsize == count && |
| 4751 | htx->used == 1 && type == HTX_BLK_DATA && |
| 4752 | fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
| 4753 | void *old_area = h2c->mbuf.area; |
| 4754 | |
| 4755 | if (b_data(&h2c->mbuf)) { |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4756 | /* Too bad there are data left there. We're willing to memcpy/memmove |
| 4757 | * up to 1/4 of the buffer, which means that it's OK to copy a large |
| 4758 | * frame into a buffer containing few data if it needs to be realigned, |
| 4759 | * and that it's also OK to copy few data without realigning. Otherwise |
| 4760 | * we'll pretend the mbuf is full and wait for it to become empty. |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4761 | */ |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4762 | if (fsize + 9 <= b_room(&h2c->mbuf) && |
| 4763 | (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 || |
| 4764 | (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf)))) |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4765 | goto copy; |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4766 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4767 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4768 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4769 | goto end; |
| 4770 | } |
| 4771 | |
| 4772 | /* map an H2 frame to the HTX block so that we can put the |
| 4773 | * frame header there. |
| 4774 | */ |
| 4775 | h2c->mbuf.area = buf->area; |
Olivier Houchard | 84cca66 | 2018-12-14 16:28:08 +0100 | [diff] [blame] | 4776 | h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4777 | h2c->mbuf.data = fsize + 9; |
| 4778 | outbuf.area = b_head(&h2c->mbuf); |
| 4779 | |
| 4780 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 4781 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4782 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4783 | h2_set_frame_size(outbuf.area, fsize); |
| 4784 | |
| 4785 | /* update windows */ |
| 4786 | h2s->mws -= fsize; |
| 4787 | h2c->mws -= fsize; |
| 4788 | |
| 4789 | /* and exchange with our old area */ |
| 4790 | buf->area = old_area; |
| 4791 | buf->data = buf->head = 0; |
| 4792 | total += fsize; |
| 4793 | goto end; |
| 4794 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4795 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4796 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4797 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 4798 | |
| 4799 | while (1) { |
| 4800 | outbuf.area = b_tail(&h2c->mbuf); |
| 4801 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4802 | outbuf.data = 0; |
| 4803 | |
| 4804 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4805 | break; |
| 4806 | realign_again: |
| 4807 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4808 | } |
| 4809 | |
| 4810 | if (outbuf.size < 9) { |
| 4811 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4812 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4813 | goto end; |
| 4814 | } |
| 4815 | |
| 4816 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 4817 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4818 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4819 | outbuf.data = 9; |
| 4820 | |
| 4821 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 4822 | * the HTX buffer. We need to check this against the connection's and |
| 4823 | * the stream's send windows, and to ensure that this fits in the max |
| 4824 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4825 | * the frame header). The connection's flow control is applied last so |
| 4826 | * that we can use a separate list of streams which are immediately |
| 4827 | * unblocked on window opening. Note: we don't implement padding. |
| 4828 | */ |
| 4829 | |
| 4830 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 4831 | * empty frame, thus we force it to zero here. |
| 4832 | */ |
| 4833 | if (type == HTX_BLK_EOM) |
| 4834 | bsize = fsize = 0; |
| 4835 | |
| 4836 | if (!fsize) |
| 4837 | goto send_empty; |
| 4838 | |
| 4839 | if (h2s->mws <= 0) { |
| 4840 | h2s->flags |= H2_SF_BLK_SFCTL; |
| 4841 | if (h2s->send_wait) { |
| 4842 | LIST_DEL(&h2s->list); |
| 4843 | LIST_INIT(&h2s->list); |
| 4844 | } |
| 4845 | goto end; |
| 4846 | } |
| 4847 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4848 | if (fsize > count) |
| 4849 | fsize = count; |
| 4850 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4851 | if (fsize > h2s->mws) |
| 4852 | fsize = h2s->mws; // >0 |
| 4853 | |
| 4854 | if (h2c->mfs && fsize > h2c->mfs) |
| 4855 | fsize = h2c->mfs; // >0 |
| 4856 | |
| 4857 | if (fsize + 9 > outbuf.size) { |
| 4858 | /* we have an opportunity for enlarging the too small |
| 4859 | * available space, let's try. |
| 4860 | * FIXME: is this really interesting to do? Maybe we'll |
| 4861 | * spend lots of time realigning instead of using two |
| 4862 | * frames. |
| 4863 | */ |
| 4864 | if (b_space_wraps(&h2c->mbuf)) |
| 4865 | goto realign_again; |
| 4866 | fsize = outbuf.size - 9; |
| 4867 | |
| 4868 | if (fsize <= 0) { |
| 4869 | /* no need to send an empty frame here */ |
| 4870 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4871 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4872 | goto end; |
| 4873 | } |
| 4874 | } |
| 4875 | |
| 4876 | if (h2c->mws <= 0) { |
| 4877 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4878 | goto end; |
| 4879 | } |
| 4880 | |
| 4881 | if (fsize > h2c->mws) |
| 4882 | fsize = h2c->mws; |
| 4883 | |
| 4884 | /* now let's copy this this into the output buffer */ |
| 4885 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 4886 | h2s->mws -= fsize; |
| 4887 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4888 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4889 | |
| 4890 | send_empty: |
| 4891 | /* update the frame's size */ |
| 4892 | h2_set_frame_size(outbuf.area, fsize); |
| 4893 | |
| 4894 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 4895 | * meeting EOM. We should optimize this later. |
| 4896 | */ |
| 4897 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4898 | total++; // EOM counts as one byte |
| 4899 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4900 | es_now = 1; |
| 4901 | } |
| 4902 | |
| 4903 | if (es_now) |
| 4904 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 4905 | |
| 4906 | /* commit the H2 response */ |
| 4907 | b_add(&h2c->mbuf, fsize + 9); |
| 4908 | |
| 4909 | /* consume incoming HTX block, including EOM */ |
| 4910 | total += fsize; |
| 4911 | if (fsize == bsize) { |
| 4912 | htx_remove_blk(htx, blk); |
| 4913 | if (fsize) |
| 4914 | goto new_frame; |
| 4915 | } else { |
| 4916 | /* we've truncated this block */ |
| 4917 | htx_cut_data_blk(htx, blk, fsize); |
| 4918 | } |
| 4919 | |
| 4920 | if (es_now) { |
| 4921 | if (h2s->st == H2_SS_OPEN) |
| 4922 | h2s->st = H2_SS_HLOC; |
| 4923 | else |
| 4924 | h2s_close(h2s); |
| 4925 | |
| 4926 | h2s->flags |= H2_SF_ES_SENT; |
| 4927 | } |
| 4928 | |
| 4929 | end: |
| 4930 | return total; |
| 4931 | } |
| 4932 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4933 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 4934 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 4935 | * processed. The caller must check the stream's status to detect any error |
| 4936 | * which might have happened subsequently to a successful send. The htx blocks |
| 4937 | * are automatically removed from the message. The htx message is assumed to be |
| 4938 | * valid since produced from the internal code. Processing stops when meeting |
| 4939 | * the EOM, which is also removed. All trailers are processed at once and sent |
| 4940 | * as a single frame. The ES flag is always set. |
| 4941 | */ |
| 4942 | static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx) |
| 4943 | { |
| 4944 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4945 | struct h2c *h2c = h2s->h2c; |
| 4946 | struct htx_blk *blk; |
| 4947 | struct htx_blk *blk_end; |
| 4948 | struct buffer outbuf; |
| 4949 | struct h1m h1m; |
| 4950 | enum htx_blk_type type; |
| 4951 | uint32_t size; |
| 4952 | int ret = 0; |
| 4953 | int hdr; |
| 4954 | int idx; |
| 4955 | void *start; |
| 4956 | |
| 4957 | if (h2c_mux_busy(h2c, h2s)) { |
| 4958 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4959 | goto end; |
| 4960 | } |
| 4961 | |
| 4962 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4963 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4964 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4965 | goto end; |
| 4966 | } |
| 4967 | |
| 4968 | /* The principle is that we parse each and every trailers block using |
| 4969 | * the H1 headers parser, and append it to the list. We don't proceed |
| 4970 | * until EOM is met. blk_end will point to the EOM block. |
| 4971 | */ |
| 4972 | hdr = 0; |
| 4973 | memset(list, 0, sizeof(list)); |
| 4974 | blk_end = NULL; |
| 4975 | |
| 4976 | for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) { |
| 4977 | blk = htx_get_blk(htx, idx); |
| 4978 | type = htx_get_blk_type(blk); |
| 4979 | |
| 4980 | if (type == HTX_BLK_UNUSED) |
| 4981 | continue; |
| 4982 | |
| 4983 | if (type != HTX_BLK_TLR) { |
| 4984 | if (type == HTX_BLK_EOM) |
| 4985 | blk_end = blk; |
| 4986 | break; |
| 4987 | } |
| 4988 | |
| 4989 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4990 | goto fail; |
| 4991 | |
| 4992 | size = htx_get_blksz(blk); |
| 4993 | start = htx_get_blk_ptr(htx, blk); |
| 4994 | |
| 4995 | h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER; |
| 4996 | h1m.err_pos = 0; |
| 4997 | ret = h1_headers_to_hdr_list(start, start + size, |
| 4998 | list + hdr, sizeof(list)/sizeof(list[0]) - hdr, |
| 4999 | &h1m, NULL); |
| 5000 | if (ret < 0) |
| 5001 | goto fail; |
| 5002 | |
| 5003 | /* ret == 0 if an incomplete trailers block was found (missing |
| 5004 | * empty line), or > 0 if it was found. We have to continue on |
| 5005 | * incomplete messages because the trailers block might be |
| 5006 | * incomplete. |
| 5007 | */ |
| 5008 | |
| 5009 | /* search the new end */ |
| 5010 | while (hdr <= sizeof(list)/sizeof(list[0])) { |
| 5011 | if (!list[hdr].n.len) |
| 5012 | break; |
| 5013 | hdr++; |
| 5014 | } |
| 5015 | } |
| 5016 | |
| 5017 | if (!blk_end) |
| 5018 | goto end; // end not found yet |
| 5019 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5020 | chunk_reset(&outbuf); |
| 5021 | |
| 5022 | while (1) { |
| 5023 | outbuf.area = b_tail(&h2c->mbuf); |
| 5024 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 5025 | outbuf.data = 0; |
| 5026 | |
| 5027 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 5028 | break; |
| 5029 | realign_again: |
| 5030 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 5031 | } |
| 5032 | |
| 5033 | if (outbuf.size < 9) |
| 5034 | goto full; |
| 5035 | |
| 5036 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 5037 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 5038 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5039 | outbuf.data = 9; |
| 5040 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5041 | /* encode all headers */ |
| 5042 | for (idx = 0; idx < hdr; idx++) { |
| 5043 | /* these ones do not exist in H2 or must not appear in |
| 5044 | * trailers and must be dropped. |
| 5045 | */ |
| 5046 | if (isteq(list[idx].n, ist("host")) || |
| 5047 | isteq(list[idx].n, ist("content-length")) || |
| 5048 | isteq(list[idx].n, ist("connection")) || |
| 5049 | isteq(list[idx].n, ist("proxy-connection")) || |
| 5050 | isteq(list[idx].n, ist("keep-alive")) || |
| 5051 | isteq(list[idx].n, ist("upgrade")) || |
| 5052 | isteq(list[idx].n, ist("te")) || |
| 5053 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 5054 | continue; |
| 5055 | |
| 5056 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 5057 | /* output full */ |
| 5058 | if (b_space_wraps(&h2c->mbuf)) |
| 5059 | goto realign_again; |
| 5060 | goto full; |
| 5061 | } |
| 5062 | } |
| 5063 | |
Willy Tarreau | 67b8cae | 2019-02-21 18:16:35 +0100 | [diff] [blame] | 5064 | if (!hdr) { |
| 5065 | /* here we have a problem, we've received an empty trailers |
| 5066 | * block followed by an EOM. Because of this we can't send a |
| 5067 | * HEADERS frame, so we have to cheat and instead send an empty |
| 5068 | * DATA frame conveying the ES flag. |
| 5069 | */ |
| 5070 | outbuf.area[3] = H2_FT_DATA; |
| 5071 | outbuf.area[4] = H2_F_DATA_END_STREAM; |
| 5072 | } |
| 5073 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5074 | /* update the frame's size */ |
| 5075 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 5076 | |
| 5077 | /* commit the H2 response */ |
| 5078 | b_add(&h2c->mbuf, outbuf.data); |
| 5079 | h2s->flags |= H2_SF_ES_SENT; |
| 5080 | |
| 5081 | if (h2s->st == H2_SS_OPEN) |
| 5082 | h2s->st = H2_SS_HLOC; |
| 5083 | else |
| 5084 | h2s_close(h2s); |
| 5085 | |
| 5086 | /* OK we could properly deliver the response */ |
| 5087 | done: |
| 5088 | /* remove all header blocks including EOM and compute the corresponding size. */ |
| 5089 | ret = 0; |
| 5090 | idx = htx_get_head(htx); |
| 5091 | blk = htx_get_blk(htx, idx); |
| 5092 | while (blk != blk_end) { |
| 5093 | ret += htx_get_blksz(blk); |
| 5094 | blk = htx_remove_blk(htx, blk); |
| 5095 | } |
| 5096 | blk = htx_remove_blk(htx, blk); |
| 5097 | end: |
| 5098 | return ret; |
| 5099 | full: |
| 5100 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5101 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5102 | ret = 0; |
| 5103 | goto end; |
| 5104 | fail: |
| 5105 | /* unparsable HTX messages, too large ones to be produced in the local |
| 5106 | * list etc go here (unrecoverable errors). |
| 5107 | */ |
| 5108 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5109 | ret = 0; |
| 5110 | goto end; |
| 5111 | } |
| 5112 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5113 | /* Called from the upper layer, to subscribe to events, such as being able to send. |
| 5114 | * The <param> argument here is supposed to be a pointer to a wait_event struct |
| 5115 | * which will be passed to h2s->recv_wait or h2s->send_wait depending on the |
| 5116 | * event_type. The event_type must only be a combination of SUB_RETRY_RECV and |
| 5117 | * SUB_RETRY_SEND, other values will lead to -1 being returned. It always |
| 5118 | * returns 0 except for the error above. |
| 5119 | */ |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5120 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 5121 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5122 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5123 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5124 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5125 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5126 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5127 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5128 | if (!(sw->events & SUB_RETRY_RECV)) { |
| 5129 | sw->events |= SUB_RETRY_RECV; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 5130 | sw->handle = h2s; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5131 | h2s->recv_wait = sw; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5132 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5133 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5134 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5135 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5136 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5137 | if (!(sw->events & SUB_RETRY_SEND)) { |
| 5138 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 5139 | sw->handle = h2s; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5140 | h2s->send_wait = sw; |
| 5141 | if (!(h2s->flags & H2_SF_BLK_SFCTL)) { |
| 5142 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 5143 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 5144 | else |
| 5145 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 5146 | } |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 5147 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5148 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5149 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5150 | if (event_type != 0) |
| 5151 | return -1; |
| 5152 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5153 | } |
| 5154 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5155 | /* Called from the upper layer, to unsubscribe some events (undo h2_subscribe). |
| 5156 | * The <param> argument here is supposed to be a pointer to the same wait_event |
| 5157 | * struct that was passed to h2_subscribe() otherwise nothing will be changed. |
| 5158 | * It always returns zero. |
| 5159 | */ |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5160 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 5161 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5162 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5163 | struct h2s *h2s = cs->ctx; |
| 5164 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5165 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5166 | sw = param; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5167 | if (h2s->recv_wait == sw) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5168 | sw->events &= ~SUB_RETRY_RECV; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5169 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5170 | } |
| 5171 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5172 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5173 | sw = param; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5174 | if (h2s->send_wait == sw) { |
| 5175 | LIST_DEL(&h2s->list); |
| 5176 | LIST_INIT(&h2s->list); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5177 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5178 | h2s->send_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5179 | } |
| 5180 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5181 | if (event_type & SUB_CALL_UNSUBSCRIBE) { |
| 5182 | sw = param; |
| 5183 | if (h2s->send_wait == sw) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5184 | sw->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | 3ea3513 | 2019-03-25 14:10:42 +0100 | [diff] [blame^] | 5185 | task_remove_from_task_list((struct task *)h2s->send_wait->task); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5186 | h2s->send_wait = NULL; |
Olivier Houchard | f29cd5c | 2018-12-20 11:56:28 +0100 | [diff] [blame] | 5187 | LIST_DEL(&h2s->list); |
| 5188 | LIST_INIT(&h2s->list); |
Olivier Houchard | 3ea3513 | 2019-03-25 14:10:42 +0100 | [diff] [blame^] | 5189 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5190 | } |
| 5191 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5192 | return 0; |
| 5193 | } |
| 5194 | |
| 5195 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5196 | /* Called from the upper layer, to receive data */ |
| 5197 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 5198 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5199 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5200 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5201 | struct htx *h2s_htx = NULL; |
| 5202 | struct htx *buf_htx = NULL; |
| 5203 | struct htx_ret htx_ret; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5204 | size_t ret = 0; |
| 5205 | |
| 5206 | /* transfer possibly pending data to the upper layer */ |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5207 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
| 5208 | /* in HTX mode we ignore the count argument */ |
| 5209 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5210 | if (htx_is_empty(h2s_htx)) { |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5211 | /* Here htx_to_buf() will set buffer data to 0 because |
| 5212 | * the HTX is empty. |
| 5213 | */ |
| 5214 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5215 | goto end; |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5216 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5217 | |
| 5218 | buf_htx = htx_from_buf(buf); |
Christopher Faulet | a413e95 | 2019-01-21 11:49:37 +0100 | [diff] [blame] | 5219 | count = htx_free_data_space(buf_htx); |
| 5220 | if (flags & CO_RFL_KEEP_RSV) { |
| 5221 | if (count <= global.tune.maxrewrite) |
| 5222 | goto end; |
| 5223 | count -= global.tune.maxrewrite; |
| 5224 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5225 | |
Willy Tarreau | 0c22fa7 | 2018-12-04 15:21:35 +0100 | [diff] [blame] | 5226 | 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] | 5227 | |
| 5228 | if (h2s_htx->flags & HTX_FL_PARSING_ERROR) |
| 5229 | buf_htx->flags |= HTX_FL_PARSING_ERROR; |
| 5230 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 5231 | 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] | 5232 | htx_to_buf(buf_htx, buf); |
| 5233 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5234 | ret = htx_ret.ret; |
| 5235 | } |
| 5236 | else { |
| 5237 | ret = b_xfer(buf, &h2s->rxbuf, count); |
| 5238 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5239 | |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5240 | end: |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5241 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5242 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5243 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5244 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5245 | if (cs->flags & CS_FL_REOS) |
| 5246 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5247 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5248 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5249 | if (b_size(&h2s->rxbuf)) { |
| 5250 | b_free(&h2s->rxbuf); |
| 5251 | offer_buffers(NULL, tasks_run_queue); |
| 5252 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5253 | } |
| 5254 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5255 | if (ret && h2c->dsi == h2s->id) { |
| 5256 | /* demux is blocking on this stream's buffer */ |
| 5257 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 872e2fa | 2019-01-03 08:27:41 +0100 | [diff] [blame] | 5258 | h2c_restart_reading(h2c); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5259 | } |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5260 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5261 | return ret; |
| 5262 | } |
| 5263 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5264 | /* stops all senders of this connection for example when the mux buffer is full. |
| 5265 | * They are moved from the sending_list to either fctl_list or send_list. |
| 5266 | */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5267 | static void h2_stop_senders(struct h2c *h2c) |
| 5268 | { |
| 5269 | struct h2s *h2s, *h2s_back; |
| 5270 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5271 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) { |
| 5272 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5273 | task_remove_from_task_list((struct task *)h2s->send_wait->task); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5274 | h2s->send_wait->events |= SUB_RETRY_SEND; |
| 5275 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5276 | } |
| 5277 | } |
| 5278 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5279 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 5280 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 5281 | * flags may be updated on the conn_stream. |
| 5282 | */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 5283 | 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] | 5284 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5285 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5286 | size_t orig_count = count; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 5287 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5288 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5289 | struct htx *htx; |
| 5290 | struct htx_blk *blk; |
| 5291 | enum htx_blk_type btype; |
| 5292 | uint32_t bsize; |
| 5293 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5294 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5295 | /* If we were not just woken because we wanted to send but couldn't, |
| 5296 | * and there's somebody else that is waiting to send, do nothing, |
| 5297 | * we will subscribe later and be put at the end of the list |
| 5298 | */ |
| 5299 | LIST_DEL_INIT(&h2s->sending_list); |
| 5300 | if ((!(h2s->send_wait) || !(h2s->send_wait->events & SUB_CALL_UNSUBSCRIBE)) && |
| 5301 | (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) |
| 5302 | return 0; |
| 5303 | |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5304 | if (h2s->send_wait) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5305 | /* We want to stay in the send_list, so prepare ourself to be |
| 5306 | * eventually recalled if needed, and only remove ourself from |
| 5307 | * the list if we managed to send anything. |
| 5308 | */ |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5309 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5310 | h2s->send_wait->events |= SUB_RETRY_SEND; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5311 | } |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 5312 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 5313 | return 0; |
| 5314 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5315 | /* htx will be enough to decide if we're using HTX or legacy */ |
| 5316 | htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL; |
| 5317 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5318 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 5319 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 5320 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5321 | if (h2s->id == 0) { |
| 5322 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 5323 | |
| 5324 | if (id < 0) { |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5325 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5326 | return 0; |
| 5327 | } |
| 5328 | |
| 5329 | eb32_delete(&h2s->by_id); |
| 5330 | h2s->by_id.key = h2s->id = id; |
| 5331 | h2s->h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 5332 | h2s->h2c->nb_reserved--; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5333 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 5334 | } |
| 5335 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5336 | if (htx) { |
Willy Tarreau | c14999b | 2018-12-06 14:09:09 +0100 | [diff] [blame] | 5337 | while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) && |
| 5338 | count && !htx_is_empty(htx)) { |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5339 | idx = htx_get_head(htx); |
| 5340 | blk = htx_get_blk(htx, idx); |
| 5341 | btype = htx_get_blk_type(blk); |
| 5342 | bsize = htx_get_blksz(blk); |
| 5343 | |
| 5344 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5345 | case HTX_BLK_REQ_SL: |
| 5346 | /* start-line before headers */ |
| 5347 | ret = h2s_htx_bck_make_req_headers(h2s, htx); |
| 5348 | if (ret > 0) { |
| 5349 | total += ret; |
| 5350 | count -= ret; |
| 5351 | if (ret < bsize) |
| 5352 | goto done; |
| 5353 | } |
| 5354 | break; |
| 5355 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 5356 | case HTX_BLK_RES_SL: |
| 5357 | /* start-line before headers */ |
| 5358 | ret = h2s_htx_frt_make_resp_headers(h2s, htx); |
| 5359 | if (ret > 0) { |
| 5360 | total += ret; |
| 5361 | count -= ret; |
| 5362 | if (ret < bsize) |
| 5363 | goto done; |
| 5364 | } |
| 5365 | break; |
| 5366 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5367 | case HTX_BLK_DATA: |
| 5368 | case HTX_BLK_EOD: |
| 5369 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5370 | /* all these cause the emission of a DATA frame (possibly empty). |
| 5371 | * This EOM necessarily is one before trailers, as the EOM following |
| 5372 | * trailers would have been consumed by the trailers parser. |
| 5373 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5374 | ret = h2s_htx_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5375 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5376 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5377 | total += ret; |
| 5378 | count -= ret; |
| 5379 | if (ret < bsize) |
| 5380 | goto done; |
| 5381 | } |
| 5382 | break; |
| 5383 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5384 | case HTX_BLK_TLR: |
| 5385 | /* This is the first trailers block, all the subsequent ones AND |
| 5386 | * the EOM will be swallowed by the parser. |
| 5387 | */ |
| 5388 | ret = h2s_htx_make_trailers(h2s, htx); |
| 5389 | if (ret > 0) { |
| 5390 | total += ret; |
| 5391 | count -= ret; |
| 5392 | if (ret < bsize) |
| 5393 | goto done; |
| 5394 | } |
| 5395 | break; |
| 5396 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5397 | default: |
| 5398 | htx_remove_blk(htx, blk); |
| 5399 | total += bsize; |
| 5400 | count -= bsize; |
| 5401 | break; |
| 5402 | } |
| 5403 | } |
| 5404 | goto done; |
| 5405 | } |
| 5406 | |
| 5407 | /* legacy transfer mode */ |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5408 | while (h2s->h1m.state < H1_MSG_DONE && count) { |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 5409 | if (h2s->h1m.state <= H1_MSG_LAST_LF) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5410 | if (h2s->h2c->flags & H2_CF_IS_BACK) |
| 5411 | ret = -1; |
| 5412 | else |
| 5413 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5414 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5415 | else if (h2s->h1m.state < H1_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5416 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5417 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5418 | else if (h2s->h1m.state == H1_MSG_TRAILERS) { |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5419 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5420 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5421 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5422 | if (unlikely((int)ret <= 0)) { |
| 5423 | if ((int)ret < 0) |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5424 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5425 | break; |
| 5426 | } |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 5427 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5428 | total += count; |
| 5429 | count = 0; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5430 | h2s->h1m.state = H1_MSG_DONE; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5431 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 5432 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5433 | else { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5434 | cs_set_error(cs); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5435 | break; |
| 5436 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5437 | |
| 5438 | total += ret; |
| 5439 | count -= ret; |
| 5440 | |
| 5441 | if (h2s->st >= H2_SS_ERROR) |
| 5442 | break; |
| 5443 | |
| 5444 | if (h2s->flags & H2_SF_BLK_ANY) |
| 5445 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5446 | } |
| 5447 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5448 | done: |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5449 | if (h2s->st >= H2_SS_ERROR) { |
| 5450 | /* trim any possibly pending data after we close (extra CR-LF, |
| 5451 | * unprocessed trailers, abnormal extra data, ...) |
| 5452 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5453 | total += count; |
| 5454 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5455 | } |
| 5456 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5457 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 5458 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5459 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 5460 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 5461 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5462 | } |
| 5463 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5464 | if (htx) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5465 | htx_to_buf(htx, buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5466 | } else { |
| 5467 | b_del(buf, total); |
| 5468 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5469 | |
| 5470 | /* The mux is full, cancel the pending tasks */ |
| 5471 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 5472 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 5473 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5474 | |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5475 | /* If we're running HTX, and we read the whole buffer, then pretend |
| 5476 | * we read exactly what the caller specified, as with HTX the caller |
| 5477 | * will always give the buffer size, instead of the amount of data |
| 5478 | * available. |
| 5479 | */ |
| 5480 | if (htx && !b_data(buf)) |
| 5481 | total = orig_count; |
| 5482 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5483 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5484 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5485 | tasklet_wakeup(h2s->h2c->wait_event.task); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5486 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5487 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 5488 | /* If we're waiting for flow control, and we got a shutr on the |
| 5489 | * connection, we will never be unlocked, so add an error on |
| 5490 | * the conn_stream. |
| 5491 | */ |
| 5492 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 5493 | !b_data(&h2s->h2c->dbuf) && |
| 5494 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 5495 | if (cs->flags & CS_FL_EOS) |
| 5496 | cs->flags |= CS_FL_ERROR; |
| 5497 | else |
| 5498 | cs->flags |= CS_FL_ERR_PENDING; |
| 5499 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5500 | if (total > 0 && h2s->send_wait) { |
| 5501 | /* Ok we managed to send something, leave the send_list */ |
| 5502 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 5503 | h2s->send_wait = NULL; |
| 5504 | LIST_DEL_INIT(&h2s->list); |
| 5505 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5506 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5507 | } |
| 5508 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5509 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5510 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5511 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5512 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5513 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5514 | struct eb32_node *node; |
| 5515 | int fctl_cnt = 0; |
| 5516 | int send_cnt = 0; |
| 5517 | int tree_cnt = 0; |
| 5518 | int orph_cnt = 0; |
| 5519 | |
| 5520 | if (!h2c) |
| 5521 | return; |
| 5522 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5523 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5524 | fctl_cnt++; |
| 5525 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5526 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5527 | send_cnt++; |
| 5528 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5529 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5530 | node = eb32_first(&h2c->streams_by_id); |
| 5531 | while (node) { |
| 5532 | h2s = container_of(node, struct h2s, by_id); |
| 5533 | tree_cnt++; |
| 5534 | if (!h2s->cs) |
| 5535 | orph_cnt++; |
| 5536 | node = eb32_next(node); |
| 5537 | } |
| 5538 | |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5539 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5540 | " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d" |
| 5541 | " .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] | 5542 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5543 | 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] | 5544 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5545 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5546 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5547 | h2c->msi, |
| 5548 | (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf), |
| 5549 | (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf)); |
| 5550 | |
| 5551 | if (h2s) { |
| 5552 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5553 | h2s, h2s->id, h2s->flags, |
| 5554 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5555 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5556 | h2s->cs); |
| 5557 | if (h2s->cs) |
| 5558 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5559 | h2s->cs->flags, h2s->cs->data); |
| 5560 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5561 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5562 | |
| 5563 | /*******************************************************/ |
| 5564 | /* functions below are dedicated to the config parsers */ |
| 5565 | /*******************************************************/ |
| 5566 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5567 | /* config parser for global "tune.h2.header-table-size" */ |
| 5568 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5569 | struct proxy *defpx, const char *file, int line, |
| 5570 | char **err) |
| 5571 | { |
| 5572 | if (too_many_args(1, args, err, NULL)) |
| 5573 | return -1; |
| 5574 | |
| 5575 | h2_settings_header_table_size = atoi(args[1]); |
| 5576 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5577 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5578 | return -1; |
| 5579 | } |
| 5580 | return 0; |
| 5581 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5582 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5583 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5584 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5585 | struct proxy *defpx, const char *file, int line, |
| 5586 | char **err) |
| 5587 | { |
| 5588 | if (too_many_args(1, args, err, NULL)) |
| 5589 | return -1; |
| 5590 | |
| 5591 | h2_settings_initial_window_size = atoi(args[1]); |
| 5592 | if (h2_settings_initial_window_size < 0) { |
| 5593 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5594 | return -1; |
| 5595 | } |
| 5596 | return 0; |
| 5597 | } |
| 5598 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5599 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5600 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5601 | struct proxy *defpx, const char *file, int line, |
| 5602 | char **err) |
| 5603 | { |
| 5604 | if (too_many_args(1, args, err, NULL)) |
| 5605 | return -1; |
| 5606 | |
| 5607 | h2_settings_max_concurrent_streams = atoi(args[1]); |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 5608 | if ((int)h2_settings_max_concurrent_streams < 0) { |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5609 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5610 | return -1; |
| 5611 | } |
| 5612 | return 0; |
| 5613 | } |
| 5614 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5615 | /* config parser for global "tune.h2.max-frame-size" */ |
| 5616 | static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx, |
| 5617 | struct proxy *defpx, const char *file, int line, |
| 5618 | char **err) |
| 5619 | { |
| 5620 | if (too_many_args(1, args, err, NULL)) |
| 5621 | return -1; |
| 5622 | |
| 5623 | h2_settings_max_frame_size = atoi(args[1]); |
| 5624 | if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) { |
| 5625 | memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]); |
| 5626 | return -1; |
| 5627 | } |
| 5628 | return 0; |
| 5629 | } |
| 5630 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5631 | |
| 5632 | /****************************************/ |
| 5633 | /* MUX initialization and instanciation */ |
| 5634 | /***************************************/ |
| 5635 | |
| 5636 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5637 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5638 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5639 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5640 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5641 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5642 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5643 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5644 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5645 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5646 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5647 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5648 | .avail_streams = h2_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 5649 | .used_streams = h2_used_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5650 | .shutr = h2_shutr, |
| 5651 | .shutw = h2_shutw, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5652 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 5653 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5654 | .name = "H2", |
| 5655 | }; |
| 5656 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5657 | /* PROTO selection : this mux registers PROTO token "h2" */ |
| 5658 | static struct mux_proto_list mux_proto_h2 = |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5659 | { .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] | 5660 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5661 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 5662 | |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5663 | static struct mux_proto_list mux_proto_h2_htx = |
| 5664 | { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops }; |
| 5665 | |
| 5666 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx); |
| 5667 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5668 | /* config keyword parsers */ |
| 5669 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5670 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5671 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5672 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5673 | { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5674 | { 0, NULL, NULL } |
| 5675 | }}; |
| 5676 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5677 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |