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 | |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 81 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 82 | /* 32 buffers: one for the ring's root, rest for the mbuf itself */ |
| 83 | #define H2C_MBUF_CNT 32 |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 84 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 85 | /* H2 connection descriptor */ |
| 86 | struct h2c { |
| 87 | struct connection *conn; |
| 88 | |
| 89 | enum h2_cs st0; /* mux state */ |
| 90 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 91 | |
| 92 | /* 16 bit hole here */ |
| 93 | uint32_t flags; /* connection flags: H2_CF_* */ |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 94 | uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 95 | int32_t max_id; /* highest ID known on this connection, <0 before preface */ |
| 96 | uint32_t rcvd_c; /* newly received data to ACK for the connection */ |
| 97 | uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */ |
| 98 | |
| 99 | /* states for the demux direction */ |
| 100 | struct hpack_dht *ddht; /* demux dynamic header table */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 101 | struct buffer dbuf; /* demux buffer */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 102 | |
| 103 | int32_t dsi; /* demux stream ID (<0 = idle) */ |
| 104 | int32_t dfl; /* demux frame length (if dsi >= 0) */ |
| 105 | int8_t dft; /* demux frame type (if dsi >= 0) */ |
| 106 | int8_t dff; /* demux frame flags (if dsi >= 0) */ |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 107 | uint8_t dpl; /* demux pad length (part of dfl), init to 0 */ |
| 108 | /* 8 bit hole here */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 109 | int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */ |
| 110 | |
| 111 | /* states for the mux direction */ |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 112 | struct buffer mbuf[H2C_MBUF_CNT]; /* mux buffers (ring) */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 113 | int32_t msi; /* mux stream ID (<0 = idle) */ |
| 114 | int32_t mfl; /* mux frame length (if dsi >= 0) */ |
| 115 | int8_t mft; /* mux frame type (if dsi >= 0) */ |
| 116 | int8_t mff; /* mux frame flags (if dsi >= 0) */ |
| 117 | /* 16 bit hole here */ |
| 118 | int32_t miw; /* mux initial window size for all new streams */ |
| 119 | int32_t mws; /* mux window size. Can be negative. */ |
| 120 | int32_t mfs; /* mux's max frame size */ |
| 121 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 122 | int timeout; /* idle timeout duration in ticks */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 123 | int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */ |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 124 | unsigned int nb_streams; /* number of streams in the tree */ |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 125 | unsigned int nb_cs; /* number of attached conn_streams */ |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 126 | unsigned int nb_reserved; /* number of reserved streams */ |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 127 | unsigned int stream_cnt; /* total number of streams seen */ |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 128 | struct proxy *proxy; /* the proxy this connection was created for */ |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 129 | struct task *task; /* timeout management task */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 130 | struct eb_root streams_by_id; /* all active streams by their ID */ |
| 131 | struct list send_list; /* list of blocked streams requesting to send */ |
| 132 | struct list fctl_list; /* list of streams blocked by connection's fctl */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 133 | struct list sending_list; /* list of h2s scheduled to send data */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 134 | struct buffer_wait buf_wait; /* wait list for buffer allocations */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 135 | 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] | 136 | }; |
| 137 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 138 | /* H2 stream state, in h2s->st */ |
| 139 | enum h2_ss { |
| 140 | H2_SS_IDLE = 0, // idle |
| 141 | H2_SS_RLOC, // reserved(local) |
| 142 | H2_SS_RREM, // reserved(remote) |
| 143 | H2_SS_OPEN, // open |
| 144 | H2_SS_HREM, // half-closed(remote) |
| 145 | H2_SS_HLOC, // half-closed(local) |
Willy Tarreau | 96060ba | 2017-10-16 18:34:34 +0200 | [diff] [blame] | 146 | H2_SS_ERROR, // an error needs to be sent using RST_STREAM |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 147 | H2_SS_CLOSED, // closed |
| 148 | H2_SS_ENTRIES // must be last |
| 149 | } __attribute__((packed)); |
| 150 | |
Willy Tarreau | 4c688eb | 2019-05-14 11:44:03 +0200 | [diff] [blame] | 151 | #define H2_SS_MASK(state) (1UL << (state)) |
| 152 | #define H2_SS_IDLE_BIT (1UL << H2_SS_IDLE) |
| 153 | #define H2_SS_RLOC_BIT (1UL << H2_SS_RLOC) |
| 154 | #define H2_SS_RREM_BIT (1UL << H2_SS_RREM) |
| 155 | #define H2_SS_OPEN_BIT (1UL << H2_SS_OPEN) |
| 156 | #define H2_SS_HREM_BIT (1UL << H2_SS_HREM) |
| 157 | #define H2_SS_HLOC_BIT (1UL << H2_SS_HLOC) |
| 158 | #define H2_SS_ERROR_BIT (1UL << H2_SS_ERROR) |
| 159 | #define H2_SS_CLOSED_BIT (1UL << H2_SS_CLOSED) |
Willy Tarreau | 4c688eb | 2019-05-14 11:44:03 +0200 | [diff] [blame] | 160 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 161 | /* HTTP/2 stream flags (32 bit), in h2s->flags */ |
| 162 | #define H2_SF_NONE 0x00000000 |
| 163 | #define H2_SF_ES_RCVD 0x00000001 |
| 164 | #define H2_SF_ES_SENT 0x00000002 |
| 165 | |
| 166 | #define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM |
| 167 | #define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM |
| 168 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 169 | /* stream flags indicating the reason the stream is blocked */ |
| 170 | #define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient) |
| 171 | #define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux |
| 172 | #define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl |
| 173 | #define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl |
| 174 | #define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above |
| 175 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 176 | /* stream flags indicating how data is supposed to be sent */ |
| 177 | #define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length |
| 178 | #define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding |
| 179 | |
| 180 | /* step we're currently in when sending chunks. This is needed because we may |
| 181 | * have to transfer chunks as large as a full buffer so there's no room left |
| 182 | * for size nor crlf around. |
| 183 | */ |
| 184 | #define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size |
| 185 | #define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data |
| 186 | #define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data |
| 187 | |
| 188 | #define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size |
| 189 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 190 | #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] | 191 | #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] | 192 | |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 193 | #define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream |
| 194 | |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 195 | #define H2_SF_WANT_SHUTR 0x00008000 // a stream couldn't shutr() (mux full/busy) |
| 196 | #define H2_SF_WANT_SHUTW 0x00010000 // a stream couldn't shutw() (mux full/busy) |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 197 | #define H2_SF_KILL_CONN 0x00020000 // kill the whole connection with this stream |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 198 | |
| 199 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 200 | /* H2 stream descriptor, describing the stream as it appears in the H2C, and as |
| 201 | * it is being processed in the internal HTTP representation (H1 for now). |
| 202 | */ |
| 203 | struct h2s { |
| 204 | struct conn_stream *cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 205 | struct session *sess; |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 206 | struct h2c *h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 207 | struct h1m h1m; /* request or response parser state for H1 */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 208 | struct eb32_node by_id; /* place in h2c's streams_by_id */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 209 | int32_t id; /* stream ID */ |
| 210 | uint32_t flags; /* H2_SF_* */ |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 211 | int sws; /* stream window size, to be added to the mux's initial window size */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 212 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 213 | enum h2_ss st; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 214 | uint16_t status; /* HTTP response status */ |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 215 | 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] | 216 | struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 217 | 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] | 218 | struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */ |
| 219 | 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] | 220 | 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] | 221 | 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] | 222 | }; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 223 | |
Willy Tarreau | c640514 | 2017-09-21 20:23:50 +0200 | [diff] [blame] | 224 | /* descriptor for an h2 frame header */ |
| 225 | struct h2_fh { |
| 226 | uint32_t len; /* length, host order, 24 bits */ |
| 227 | uint32_t sid; /* stream id, host order, 31 bits */ |
| 228 | uint8_t ft; /* frame type */ |
| 229 | uint8_t ff; /* frame flags */ |
| 230 | }; |
| 231 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 232 | /* the h2c connection pool */ |
| 233 | DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c)); |
| 234 | |
| 235 | /* the h2s stream pool */ |
| 236 | DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s)); |
| 237 | |
Willy Tarreau | dc57236 | 2018-12-12 08:08:05 +0100 | [diff] [blame] | 238 | /* The default connection window size is 65535, it may only be enlarged using |
| 239 | * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1, |
| 240 | * we'll pretend we already received the difference between the two to send |
| 241 | * an equivalent window update to enlarge it to 2G-1. |
| 242 | */ |
| 243 | #define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535) |
| 244 | |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 245 | /* maximum amount of data we're OK with re-aligning for buffer optimizations */ |
| 246 | #define MAX_DATA_REALIGN 1024 |
| 247 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 248 | /* a few settings from the global section */ |
| 249 | static int h2_settings_header_table_size = 4096; /* initial value */ |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 250 | static int h2_settings_initial_window_size = 65535; /* initial value */ |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 251 | static unsigned int h2_settings_max_concurrent_streams = 100; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 252 | static int h2_settings_max_frame_size = 0; /* unset */ |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 253 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 254 | /* a dmumy closed stream */ |
| 255 | static const struct h2s *h2_closed_stream = &(const struct h2s){ |
| 256 | .cs = NULL, |
| 257 | .h2c = NULL, |
| 258 | .st = H2_SS_CLOSED, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 259 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 260 | .flags = H2_SF_RST_RCVD, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 261 | .id = 0, |
| 262 | }; |
| 263 | |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 264 | /* a dmumy closed stream returning a PROTOCOL_ERROR error */ |
| 265 | static const struct h2s *h2_error_stream = &(const struct h2s){ |
| 266 | .cs = NULL, |
| 267 | .h2c = NULL, |
| 268 | .st = H2_SS_CLOSED, |
| 269 | .errcode = H2_ERR_PROTOCOL_ERROR, |
| 270 | .flags = 0, |
| 271 | .id = 0, |
| 272 | }; |
| 273 | |
Willy Tarreau | 8d0d58b | 2018-12-23 18:29:12 +0100 | [diff] [blame] | 274 | /* a dmumy closed stream returning a REFUSED_STREAM error */ |
| 275 | static const struct h2s *h2_refused_stream = &(const struct h2s){ |
| 276 | .cs = NULL, |
| 277 | .h2c = NULL, |
| 278 | .st = H2_SS_CLOSED, |
| 279 | .errcode = H2_ERR_REFUSED_STREAM, |
| 280 | .flags = 0, |
| 281 | .id = 0, |
| 282 | }; |
| 283 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 284 | /* and a dummy idle stream for use with any unannounced stream */ |
| 285 | static const struct h2s *h2_idle_stream = &(const struct h2s){ |
| 286 | .cs = NULL, |
| 287 | .h2c = NULL, |
| 288 | .st = H2_SS_IDLE, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 289 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 290 | .id = 0, |
| 291 | }; |
| 292 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 293 | 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] | 294 | static int h2_send(struct h2c *h2c); |
| 295 | static int h2_recv(struct h2c *h2c); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 296 | static int h2_process(struct h2c *h2c); |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 297 | 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] | 298 | 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] | 299 | 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] | 300 | static int h2_frt_transfer_data(struct h2s *h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 301 | 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] | 302 | 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] | 303 | static void h2s_alert(struct h2s *h2s); |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 304 | |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 305 | static __inline int |
| 306 | h2c_is_dead(struct h2c *h2c) |
| 307 | { |
| 308 | if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */ |
| 309 | ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */ |
| 310 | (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */ |
| 311 | (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */ |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 312 | (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 313 | (conn_xprt_read0_pending(h2c->conn) || |
| 314 | (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) |
| 315 | return 1; |
| 316 | |
| 317 | return 0; |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 320 | /*****************************************************/ |
| 321 | /* functions below are for dynamic buffer management */ |
| 322 | /*****************************************************/ |
| 323 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 324 | /* indicates whether or not the we may call the h2_recv() function to attempt |
| 325 | * to receive data into the buffer and/or demux pending data. The condition is |
| 326 | * a bit complex due to some API limits for now. The rules are the following : |
| 327 | * - if an error or a shutdown was detected on the connection and the buffer |
| 328 | * is empty, we must not attempt to receive |
| 329 | * - if the demux buf failed to be allocated, we must not try to receive and |
| 330 | * we know there is nothing pending |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 331 | * - if no flag indicates a blocking condition, we may attempt to receive, |
| 332 | * regardless of whether the demux buffer is full or not, so that only |
| 333 | * de demux part decides whether or not to block. This is needed because |
| 334 | * the connection API indeed prevents us from re-enabling receipt that is |
| 335 | * already enabled in a polled state, so we must always immediately stop |
| 336 | * as soon as the demux can't proceed so as never to hit an end of read |
| 337 | * with data pending in the buffers. |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 338 | * - otherwise must may not attempt |
| 339 | */ |
| 340 | static inline int h2_recv_allowed(const struct h2c *h2c) |
| 341 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 342 | if (b_data(&h2c->dbuf) == 0 && |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 343 | (h2c->st0 >= H2_CS_ERROR || |
| 344 | h2c->conn->flags & CO_FL_ERROR || |
| 345 | conn_xprt_read0_pending(h2c->conn))) |
| 346 | return 0; |
| 347 | |
| 348 | if (!(h2c->flags & H2_CF_DEM_DALLOC) && |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 349 | !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 350 | return 1; |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 355 | /* restarts reading on the connection if it was not enabled */ |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 356 | static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer) |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 357 | { |
| 358 | if (!h2_recv_allowed(h2c)) |
| 359 | return; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 360 | if ((!consider_buffer || !b_data(&h2c->dbuf)) |
| 361 | && (h2c->wait_event.events & SUB_RETRY_RECV)) |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 362 | return; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 363 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 367 | /* returns true if the front connection has too many conn_streams attached */ |
| 368 | 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] | 369 | { |
Willy Tarreau | a875466 | 2018-12-23 20:43:58 +0100 | [diff] [blame] | 370 | return h2c->nb_cs > h2_settings_max_concurrent_streams; |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 373 | /* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c |
| 374 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 375 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 376 | * impossible to wake up and we prefer to be woken up later. |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 377 | */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 378 | static int h2_buf_available(void *target) |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 379 | { |
| 380 | struct h2c *h2c = target; |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 381 | struct h2s *h2s; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 382 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 383 | 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] | 384 | h2c->flags &= ~H2_CF_DEM_DALLOC; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 385 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 386 | return 1; |
| 387 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 388 | |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 389 | if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(br_tail(h2c->mbuf), 0)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 390 | h2c->flags &= ~H2_CF_MUX_MALLOC; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 391 | |
| 392 | if (h2c->flags & H2_CF_DEM_MROOM) { |
| 393 | h2c->flags &= ~H2_CF_DEM_MROOM; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 394 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 395 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 396 | return 1; |
| 397 | } |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 398 | |
| 399 | if ((h2c->flags & H2_CF_DEM_SALLOC) && |
| 400 | (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs && |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 401 | b_alloc_margin(&h2s->rxbuf, 0)) { |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 402 | h2c->flags &= ~H2_CF_DEM_SALLOC; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 403 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 404 | return 1; |
| 405 | } |
| 406 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 410 | 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] | 411 | { |
| 412 | struct buffer *buf = NULL; |
| 413 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 414 | if (likely(!LIST_ADDED(&h2c->buf_wait.list)) && |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 415 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 416 | h2c->buf_wait.target = h2c; |
| 417 | h2c->buf_wait.wakeup_cb = h2_buf_available; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 418 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 419 | LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 420 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 421 | __conn_xprt_stop_recv(h2c->conn); |
| 422 | } |
| 423 | return buf; |
| 424 | } |
| 425 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 426 | static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 427 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 428 | if (bptr->size) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 429 | b_free(bptr); |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 430 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 434 | static inline void h2_release_mbuf(struct h2c *h2c) |
| 435 | { |
| 436 | struct buffer *buf; |
| 437 | unsigned int count = 0; |
| 438 | |
| 439 | while (b_size(buf = br_head_pick(h2c->mbuf))) { |
| 440 | b_free(buf); |
| 441 | count++; |
| 442 | } |
| 443 | if (count) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 444 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 445 | } |
| 446 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 447 | /* returns the number of allocatable outgoing streams for the connection taking |
| 448 | * the last_sid and the reserved ones into account. |
| 449 | */ |
| 450 | static inline int h2_streams_left(const struct h2c *h2c) |
| 451 | { |
| 452 | int ret; |
| 453 | |
| 454 | /* consider the number of outgoing streams we're allowed to create before |
| 455 | * reaching the last GOAWAY frame seen. max_id is the last assigned id, |
| 456 | * nb_reserved is the number of streams which don't yet have an ID. |
| 457 | */ |
| 458 | ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF; |
| 459 | ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1; |
| 460 | if (ret < 0) |
| 461 | ret = 0; |
| 462 | return ret; |
| 463 | } |
| 464 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 465 | /* returns the number of streams in use on a connection to figure if it's |
| 466 | * idle or not. We check nb_cs and not nb_streams as the caller will want |
| 467 | * to know if it was the last one after a detach(). |
| 468 | */ |
| 469 | static int h2_used_streams(struct connection *conn) |
| 470 | { |
| 471 | struct h2c *h2c = conn->ctx; |
| 472 | |
| 473 | return h2c->nb_cs; |
| 474 | } |
| 475 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 476 | /* returns the number of concurrent streams available on the connection */ |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 477 | static int h2_avail_streams(struct connection *conn) |
| 478 | { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 479 | struct server *srv = objt_server(conn->target); |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 480 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 481 | int ret1, ret2; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 482 | |
Willy Tarreau | 6afec46 | 2019-01-28 06:40:19 +0100 | [diff] [blame] | 483 | /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional |
| 484 | * streams on the connection. |
| 485 | */ |
| 486 | if (h2c->last_sid >= 0) |
| 487 | return 0; |
| 488 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 489 | /* note: may be negative if a SETTINGS frame changes the limit */ |
| 490 | ret1 = h2c->streams_limit - h2c->nb_streams; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 491 | |
| 492 | /* we must also consider the limit imposed by stream IDs */ |
| 493 | ret2 = h2_streams_left(h2c); |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 494 | ret1 = MIN(ret1, ret2); |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 495 | if (ret1 > 0 && srv && srv->max_reuse >= 0) { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 496 | ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0; |
| 497 | ret1 = MIN(ret1, ret2); |
| 498 | } |
| 499 | return ret1; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 500 | } |
| 501 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 502 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 503 | /*****************************************************************/ |
| 504 | /* functions below are dedicated to the mux setup and management */ |
| 505 | /*****************************************************************/ |
| 506 | |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 507 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 508 | * is already initialized before installing the mux, so we detect incoming |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 509 | * connections from the fact that the context is still NULL (even during mux |
| 510 | * upgrades). <input> is always used as Input buffer and may contain data. It is |
| 511 | * the caller responsibility to not reuse it anymore. Returns < 0 on error. |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 512 | */ |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 513 | static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess, |
| 514 | struct buffer *input) |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 515 | { |
| 516 | struct h2c *h2c; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 517 | struct task *t = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 518 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 519 | h2c = pool_alloc(pool_head_h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 520 | if (!h2c) |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 521 | goto fail_no_h2c; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 522 | |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 523 | if (conn_is_back(conn)) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 524 | h2c->flags = H2_CF_IS_BACK; |
| 525 | h2c->shut_timeout = h2c->timeout = prx->timeout.server; |
| 526 | if (tick_isset(prx->timeout.serverfin)) |
| 527 | h2c->shut_timeout = prx->timeout.serverfin; |
| 528 | } else { |
| 529 | h2c->flags = H2_CF_NONE; |
| 530 | h2c->shut_timeout = h2c->timeout = prx->timeout.client; |
| 531 | if (tick_isset(prx->timeout.clientfin)) |
| 532 | h2c->shut_timeout = prx->timeout.clientfin; |
| 533 | } |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 534 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 535 | h2c->proxy = prx; |
Willy Tarreau | 3340029 | 2017-11-05 11:23:40 +0100 | [diff] [blame] | 536 | h2c->task = NULL; |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 537 | if (tick_isset(h2c->timeout)) { |
| 538 | t = task_new(tid_bit); |
| 539 | if (!t) |
| 540 | goto fail; |
| 541 | |
| 542 | h2c->task = t; |
| 543 | t->process = h2_timeout_task; |
| 544 | t->context = h2c; |
| 545 | t->expire = tick_add(now_ms, h2c->timeout); |
| 546 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 547 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 548 | h2c->wait_event.tasklet = tasklet_new(); |
| 549 | if (!h2c->wait_event.tasklet) |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 550 | goto fail; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 551 | h2c->wait_event.tasklet->process = h2_io_cb; |
| 552 | h2c->wait_event.tasklet->context = h2c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 553 | h2c->wait_event.events = 0; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 554 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 555 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 556 | if (!h2c->ddht) |
| 557 | goto fail; |
| 558 | |
| 559 | /* Initialise the context. */ |
| 560 | h2c->st0 = H2_CS_PREFACE; |
| 561 | h2c->conn = conn; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 562 | h2c->streams_limit = h2_settings_max_concurrent_streams; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 563 | h2c->max_id = -1; |
| 564 | h2c->errcode = H2_ERR_NO_ERROR; |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 565 | h2c->rcvd_c = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 566 | h2c->rcvd_s = 0; |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 567 | h2c->nb_streams = 0; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 568 | h2c->nb_cs = 0; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 569 | h2c->nb_reserved = 0; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 570 | h2c->stream_cnt = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 571 | |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 572 | h2c->dbuf = *input; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 573 | h2c->dsi = -1; |
| 574 | h2c->msi = -1; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 575 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 576 | h2c->last_sid = -1; |
| 577 | |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 578 | br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0])); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 579 | h2c->miw = 65535; /* mux initial window size */ |
| 580 | h2c->mws = 65535; /* mux window size */ |
| 581 | h2c->mfs = 16384; /* initial max frame size */ |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 582 | h2c->streams_by_id = EB_ROOT; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 583 | LIST_INIT(&h2c->send_list); |
| 584 | LIST_INIT(&h2c->fctl_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 585 | LIST_INIT(&h2c->sending_list); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 586 | LIST_INIT(&h2c->buf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 587 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 588 | if (t) |
| 589 | task_queue(t); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 590 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 591 | if (h2c->flags & H2_CF_IS_BACK) { |
| 592 | /* FIXME: this is temporary, for outgoing connections we need |
| 593 | * to immediately allocate a stream until the code is modified |
| 594 | * so that the caller calls ->attach(). For now the outgoing cs |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 595 | * is stored as conn->ctx by the caller. |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 596 | */ |
| 597 | struct h2s *h2s; |
| 598 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 599 | h2s = h2c_bck_stream_new(h2c, conn->ctx, sess); |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 600 | if (!h2s) |
| 601 | goto fail_stream; |
| 602 | } |
| 603 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 604 | conn->ctx = h2c; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 605 | |
Willy Tarreau | 0f38358 | 2018-10-03 14:22:21 +0200 | [diff] [blame] | 606 | /* prepare to read something */ |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 607 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 608 | return 0; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 609 | fail_stream: |
| 610 | hpack_dht_free(h2c->ddht); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 611 | fail: |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 612 | task_destroy(t); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 613 | if (h2c->wait_event.tasklet) |
| 614 | tasklet_free(h2c->wait_event.tasklet); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 615 | pool_free(pool_head_h2c, h2c); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 616 | fail_no_h2c: |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 617 | return -1; |
| 618 | } |
| 619 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 620 | /* returns the next allocatable outgoing stream ID for the H2 connection, or |
| 621 | * -1 if no more is allocatable. |
| 622 | */ |
| 623 | static inline int32_t h2c_get_next_sid(const struct h2c *h2c) |
| 624 | { |
| 625 | int32_t id = (h2c->max_id + 1) | 1; |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 626 | |
| 627 | if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid)) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 628 | id = -1; |
| 629 | return id; |
| 630 | } |
| 631 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 632 | /* returns the stream associated with id <id> or NULL if not found */ |
| 633 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 634 | { |
| 635 | struct eb32_node *node; |
| 636 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 637 | if (id == 0) |
| 638 | return (struct h2s *)h2_closed_stream; |
| 639 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 640 | if (id > h2c->max_id) |
| 641 | return (struct h2s *)h2_idle_stream; |
| 642 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 643 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 644 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 645 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 646 | |
| 647 | return container_of(node, struct h2s, by_id); |
| 648 | } |
| 649 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 650 | /* release function. This one should be called to free all resources allocated |
| 651 | * to the mux. |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 652 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 653 | static void h2_release(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 654 | { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 655 | struct connection *conn = NULL;; |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 656 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 657 | if (h2c) { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 658 | /* The connection must be aattached to this mux to be released */ |
| 659 | if (h2c->conn && h2c->conn->ctx == h2c) |
| 660 | conn = h2c->conn; |
| 661 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 662 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 663 | |
Willy Tarreau | 186e96e | 2019-05-28 17:21:18 +0200 | [diff] [blame] | 664 | if (LIST_ADDED(&h2c->buf_wait.list)) { |
| 665 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 666 | LIST_DEL(&h2c->buf_wait.list); |
| 667 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 668 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 669 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 670 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 671 | h2_release_mbuf(h2c); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 672 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 673 | if (h2c->task) { |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 674 | h2c->task->context = NULL; |
| 675 | task_wakeup(h2c->task, TASK_WOKEN_OTHER); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 676 | h2c->task = NULL; |
| 677 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 678 | if (h2c->wait_event.tasklet) |
| 679 | tasklet_free(h2c->wait_event.tasklet); |
Christopher Faulet | 0e01256 | 2019-09-18 11:07:20 +0200 | [diff] [blame] | 680 | if (conn && h2c->wait_event.events != 0) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 681 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events, |
Christopher Faulet | 0e01256 | 2019-09-18 11:07:20 +0200 | [diff] [blame] | 682 | &h2c->wait_event); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 683 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 684 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 685 | } |
| 686 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 687 | if (conn) { |
| 688 | conn->mux = NULL; |
| 689 | conn->ctx = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 690 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 691 | conn_stop_tracking(conn); |
| 692 | conn_full_close(conn); |
| 693 | if (conn->destroy_cb) |
| 694 | conn->destroy_cb(conn); |
| 695 | conn_free(conn); |
| 696 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 700 | /******************************************************/ |
| 701 | /* functions below are for the H2 protocol processing */ |
| 702 | /******************************************************/ |
| 703 | |
| 704 | /* 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] | 705 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 706 | { |
| 707 | return h2s ? h2s->id : 0; |
| 708 | } |
| 709 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 710 | /* returns the sum of the stream's own window size and the mux's initial |
| 711 | * window, which together form the stream's effective window size. |
| 712 | */ |
| 713 | static inline int h2s_mws(const struct h2s *h2s) |
| 714 | { |
| 715 | return h2s->sws + h2s->h2c->miw; |
| 716 | } |
| 717 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 718 | /* 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] | 719 | 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] | 720 | { |
| 721 | if (h2c->msi < 0) |
| 722 | return 0; |
| 723 | |
| 724 | if (h2c->msi == h2s_id(h2s)) |
| 725 | return 0; |
| 726 | |
| 727 | return 1; |
| 728 | } |
| 729 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 730 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 731 | 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] | 732 | { |
| 733 | h2c->errcode = err; |
| 734 | h2c->st0 = H2_CS_ERROR; |
| 735 | } |
| 736 | |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 737 | /* marks an error on the stream. It may also update an already closed stream |
| 738 | * (e.g. to report an error after an RST was received). |
| 739 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 740 | 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] | 741 | { |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 742 | if (h2s->id && h2s->st != H2_SS_ERROR) { |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 743 | h2s->errcode = err; |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 744 | if (h2s->st < H2_SS_ERROR) |
| 745 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 746 | if (h2s->cs) |
| 747 | cs_set_error(h2s->cs); |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 751 | /* attempt to notify the data layer of recv availability */ |
| 752 | static void __maybe_unused h2s_notify_recv(struct h2s *h2s) |
| 753 | { |
| 754 | struct wait_event *sw; |
| 755 | |
| 756 | if (h2s->recv_wait) { |
| 757 | sw = h2s->recv_wait; |
| 758 | sw->events &= ~SUB_RETRY_RECV; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 759 | tasklet_wakeup(sw->tasklet); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 760 | h2s->recv_wait = NULL; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | /* attempt to notify the data layer of send availability */ |
| 765 | static void __maybe_unused h2s_notify_send(struct h2s *h2s) |
| 766 | { |
| 767 | struct wait_event *sw; |
| 768 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 769 | if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) { |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 770 | sw = h2s->send_wait; |
| 771 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fd1e96d | 2019-03-25 14:04:25 +0100 | [diff] [blame] | 772 | LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 773 | tasklet_wakeup(sw->tasklet); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 777 | /* alerts the data layer, trying to wake it up by all means, following |
| 778 | * this sequence : |
| 779 | * - if the h2s' data layer is subscribed to recv, then it's woken up for recv |
| 780 | * - if its subscribed to send, then it's woken up for send |
| 781 | * - if it was subscribed to neither, its ->wake() callback is called |
| 782 | * It is safe to call this function with a closed stream which doesn't have a |
| 783 | * conn_stream anymore. |
| 784 | */ |
| 785 | static void __maybe_unused h2s_alert(struct h2s *h2s) |
| 786 | { |
| 787 | if (h2s->recv_wait || h2s->send_wait) { |
| 788 | h2s_notify_recv(h2s); |
| 789 | h2s_notify_send(h2s); |
| 790 | } |
| 791 | else if (h2s->cs && h2s->cs->data_cb->wake != NULL) |
| 792 | h2s->cs->data_cb->wake(h2s->cs); |
| 793 | } |
| 794 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 795 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 796 | 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] | 797 | { |
| 798 | uint8_t *out = frame; |
| 799 | |
| 800 | *out = len >> 16; |
| 801 | write_n16(out + 1, len); |
| 802 | } |
| 803 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 804 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 805 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 806 | * 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] | 807 | * available in the buffer's input prior to calling this function. The buffer |
| 808 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 809 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 810 | 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] | 811 | const struct buffer *b, int o) |
| 812 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 813 | 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] | 814 | } |
| 815 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 816 | 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] | 817 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 818 | 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] | 819 | } |
| 820 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 821 | 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] | 822 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 823 | 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] | 824 | } |
| 825 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 826 | 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] | 827 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 828 | 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] | 829 | } |
| 830 | |
| 831 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 832 | /* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>. |
| 833 | * The algorithm is not obvious. It turns out that H2 headers are neither |
| 834 | * aligned nor do they use regular sizes. And to add to the trouble, the buffer |
| 835 | * 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] | 836 | * |
| 837 | * b0 b1 b2 b3 b4 b5..b8 |
| 838 | * +----------+---------+--------+----+----+----------------------+ |
| 839 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 840 | * +----------+---------+--------+----+----+----------------------+ |
| 841 | * |
| 842 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 843 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 844 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 845 | * 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] | 846 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 847 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 848 | */ |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 849 | 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] | 850 | { |
| 851 | uint64_t w; |
| 852 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 853 | if (b_data(b) < o + 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 854 | return 0; |
| 855 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 856 | w = h2_get_n64(b, o + 1); |
| 857 | h->len = *(uint8_t*)b_peek(b, o) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 858 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 859 | h->ff = w >> 32; |
| 860 | h->ft = w >> 40; |
| 861 | h->len += w >> 48; |
| 862 | return 1; |
| 863 | } |
| 864 | |
| 865 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 866 | * h2_peek_frame_hdr() above. |
| 867 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 868 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 869 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 870 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 874 | 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] | 875 | { |
| 876 | int ret; |
| 877 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 878 | ret = h2_peek_frame_hdr(b, 0, h); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 879 | if (ret > 0) |
| 880 | h2_skip_frame_hdr(b); |
| 881 | return ret; |
| 882 | } |
| 883 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 884 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 885 | * its connection if the stream was not yet closed. Please use this exclusively |
| 886 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 887 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 888 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 889 | { |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 890 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 891 | h2s->h2c->nb_streams--; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 892 | if (!h2s->id) |
| 893 | h2s->h2c->nb_reserved--; |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 894 | if (h2s->cs) { |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 895 | if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf)) |
| 896 | h2s_notify_recv(h2s); |
| 897 | } |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 898 | } |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 899 | h2s->st = H2_SS_CLOSED; |
| 900 | } |
| 901 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 902 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 903 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 904 | { |
| 905 | h2s_close(h2s); |
| 906 | eb32_delete(&h2s->by_id); |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 907 | if (b_size(&h2s->rxbuf)) { |
| 908 | b_free(&h2s->rxbuf); |
| 909 | offer_buffers(NULL, tasks_run_queue); |
| 910 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 911 | if (h2s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 912 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 913 | if (h2s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 914 | h2s->recv_wait->events &= ~SUB_RETRY_RECV; |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 915 | /* There's no need to explicitly call unsubscribe here, the only |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 916 | * reference left would be in the h2c send_list/fctl_list, and if |
| 917 | * we're in it, we're getting out anyway |
| 918 | */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 919 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 920 | if (LIST_ADDED(&h2s->sending_list)) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 921 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 922 | LIST_DEL_INIT(&h2s->sending_list); |
| 923 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 924 | tasklet_free(h2s->wait_event.tasklet); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 925 | pool_free(pool_head_h2s, h2s); |
| 926 | } |
| 927 | |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 928 | /* allocates a new stream <id> for connection <h2c> and adds it into h2c's |
| 929 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 930 | * causes of errors can be any failed memory allocation. The caller is |
| 931 | * responsible for checking if the connection may support an extra stream |
| 932 | * prior to calling this function. |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 933 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 934 | static struct h2s *h2s_new(struct h2c *h2c, int id) |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 935 | { |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 936 | struct h2s *h2s; |
| 937 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 938 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 939 | if (!h2s) |
| 940 | goto out; |
| 941 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 942 | h2s->wait_event.tasklet = tasklet_new(); |
| 943 | if (!h2s->wait_event.tasklet) { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 944 | pool_free(pool_head_h2s, h2s); |
| 945 | goto out; |
| 946 | } |
| 947 | h2s->send_wait = NULL; |
| 948 | h2s->recv_wait = NULL; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 949 | h2s->wait_event.tasklet->process = h2_deferred_shut; |
| 950 | h2s->wait_event.tasklet->context = h2s; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 951 | h2s->wait_event.events = 0; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 952 | LIST_INIT(&h2s->list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 953 | LIST_INIT(&h2s->sending_list); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 954 | h2s->h2c = h2c; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 955 | h2s->cs = NULL; |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 956 | h2s->sws = 0; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 957 | h2s->flags = H2_SF_NONE; |
| 958 | h2s->errcode = H2_ERR_NO_ERROR; |
| 959 | h2s->st = H2_SS_IDLE; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 960 | h2s->status = 0; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 961 | h2s->body_len = 0; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 962 | h2s->rxbuf = BUF_NULL; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 963 | |
| 964 | if (h2c->flags & H2_CF_IS_BACK) { |
| 965 | h1m_init_req(&h2s->h1m); |
| 966 | h2s->h1m.err_pos = -1; // don't care about errors on the request path |
| 967 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 968 | } else { |
| 969 | h1m_init_res(&h2s->h1m); |
| 970 | h2s->h1m.err_pos = -1; // don't care about errors on the response path |
| 971 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 972 | } |
| 973 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 974 | h2s->by_id.key = h2s->id = id; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 975 | if (id > 0) |
| 976 | h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 977 | else |
| 978 | h2c->nb_reserved++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 979 | |
| 980 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 981 | h2c->nb_streams++; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 982 | h2c->stream_cnt++; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 983 | |
| 984 | return h2s; |
| 985 | |
| 986 | out_free_h2s: |
| 987 | pool_free(pool_head_h2s, h2s); |
| 988 | out: |
| 989 | return NULL; |
| 990 | } |
| 991 | |
| 992 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 993 | * case of memory allocation error. |
| 994 | */ |
| 995 | static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id) |
| 996 | { |
| 997 | struct session *sess = h2c->conn->owner; |
| 998 | struct conn_stream *cs; |
| 999 | struct h2s *h2s; |
| 1000 | |
| 1001 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 1002 | goto out; |
| 1003 | |
| 1004 | h2s = h2s_new(h2c, id); |
| 1005 | if (!h2s) |
| 1006 | goto out; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1007 | |
| 1008 | cs = cs_new(h2c->conn); |
| 1009 | if (!cs) |
| 1010 | goto out_close; |
| 1011 | |
Olivier Houchard | 746fb77 | 2018-12-15 19:42:00 +0100 | [diff] [blame] | 1012 | cs->flags |= CS_FL_NOT_FIRST; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1013 | h2s->cs = cs; |
| 1014 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 1015 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1016 | |
| 1017 | if (stream_create_from_cs(cs) < 0) |
| 1018 | goto out_free_cs; |
| 1019 | |
Willy Tarreau | 590a051 | 2018-09-05 11:56:48 +0200 | [diff] [blame] | 1020 | /* We want the accept date presented to the next stream to be the one |
| 1021 | * we have now, the handshake time to be null (since the next stream |
| 1022 | * is not delayed by a handshake), and the idle time to count since |
| 1023 | * right now. |
| 1024 | */ |
| 1025 | sess->accept_date = date; |
| 1026 | sess->tv_accept = now; |
| 1027 | sess->t_handshake = 0; |
| 1028 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1029 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 1030 | if (h2_frt_has_too_many_cs(h2c)) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 1031 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1032 | return h2s; |
| 1033 | |
| 1034 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 1035 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1036 | cs_free(cs); |
Olivier Houchard | 58d87f3 | 2019-05-29 16:44:17 +0200 | [diff] [blame] | 1037 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1038 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 1039 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1040 | out: |
Willy Tarreau | 45efc07 | 2018-10-03 18:27:52 +0200 | [diff] [blame] | 1041 | sess_log(sess); |
| 1042 | return NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1043 | } |
| 1044 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1045 | /* allocates a new stream associated to conn_stream <cs> on the h2c connection |
| 1046 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 1047 | * possible stream ID was reached. |
| 1048 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1049 | 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] | 1050 | { |
| 1051 | struct h2s *h2s = NULL; |
| 1052 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 1053 | if (h2c->nb_streams >= h2c->streams_limit) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1054 | goto out; |
| 1055 | |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 1056 | if (h2_streams_left(h2c) < 1) |
| 1057 | goto out; |
| 1058 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1059 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 1060 | h2s = h2s_new(h2c, 0); |
| 1061 | if (!h2s) |
| 1062 | goto out; |
| 1063 | |
| 1064 | h2s->cs = cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1065 | h2s->sess = sess; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1066 | cs->ctx = h2s; |
| 1067 | h2c->nb_cs++; |
| 1068 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1069 | out: |
| 1070 | return h2s; |
| 1071 | } |
| 1072 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1073 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 1074 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 1075 | * the various settings codes. |
| 1076 | */ |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1077 | static int h2c_send_settings(struct h2c *h2c) |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1078 | { |
| 1079 | struct buffer *res; |
| 1080 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1081 | struct buffer buf; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1082 | int mfs; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1083 | int ret; |
| 1084 | |
| 1085 | if (h2c_mux_busy(h2c, NULL)) { |
| 1086 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1090 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 1091 | chunk_memcpy(&buf, |
| 1092 | "\x00\x00\x00" /* length : 0 for now */ |
| 1093 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 1094 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 1095 | 9); |
| 1096 | |
Willy Tarreau | 0bbad6b | 2019-02-26 16:01:52 +0100 | [diff] [blame] | 1097 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1098 | /* send settings_enable_push=0 */ |
| 1099 | chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6); |
| 1100 | } |
| 1101 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1102 | if (h2_settings_header_table_size != 4096) { |
| 1103 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 1104 | |
| 1105 | write_n32(str + 2, h2_settings_header_table_size); |
| 1106 | chunk_memcat(&buf, str, 6); |
| 1107 | } |
| 1108 | |
| 1109 | if (h2_settings_initial_window_size != 65535) { |
| 1110 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 1111 | |
| 1112 | write_n32(str + 2, h2_settings_initial_window_size); |
| 1113 | chunk_memcat(&buf, str, 6); |
| 1114 | } |
| 1115 | |
| 1116 | if (h2_settings_max_concurrent_streams != 0) { |
| 1117 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 1118 | |
| 1119 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 1120 | * the protocol, so never send this value! |
| 1121 | */ |
| 1122 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 1123 | chunk_memcat(&buf, str, 6); |
| 1124 | } |
| 1125 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1126 | mfs = h2_settings_max_frame_size; |
| 1127 | if (mfs > global.tune.bufsize) |
| 1128 | mfs = global.tune.bufsize; |
| 1129 | |
| 1130 | if (!mfs) |
| 1131 | mfs = global.tune.bufsize; |
| 1132 | |
| 1133 | if (mfs != 16384) { |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1134 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 1135 | |
| 1136 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 1137 | * match bufsize - rewrite size, but at the moment it seems |
| 1138 | * that clients don't take care of it. |
| 1139 | */ |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1140 | write_n32(str + 2, mfs); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1141 | chunk_memcat(&buf, str, 6); |
| 1142 | } |
| 1143 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1144 | h2_set_frame_size(buf.area, buf.data - 9); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1145 | |
| 1146 | res = br_tail(h2c->mbuf); |
| 1147 | retry: |
| 1148 | if (!h2_get_buf(h2c, res)) { |
| 1149 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1150 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1154 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1155 | if (unlikely(ret <= 0)) { |
| 1156 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1157 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1158 | goto retry; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1159 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1160 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1161 | return 0; |
| 1162 | } |
| 1163 | else { |
| 1164 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1165 | return 0; |
| 1166 | } |
| 1167 | } |
| 1168 | return ret; |
| 1169 | } |
| 1170 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1171 | /* Try to receive a connection preface, then upon success try to send our |
| 1172 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1173 | * missing data. It may return an error in h2c. |
| 1174 | */ |
| 1175 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 1176 | { |
| 1177 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1178 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1179 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1180 | 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] | 1181 | |
| 1182 | if (unlikely(ret1 <= 0)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1183 | if (ret1 < 0) |
| 1184 | sess_log(h2c->conn->owner); |
| 1185 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1186 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 1187 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1191 | ret2 = h2c_send_settings(h2c); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1192 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1193 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1194 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1195 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1196 | } |
| 1197 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1198 | /* Try to send a connection preface, then upon success try to send our |
| 1199 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1200 | * missing data. It may return an error in h2c. |
| 1201 | */ |
| 1202 | static int h2c_bck_send_preface(struct h2c *h2c) |
| 1203 | { |
| 1204 | struct buffer *res; |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1205 | int ret; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1206 | |
| 1207 | if (h2c_mux_busy(h2c, NULL)) { |
| 1208 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1212 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1213 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1214 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1215 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1216 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
| 1220 | if (!b_data(res)) { |
| 1221 | /* preface not yet sent */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1222 | ret = b_istput(res, ist(H2_CONN_PREFACE)); |
| 1223 | if (unlikely(ret <= 0)) { |
| 1224 | if (!ret) { |
| 1225 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1226 | goto retry; |
| 1227 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1228 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1229 | return 0; |
| 1230 | } |
| 1231 | else { |
| 1232 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1233 | return 0; |
| 1234 | } |
| 1235 | } |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1236 | } |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1237 | return h2c_send_settings(h2c); |
| 1238 | } |
| 1239 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1240 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 1241 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 1242 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 1243 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 1244 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 1245 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 1246 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 1247 | * case so that it is safe to use h2c_error() to report such errors. |
| 1248 | */ |
| 1249 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 1250 | { |
| 1251 | struct buffer *res; |
| 1252 | char str[17]; |
| 1253 | int ret; |
| 1254 | |
| 1255 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 1256 | return 1; // claim that it worked |
| 1257 | |
| 1258 | if (h2c_mux_busy(h2c, h2s)) { |
| 1259 | if (h2s) |
| 1260 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1261 | else |
| 1262 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1266 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 1267 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 1268 | |
| 1269 | if (h2c->last_sid < 0) |
| 1270 | h2c->last_sid = h2c->max_id; |
| 1271 | |
| 1272 | write_n32(str + 9, h2c->last_sid); |
| 1273 | write_n32(str + 13, h2c->errcode); |
| 1274 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1275 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1276 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1277 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1278 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1279 | if (h2s) |
| 1280 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1281 | else |
| 1282 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1283 | return 0; |
| 1284 | } |
| 1285 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1286 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1287 | if (unlikely(ret <= 0)) { |
| 1288 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1289 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1290 | goto retry; |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1291 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1292 | if (h2s) |
| 1293 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1294 | else |
| 1295 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1296 | return 0; |
| 1297 | } |
| 1298 | else { |
| 1299 | /* we cannot report this error using GOAWAY, so we mark |
| 1300 | * it and claim a success. |
| 1301 | */ |
| 1302 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1303 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 1304 | return 1; |
| 1305 | } |
| 1306 | } |
| 1307 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 1308 | return ret; |
| 1309 | } |
| 1310 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1311 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 1312 | * during mux operations. This stream must be valid and cannot be closed |
| 1313 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 1314 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 1315 | * not yet. |
| 1316 | * |
| 1317 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1318 | * to write the message, it subscribes the stream to future notifications. |
| 1319 | */ |
| 1320 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1321 | { |
| 1322 | struct buffer *res; |
| 1323 | char str[13]; |
| 1324 | int ret; |
| 1325 | |
| 1326 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 1327 | return 1; |
| 1328 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1329 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1330 | * RST_STREAM in response to a RST_STREAM frame. |
| 1331 | */ |
Willy Tarreau | bf9a20b | 2019-08-06 10:01:40 +0200 | [diff] [blame] | 1332 | if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) { |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1333 | ret = 1; |
| 1334 | goto ignore; |
| 1335 | } |
| 1336 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1337 | if (h2c_mux_busy(h2c, h2s)) { |
| 1338 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1342 | /* len: 4, type: 3, flags: none */ |
| 1343 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1344 | write_n32(str + 5, h2s->id); |
| 1345 | write_n32(str + 9, h2s->errcode); |
| 1346 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1347 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1348 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1349 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1350 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1351 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1352 | return 0; |
| 1353 | } |
| 1354 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1355 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1356 | if (unlikely(ret <= 0)) { |
| 1357 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1358 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1359 | goto retry; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1360 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1361 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1362 | return 0; |
| 1363 | } |
| 1364 | else { |
| 1365 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1366 | return 0; |
| 1367 | } |
| 1368 | } |
| 1369 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1370 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1371 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1372 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1373 | return ret; |
| 1374 | } |
| 1375 | |
| 1376 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 1377 | * 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] | 1378 | * error code, even if the stream is one of the dummy ones, and will update |
| 1379 | * h2s->st to H2_SS_CLOSED if it was not yet. |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1380 | * |
| 1381 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1382 | * 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] | 1383 | * 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] | 1384 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1385 | */ |
| 1386 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1387 | { |
| 1388 | struct buffer *res; |
| 1389 | char str[13]; |
| 1390 | int ret; |
| 1391 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1392 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1393 | * RST_STREAM in response to a RST_STREAM frame. |
| 1394 | */ |
| 1395 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1396 | ret = 1; |
| 1397 | goto ignore; |
| 1398 | } |
| 1399 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1400 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1401 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1402 | return 0; |
| 1403 | } |
| 1404 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1405 | /* len: 4, type: 3, flags: none */ |
| 1406 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1407 | |
| 1408 | write_n32(str + 5, h2c->dsi); |
| 1409 | write_n32(str + 9, h2s->errcode); |
| 1410 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1411 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1412 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1413 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1414 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1415 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1416 | return 0; |
| 1417 | } |
| 1418 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1419 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1420 | if (unlikely(ret <= 0)) { |
| 1421 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1422 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1423 | goto retry; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1424 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1425 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1426 | return 0; |
| 1427 | } |
| 1428 | else { |
| 1429 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1430 | return 0; |
| 1431 | } |
| 1432 | } |
| 1433 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1434 | ignore: |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1435 | if (h2s->id) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1436 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1437 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1438 | } |
| 1439 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1440 | return ret; |
| 1441 | } |
| 1442 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1443 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 1444 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 1445 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 1446 | * on success or zero if nothing was done. In case of lack of room to write the |
| 1447 | * message, it subscribes the requesting stream to future notifications. |
| 1448 | */ |
| 1449 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 1450 | { |
| 1451 | struct h2c *h2c = h2s->h2c; |
| 1452 | struct buffer *res; |
| 1453 | char str[9]; |
| 1454 | int ret; |
| 1455 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1456 | 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] | 1457 | return 1; |
| 1458 | |
| 1459 | if (h2c_mux_busy(h2c, h2s)) { |
| 1460 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1461 | return 0; |
| 1462 | } |
| 1463 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1464 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1465 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1466 | write_n32(str + 5, h2s->id); |
| 1467 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1468 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1469 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1470 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1471 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1472 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1473 | return 0; |
| 1474 | } |
| 1475 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1476 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1477 | if (likely(ret > 0)) { |
| 1478 | h2s->flags |= H2_SF_ES_SENT; |
| 1479 | } |
| 1480 | else if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1481 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1482 | goto retry; |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1483 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1484 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1485 | return 0; |
| 1486 | } |
| 1487 | else { |
| 1488 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1489 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1490 | } |
| 1491 | return ret; |
| 1492 | } |
| 1493 | |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1494 | /* wake a specific stream and assign its conn_stream som CS_FL_* flags among |
Willy Tarreau | 99ad1b3 | 2019-05-14 11:46:28 +0200 | [diff] [blame] | 1495 | * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1496 | * is automatically updated accordingly. If the stream is orphaned, it is |
| 1497 | * destroyed. |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1498 | */ |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1499 | static void h2s_wake_one_stream(struct h2s *h2s) |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1500 | { |
| 1501 | if (!h2s->cs) { |
| 1502 | /* this stream was already orphaned */ |
| 1503 | h2s_destroy(h2s); |
| 1504 | return; |
| 1505 | } |
| 1506 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1507 | if (conn_xprt_read0_pending(h2s->h2c->conn)) { |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1508 | if (h2s->st == H2_SS_OPEN) |
| 1509 | h2s->st = H2_SS_HREM; |
| 1510 | else if (h2s->st == H2_SS_HLOC) |
| 1511 | h2s_close(h2s); |
| 1512 | } |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1513 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1514 | if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) || |
| 1515 | (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) { |
| 1516 | h2s->cs->flags |= CS_FL_ERR_PENDING; |
| 1517 | if (h2s->cs->flags & CS_FL_EOS) |
| 1518 | h2s->cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1519 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1520 | if (h2s->st < H2_SS_ERROR) |
| 1521 | h2s->st = H2_SS_ERROR; |
| 1522 | } |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1523 | |
| 1524 | h2s_alert(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | /* wake the streams attached to the connection, whose id is greater than <last> |
| 1528 | * or unassigned. |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1529 | */ |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1530 | static void h2_wake_some_streams(struct h2c *h2c, int last) |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1531 | { |
| 1532 | struct eb32_node *node; |
| 1533 | struct h2s *h2s; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1534 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1535 | /* Wake all streams with ID > last */ |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1536 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1537 | while (node) { |
| 1538 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1539 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1540 | h2s_wake_one_stream(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1541 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1542 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1543 | /* Wake all streams with unassigned ID (ID == 0) */ |
| 1544 | node = eb32_lookup(&h2c->streams_by_id, 0); |
| 1545 | while (node) { |
| 1546 | h2s = container_of(node, struct h2s, by_id); |
| 1547 | if (h2s->id > 0) |
| 1548 | break; |
| 1549 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1550 | h2s_wake_one_stream(h2s); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1551 | } |
| 1552 | } |
| 1553 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1554 | /* Wake up all blocked streams whose window size has become positive after the |
| 1555 | * mux's initial window was adjusted. This should be done after having processed |
| 1556 | * SETTINGS frames which have updated the mux's initial window size. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1557 | */ |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1558 | static void h2c_unblock_sfctl(struct h2c *h2c) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1559 | { |
| 1560 | struct h2s *h2s; |
| 1561 | struct eb32_node *node; |
| 1562 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1563 | node = eb32_first(&h2c->streams_by_id); |
| 1564 | while (node) { |
| 1565 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1566 | if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) { |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1567 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 1568 | if (h2s->send_wait && !LIST_ADDED(&h2s->list)) |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1569 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1570 | } |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1571 | node = eb32_next(node); |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1576 | * 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] | 1577 | * return an error in h2c. The caller must have already verified frame length |
| 1578 | * and stream ID validity. Described in RFC7540#6.5. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1579 | */ |
| 1580 | static int h2c_handle_settings(struct h2c *h2c) |
| 1581 | { |
| 1582 | unsigned int offset; |
| 1583 | int error; |
| 1584 | |
| 1585 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1586 | if (h2c->dfl) { |
| 1587 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1588 | goto fail; |
| 1589 | } |
| 1590 | return 1; |
| 1591 | } |
| 1592 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1593 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1594 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1595 | return 0; |
| 1596 | |
| 1597 | /* parse the frame */ |
| 1598 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1599 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1600 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1601 | |
| 1602 | switch (type) { |
| 1603 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1604 | /* we need to update all existing streams with the |
| 1605 | * difference from the previous iws. |
| 1606 | */ |
| 1607 | if (arg < 0) { // RFC7540#6.5.2 |
| 1608 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1609 | goto fail; |
| 1610 | } |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1611 | h2c->miw = arg; |
| 1612 | break; |
| 1613 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1614 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1615 | error = H2_ERR_PROTOCOL_ERROR; |
| 1616 | goto fail; |
| 1617 | } |
| 1618 | h2c->mfs = arg; |
| 1619 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1620 | case H2_SETTINGS_ENABLE_PUSH: |
| 1621 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1622 | error = H2_ERR_PROTOCOL_ERROR; |
| 1623 | goto fail; |
| 1624 | } |
| 1625 | break; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 1626 | case H2_SETTINGS_MAX_CONCURRENT_STREAMS: |
| 1627 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1628 | /* the limit is only for the backend; for the frontend it is our limit */ |
| 1629 | if ((unsigned int)arg > h2_settings_max_concurrent_streams) |
| 1630 | arg = h2_settings_max_concurrent_streams; |
| 1631 | h2c->streams_limit = arg; |
| 1632 | } |
| 1633 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | /* need to ACK this frame now */ |
| 1638 | h2c->st0 = H2_CS_FRAME_A; |
| 1639 | return 1; |
| 1640 | fail: |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1641 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1642 | h2c_error(h2c, error); |
| 1643 | return 0; |
| 1644 | } |
| 1645 | |
| 1646 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1647 | * success or one of the h2_status values. |
| 1648 | */ |
| 1649 | static int h2c_ack_settings(struct h2c *h2c) |
| 1650 | { |
| 1651 | struct buffer *res; |
| 1652 | char str[9]; |
| 1653 | int ret = -1; |
| 1654 | |
| 1655 | if (h2c_mux_busy(h2c, NULL)) { |
| 1656 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1657 | return 0; |
| 1658 | } |
| 1659 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1660 | memcpy(str, |
| 1661 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1662 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1663 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1664 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1665 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1666 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1667 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1668 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1669 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1670 | return 0; |
| 1671 | } |
| 1672 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1673 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1674 | if (unlikely(ret <= 0)) { |
| 1675 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1676 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1677 | goto retry; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1678 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1679 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1680 | return 0; |
| 1681 | } |
| 1682 | else { |
| 1683 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1684 | return 0; |
| 1685 | } |
| 1686 | } |
| 1687 | return ret; |
| 1688 | } |
| 1689 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1690 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1691 | * 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] | 1692 | * missing data. The caller must have already verified frame length |
| 1693 | * and stream ID validity. |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1694 | */ |
| 1695 | static int h2c_handle_ping(struct h2c *h2c) |
| 1696 | { |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1697 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1698 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1699 | h2c->st0 = H2_CS_FRAME_A; |
| 1700 | return 1; |
| 1701 | } |
| 1702 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1703 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1704 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1705 | * error in h2c. |
| 1706 | */ |
| 1707 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1708 | { |
| 1709 | struct buffer *res; |
| 1710 | char str[13]; |
| 1711 | int ret = -1; |
| 1712 | |
| 1713 | if (h2c_mux_busy(h2c, NULL)) { |
| 1714 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1718 | /* length: 4, type: 8, flags: none */ |
| 1719 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1720 | write_n32(str + 5, sid); |
| 1721 | write_n32(str + 9, increment); |
| 1722 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1723 | res = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1724 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 1725 | if (!h2_get_buf(h2c, res)) { |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1726 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1727 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1728 | return 0; |
| 1729 | } |
| 1730 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1731 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1732 | if (unlikely(ret <= 0)) { |
| 1733 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1734 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1735 | goto retry; |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1736 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1737 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1738 | return 0; |
| 1739 | } |
| 1740 | else { |
| 1741 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1742 | return 0; |
| 1743 | } |
| 1744 | } |
| 1745 | return ret; |
| 1746 | } |
| 1747 | |
| 1748 | /* try to send pending window update for the connection. It's safe to call it |
| 1749 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1750 | * failure. It may return an error in h2c. |
| 1751 | */ |
| 1752 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1753 | { |
| 1754 | int ret = 1; |
| 1755 | |
| 1756 | if (h2c->rcvd_c <= 0) |
| 1757 | return 1; |
| 1758 | |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 1759 | if (!(h2c->flags & H2_CF_WINDOW_OPENED)) { |
| 1760 | /* increase the advertised connection window to 2G on |
| 1761 | * first update. |
| 1762 | */ |
| 1763 | h2c->flags |= H2_CF_WINDOW_OPENED; |
| 1764 | h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT; |
| 1765 | } |
| 1766 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1767 | /* send WU for the connection */ |
| 1768 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1769 | if (ret > 0) |
| 1770 | h2c->rcvd_c = 0; |
| 1771 | |
| 1772 | return ret; |
| 1773 | } |
| 1774 | |
| 1775 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1776 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1777 | * room or failure. It may return an error in h2c. |
| 1778 | */ |
| 1779 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1780 | { |
| 1781 | int ret = 1; |
| 1782 | |
| 1783 | if (h2c->rcvd_s <= 0) |
| 1784 | return 1; |
| 1785 | |
| 1786 | /* send WU for the stream */ |
| 1787 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1788 | if (ret > 0) |
| 1789 | h2c->rcvd_s = 0; |
| 1790 | |
| 1791 | return ret; |
| 1792 | } |
| 1793 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1794 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1795 | * success, 0 on missing data or one of the h2_status values. |
| 1796 | */ |
| 1797 | static int h2c_ack_ping(struct h2c *h2c) |
| 1798 | { |
| 1799 | struct buffer *res; |
| 1800 | char str[17]; |
| 1801 | int ret = -1; |
| 1802 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1803 | if (b_data(&h2c->dbuf) < 8) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1804 | return 0; |
| 1805 | |
| 1806 | if (h2c_mux_busy(h2c, NULL)) { |
| 1807 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1808 | return 0; |
| 1809 | } |
| 1810 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1811 | memcpy(str, |
| 1812 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1813 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1814 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1815 | |
| 1816 | /* copy the original payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1817 | h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1818 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1819 | res = br_tail(h2c->mbuf); |
| 1820 | retry: |
| 1821 | if (!h2_get_buf(h2c, res)) { |
| 1822 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1823 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1824 | return 0; |
| 1825 | } |
| 1826 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1827 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1828 | if (unlikely(ret <= 0)) { |
| 1829 | if (!ret) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 1830 | if ((res = br_tail_add(h2c->mbuf)) != NULL) |
| 1831 | goto retry; |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1832 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1833 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1834 | return 0; |
| 1835 | } |
| 1836 | else { |
| 1837 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1838 | return 0; |
| 1839 | } |
| 1840 | } |
| 1841 | return ret; |
| 1842 | } |
| 1843 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1844 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1845 | * 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] | 1846 | * h2c or h2s. The caller must have already verified frame length and stream ID |
| 1847 | * validity. Described in RFC7540#6.9. |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1848 | */ |
| 1849 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1850 | { |
| 1851 | int32_t inc; |
| 1852 | int error; |
| 1853 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1854 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1855 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1856 | return 0; |
| 1857 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1858 | inc = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1859 | |
| 1860 | if (h2c->dsi != 0) { |
| 1861 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1862 | |
| 1863 | /* it's not an error to receive WU on a closed stream */ |
| 1864 | if (h2s->st == H2_SS_CLOSED) |
| 1865 | return 1; |
| 1866 | |
| 1867 | if (!inc) { |
| 1868 | error = H2_ERR_PROTOCOL_ERROR; |
| 1869 | goto strm_err; |
| 1870 | } |
| 1871 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1872 | if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) { |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1873 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1874 | goto strm_err; |
| 1875 | } |
| 1876 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 1877 | h2s->sws += inc; |
| 1878 | if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1879 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 1880 | if (h2s->send_wait && !LIST_ADDED(&h2s->list)) |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 1881 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1882 | } |
| 1883 | } |
| 1884 | else { |
| 1885 | /* connection window update */ |
| 1886 | if (!inc) { |
| 1887 | error = H2_ERR_PROTOCOL_ERROR; |
| 1888 | goto conn_err; |
| 1889 | } |
| 1890 | |
| 1891 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1892 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1893 | goto conn_err; |
| 1894 | } |
| 1895 | |
| 1896 | h2c->mws += inc; |
| 1897 | } |
| 1898 | |
| 1899 | return 1; |
| 1900 | |
| 1901 | conn_err: |
| 1902 | h2c_error(h2c, error); |
| 1903 | return 0; |
| 1904 | |
| 1905 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 1906 | h2s_error(h2s, error); |
| 1907 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1908 | return 0; |
| 1909 | } |
| 1910 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1911 | /* 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] | 1912 | * the last ID. Returns > 0 on success or zero on missing data. The caller must |
| 1913 | * have already verified frame length and stream ID validity. Described in |
| 1914 | * RFC7540#6.8. |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1915 | */ |
| 1916 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1917 | { |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1918 | int last; |
| 1919 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1920 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1921 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1922 | return 0; |
| 1923 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1924 | last = h2_get_n32(&h2c->dbuf, 0); |
| 1925 | h2c->errcode = h2_get_n32(&h2c->dbuf, 4); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1926 | if (h2c->last_sid < 0) |
| 1927 | h2c->last_sid = last; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1928 | h2_wake_some_streams(h2c, last); |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1929 | return 1; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1930 | } |
| 1931 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1932 | /* 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] | 1933 | * invalid. Returns > 0 on success or zero on missing data. It may return an |
| 1934 | * error in h2c. The caller must have already verified frame length and stream |
| 1935 | * ID validity. Described in RFC7540#6.3. |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1936 | */ |
| 1937 | static int h2c_handle_priority(struct h2c *h2c) |
| 1938 | { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1939 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1940 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1941 | return 0; |
| 1942 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1943 | if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1944 | /* 7540#5.3 : can't depend on itself */ |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1945 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1946 | return 0; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1947 | } |
| 1948 | return 1; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1949 | } |
| 1950 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1951 | /* 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] | 1952 | * Returns > 0 on success or zero on missing data. The caller must have already |
| 1953 | * verified frame length and stream ID validity. Described in RFC7540#6.4. |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1954 | */ |
| 1955 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1956 | { |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1957 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1958 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1959 | return 0; |
| 1960 | |
| 1961 | /* late RST, already handled */ |
| 1962 | if (h2s->st == H2_SS_CLOSED) |
| 1963 | return 1; |
| 1964 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1965 | h2s->errcode = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1966 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1967 | |
| 1968 | if (h2s->cs) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 1969 | cs_set_error(h2s->cs); |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 1970 | h2s_alert(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | h2s->flags |= H2_SF_RST_RCVD; |
| 1974 | return 1; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1975 | } |
| 1976 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1977 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 1978 | * It may return an error in h2c or h2s. The caller must consider that the |
| 1979 | * return value is the new h2s in case one was allocated (most common case). |
| 1980 | * Described in RFC7540#6.2. Most of the |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1981 | * errors here are reported as connection errors since it's impossible to |
| 1982 | * recover from such errors after the compression context has been altered. |
| 1983 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1984 | 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] | 1985 | { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1986 | struct buffer rxbuf = BUF_NULL; |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1987 | unsigned long long body_len = 0; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1988 | uint32_t flags = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1989 | int error; |
| 1990 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1991 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1992 | return NULL; // empty buffer |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1993 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1994 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1995 | return NULL; // incomplete frame |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1996 | |
| 1997 | /* now either the frame is complete or the buffer is complete */ |
| 1998 | if (h2s->st != H2_SS_IDLE) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1999 | /* The stream exists/existed, this must be a trailers frame */ |
| 2000 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 2001 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len); |
| 2002 | /* unrecoverable error ? */ |
| 2003 | if (h2c->st0 >= H2_CS_ERROR) |
| 2004 | goto out; |
| 2005 | |
| 2006 | if (error == 0) |
| 2007 | goto out; // missing data |
| 2008 | |
| 2009 | if (error < 0) { |
| 2010 | /* Failed to decode this frame (e.g. too large request) |
| 2011 | * but the HPACK decompressor is still synchronized. |
| 2012 | */ |
| 2013 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 2014 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2015 | goto out; |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 2016 | } |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2017 | goto done; |
| 2018 | } |
Willy Tarreau | 1f03550 | 2019-01-30 11:44:07 +0100 | [diff] [blame] | 2019 | /* the connection was already killed by an RST, let's consume |
| 2020 | * the data and send another RST. |
| 2021 | */ |
| 2022 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
| 2023 | h2s = (struct h2s*)h2_error_stream; |
| 2024 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2025 | } |
| 2026 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 2027 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 2028 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2029 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2030 | goto conn_err; |
| 2031 | } |
Willy Tarreau | 415b1ee | 2019-01-02 13:59:43 +0100 | [diff] [blame] | 2032 | else if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 2033 | goto out; // IDLE but too many cs still present |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2034 | |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 2035 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2036 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2037 | /* unrecoverable error ? */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2038 | if (h2c->st0 >= H2_CS_ERROR) |
| 2039 | goto out; |
| 2040 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2041 | if (error <= 0) { |
| 2042 | if (error == 0) |
| 2043 | goto out; // missing data |
| 2044 | |
| 2045 | /* Failed to decode this stream (e.g. too large request) |
| 2046 | * but the HPACK decompressor is still synchronized. |
| 2047 | */ |
| 2048 | h2s = (struct h2s*)h2_error_stream; |
| 2049 | goto send_rst; |
| 2050 | } |
| 2051 | |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2052 | /* 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] | 2053 | * positively from h2c_frt_stream_new(), the stream will report the error, |
| 2054 | * 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] | 2055 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 2056 | h2s = h2c_frt_stream_new(h2c, h2c->dsi); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2057 | if (!h2s) { |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2058 | h2s = (struct h2s*)h2_refused_stream; |
| 2059 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | h2s->st = H2_SS_OPEN; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2063 | h2s->rxbuf = rxbuf; |
| 2064 | h2s->flags |= flags; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2065 | h2s->body_len = body_len; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2066 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2067 | done: |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2068 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2069 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2070 | |
| 2071 | if (h2s->flags & H2_SF_ES_RCVD) { |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2072 | if (h2s->st == H2_SS_OPEN) |
| 2073 | h2s->st = H2_SS_HREM; |
| 2074 | else |
| 2075 | h2s_close(h2s); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2076 | } |
| 2077 | |
Willy Tarreau | 3a429f0 | 2019-01-03 11:41:50 +0100 | [diff] [blame] | 2078 | /* update the max stream ID if the request is being processed */ |
| 2079 | if (h2s->id > h2c->max_id) |
| 2080 | h2c->max_id = h2s->id; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2081 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2082 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2083 | |
| 2084 | conn_err: |
| 2085 | h2c_error(h2c, error); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2086 | goto out; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2087 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2088 | out: |
| 2089 | h2_release_buf(h2c, &rxbuf); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2090 | return NULL; |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2091 | |
| 2092 | send_rst: |
| 2093 | /* make the demux send an RST for the current stream. We may only |
| 2094 | * do this if we're certain that the HEADERS frame was properly |
| 2095 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2096 | */ |
| 2097 | h2_release_buf(h2c, &rxbuf); |
| 2098 | h2c->st0 = H2_CS_FRAME_E; |
| 2099 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2100 | } |
| 2101 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2102 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 2103 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 2104 | * errors here are reported as connection errors since it's impossible to |
| 2105 | * recover from such errors after the compression context has been altered. |
| 2106 | */ |
| 2107 | static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 2108 | { |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame^] | 2109 | struct buffer rxbuf = BUF_NULL; |
| 2110 | unsigned long long body_len = 0; |
| 2111 | uint32_t flags = 0; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2112 | int error; |
| 2113 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2114 | if (!b_size(&h2c->dbuf)) |
| 2115 | return NULL; // empty buffer |
| 2116 | |
| 2117 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
| 2118 | return NULL; // incomplete frame |
| 2119 | |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame^] | 2120 | if (h2s->st != H2_SS_CLOSED) { |
| 2121 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len); |
| 2122 | } |
| 2123 | else { |
| 2124 | /* the connection was already killed by an RST, let's consume |
| 2125 | * the data and send another RST. |
| 2126 | */ |
| 2127 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
| 2128 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2129 | h2c->st0 = H2_CS_FRAME_E; |
| 2130 | goto send_rst; |
| 2131 | } |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2132 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2133 | /* unrecoverable error ? */ |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2134 | if (h2c->st0 >= H2_CS_ERROR) |
| 2135 | return NULL; |
| 2136 | |
Willy Tarreau | 08bb1d6 | 2019-01-30 16:55:48 +0100 | [diff] [blame] | 2137 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2138 | /* RFC7540#5.1 */ |
| 2139 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2140 | h2c->st0 = H2_CS_FRAME_E; |
| 2141 | return NULL; |
| 2142 | } |
| 2143 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2144 | if (error <= 0) { |
| 2145 | if (error == 0) |
| 2146 | return NULL; // missing data |
| 2147 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2148 | /* stream error : send RST_STREAM */ |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2149 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2150 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2151 | return NULL; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2152 | } |
| 2153 | |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2154 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2155 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2156 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2157 | 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] | 2158 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2159 | else if (h2s->flags & H2_SF_ES_RCVD) { |
| 2160 | if (h2s->st == H2_SS_OPEN) |
| 2161 | h2s->st = H2_SS_HREM; |
| 2162 | else if (h2s->st == H2_SS_HLOC) |
| 2163 | h2s_close(h2s); |
| 2164 | } |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2165 | |
| 2166 | return h2s; |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame^] | 2167 | |
| 2168 | send_rst: |
| 2169 | /* make the demux send an RST for the current stream. We may only |
| 2170 | * do this if we're certain that the HEADERS frame was properly |
| 2171 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2172 | */ |
| 2173 | h2_release_buf(h2c, &rxbuf); |
| 2174 | h2c->st0 = H2_CS_FRAME_E; |
| 2175 | return h2s; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2176 | } |
| 2177 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2178 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 2179 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 2180 | */ |
| 2181 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 2182 | { |
| 2183 | int error; |
| 2184 | |
| 2185 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 2186 | * to signal an end of stream (with the ES flag). |
| 2187 | */ |
| 2188 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2189 | if (!b_size(&h2c->dbuf) && h2c->dfl) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2190 | return 0; // empty buffer |
| 2191 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2192 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2193 | return 0; // incomplete frame |
| 2194 | |
| 2195 | /* now either the frame is complete or the buffer is complete */ |
| 2196 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2197 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2198 | /* RFC7540#6.1 */ |
| 2199 | error = H2_ERR_STREAM_CLOSED; |
| 2200 | goto strm_err; |
| 2201 | } |
| 2202 | |
Christopher Faulet | 4fb65f4 | 2019-06-19 09:25:58 +0200 | [diff] [blame] | 2203 | if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) { |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2204 | /* RFC7540#8.1.2 */ |
| 2205 | error = H2_ERR_PROTOCOL_ERROR; |
| 2206 | goto strm_err; |
| 2207 | } |
| 2208 | |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 2209 | if (!h2_frt_transfer_data(h2s)) |
| 2210 | return 0; |
| 2211 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2212 | /* call the upper layers to process the frame, then let the upper layer |
| 2213 | * notify the stream about any change. |
| 2214 | */ |
| 2215 | if (!h2s->cs) { |
Willy Tarreau | c1a2965 | 2019-08-06 10:11:02 +0200 | [diff] [blame] | 2216 | /* The upper layer has already closed, this may happen on |
| 2217 | * 4xx/redirects during POST, or when receiving a response |
| 2218 | * from an H2 server after the client has aborted. |
| 2219 | */ |
| 2220 | error = H2_ERR_CANCEL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2221 | goto strm_err; |
| 2222 | } |
| 2223 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 2224 | if (h2c->st0 >= H2_CS_ERROR) |
| 2225 | return 0; |
| 2226 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2227 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2228 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2229 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | /* check for completion : the callee will change this to FRAME_A or |
| 2233 | * FRAME_H once done. |
| 2234 | */ |
| 2235 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2236 | return 0; |
| 2237 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2238 | /* last frame */ |
| 2239 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2240 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2241 | if (h2s->st == H2_SS_OPEN) |
| 2242 | h2s->st = H2_SS_HREM; |
| 2243 | else |
| 2244 | h2s_close(h2s); |
| 2245 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2246 | if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) { |
| 2247 | /* RFC7540#8.1.2 */ |
| 2248 | error = H2_ERR_PROTOCOL_ERROR; |
| 2249 | goto strm_err; |
| 2250 | } |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2251 | } |
| 2252 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2253 | return 1; |
| 2254 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2255 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 2256 | h2s_error(h2s, error); |
| 2257 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2258 | return 0; |
| 2259 | } |
| 2260 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2261 | /* process Rx frames to be demultiplexed */ |
| 2262 | static void h2_process_demux(struct h2c *h2c) |
| 2263 | { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2264 | struct h2s *h2s = NULL, *tmp_h2s; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2265 | struct h2_fh hdr; |
| 2266 | unsigned int padlen = 0; |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 2267 | int32_t old_iw = h2c->miw; |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 2268 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2269 | if (h2c->st0 >= H2_CS_ERROR) |
| 2270 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2271 | |
| 2272 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2273 | if (h2c->st0 == H2_CS_PREFACE) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2274 | if (h2c->flags & H2_CF_IS_BACK) |
| 2275 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2276 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 2277 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2278 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2279 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2280 | sess_log(h2c->conn->owner); |
| 2281 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2282 | goto fail; |
| 2283 | } |
| 2284 | |
| 2285 | h2c->max_id = 0; |
| 2286 | h2c->st0 = H2_CS_SETTINGS1; |
| 2287 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2288 | |
| 2289 | if (h2c->st0 == H2_CS_SETTINGS1) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2290 | /* ensure that what is pending is a valid SETTINGS frame |
| 2291 | * without an ACK. |
| 2292 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2293 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2294 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2295 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2296 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2297 | sess_log(h2c->conn->owner); |
| 2298 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2299 | goto fail; |
| 2300 | } |
| 2301 | |
| 2302 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 2303 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2304 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2305 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2306 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2307 | goto fail; |
| 2308 | } |
| 2309 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2310 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2311 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2312 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2313 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2314 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2315 | goto fail; |
| 2316 | } |
| 2317 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2318 | /* that's OK, switch to FRAME_P to process it. This is |
| 2319 | * a SETTINGS frame whose header has already been |
| 2320 | * deleted above. |
| 2321 | */ |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2322 | padlen = 0; |
| 2323 | goto new_frame; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2324 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2325 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2326 | |
| 2327 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2328 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2329 | int ret = 0; |
| 2330 | |
| 2331 | if (h2c->st0 >= H2_CS_ERROR) |
| 2332 | break; |
| 2333 | |
| 2334 | if (h2c->st0 == H2_CS_FRAME_H) { |
Willy Tarreau | ab3ebbc | 2019-08-06 15:49:51 +0200 | [diff] [blame] | 2335 | h2c->rcvd_s = 0; |
| 2336 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 2337 | if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2338 | break; |
| 2339 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2340 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2341 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2342 | if (!h2c->nb_streams) { |
| 2343 | /* only log if no other stream can report the error */ |
| 2344 | sess_log(h2c->conn->owner); |
| 2345 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2346 | break; |
| 2347 | } |
| 2348 | |
Christopher Faulet | 3d574a5 | 2019-06-18 12:22:38 +0200 | [diff] [blame] | 2349 | padlen = 0; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2350 | if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) { |
| 2351 | /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA), |
| 2352 | * we read the pad length and drop it from the remaining |
| 2353 | * payload (one byte + the 9 remaining ones = 10 total |
| 2354 | * removed), so we have a frame payload starting after the |
| 2355 | * pad len. Flow controlled frames (DATA) also count the |
| 2356 | * padlen in the flow control, so it must be adjusted. |
| 2357 | */ |
| 2358 | if (hdr.len < 1) { |
| 2359 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2360 | sess_log(h2c->conn->owner); |
| 2361 | goto fail; |
| 2362 | } |
| 2363 | hdr.len--; |
| 2364 | |
| 2365 | if (b_data(&h2c->dbuf) < 10) |
| 2366 | break; // missing padlen |
| 2367 | |
| 2368 | padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9); |
| 2369 | |
| 2370 | if (padlen > hdr.len) { |
| 2371 | /* RFC7540#6.1 : pad length = length of |
| 2372 | * frame payload or greater => error. |
| 2373 | */ |
| 2374 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2375 | sess_log(h2c->conn->owner); |
| 2376 | goto fail; |
| 2377 | } |
| 2378 | |
| 2379 | if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) { |
| 2380 | h2c->rcvd_c++; |
| 2381 | h2c->rcvd_s++; |
| 2382 | } |
| 2383 | b_del(&h2c->dbuf, 1); |
| 2384 | } |
| 2385 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2386 | |
| 2387 | new_frame: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2388 | h2c->dfl = hdr.len; |
| 2389 | h2c->dsi = hdr.sid; |
| 2390 | h2c->dft = hdr.ft; |
| 2391 | h2c->dff = hdr.ff; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2392 | h2c->dpl = padlen; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2393 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2394 | |
| 2395 | /* check for minimum basic frame format validity */ |
| 2396 | ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize); |
| 2397 | if (ret != H2_ERR_NO_ERROR) { |
| 2398 | h2c_error(h2c, ret); |
| 2399 | sess_log(h2c->conn->owner); |
| 2400 | goto fail; |
| 2401 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2402 | } |
| 2403 | |
Willy Tarreau | bde2f0a | 2019-08-06 15:21:45 +0200 | [diff] [blame] | 2404 | /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here. |
| 2405 | * H2_CS_FRAME_P indicates an incomplete previous operation |
| 2406 | * (most often the first attempt) and requires some validity |
| 2407 | * checks for the frame and the current state. The two other |
| 2408 | * ones are set after completion (or abortion) and must skip |
| 2409 | * validity checks. |
| 2410 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2411 | tmp_h2s = h2c_st_by_id(h2c, h2c->dsi); |
| 2412 | |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2413 | if (tmp_h2s != h2s && h2s && h2s->cs && |
| 2414 | (b_data(&h2s->rxbuf) || |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 2415 | conn_xprt_read0_pending(h2c->conn) || |
| 2416 | h2s->st == H2_SS_CLOSED || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2417 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2418 | (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2419 | /* we may have to signal the upper layers */ |
| 2420 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2421 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2422 | } |
| 2423 | h2s = tmp_h2s; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2424 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 2425 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2426 | goto strm_err; |
| 2427 | |
Willy Tarreau | bde2f0a | 2019-08-06 15:21:45 +0200 | [diff] [blame] | 2428 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2429 | goto valid_frame_type; |
| 2430 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2431 | if (h2s->st == H2_SS_IDLE && |
| 2432 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 2433 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 2434 | * this state MUST be treated as a connection error |
| 2435 | */ |
| 2436 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2437 | if (!h2c->nb_streams) { |
| 2438 | /* only log if no other stream can report the error */ |
| 2439 | sess_log(h2c->conn->owner); |
| 2440 | } |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2441 | break; |
| 2442 | } |
| 2443 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2444 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 2445 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 2446 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2447 | * this state MUST be treated as a stream error. |
| 2448 | * 6.2, 6.6 and 6.10 further mandate that HEADERS/ |
| 2449 | * PUSH_PROMISE/CONTINUATION cause connection errors. |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2450 | */ |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2451 | if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) |
| 2452 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2453 | else |
| 2454 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2455 | goto strm_err; |
| 2456 | } |
| 2457 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2458 | /* Below the management of frames received in closed state is a |
| 2459 | * bit hackish because the spec makes strong differences between |
| 2460 | * streams closed by receiving RST, sending RST, and seeing ES |
| 2461 | * in both directions. In addition to this, the creation of a |
| 2462 | * new stream reusing the identifier of a closed one will be |
| 2463 | * detected here. Given that we cannot keep track of all closed |
| 2464 | * streams forever, we consider that unknown closed streams were |
| 2465 | * closed on RST received, which allows us to respond with an |
| 2466 | * RST without breaking the connection (eg: to abort a transfer). |
| 2467 | * Some frames have to be silently ignored as well. |
| 2468 | */ |
| 2469 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
Willy Tarreau | 3ad5d31 | 2019-01-29 18:33:26 +0100 | [diff] [blame] | 2470 | 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] | 2471 | /* #5.1.1: The identifier of a newly |
| 2472 | * established stream MUST be numerically |
| 2473 | * greater than all streams that the initiating |
| 2474 | * endpoint has opened or reserved. This |
| 2475 | * governs streams that are opened using a |
| 2476 | * HEADERS frame and streams that are reserved |
| 2477 | * using PUSH_PROMISE. An endpoint that |
| 2478 | * receives an unexpected stream identifier |
| 2479 | * MUST respond with a connection error. |
| 2480 | */ |
| 2481 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2482 | goto strm_err; |
| 2483 | } |
| 2484 | |
Christopher Faulet | 96b88f2 | 2019-09-23 15:28:20 +0200 | [diff] [blame^] | 2485 | 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] | 2486 | /* RFC7540#5.1:closed: an endpoint that |
| 2487 | * receives any frame other than PRIORITY after |
| 2488 | * receiving a RST_STREAM MUST treat that as a |
| 2489 | * stream error of type STREAM_CLOSED. |
| 2490 | * |
| 2491 | * Note that old streams fall into this category |
| 2492 | * and will lead to an RST being sent. |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2493 | * |
| 2494 | * However, we cannot generalize this to all frame types. Those |
| 2495 | * carrying compression state must still be processed before |
| 2496 | * being dropped or we'll desynchronize the decoder. This can |
| 2497 | * happen with request trailers received after sending an |
| 2498 | * RST_STREAM, or with header/trailers responses received after |
| 2499 | * sending RST_STREAM (aborted stream). |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2500 | */ |
| 2501 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2502 | h2c->st0 = H2_CS_FRAME_E; |
| 2503 | goto strm_err; |
| 2504 | } |
| 2505 | |
| 2506 | /* RFC7540#5.1:closed: if this state is reached as a |
| 2507 | * result of sending a RST_STREAM frame, the peer that |
| 2508 | * receives the RST_STREAM might have already sent |
| 2509 | * frames on the stream that cannot be withdrawn. An |
| 2510 | * endpoint MUST ignore frames that it receives on |
| 2511 | * closed streams after it has sent a RST_STREAM |
| 2512 | * frame. An endpoint MAY choose to limit the period |
| 2513 | * over which it ignores frames and treat frames that |
| 2514 | * arrive after this time as being in error. |
| 2515 | */ |
Willy Tarreau | 24ff1f8 | 2019-01-30 19:20:09 +0100 | [diff] [blame] | 2516 | if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2517 | /* RFC7540#5.1:closed: any frame other than |
| 2518 | * PRIO/WU/RST in this state MUST be treated as |
| 2519 | * a connection error |
| 2520 | */ |
| 2521 | if (h2c->dft != H2_FT_RST_STREAM && |
| 2522 | h2c->dft != H2_FT_PRIORITY && |
| 2523 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 2524 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2525 | goto strm_err; |
| 2526 | } |
| 2527 | } |
| 2528 | } |
| 2529 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2530 | #if 0 |
| 2531 | // problem below: it is not possible to completely ignore such |
| 2532 | // streams as we need to maintain the compression state as well |
| 2533 | // and for this we need to completely process these frames (eg: |
| 2534 | // HEADERS frames) as well as counting DATA frames to emit |
| 2535 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 2536 | // This is a typical case of layer violation where the |
| 2537 | // transported contents are critical to the connection's |
| 2538 | // validity and must be ignored at the same time :-( |
| 2539 | |
| 2540 | /* graceful shutdown, ignore streams whose ID is higher than |
| 2541 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 2542 | */ |
| 2543 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2544 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2545 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2546 | h2c->dfl -= ret; |
| 2547 | ret = h2c->dfl == 0; |
| 2548 | goto strm_err; |
| 2549 | } |
| 2550 | #endif |
| 2551 | |
Willy Tarreau | bde2f0a | 2019-08-06 15:21:45 +0200 | [diff] [blame] | 2552 | valid_frame_type: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2553 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 2554 | case H2_FT_SETTINGS: |
| 2555 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2556 | ret = h2c_handle_settings(h2c); |
| 2557 | |
| 2558 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2559 | ret = h2c_ack_settings(h2c); |
| 2560 | break; |
| 2561 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 2562 | case H2_FT_PING: |
| 2563 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2564 | ret = h2c_handle_ping(h2c); |
| 2565 | |
| 2566 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2567 | ret = h2c_ack_ping(h2c); |
| 2568 | break; |
| 2569 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 2570 | case H2_FT_WINDOW_UPDATE: |
| 2571 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2572 | ret = h2c_handle_window_update(h2c, h2s); |
| 2573 | break; |
| 2574 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2575 | case H2_FT_CONTINUATION: |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2576 | /* RFC7540#6.10: CONTINUATION may only be preceeded by |
| 2577 | * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These |
| 2578 | * frames' parsers consume all following CONTINUATION |
| 2579 | * frames so this one is out of sequence. |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2580 | */ |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2581 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2582 | sess_log(h2c->conn->owner); |
| 2583 | goto fail; |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2584 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2585 | case H2_FT_HEADERS: |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2586 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2587 | if (h2c->flags & H2_CF_IS_BACK) |
| 2588 | tmp_h2s = h2c_bck_handle_headers(h2c, h2s); |
| 2589 | else |
| 2590 | tmp_h2s = h2c_frt_handle_headers(h2c, h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2591 | if (tmp_h2s) { |
| 2592 | h2s = tmp_h2s; |
| 2593 | ret = 1; |
| 2594 | } |
| 2595 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2596 | break; |
| 2597 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2598 | case H2_FT_DATA: |
| 2599 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2600 | ret = h2c_frt_handle_data(h2c, h2s); |
| 2601 | |
| 2602 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2603 | ret = h2c_send_strm_wu(h2c); |
| 2604 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2605 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 2606 | case H2_FT_PRIORITY: |
| 2607 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2608 | ret = h2c_handle_priority(h2c); |
| 2609 | break; |
| 2610 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2611 | case H2_FT_RST_STREAM: |
| 2612 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2613 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 2614 | break; |
| 2615 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 2616 | case H2_FT_GOAWAY: |
| 2617 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2618 | ret = h2c_handle_goaway(h2c); |
| 2619 | break; |
| 2620 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2621 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2622 | default: |
| 2623 | /* drop frames that we ignore. They may be larger than |
| 2624 | * the buffer so we drain all of their contents until |
| 2625 | * we reach the end. |
| 2626 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2627 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2628 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2629 | h2c->dfl -= ret; |
| 2630 | ret = h2c->dfl == 0; |
| 2631 | } |
| 2632 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2633 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2634 | /* We may have to send an RST if not done yet */ |
| 2635 | if (h2s->st == H2_SS_ERROR) |
| 2636 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2637 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2638 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2639 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2640 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2641 | /* error or missing data condition met above ? */ |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2642 | if (ret <= 0) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2643 | break; |
| 2644 | |
| 2645 | if (h2c->st0 != H2_CS_FRAME_H) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2646 | b_del(&h2c->dbuf, h2c->dfl); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2647 | h2c->st0 = H2_CS_FRAME_H; |
| 2648 | } |
| 2649 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2650 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2651 | if (h2c->rcvd_c > 0 && |
| 2652 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2653 | h2c_send_conn_wu(h2c); |
| 2654 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2655 | fail: |
| 2656 | /* we can go here on missing data, blocked response or error */ |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2657 | if (h2s && h2s->cs && |
| 2658 | (b_data(&h2s->rxbuf) || |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 2659 | conn_xprt_read0_pending(h2c->conn) || |
| 2660 | h2s->st == H2_SS_CLOSED || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2661 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2662 | (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2663 | /* we may have to signal the upper layers */ |
| 2664 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2665 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2666 | } |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2667 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 2668 | if (old_iw != h2c->miw) |
| 2669 | h2c_unblock_sfctl(h2c); |
| 2670 | |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 2671 | h2c_restart_reading(h2c, 0); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2675 | * the end. |
| 2676 | */ |
| 2677 | static int h2_process_mux(struct h2c *h2c) |
| 2678 | { |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2679 | struct h2s *h2s, *h2s_back; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2680 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2681 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2682 | if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) { |
| 2683 | if (unlikely(h2c_bck_send_preface(h2c) <= 0)) { |
| 2684 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2685 | if (h2c->st0 == H2_CS_ERROR) { |
| 2686 | h2c->st0 = H2_CS_ERROR2; |
| 2687 | sess_log(h2c->conn->owner); |
| 2688 | } |
| 2689 | goto fail; |
| 2690 | } |
| 2691 | h2c->st0 = H2_CS_SETTINGS1; |
| 2692 | } |
| 2693 | /* need to wait for the other side */ |
Willy Tarreau | 75a930a | 2018-12-12 08:03:58 +0100 | [diff] [blame] | 2694 | if (h2c->st0 < H2_CS_FRAME_H) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2695 | return 1; |
| 2696 | } |
| 2697 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2698 | /* start by sending possibly pending window updates */ |
Willy Tarreau | 9c497c2 | 2019-08-06 15:39:32 +0200 | [diff] [blame] | 2699 | if (h2c->rcvd_s > 0 && |
| 2700 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2701 | h2c_send_strm_wu(h2c) < 0) |
| 2702 | goto fail; |
| 2703 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2704 | if (h2c->rcvd_c > 0 && |
| 2705 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2706 | h2c_send_conn_wu(h2c) < 0) |
| 2707 | goto fail; |
| 2708 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2709 | /* First we always process the flow control list because the streams |
| 2710 | * waiting there were already elected for immediate emission but were |
| 2711 | * blocked just on this. |
| 2712 | */ |
| 2713 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2714 | list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2715 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2716 | h2c->st0 >= H2_CS_ERROR) |
| 2717 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2718 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2719 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2720 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2721 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2722 | h2s->flags &= ~H2_SF_BLK_ANY; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2723 | /* For some reason, the upper layer failed to subsribe again, |
| 2724 | * so remove it from the send_list |
| 2725 | */ |
| 2726 | if (!h2s->send_wait) { |
| 2727 | LIST_DEL_INIT(&h2s->list); |
| 2728 | continue; |
| 2729 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2730 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2731 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2732 | tasklet_wakeup(h2s->send_wait->tasklet); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2733 | } |
| 2734 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2735 | list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2736 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2737 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2738 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2739 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2740 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2741 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2742 | /* For some reason, the upper layer failed to subsribe again, |
| 2743 | * so remove it from the send_list |
| 2744 | */ |
| 2745 | if (!h2s->send_wait) { |
| 2746 | LIST_DEL_INIT(&h2s->list); |
| 2747 | continue; |
| 2748 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2749 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2750 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2751 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2752 | tasklet_wakeup(h2s->send_wait->tasklet); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2753 | } |
| 2754 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2755 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2756 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2757 | if (h2c->st0 == H2_CS_ERROR) { |
| 2758 | if (h2c->max_id >= 0) { |
| 2759 | h2c_send_goaway_error(h2c, NULL); |
| 2760 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2761 | return 0; |
| 2762 | } |
| 2763 | |
| 2764 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2765 | } |
| 2766 | return 1; |
| 2767 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2768 | return (1); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2769 | } |
| 2770 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2771 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2772 | /* Attempt to read data, and subscribe if none available. |
| 2773 | * The function returns 1 if data has been received, otherwise zero. |
| 2774 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2775 | static int h2_recv(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2776 | { |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2777 | struct connection *conn = h2c->conn; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2778 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2779 | int max; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2780 | size_t ret; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2781 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2782 | if (h2c->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2783 | return (b_data(&h2c->dbuf)); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2784 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2785 | if (!h2_recv_allowed(h2c)) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2786 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2787 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2788 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2789 | if (!buf) { |
| 2790 | h2c->flags |= H2_CF_DEM_DALLOC; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2791 | return 0; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2792 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2793 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2794 | do { |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 2795 | b_realign_if_empty(buf); |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2796 | if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
| 2797 | /* HTX in use : try to pre-align the buffer like the |
| 2798 | * rxbufs will be to optimize memory copies. We'll make |
| 2799 | * sure that the frame header lands at the end of the |
| 2800 | * HTX block to alias it upon recv. We cannot use the |
| 2801 | * head because rcv_buf() will realign the buffer if |
| 2802 | * it's empty. Thus we cheat and pretend we already |
| 2803 | * have a few bytes there. |
| 2804 | */ |
| 2805 | max = buf_room_for_htx_data(buf) + 9; |
Willy Tarreau | c0960d1 | 2018-12-14 10:59:15 +0100 | [diff] [blame] | 2806 | buf->head = sizeof(struct htx) - 9; |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2807 | } |
| 2808 | else |
| 2809 | max = b_room(buf); |
| 2810 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2811 | if (max) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2812 | ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2813 | else |
| 2814 | ret = 0; |
| 2815 | } while (ret > 0); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2816 | |
Willy Tarreau | a8fcdac | 2019-08-02 07:48:47 +0200 | [diff] [blame] | 2817 | if (max && !ret && h2_recv_allowed(h2c)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2818 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event); |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2819 | |
Olivier Houchard | a1411e6 | 2018-08-17 18:42:48 +0200 | [diff] [blame] | 2820 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2821 | h2_release_buf(h2c, &h2c->dbuf); |
Olivier Houchard | 4667773 | 2018-11-29 17:06:17 +0100 | [diff] [blame] | 2822 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2823 | } |
| 2824 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2825 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2826 | h2c->flags |= H2_CF_DEM_DFULL; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2827 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2828 | } |
| 2829 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2830 | /* Try to send data if possible. |
| 2831 | * The function returns 1 if data have been sent, otherwise zero. |
| 2832 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2833 | static int h2_send(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2834 | { |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2835 | struct connection *conn = h2c->conn; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2836 | int done; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2837 | int sent = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2838 | |
| 2839 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 2840 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2841 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2842 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2843 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2844 | /* a handshake was requested */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2845 | goto schedule; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2846 | } |
| 2847 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2848 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2849 | * pending streams into the existing buffer until it's reportedly full |
| 2850 | * or the end of send requests is reached. Then it tries to send this |
| 2851 | * buffer's contents out, marks it not full if at least one byte could |
| 2852 | * be sent, and tries again. |
| 2853 | * |
| 2854 | * The snd_buf() function normally takes a "flags" argument which may |
| 2855 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2856 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2857 | * connection is streaming lots of data (used to increase TLS record |
| 2858 | * size at the expense of latency). The former can be sent any time |
| 2859 | * there's a buffer full flag, as it indicates at least one stream |
| 2860 | * attempted to send and failed so there are pending data. An |
| 2861 | * alternative would be to set it as long as there's an active stream |
| 2862 | * but that would be problematic for ACKs until we have an absolute |
| 2863 | * guarantee that all waiters have at least one byte to send. The |
| 2864 | * latter should possibly not be set for now. |
| 2865 | */ |
| 2866 | |
| 2867 | done = 0; |
| 2868 | while (!done) { |
| 2869 | unsigned int flags = 0; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2870 | unsigned int released = 0; |
| 2871 | struct buffer *buf; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2872 | |
| 2873 | /* fill as much as we can into the current buffer */ |
| 2874 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2875 | done = h2_process_mux(h2c); |
| 2876 | |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2877 | if (h2c->flags & H2_CF_MUX_MALLOC) |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2878 | done = 1; // we won't go further without extra buffers |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2879 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2880 | if (conn->flags & CO_FL_ERROR) |
| 2881 | break; |
| 2882 | |
| 2883 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2884 | flags |= CO_SFL_MSG_MORE; |
| 2885 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2886 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 2887 | if (b_data(buf)) { |
| 2888 | int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags); |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2889 | if (!ret) { |
| 2890 | done = 1; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2891 | break; |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2892 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2893 | sent = 1; |
| 2894 | b_del(buf, ret); |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2895 | if (b_data(buf)) { |
| 2896 | done = 1; |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2897 | break; |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2898 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2899 | } |
| 2900 | b_free(buf); |
| 2901 | released++; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2902 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2903 | |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2904 | if (released) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 2905 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 2906 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2907 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2908 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2909 | } |
| 2910 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2911 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2912 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | 5133096 | 2019-05-26 09:38:07 +0200 | [diff] [blame] | 2913 | b_reset(br_tail(h2c->mbuf)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2914 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2915 | /* We're not full anymore, so we can wake any task that are waiting |
| 2916 | * for us. |
| 2917 | */ |
| 2918 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2919 | struct h2s *h2s; |
| 2920 | |
| 2921 | list_for_each_entry(h2s, &h2c->send_list, list) { |
| 2922 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2923 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2924 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2925 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2926 | continue; |
| 2927 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2928 | /* For some reason, the upper layer failed to subsribe again, |
| 2929 | * so remove it from the send_list |
| 2930 | */ |
| 2931 | if (!h2s->send_wait) { |
| 2932 | LIST_DEL_INIT(&h2s->list); |
| 2933 | continue; |
| 2934 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2935 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2936 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2937 | tasklet_wakeup(h2s->send_wait->tasklet); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2938 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 2939 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2940 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2941 | /* We're done, no more to send */ |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 2942 | if (!br_data(h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2943 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2944 | schedule: |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2945 | if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2946 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event); |
Willy Tarreau | 7f1265a | 2019-05-29 17:36:37 +0200 | [diff] [blame] | 2947 | |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2948 | return sent; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2949 | } |
| 2950 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2951 | /* this is the tasklet referenced in h2c->wait_event.tasklet */ |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2952 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2953 | { |
| 2954 | struct h2c *h2c = ctx; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2955 | int ret = 0; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2956 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2957 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2958 | ret = h2_send(h2c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2959 | if (!(h2c->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2960 | ret |= h2_recv(h2c); |
Willy Tarreau | cef5c8e | 2018-12-18 10:29:54 +0100 | [diff] [blame] | 2961 | if (ret || b_data(&h2c->dbuf)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2962 | h2_process(h2c); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2963 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2964 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2965 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2966 | /* callback called on any event by the connection handler. |
| 2967 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2968 | * destruction of the connection (which normally doesn not happen in h2). |
| 2969 | */ |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2970 | static int h2_process(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2971 | { |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2972 | struct connection *conn = h2c->conn; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2973 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2974 | if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2975 | h2_process_demux(h2c); |
| 2976 | |
| 2977 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2978 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2979 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2980 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2981 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 2982 | } |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2983 | h2_send(h2c); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2984 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 2985 | if (unlikely(h2c->proxy->state == PR_STSTOPPED)) { |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2986 | /* frontend is stopping, reload likely in progress, let's try |
| 2987 | * to announce a graceful shutdown if not yet done. We don't |
| 2988 | * care if it fails, it will be tried again later. |
| 2989 | */ |
| 2990 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 2991 | if (h2c->last_sid < 0) |
| 2992 | h2c->last_sid = (1U << 31) - 1; |
| 2993 | h2c_send_goaway_error(h2c, NULL); |
| 2994 | } |
| 2995 | } |
| 2996 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2997 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2998 | * If we received early data, and the handshake is done, wake |
| 2999 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 3000 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3001 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 3002 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 3003 | struct eb32_node *node; |
| 3004 | struct h2s *h2s; |
| 3005 | |
| 3006 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 3007 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 3008 | |
| 3009 | while (node) { |
| 3010 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | fde287c | 2018-12-19 18:33:16 +0100 | [diff] [blame] | 3011 | if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 3012 | h2s_notify_recv(h2s); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3013 | node = eb32_next(node); |
| 3014 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 3015 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 3016 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 3017 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 3018 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 3019 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 3020 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 3021 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3022 | |
| 3023 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3024 | /* no more stream, kill the connection now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3025 | h2_release(h2c); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3026 | return -1; |
| 3027 | } |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3028 | } |
| 3029 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3030 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3031 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 3032 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 3033 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 3034 | h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 3035 | (h2c->st0 != H2_CS_ERROR && |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3036 | !br_data(h2c->mbuf) && |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 3037 | (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && |
| 3038 | ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list)))) |
Willy Tarreau | 2e3c000 | 2019-05-26 09:45:23 +0200 | [diff] [blame] | 3039 | h2_release_mbuf(h2c); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 3040 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 3041 | if (h2c->task) { |
Willy Tarreau | 7348119 | 2019-06-07 08:20:46 +0200 | [diff] [blame] | 3042 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3043 | task_queue(h2c->task); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3044 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 3045 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 3046 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3047 | return 0; |
| 3048 | } |
| 3049 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3050 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 3051 | static int h2_wake(struct connection *conn) |
| 3052 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3053 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 3054 | |
| 3055 | return (h2_process(h2c)); |
| 3056 | } |
| 3057 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3058 | /* Connection timeout management. The principle is that if there's no receipt |
| 3059 | * nor sending for a certain amount of time, the connection is closed. If the |
| 3060 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 3061 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 3062 | * GOAWAY frame. |
| 3063 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3064 | 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] | 3065 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 3066 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3067 | int expired = tick_is_expired(t->expire, now_ms); |
| 3068 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3069 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3070 | return t; |
| 3071 | |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 3072 | task_destroy(t); |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3073 | |
| 3074 | if (!h2c) { |
| 3075 | /* resources were already deleted */ |
| 3076 | return NULL; |
| 3077 | } |
| 3078 | |
| 3079 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3080 | h2c_error(h2c, H2_ERR_NO_ERROR); |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 3081 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3082 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3083 | if (br_data(h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3084 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 3085 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3086 | } |
| 3087 | |
| 3088 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 3089 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3090 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 3091 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 3092 | |
Willy Tarreau | 662fafc | 2019-05-26 09:43:07 +0200 | [diff] [blame] | 3093 | if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) { |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 3094 | unsigned int released = 0; |
| 3095 | struct buffer *buf; |
| 3096 | |
| 3097 | for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) { |
| 3098 | if (b_data(buf)) { |
| 3099 | int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0); |
| 3100 | if (!ret) |
| 3101 | break; |
| 3102 | b_del(buf, ret); |
| 3103 | if (b_data(buf)) |
| 3104 | break; |
| 3105 | b_free(buf); |
| 3106 | released++; |
| 3107 | } |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3108 | } |
Willy Tarreau | 41c4d6a | 2019-05-26 09:49:17 +0200 | [diff] [blame] | 3109 | |
| 3110 | if (released) |
Willy Tarreau | 201840a | 2019-05-29 17:50:48 +0200 | [diff] [blame] | 3111 | offer_buffers(NULL, tasks_run_queue); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 3112 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3113 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 3114 | /* either we can release everything now or it will be done later once |
| 3115 | * the last stream closes. |
| 3116 | */ |
| 3117 | if (eb_is_empty(&h2c->streams_by_id)) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3118 | h2_release(h2c); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3119 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 3120 | return NULL; |
| 3121 | } |
| 3122 | |
| 3123 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3124 | /*******************************************/ |
| 3125 | /* functions below are used by the streams */ |
| 3126 | /*******************************************/ |
| 3127 | |
| 3128 | /* |
| 3129 | * Attach a new stream to a connection |
| 3130 | * (Used for outgoing connections) |
| 3131 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3132 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3133 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3134 | struct conn_stream *cs; |
| 3135 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3136 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3137 | |
| 3138 | cs = cs_new(conn); |
| 3139 | if (!cs) |
| 3140 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3141 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3142 | if (!h2s) { |
| 3143 | cs_free(cs); |
| 3144 | return NULL; |
| 3145 | } |
| 3146 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3147 | } |
| 3148 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3149 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 3150 | * We have to scan because we may have some orphan streams. It might be |
| 3151 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 3152 | * orphans. |
| 3153 | */ |
| 3154 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 3155 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3156 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3157 | struct h2s *h2s; |
| 3158 | struct eb32_node *node; |
| 3159 | |
| 3160 | node = eb32_first(&h2c->streams_by_id); |
| 3161 | while (node) { |
| 3162 | h2s = container_of(node, struct h2s, by_id); |
| 3163 | if (h2s->cs) |
| 3164 | return h2s->cs; |
| 3165 | node = eb32_next(node); |
| 3166 | } |
| 3167 | return NULL; |
| 3168 | } |
| 3169 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3170 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3171 | * Destroy the mux and the associated connection, if it is no longer used |
| 3172 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3173 | static void h2_destroy(void *ctx) |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3174 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3175 | struct h2c *h2c = ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3176 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 3177 | if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3178 | h2_release(h2c); |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3179 | } |
| 3180 | |
| 3181 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3182 | * Detach the stream from the connection and possibly release the connection. |
| 3183 | */ |
| 3184 | static void h2_detach(struct conn_stream *cs) |
| 3185 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3186 | struct h2s *h2s = cs->ctx; |
| 3187 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3188 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3189 | |
| 3190 | cs->ctx = NULL; |
| 3191 | if (!h2s) |
| 3192 | return; |
| 3193 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3194 | /* The stream is about to die, so no need to attempt to run its task */ |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3195 | if (LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3196 | h2s->send_wait != &h2s->wait_event) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 3197 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3198 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d9986ed | 2019-05-09 13:24:08 +0200 | [diff] [blame] | 3199 | /* |
| 3200 | * At this point, the stream_interface is supposed to have called |
| 3201 | * h2_unsubscribe(), so the only way there's still a |
| 3202 | * subscription that came from the stream_interface (as we |
| 3203 | * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(), |
| 3204 | * without the stream_interface involved) is that we subscribed |
| 3205 | * for sending, we woke the tasklet up and removed the |
| 3206 | * SUB_RETRY_SEND flag, so the stream_interface would not |
| 3207 | * know it has to unsubscribe for send, but the tasklet hasn't |
| 3208 | * run yet. Make sure to handle that by explicitely setting |
| 3209 | * send_wait to NULL, as nothing else will do it for us. |
| 3210 | */ |
| 3211 | h2s->send_wait = NULL; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3212 | } |
| 3213 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3214 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3215 | h2c = h2s->h2c; |
| 3216 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 3217 | h2c->nb_cs--; |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 3218 | if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY && |
| 3219 | !h2_frt_has_too_many_cs(h2c)) { |
| 3220 | /* frontend connection was blocking new streams creation */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3221 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3222 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3223 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3224 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3225 | /* this stream may be blocked waiting for some data to leave (possibly |
| 3226 | * an ES or RST frame), so orphan it in this case. |
| 3227 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 3228 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 3229 | (h2c->st0 < H2_CS_ERROR) && |
Olivier Houchard | 16ff261 | 2019-03-21 15:48:46 +0100 | [diff] [blame] | 3230 | (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] | 3231 | return; |
| 3232 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3233 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 3234 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 3235 | /* unblock the connection if it was blocked on this |
| 3236 | * stream. |
| 3237 | */ |
| 3238 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 3239 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3240 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3241 | } |
| 3242 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 3243 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3244 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3245 | if (h2c->flags & H2_CF_IS_BACK && |
| 3246 | (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3247 | if (!(h2c->conn->flags & |
| 3248 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 3249 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3250 | h2c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3251 | if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) { |
| 3252 | h2c->conn->owner = NULL; |
| 3253 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3254 | if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn)) |
| 3255 | /* The server doesn't want it, let's kill the connection right away */ |
| 3256 | h2c->conn->mux->destroy(h2c->conn); |
| 3257 | return; |
| 3258 | } |
| 3259 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3260 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3261 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3262 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 3263 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 3264 | return; |
| 3265 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3266 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3267 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3268 | h2c->conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3269 | if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3270 | struct server *srv = objt_server(h2c->conn->target); |
| 3271 | |
| 3272 | if (srv) { |
| 3273 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 3274 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 3275 | else |
| 3276 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 3277 | } |
| 3278 | |
| 3279 | } |
| 3280 | } |
| 3281 | } |
| 3282 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3283 | /* We don't want to close right now unless we're removing the |
| 3284 | * last stream, and either the connection is in error, or it |
| 3285 | * reached the ID already specified in a GOAWAY frame received |
| 3286 | * or sent (as seen by last_sid >= 0). |
| 3287 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3288 | if (h2c_is_dead(h2c)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3289 | /* no more stream will come, kill it now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3290 | h2_release(h2c); |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3291 | } |
| 3292 | else if (h2c->task) { |
Willy Tarreau | 7348119 | 2019-06-07 08:20:46 +0200 | [diff] [blame] | 3293 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3294 | task_queue(h2c->task); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3295 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3296 | } |
| 3297 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3298 | /* Performs a synchronous or asynchronous shutr(). */ |
| 3299 | static void h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3300 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3301 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3302 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3303 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3304 | if (h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3305 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3306 | |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3307 | /* a connstream may require us to immediately kill the whole connection |
| 3308 | * for example because of a "tcp-request content reject" rule that is |
| 3309 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3310 | * close the connection. |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3311 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3312 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3313 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3314 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3315 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3316 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3317 | else if (!(h2s->flags & H2_SF_HEADERS_SENT)) { |
| 3318 | /* Nothing was never sent for this stream, so reset with |
| 3319 | * REFUSED_STREAM error to let the client retry the |
| 3320 | * request. |
| 3321 | */ |
| 3322 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3323 | } |
Willy Tarreau | ebdb6a0 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3324 | else { |
| 3325 | /* a final response was already provided, we don't want this |
| 3326 | * stream anymore. This may happen when the server responds |
| 3327 | * before the end of an upload and closes quickly (redirect, |
| 3328 | * deny, ...) |
| 3329 | */ |
| 3330 | h2s_error(h2s, H2_ERR_CANCEL); |
| 3331 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3332 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3333 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3334 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3335 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3336 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3337 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3338 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3339 | h2s_close(h2s); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3340 | done: |
| 3341 | h2s->flags &= ~H2_SF_WANT_SHUTR; |
| 3342 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3343 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3344 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3345 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3346 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3347 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3348 | h2s->send_wait = sw; |
| 3349 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3350 | h2s->send_wait = sw; |
| 3351 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3352 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3353 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3354 | /* Let the handler know we want shutr */ |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3355 | h2s->flags |= H2_SF_WANT_SHUTR; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3356 | return; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3357 | } |
| 3358 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3359 | /* Performs a synchronous or asynchronous shutw(). */ |
| 3360 | static void h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3361 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3362 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3363 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3364 | |
Willy Tarreau | ebdb6a0 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3365 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3366 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3367 | |
Willy Tarreau | ebdb6a0 | 2019-08-06 10:30:58 +0200 | [diff] [blame] | 3368 | if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3369 | /* we can cleanly close using an empty data frame only after headers */ |
| 3370 | |
| 3371 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 3372 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3373 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3374 | |
| 3375 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3376 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3377 | else |
| 3378 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3379 | } else { |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3380 | /* a connstream may require us to immediately kill the whole connection |
| 3381 | * for example because of a "tcp-request content reject" rule that is |
| 3382 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3383 | * close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3384 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3385 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3386 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3387 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3388 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3389 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3390 | else { |
| 3391 | /* Nothing was never sent for this stream, so reset with |
| 3392 | * REFUSED_STREAM error to let the client retry the |
| 3393 | * request. |
| 3394 | */ |
| 3395 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3396 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3397 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3398 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3399 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3400 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3401 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3402 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3403 | } |
| 3404 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3405 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3406 | tasklet_wakeup(h2c->wait_event.tasklet); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3407 | done: |
| 3408 | h2s->flags &= ~H2_SF_WANT_SHUTW; |
| 3409 | return; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3410 | |
| 3411 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3412 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3413 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3414 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3415 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3416 | h2s->send_wait = sw; |
| 3417 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3418 | h2s->send_wait = sw; |
| 3419 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3420 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3421 | } |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3422 | /* let the handler know we want to shutw */ |
| 3423 | h2s->flags |= H2_SF_WANT_SHUTW; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3424 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3425 | } |
| 3426 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 3427 | /* This is the tasklet referenced in h2s->wait_event.tasklet, it is used for |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3428 | * deferred shutdowns when the h2_detach() was done but the mux buffer was full |
| 3429 | * and prevented the last frame from being emitted. |
| 3430 | */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3431 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3432 | { |
| 3433 | struct h2s *h2s = ctx; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3434 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3435 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3436 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3437 | if (h2s->flags & H2_SF_WANT_SHUTW) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3438 | h2_do_shutw(h2s); |
| 3439 | |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3440 | if (h2s->flags & H2_SF_WANT_SHUTR) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3441 | h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3442 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3443 | if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) { |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3444 | /* We're done trying to send, remove ourself from the send_list */ |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3445 | LIST_DEL_INIT(&h2s->list); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3446 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3447 | if (!h2s->cs) { |
| 3448 | h2s_destroy(h2s); |
| 3449 | if (h2c_is_dead(h2c)) |
| 3450 | h2_release(h2c); |
| 3451 | } |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3452 | } |
| 3453 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3454 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3455 | } |
| 3456 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3457 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3458 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3459 | { |
| 3460 | struct h2s *h2s = cs->ctx; |
| 3461 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3462 | if (cs->flags & CS_FL_KILL_CONN) |
| 3463 | h2s->flags |= H2_SF_KILL_CONN; |
| 3464 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3465 | if (!mode) |
| 3466 | return; |
| 3467 | |
| 3468 | h2_do_shutr(h2s); |
| 3469 | } |
| 3470 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3471 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3472 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3473 | { |
| 3474 | struct h2s *h2s = cs->ctx; |
| 3475 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3476 | if (cs->flags & CS_FL_KILL_CONN) |
| 3477 | h2s->flags |= H2_SF_KILL_CONN; |
| 3478 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3479 | h2_do_shutw(h2s); |
| 3480 | } |
| 3481 | |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3482 | /* 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] | 3483 | * HTX request or response depending on the connection's side. Returns a |
| 3484 | * positive value on success, a negative value on failure, or 0 if it couldn't |
| 3485 | * proceed. May report connection errors in h2c->errcode if the frame is |
| 3486 | * non-decodable and the connection unrecoverable. In absence of connection |
| 3487 | * 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] | 3488 | * |
| 3489 | * The function may fold CONTINUATION frames into the initial HEADERS frame |
| 3490 | * by removing padding and next frame header, then moving the CONTINUATION |
| 3491 | * frame's payload and adjusting h2c->dfl to match the new aggregated frame, |
| 3492 | * leaving a hole between the main frame and the beginning of the next one. |
| 3493 | * The possibly remaining incomplete or next frame at the end may be moved |
| 3494 | * if the aggregated frame is not deleted, in order to fill the hole. Wrapped |
| 3495 | * HEADERS frames are unwrapped into a temporary buffer before decoding. |
| 3496 | * |
| 3497 | * A buffer at the beginning of processing may look like this : |
| 3498 | * |
| 3499 | * ,---.---------.-----.--------------.--------------.------.---. |
| 3500 | * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///| |
| 3501 | * `---^---------^-----^--------------^--------------^------^---' |
| 3502 | * | | <-----> | | |
| 3503 | * area | dpl | wrap |
| 3504 | * |<--------------> | |
| 3505 | * | dfl | |
| 3506 | * |<-------------------------------------------------->| |
| 3507 | * head data |
| 3508 | * |
| 3509 | * Padding is automatically overwritten when folding, participating to the |
| 3510 | * hole size after dfl : |
| 3511 | * |
| 3512 | * ,---.------------------------.-----.--------------.------.---. |
| 3513 | * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///| |
| 3514 | * `---^------------------------^-----^--------------^------^---' |
| 3515 | * | | <-----> | | |
| 3516 | * area | hole | wrap |
| 3517 | * |<-----------------------> | |
| 3518 | * | dfl | |
| 3519 | * |<-------------------------------------------------->| |
| 3520 | * head data |
| 3521 | * |
| 3522 | * Please note that the HEADERS frame is always deprived from its PADLEN byte |
| 3523 | * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY |
| 3524 | * bit. |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3525 | * |
| 3526 | * The <flags> field must point to either the stream's flags or to a copy of it |
| 3527 | * so that the function can update the following flags : |
| 3528 | * - H2_SF_DATA_CLEN when content-length is seen |
| 3529 | * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion |
| 3530 | * - H2_SF_HEADERS_RCVD once the frame is successfully decoded |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3531 | * |
| 3532 | * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to |
| 3533 | * decoding, in order to detect if we're dealing with a headers or a trailers |
| 3534 | * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3535 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3536 | 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] | 3537 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3538 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3539 | struct buffer *tmp = get_trash_chunk(); |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 3540 | struct http_hdr list[global.tune.max_http_hdr * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3541 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3542 | unsigned int msgf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3543 | struct htx *htx = NULL; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3544 | int flen; // header frame len |
| 3545 | int hole = 0; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3546 | int ret = 0; |
| 3547 | int outlen; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3548 | int wrap; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3549 | int try = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3550 | |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3551 | next_frame: |
| 3552 | if (b_data(&h2c->dbuf) - hole < h2c->dfl) |
| 3553 | goto leave; // incomplete input frame |
| 3554 | |
| 3555 | /* No END_HEADERS means there's one or more CONTINUATION frames. In |
| 3556 | * this case, we'll try to paste it immediately after the initial |
| 3557 | * HEADERS frame payload and kill any possible padding. The initial |
| 3558 | * frame's length will be increased to represent the concatenation |
| 3559 | * of the two frames. The next frame is read from position <tlen> |
| 3560 | * and written at position <flen> (minus padding if some is present). |
| 3561 | */ |
| 3562 | if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) { |
| 3563 | struct h2_fh hdr; |
| 3564 | int clen; // CONTINUATION frame's payload length |
| 3565 | |
| 3566 | if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) { |
| 3567 | /* no more data, the buffer may be full, either due to |
| 3568 | * too large a frame or because of too large a hole that |
| 3569 | * we're going to compact at the end. |
| 3570 | */ |
| 3571 | goto leave; |
| 3572 | } |
| 3573 | |
| 3574 | if (hdr.ft != H2_FT_CONTINUATION) { |
| 3575 | /* RFC7540#6.10: frame of unexpected type */ |
| 3576 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3577 | goto fail; |
| 3578 | } |
| 3579 | |
| 3580 | if (hdr.sid != h2c->dsi) { |
| 3581 | /* RFC7540#6.10: frame of different stream */ |
| 3582 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3583 | goto fail; |
| 3584 | } |
| 3585 | |
| 3586 | if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { |
| 3587 | /* RFC7540#4.2: invalid frame length */ |
| 3588 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3589 | goto fail; |
| 3590 | } |
| 3591 | |
| 3592 | /* detect when we must stop aggragating frames */ |
| 3593 | h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS; |
| 3594 | |
| 3595 | /* Take as much as we can of the CONTINUATION frame's payload */ |
| 3596 | clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9); |
| 3597 | if (clen > hdr.len) |
| 3598 | clen = hdr.len; |
| 3599 | |
| 3600 | /* Move the frame's payload over the padding, hole and frame |
| 3601 | * header. At least one of hole or dpl is null (see diagrams |
| 3602 | * above). The hole moves after the new aggragated frame. |
| 3603 | */ |
| 3604 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9)); |
| 3605 | h2c->dfl += clen - h2c->dpl; |
| 3606 | hole += h2c->dpl + 9; |
| 3607 | h2c->dpl = 0; |
| 3608 | goto next_frame; |
| 3609 | } |
| 3610 | |
| 3611 | flen = h2c->dfl - h2c->dpl; |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3612 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3613 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3614 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3615 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3616 | copy = alloc_trash_chunk(); |
| 3617 | if (!copy) { |
| 3618 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3619 | goto fail; |
| 3620 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3621 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3622 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3623 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3624 | } |
| 3625 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3626 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3627 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3628 | if (read_n32(hdrs) == h2c->dsi) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3629 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3630 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3631 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3632 | } |
| 3633 | |
Willy Tarreau | a01f45e | 2018-12-31 07:41:24 +0100 | [diff] [blame] | 3634 | if (flen < 5) { |
| 3635 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3636 | goto fail; |
| 3637 | } |
| 3638 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3639 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3640 | flen -= 5; |
| 3641 | } |
| 3642 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3643 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3644 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3645 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3646 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3647 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3648 | /* we can't retry a failed decompression operation so we must be very |
| 3649 | * careful not to take any risks. In practice the output buffer is |
| 3650 | * always empty except maybe for trailers, in which case we simply have |
| 3651 | * to wait for the upper layer to finish consuming what is available. |
| 3652 | */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3653 | |
| 3654 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3655 | htx = htx_from_buf(rxbuf); |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3656 | if (!htx_is_empty(htx)) { |
| 3657 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3658 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3659 | } |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3660 | } else { |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3661 | if (b_data(rxbuf)) { |
| 3662 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3663 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3664 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3665 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3666 | rxbuf->head = 0; |
| 3667 | try = b_size(rxbuf); |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3668 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3669 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3670 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3671 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3672 | sizeof(list)/sizeof(list[0]), tmp); |
| 3673 | if (outlen < 0) { |
| 3674 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3675 | goto fail; |
| 3676 | } |
| 3677 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3678 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3679 | * the parser's state to commit these changes and allow us to later |
| 3680 | * fail solely on the stream if needed. |
| 3681 | */ |
| 3682 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3683 | h2c->dfl = hole = 0; |
| 3684 | h2c->st0 = H2_CS_FRAME_H; |
| 3685 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3686 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3687 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3688 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3689 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3690 | goto trailers; |
| 3691 | |
| 3692 | /* This is the first HEADERS frame so it's a headers block */ |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3693 | if (htx) { |
| 3694 | /* HTX mode */ |
| 3695 | if (h2c->flags & H2_CF_IS_BACK) |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3696 | outlen = h2_make_htx_response(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3697 | else |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3698 | outlen = h2_make_htx_request(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3699 | } else { |
| 3700 | /* HTTP/1 mode */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3701 | 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] | 3702 | if (outlen > 0) |
| 3703 | b_add(rxbuf, outlen); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3704 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3705 | |
| 3706 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3707 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3708 | goto fail; |
| 3709 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3710 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3711 | if (msgf & H2_MSGF_BODY) { |
| 3712 | /* a payload is present */ |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3713 | if (msgf & H2_MSGF_BODY_CL) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3714 | *flags |= H2_SF_DATA_CLEN; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3715 | if (htx) |
| 3716 | htx->extra = *body_len; |
| 3717 | } |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3718 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3719 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3720 | } |
| 3721 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3722 | done: |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3723 | /* indicate that a HEADERS frame was received for this stream, except |
| 3724 | * for 1xx responses. For 1xx responses, another HEADERS frame is |
| 3725 | * expected. |
| 3726 | */ |
| 3727 | if (!(msgf & H2_MSGF_RSP_1XX)) |
| 3728 | *flags |= H2_SF_HEADERS_RCVD; |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3729 | |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 3730 | if ((h2c->dff & H2_F_HEADERS_END_STREAM)) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3731 | /* Mark the end of message, either using EOM in HTX or with the |
| 3732 | * trailing CRLF after the end of trailers. Note that DATA_CHNK |
Willy Tarreau | 2135f91 | 2019-05-07 11:17:32 +0200 | [diff] [blame] | 3733 | * is not set during headers with END_STREAM. For HTX trailers, |
| 3734 | * we must not leave an HTX trailers block not followed by an |
| 3735 | * EOM block, the two must be atomic. Thus if we fail to emit |
| 3736 | * the EOM block we must remove the TLR block we've just added. |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3737 | */ |
| 3738 | if (htx) { |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 3739 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3740 | goto fail; |
| 3741 | } |
| 3742 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3743 | if (!b_putblk(rxbuf, "\r\n", 2)) |
| 3744 | goto fail; |
| 3745 | } |
| 3746 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3747 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3748 | /* success */ |
| 3749 | ret = 1; |
| 3750 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3751 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3752 | /* 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] | 3753 | * move the remaining data over it. |
| 3754 | */ |
| 3755 | if (hole) { |
| 3756 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3757 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3758 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3759 | b_sub(&h2c->dbuf, hole); |
| 3760 | } |
| 3761 | |
Willy Tarreau | 97215ca | 2019-04-29 10:20:21 +0200 | [diff] [blame] | 3762 | if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) { |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3763 | /* too large frames */ |
| 3764 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3765 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3766 | } |
| 3767 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3768 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3769 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3770 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3771 | return ret; |
| 3772 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3773 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3774 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3775 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3776 | |
| 3777 | trailers: |
| 3778 | /* This is the last HEADERS frame hence a trailer */ |
| 3779 | |
| 3780 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3781 | /* It's a trailer but it's missing ES flag */ |
| 3782 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3783 | goto fail; |
| 3784 | } |
| 3785 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 3786 | /* Trailers terminate a DATA sequence. In HTX we always handle them. In |
| 3787 | * legacy, when using chunks, we have to emit the 0 CRLF marker first |
| 3788 | * and then handle the trailers. For other modes, the trailers are |
| 3789 | * silently dropped. |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3790 | */ |
| 3791 | if (htx) { |
Willy Tarreau | 5255f28 | 2019-01-03 18:41:05 +0100 | [diff] [blame] | 3792 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3793 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3794 | } |
| 3795 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3796 | /* Legacy mode with chunked encoding : we must finalize the |
| 3797 | * data block message emit the trailing CRLF */ |
| 3798 | if (!b_putblk(rxbuf, "0\r\n", 3)) |
| 3799 | goto fail; |
Willy Tarreau | e2b05cc | 2019-01-03 16:18:34 +0100 | [diff] [blame] | 3800 | |
| 3801 | outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try); |
| 3802 | if (outlen > 0) |
| 3803 | b_add(rxbuf, outlen); |
| 3804 | else |
| 3805 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3809 | } |
| 3810 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3811 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 3812 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 3813 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 3814 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 3815 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 3816 | * 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] | 3817 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3818 | * send the current frame, 0 if it couldn't complete, in which case |
| 3819 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3820 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3821 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3822 | * have checked the frame header and ensured that the frame was complete or the |
| 3823 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3824 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3825 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3826 | { |
| 3827 | struct h2c *h2c = h2s->h2c; |
| 3828 | int block1, block2; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3829 | unsigned int flen = 0; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3830 | unsigned int chklen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3831 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3832 | struct buffer *csbuf; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3833 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3834 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3835 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3836 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3837 | if (!csbuf) { |
| 3838 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3839 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3840 | } |
| 3841 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3842 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3843 | flen = h2c->dfl - h2c->dpl; |
Olivier Houchard | 2f30883 | 2018-12-19 15:53:53 +0100 | [diff] [blame] | 3844 | if (h2c->proxy->options2 & PR_O2_USE_HTX) |
| 3845 | htx = htx_from_buf(csbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3846 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3847 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3848 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3849 | if (flen > b_data(&h2c->dbuf)) { |
| 3850 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3851 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3852 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3853 | } |
| 3854 | |
Willy Tarreau | a9b7796 | 2019-01-31 07:23:00 +0100 | [diff] [blame] | 3855 | if (htx) { |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3856 | unsigned int sent; |
| 3857 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3858 | block1 = htx_free_data_space(htx); |
| 3859 | if (!block1) { |
| 3860 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3861 | goto fail; |
| 3862 | } |
| 3863 | if (flen > block1) |
| 3864 | flen = block1; |
| 3865 | |
| 3866 | /* here, flen is the max we can copy into the output buffer */ |
| 3867 | block1 = b_contig_data(&h2c->dbuf, 0); |
| 3868 | if (flen > block1) |
| 3869 | flen = block1; |
| 3870 | |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3871 | sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen)); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3872 | |
Willy Tarreau | b6563f4 | 2019-06-15 11:34:41 +0200 | [diff] [blame] | 3873 | b_del(&h2c->dbuf, sent); |
| 3874 | h2c->dfl -= sent; |
| 3875 | h2c->rcvd_c += sent; |
| 3876 | h2c->rcvd_s += sent; // warning, this can also affect the closed streams! |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3877 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3878 | if (h2s->flags & H2_SF_DATA_CLEN) { |
Willy Tarreau | b6563f4 | 2019-06-15 11:34:41 +0200 | [diff] [blame] | 3879 | h2s->body_len -= sent; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3880 | htx->extra = h2s->body_len; |
| 3881 | } |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 3882 | |
| 3883 | if (sent < flen) { |
| 3884 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3885 | goto fail; |
| 3886 | } |
| 3887 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3888 | goto try_again; |
| 3889 | } |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 3890 | else if (unlikely(b_space_wraps(csbuf) && |
| 3891 | flen + chklen <= b_room(csbuf) && |
| 3892 | b_data(csbuf) <= MAX_DATA_REALIGN)) { |
| 3893 | /* it doesn't fit in a single block and the buffer is fragmented, if there are |
| 3894 | * not too many data in the buffer, let's defragment it and try |
| 3895 | * again. |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3896 | */ |
| 3897 | b_slow_realign(csbuf, trash.area, 0); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3898 | } |
| 3899 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3900 | /* chunked-encoding requires more room */ |
| 3901 | if (h2s->flags & H2_SF_DATA_CHNK) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3902 | chklen = MIN(flen, b_room(csbuf)); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3903 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 3904 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 3905 | (chklen < 1048576) ? 4 : 8; |
| 3906 | chklen += 4; // CRLF, CRLF |
| 3907 | } |
| 3908 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3909 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3910 | if (flen + chklen > b_room(csbuf)) { |
| 3911 | if (chklen >= b_room(csbuf)) { |
| 3912 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3913 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3914 | } |
| 3915 | flen = b_room(csbuf) - chklen; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3916 | } |
| 3917 | |
| 3918 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3919 | /* emit the chunk size */ |
| 3920 | unsigned int chksz = flen; |
| 3921 | char str[10]; |
| 3922 | char *beg; |
| 3923 | |
| 3924 | beg = str + sizeof(str); |
| 3925 | *--beg = '\n'; |
| 3926 | *--beg = '\r'; |
| 3927 | do { |
| 3928 | *--beg = hextab[chksz & 0xF]; |
| 3929 | } while (chksz >>= 4); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3930 | b_putblk(csbuf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3931 | } |
| 3932 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3933 | /* Block1 is the length of the first block before the buffer wraps, |
| 3934 | * block2 is the optional second block to reach the end of the frame. |
| 3935 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3936 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3937 | if (block1 > flen) |
| 3938 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3939 | block2 = flen - block1; |
| 3940 | |
| 3941 | if (block1) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3942 | b_putblk(csbuf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3943 | |
| 3944 | if (block2) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3945 | b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3946 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3947 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3948 | /* emit the CRLF */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3949 | b_putblk(csbuf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3950 | } |
| 3951 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3952 | /* now mark the input data as consumed (will be deleted from the buffer |
| 3953 | * by the caller when seeing FRAME_A after sending the window update). |
| 3954 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3955 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3956 | h2c->dfl -= flen; |
| 3957 | h2c->rcvd_c += flen; |
| 3958 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 3959 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3960 | if (h2s->flags & H2_SF_DATA_CLEN) |
| 3961 | h2s->body_len -= flen; |
| 3962 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3963 | if (h2c->dfl > h2c->dpl) { |
| 3964 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3965 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3966 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3967 | } |
| 3968 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3969 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3970 | /* here we're done with the frame, all the payload (except padding) was |
| 3971 | * transferred. |
| 3972 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3973 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3974 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 3975 | if (htx) { |
| 3976 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3977 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3978 | goto fail; |
| 3979 | } |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3980 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3981 | else if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3982 | /* emit the trailing 0 CRLF CRLF */ |
| 3983 | if (b_room(csbuf) < 5) { |
| 3984 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3985 | goto fail; |
| 3986 | } |
| 3987 | chklen += 5; |
| 3988 | b_putblk(csbuf, "0\r\n\r\n", 5); |
| 3989 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3990 | } |
| 3991 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 3992 | h2c->rcvd_c += h2c->dpl; |
| 3993 | h2c->rcvd_s += h2c->dpl; |
| 3994 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3995 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3996 | if (htx) |
| 3997 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3998 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3999 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4000 | if (htx) |
| 4001 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 4002 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 4003 | } |
| 4004 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4005 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 4006 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 4007 | * number of bytes sent. The caller must check the stream's status to detect |
| 4008 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4009 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4010 | 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] | 4011 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4012 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4013 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4014 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4015 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4016 | struct buffer *mbuf; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4017 | union h1_sl sl; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4018 | int es_now = 0; |
| 4019 | int ret = 0; |
| 4020 | int hdr; |
| 4021 | |
| 4022 | if (h2c_mux_busy(h2c, h2s)) { |
| 4023 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4024 | return 0; |
| 4025 | } |
| 4026 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4027 | /* First, try to parse the H1 response and index it into <list>. |
| 4028 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 4029 | * block does not wrap and we can safely read it this way without |
| 4030 | * having to realign the buffer. |
| 4031 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4032 | 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] | 4033 | list, sizeof(list)/sizeof(list[0]), h1m, &sl); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4034 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 4035 | /* incomplete or invalid response, this is abnormal coming from |
| 4036 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 4037 | * so that won't be fixed, raise an error now. |
| 4038 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4039 | * FIXME: we should instead add the ability to only return a |
| 4040 | * 502 bad gateway. But in theory this is not supposed to |
| 4041 | * happen. |
| 4042 | */ |
| 4043 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4044 | ret = 0; |
| 4045 | goto end; |
| 4046 | } |
| 4047 | |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4048 | h2s->status = sl.st.status; |
Willy Tarreau | db72da0 | 2018-09-13 11:52:20 +0200 | [diff] [blame] | 4049 | |
| 4050 | /* certain statuses have no body or an empty one, regardless of |
| 4051 | * what the headers say. |
| 4052 | */ |
| 4053 | if (sl.st.status >= 100 && sl.st.status < 200) { |
| 4054 | h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK); |
| 4055 | h1m->curr_len = h1m->body_len = 0; |
| 4056 | } |
| 4057 | else if (sl.st.status == 204 || sl.st.status == 304) { |
| 4058 | /* no contents, claim c-len is present and set to zero */ |
| 4059 | h1m->flags &= ~H1_MF_CHNK; |
| 4060 | h1m->flags |= H1_MF_CLEN; |
| 4061 | h1m->curr_len = h1m->body_len = 0; |
| 4062 | } |
| 4063 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4064 | mbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4065 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4066 | if (!h2_get_buf(h2c, mbuf)) { |
| 4067 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4068 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4069 | return 0; |
| 4070 | } |
| 4071 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4072 | chunk_reset(&outbuf); |
| 4073 | |
| 4074 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4075 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4076 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4077 | break; |
| 4078 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4079 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4080 | } |
| 4081 | |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4082 | if (outbuf.size < 9) |
| 4083 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4084 | |
| 4085 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4086 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4087 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4088 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4089 | |
| 4090 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4091 | if (unlikely(list[0].v.len != 3)) { |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 4092 | /* this is an unparsable response */ |
| 4093 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4094 | ret = 0; |
| 4095 | goto end; |
| 4096 | } |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4097 | |
| 4098 | if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4099 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4100 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4101 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | /* encode all headers, stop at empty name */ |
| 4105 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 4106 | /* these ones do not exist in H2 and must be dropped. */ |
| 4107 | if (isteq(list[hdr].n, ist("connection")) || |
| 4108 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4109 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4110 | isteq(list[hdr].n, ist("upgrade")) || |
| 4111 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4112 | continue; |
| 4113 | |
| 4114 | if (isteq(list[hdr].n, ist(""))) |
| 4115 | break; // end |
| 4116 | |
| 4117 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4118 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4119 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4120 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4121 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4122 | } |
| 4123 | } |
| 4124 | |
| 4125 | /* we may need to add END_STREAM */ |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4126 | 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] | 4127 | es_now = 1; |
| 4128 | |
| 4129 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4130 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4131 | |
| 4132 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4133 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4134 | |
| 4135 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4136 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4137 | |
| 4138 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4139 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 4140 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4141 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4142 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4143 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4144 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4145 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4146 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4147 | h2s->flags |= H2_SF_ES_SENT; |
| 4148 | if (h2s->st == H2_SS_OPEN) |
| 4149 | h2s->st = H2_SS_HLOC; |
| 4150 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4151 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4152 | } |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4153 | else if (h2s->status >= 100 && h2s->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4154 | /* 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] | 4155 | h1m_init_res(h1m); |
Willy Tarreau | 9b8cd1f | 2018-09-12 09:24:38 +0200 | [diff] [blame] | 4156 | 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] | 4157 | h2s->h1m.flags |= H1_MF_TOLOWER; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4158 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 4159 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 4160 | |
| 4161 | /* 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] | 4162 | |
| 4163 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4164 | //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] | 4165 | return ret; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4166 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4167 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4168 | goto retry; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4169 | h1m_init_res(h1m); |
| 4170 | h1m->err_pos = -1; // don't care about errors on the response path |
| 4171 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4172 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4173 | ret = 0; |
| 4174 | goto end; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4175 | } |
| 4176 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4177 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 4178 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 4179 | * the number of bytes sent. The caller must check the stream's status to |
| 4180 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4181 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4182 | 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] | 4183 | { |
| 4184 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4185 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4186 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4187 | struct buffer *mbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4188 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 4189 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4190 | int es_now = 0; |
| 4191 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4192 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4193 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4194 | |
| 4195 | if (h2c_mux_busy(h2c, h2s)) { |
| 4196 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4197 | goto end; |
| 4198 | } |
| 4199 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4200 | mbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4201 | retry: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4202 | if (!h2_get_buf(h2c, mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4203 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4204 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4205 | goto end; |
| 4206 | } |
| 4207 | |
| 4208 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4209 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4210 | goto end; |
| 4211 | |
| 4212 | chunk_reset(&outbuf); |
| 4213 | |
| 4214 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4215 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4216 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4217 | break; |
| 4218 | realign_again: |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4219 | /* If there are pending data in the output buffer, and we have |
| 4220 | * less than 1/4 of the mbuf's size and everything fits, we'll |
| 4221 | * still perform a copy anyway. Otherwise we'll pretend the mbuf |
| 4222 | * is full and wait, to save some slow realign calls. |
| 4223 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4224 | if ((max + 9 > b_room(mbuf) || max >= b_size(mbuf) / 4)) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4225 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4226 | goto retry; |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4227 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4228 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4229 | goto end; |
| 4230 | } |
| 4231 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4232 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | if (outbuf.size < 9) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4236 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4237 | goto retry; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4238 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4239 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4240 | goto end; |
| 4241 | } |
| 4242 | |
| 4243 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4244 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4245 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4246 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4247 | |
| 4248 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 4249 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4250 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 4251 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4252 | break; |
| 4253 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4254 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4255 | if ((long long)size > h1m->curr_len) |
| 4256 | size = h1m->curr_len; |
| 4257 | break; |
| 4258 | default: /* te:chunked : parse chunks */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4259 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 4260 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4261 | if (!ret) |
| 4262 | goto end; |
| 4263 | |
| 4264 | if (ret < 0) { |
| 4265 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4266 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4267 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4268 | goto end; |
| 4269 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4270 | max -= ret; |
| 4271 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4272 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4273 | h1m->state = H1_MSG_CHUNK_SIZE; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4274 | } |
| 4275 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4276 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4277 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 4278 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4279 | if (!ret) |
| 4280 | goto end; |
| 4281 | |
| 4282 | if (ret < 0) { |
| 4283 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4284 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4285 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4286 | goto end; |
| 4287 | } |
| 4288 | |
| 4289 | size = chunk; |
| 4290 | h1m->curr_len = chunk; |
| 4291 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4292 | max -= ret; |
| 4293 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4294 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4295 | h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4296 | if (!size) |
| 4297 | goto send_empty; |
| 4298 | } |
| 4299 | |
| 4300 | /* in MSG_DATA state, continue below */ |
| 4301 | size = h1m->curr_len; |
| 4302 | break; |
| 4303 | } |
| 4304 | |
| 4305 | /* we have in <size> the exact number of bytes we need to copy from |
| 4306 | * the H1 buffer. We need to check this against the connection's and |
| 4307 | * the stream's send windows, and to ensure that this fits in the max |
| 4308 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4309 | * the frame header). The connection's flow control is applied last so |
| 4310 | * that we can use a separate list of streams which are immediately |
| 4311 | * unblocked on window opening. Note: we don't implement padding. |
| 4312 | */ |
| 4313 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4314 | if (size > max) |
| 4315 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4316 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4317 | if (size > h2s_mws(h2s)) |
| 4318 | size = h2s_mws(h2s); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4319 | |
| 4320 | if (size <= 0) { |
| 4321 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 4322 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 4323 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4324 | goto end; |
| 4325 | } |
| 4326 | |
| 4327 | if (h2c->mfs && size > h2c->mfs) |
| 4328 | size = h2c->mfs; |
| 4329 | |
| 4330 | if (size + 9 > outbuf.size) { |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 4331 | /* It doesn't fit at once. If it at least fits once split and |
| 4332 | * the amount of data to move is low, let's defragment the |
| 4333 | * buffer now. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4334 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4335 | if (b_space_wraps(mbuf) && |
| 4336 | (size + 9 <= b_room(mbuf)) && |
| 4337 | b_data(mbuf) <= MAX_DATA_REALIGN) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4338 | goto realign_again; |
| 4339 | size = outbuf.size - 9; |
| 4340 | } |
| 4341 | |
| 4342 | if (size <= 0) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4343 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4344 | goto retry; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4345 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4346 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4347 | goto end; |
| 4348 | } |
| 4349 | |
| 4350 | if (size > h2c->mws) |
| 4351 | size = h2c->mws; |
| 4352 | |
| 4353 | if (size <= 0) { |
| 4354 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4355 | goto end; |
| 4356 | } |
| 4357 | |
| 4358 | /* copy whatever we can */ |
| 4359 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4360 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4361 | if (ret == 1) |
| 4362 | len2 = 0; |
| 4363 | |
| 4364 | if (!ret || len1 + len2 < size) { |
| 4365 | /* FIXME: must normally never happen */ |
| 4366 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4367 | goto end; |
| 4368 | } |
| 4369 | |
| 4370 | /* limit len1/len2 to size */ |
| 4371 | if (len1 + len2 > size) { |
| 4372 | int sub = len1 + len2 - size; |
| 4373 | |
| 4374 | if (len2 > sub) |
| 4375 | len2 -= sub; |
| 4376 | else { |
| 4377 | sub -= len2; |
| 4378 | len2 = 0; |
| 4379 | len1 -= sub; |
| 4380 | } |
| 4381 | } |
| 4382 | |
| 4383 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4384 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4385 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4386 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4387 | |
| 4388 | send_empty: |
| 4389 | /* we may need to add END_STREAM */ |
| 4390 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 4391 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4392 | * |
| 4393 | * FIXME: what we do here is not correct because we send end_stream |
| 4394 | * before knowing if we'll have to send a HEADERS frame for the |
| 4395 | * trailers. More importantly we're not consuming the trailing CRLF |
| 4396 | * after the end of trailers, so it will be left to the caller to |
| 4397 | * eat it. The right way to do it would be to measure trailers here |
| 4398 | * and to send ES only if there are no trailers. |
| 4399 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4400 | */ |
| 4401 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4402 | !h1m->curr_len || h1m->state >= H1_MSG_DONE) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4403 | es_now = 1; |
| 4404 | |
| 4405 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4406 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4407 | |
| 4408 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4409 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4410 | |
| 4411 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4412 | b_add(mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4413 | |
| 4414 | /* consume incoming H1 response */ |
| 4415 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4416 | max -= size; |
| 4417 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4418 | total += size; |
| 4419 | h1m->curr_len -= size; |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4420 | h2s->sws -= size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4421 | h2c->mws -= size; |
| 4422 | |
| 4423 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4424 | h1m->state = H1_MSG_CHUNK_CRLF; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4425 | goto new_frame; |
| 4426 | } |
| 4427 | } |
| 4428 | |
| 4429 | if (es_now) { |
| 4430 | if (h2s->st == H2_SS_OPEN) |
| 4431 | h2s->st = H2_SS_HLOC; |
| 4432 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4433 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4434 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4435 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 4436 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4437 | total += max; |
| 4438 | ofs += max; |
| 4439 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4440 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4441 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4442 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4443 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4444 | h2s->flags |= H2_SF_ES_SENT; |
| 4445 | } |
| 4446 | |
| 4447 | end: |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4448 | 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(h2s), (unsigned int)b_data(buf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4449 | return total; |
| 4450 | } |
| 4451 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4452 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 4453 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4454 | * must check the stream's status to detect any error which might have happened |
| 4455 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4456 | * from the message. The htx message is assumed to be valid since produced from |
| 4457 | * the internal code, hence it contains a start line, an optional series of |
| 4458 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4459 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4460 | */ |
| 4461 | static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
| 4462 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4463 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4464 | struct h2c *h2c = h2s->h2c; |
| 4465 | struct htx_blk *blk; |
| 4466 | struct htx_blk *blk_end; |
| 4467 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4468 | struct buffer *mbuf; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4469 | struct htx_sl *sl; |
| 4470 | enum htx_blk_type type; |
| 4471 | int es_now = 0; |
| 4472 | int ret = 0; |
| 4473 | int hdr; |
| 4474 | int idx; |
| 4475 | |
| 4476 | if (h2c_mux_busy(h2c, h2s)) { |
| 4477 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4478 | return 0; |
| 4479 | } |
| 4480 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4481 | /* determine the first block which must not be deleted, blk_end may |
| 4482 | * be NULL if all blocks have to be deleted. |
| 4483 | */ |
| 4484 | idx = htx_get_head(htx); |
| 4485 | blk_end = NULL; |
| 4486 | while (idx != -1) { |
| 4487 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4488 | idx = htx_get_next(htx, idx); |
| 4489 | if (type == HTX_BLK_EOH) { |
| 4490 | if (idx != -1) |
| 4491 | blk_end = htx_get_blk(htx, idx); |
| 4492 | break; |
| 4493 | } |
| 4494 | } |
| 4495 | |
| 4496 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4497 | blk = htx_get_head_blk(htx); |
| 4498 | BUG_ON(htx_get_blk_type(blk) != HTX_BLK_RES_SL); |
| 4499 | ALREADY_CHECKED(blk); |
| 4500 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4501 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4502 | if (h2s->status < 100 || h2s->status > 999) |
| 4503 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4504 | |
| 4505 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4506 | hdr = 0; |
| 4507 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4508 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4509 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4510 | blk = htx_get_blk(htx, idx); |
| 4511 | type = htx_get_blk_type(blk); |
| 4512 | |
| 4513 | if (type == HTX_BLK_UNUSED) |
| 4514 | continue; |
| 4515 | |
| 4516 | if (type != HTX_BLK_HDR) |
| 4517 | break; |
| 4518 | |
| 4519 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4520 | goto fail; |
| 4521 | |
| 4522 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4523 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4524 | hdr++; |
| 4525 | } |
| 4526 | |
| 4527 | /* marker for end of headers */ |
| 4528 | list[hdr].n = ist(""); |
| 4529 | |
| 4530 | if (h2s->status == 204 || h2s->status == 304) { |
| 4531 | /* no contents, claim c-len is present and set to zero */ |
| 4532 | es_now = 1; |
| 4533 | } |
| 4534 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4535 | mbuf = br_tail(h2c->mbuf); |
| 4536 | retry: |
| 4537 | if (!h2_get_buf(h2c, mbuf)) { |
| 4538 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4539 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4540 | return 0; |
| 4541 | } |
| 4542 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4543 | chunk_reset(&outbuf); |
| 4544 | |
| 4545 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4546 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4547 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4548 | break; |
| 4549 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4550 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4551 | } |
| 4552 | |
| 4553 | if (outbuf.size < 9) |
| 4554 | goto full; |
| 4555 | |
| 4556 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4557 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4558 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4559 | outbuf.data = 9; |
| 4560 | |
| 4561 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4562 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4563 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4564 | goto realign_again; |
| 4565 | goto full; |
| 4566 | } |
| 4567 | |
| 4568 | /* encode all headers, stop at empty name */ |
| 4569 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4570 | /* these ones do not exist in H2 and must be dropped. */ |
| 4571 | if (isteq(list[hdr].n, ist("connection")) || |
| 4572 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4573 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4574 | isteq(list[hdr].n, ist("upgrade")) || |
| 4575 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4576 | continue; |
| 4577 | |
| 4578 | if (isteq(list[hdr].n, ist(""))) |
| 4579 | break; // end |
| 4580 | |
| 4581 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4582 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4583 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4584 | goto realign_again; |
| 4585 | goto full; |
| 4586 | } |
| 4587 | } |
| 4588 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4589 | /* we may need to add END_STREAM except for 1xx responses. |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4590 | * FIXME: we should also set it when we know for sure that the |
| 4591 | * content-length is zero as well as on 204/304 |
| 4592 | */ |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4593 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM && |
| 4594 | (h2s->status >= 200 || h2s->status == 101)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4595 | es_now = 1; |
| 4596 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4597 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4598 | es_now = 1; |
| 4599 | |
| 4600 | /* update the frame's size */ |
| 4601 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4602 | |
| 4603 | if (es_now) |
| 4604 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4605 | |
| 4606 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4607 | b_add(mbuf, outbuf.data); |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4608 | |
| 4609 | /* indicates the HEADERS frame was sent, except for 1xx responses. For |
| 4610 | * 1xx responses, another HEADERS frame is expected. |
| 4611 | */ |
| 4612 | if (h2s->status >= 200 || h2s->status == 101) |
| 4613 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4614 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4615 | if (es_now) { |
| 4616 | h2s->flags |= H2_SF_ES_SENT; |
| 4617 | if (h2s->st == H2_SS_OPEN) |
| 4618 | h2s->st = H2_SS_HLOC; |
| 4619 | else |
| 4620 | h2s_close(h2s); |
| 4621 | } |
| 4622 | |
| 4623 | /* OK we could properly deliver the response */ |
| 4624 | |
| 4625 | /* remove all header blocks including the EOH and compute the |
| 4626 | * corresponding size. |
| 4627 | * |
| 4628 | * FIXME: We should remove everything when es_now is set. |
| 4629 | */ |
| 4630 | ret = 0; |
| 4631 | idx = htx_get_head(htx); |
| 4632 | blk = htx_get_blk(htx, idx); |
| 4633 | while (blk != blk_end) { |
| 4634 | ret += htx_get_blksz(blk); |
| 4635 | blk = htx_remove_blk(htx, blk); |
| 4636 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4637 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4638 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4639 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4640 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4641 | } |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4642 | end: |
| 4643 | return ret; |
| 4644 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4645 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4646 | goto retry; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4647 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4648 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4649 | ret = 0; |
| 4650 | goto end; |
| 4651 | fail: |
| 4652 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4653 | * list etc go here (unrecoverable errors). |
| 4654 | */ |
| 4655 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4656 | ret = 0; |
| 4657 | goto end; |
| 4658 | } |
| 4659 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4660 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4661 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4662 | * must check the stream's status to detect any error which might have happened |
| 4663 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4664 | * from the message. The htx message is assumed to be valid since produced from |
| 4665 | * the internal code, hence it contains a start line, an optional series of |
| 4666 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4667 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4668 | */ |
| 4669 | static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
| 4670 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 4671 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4672 | struct h2c *h2c = h2s->h2c; |
| 4673 | struct htx_blk *blk; |
| 4674 | struct htx_blk *blk_end; |
| 4675 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4676 | struct buffer *mbuf; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4677 | struct htx_sl *sl; |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4678 | struct ist meth, path, auth; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4679 | enum htx_blk_type type; |
| 4680 | int es_now = 0; |
| 4681 | int ret = 0; |
| 4682 | int hdr; |
| 4683 | int idx; |
| 4684 | |
| 4685 | if (h2c_mux_busy(h2c, h2s)) { |
| 4686 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4687 | return 0; |
| 4688 | } |
| 4689 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4690 | /* determine the first block which must not be deleted, blk_end may |
| 4691 | * be NULL if all blocks have to be deleted. |
| 4692 | */ |
| 4693 | idx = htx_get_head(htx); |
| 4694 | blk_end = NULL; |
| 4695 | while (idx != -1) { |
| 4696 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4697 | idx = htx_get_next(htx, idx); |
| 4698 | if (type == HTX_BLK_EOH) { |
| 4699 | if (idx != -1) |
| 4700 | blk_end = htx_get_blk(htx, idx); |
| 4701 | break; |
| 4702 | } |
| 4703 | } |
| 4704 | |
| 4705 | /* get the start line, we do have one */ |
Christopher Faulet | b77a1d2 | 2019-05-13 11:55:10 +0200 | [diff] [blame] | 4706 | blk = htx_get_head_blk(htx); |
| 4707 | BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
| 4708 | ALREADY_CHECKED(blk); |
| 4709 | sl = htx_get_blk_ptr(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4710 | meth = htx_sl_req_meth(sl); |
| 4711 | path = htx_sl_req_uri(sl); |
| 4712 | |
| 4713 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4714 | hdr = 0; |
| 4715 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4716 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4717 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4718 | blk = htx_get_blk(htx, idx); |
| 4719 | type = htx_get_blk_type(blk); |
| 4720 | |
| 4721 | if (type == HTX_BLK_UNUSED) |
| 4722 | continue; |
| 4723 | |
| 4724 | if (type != HTX_BLK_HDR) |
| 4725 | break; |
| 4726 | |
| 4727 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4728 | goto fail; |
| 4729 | |
| 4730 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4731 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4732 | hdr++; |
| 4733 | } |
| 4734 | |
| 4735 | /* marker for end of headers */ |
| 4736 | list[hdr].n = ist(""); |
| 4737 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4738 | mbuf = br_tail(h2c->mbuf); |
| 4739 | retry: |
| 4740 | if (!h2_get_buf(h2c, mbuf)) { |
| 4741 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4742 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4743 | return 0; |
| 4744 | } |
| 4745 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4746 | chunk_reset(&outbuf); |
| 4747 | |
| 4748 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4749 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 4750 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4751 | break; |
| 4752 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4753 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4754 | } |
| 4755 | |
| 4756 | if (outbuf.size < 9) |
| 4757 | goto full; |
| 4758 | |
| 4759 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4760 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4761 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4762 | outbuf.data = 9; |
| 4763 | |
| 4764 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4765 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4766 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4767 | goto realign_again; |
| 4768 | goto full; |
| 4769 | } |
| 4770 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4771 | /* RFC7540 #8.3: the CONNECT method must have : |
| 4772 | * - :authority set to the URI part (host:port) |
| 4773 | * - :method set to CONNECT |
| 4774 | * - :scheme and :path omitted |
| 4775 | */ |
| 4776 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 4777 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
Christopher Faulet | 3b44c54 | 2019-06-14 10:46:51 +0200 | [diff] [blame] | 4778 | struct ist scheme; |
| 4779 | |
| 4780 | if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) |
| 4781 | scheme = ist("http"); |
| 4782 | else |
| 4783 | scheme = ist("https"); |
| 4784 | |
| 4785 | if (!hpack_encode_scheme(&outbuf, scheme)) { |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4786 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4787 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4788 | goto realign_again; |
| 4789 | goto full; |
| 4790 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4791 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4792 | /* encode the path, which necessarily is the second one */ |
| 4793 | if (!hpack_encode_path(&outbuf, path)) { |
| 4794 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4795 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4796 | goto realign_again; |
| 4797 | goto full; |
| 4798 | } |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4799 | |
| 4800 | /* look for the Host header and place it in :authority */ |
| 4801 | auth = ist2(NULL, 0); |
| 4802 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4803 | if (isteq(list[hdr].n, ist(""))) |
| 4804 | break; // end |
| 4805 | |
| 4806 | if (isteq(list[hdr].n, ist("host"))) { |
| 4807 | auth = list[hdr].v; |
| 4808 | break; |
| 4809 | } |
| 4810 | } |
| 4811 | } |
| 4812 | else { |
| 4813 | /* for CONNECT, :authority is taken from the path */ |
| 4814 | auth = path; |
| 4815 | } |
| 4816 | |
| 4817 | if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) { |
| 4818 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4819 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4820 | goto realign_again; |
| 4821 | goto full; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4822 | } |
| 4823 | |
| 4824 | /* encode all headers, stop at empty name */ |
| 4825 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4826 | /* these ones do not exist in H2 and must be dropped. */ |
| 4827 | if (isteq(list[hdr].n, ist("connection")) || |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4828 | isteq(list[hdr].n, ist("host")) || |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4829 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4830 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4831 | isteq(list[hdr].n, ist("upgrade")) || |
| 4832 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4833 | continue; |
| 4834 | |
| 4835 | if (isteq(list[hdr].n, ist(""))) |
| 4836 | break; // end |
| 4837 | |
| 4838 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4839 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4840 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4841 | goto realign_again; |
| 4842 | goto full; |
| 4843 | } |
| 4844 | } |
| 4845 | |
| 4846 | /* we may need to add END_STREAM if we have no body : |
| 4847 | * - request already closed, or : |
| 4848 | * - no transfer-encoding, and : |
| 4849 | * - no content-length or content-length:0 |
| 4850 | * Fixme: this doesn't take into account CONNECT requests. |
| 4851 | */ |
| 4852 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4853 | es_now = 1; |
| 4854 | |
| 4855 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4856 | es_now = 1; |
| 4857 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4858 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4859 | es_now = 1; |
| 4860 | |
| 4861 | /* update the frame's size */ |
| 4862 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4863 | |
| 4864 | if (es_now) |
| 4865 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4866 | |
| 4867 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4868 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4869 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4870 | h2s->st = H2_SS_OPEN; |
| 4871 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4872 | if (es_now) { |
| 4873 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4874 | h2s->flags |= H2_SF_ES_SENT; |
| 4875 | h2s->st = H2_SS_HLOC; |
| 4876 | } |
| 4877 | |
| 4878 | /* remove all header blocks including the EOH and compute the |
| 4879 | * corresponding size. |
| 4880 | * |
| 4881 | * FIXME: We should remove everything when es_now is set. |
| 4882 | */ |
| 4883 | ret = 0; |
| 4884 | idx = htx_get_head(htx); |
| 4885 | blk = htx_get_blk(htx, idx); |
| 4886 | while (blk != blk_end) { |
| 4887 | ret += htx_get_blksz(blk); |
| 4888 | blk = htx_remove_blk(htx, blk); |
| 4889 | } |
| 4890 | |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4891 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 4892 | ret += htx_get_blksz(blk_end); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4893 | htx_remove_blk(htx, blk_end); |
Christopher Faulet | 316934d | 2019-05-15 14:22:48 +0200 | [diff] [blame] | 4894 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4895 | |
| 4896 | end: |
| 4897 | return ret; |
| 4898 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4899 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 4900 | goto retry; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4901 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4902 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4903 | ret = 0; |
| 4904 | goto end; |
| 4905 | fail: |
| 4906 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4907 | * list etc go here (unrecoverable errors). |
| 4908 | */ |
| 4909 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4910 | ret = 0; |
| 4911 | goto end; |
| 4912 | } |
| 4913 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4914 | /* 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] | 4915 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 4916 | * caller must check the stream's status to detect any error which might have |
| 4917 | * happened subsequently to a successful send. Returns the number of data bytes |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4918 | * consumed, or zero if nothing done. Note that EOM count for 1 byte. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4919 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4920 | 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] | 4921 | { |
| 4922 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4923 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4924 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4925 | struct buffer *mbuf; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4926 | size_t total = 0; |
| 4927 | int es_now = 0; |
| 4928 | int bsize; /* htx block size */ |
| 4929 | int fsize; /* h2 frame size */ |
| 4930 | struct htx_blk *blk; |
| 4931 | enum htx_blk_type type; |
| 4932 | int idx; |
| 4933 | |
| 4934 | if (h2c_mux_busy(h2c, h2s)) { |
| 4935 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4936 | goto end; |
| 4937 | } |
| 4938 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4939 | htx = htx_from_buf(buf); |
| 4940 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4941 | /* We only come here with HTX_BLK_DATA blocks. However, while looping, |
| 4942 | * we can meet an HTX_BLK_EOM block that we'll leave to the caller to |
| 4943 | * handle. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4944 | */ |
| 4945 | |
| 4946 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4947 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4948 | goto end; |
| 4949 | |
| 4950 | idx = htx_get_head(htx); |
| 4951 | blk = htx_get_blk(htx, idx); |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4952 | type = htx_get_blk_type(blk); // DATA or EOM |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4953 | bsize = htx_get_blksz(blk); |
| 4954 | fsize = bsize; |
| 4955 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4956 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 4957 | if (h2s->flags & H2_SF_ES_SENT) { |
| 4958 | /* ES already sent */ |
| 4959 | htx_remove_blk(htx, blk); |
| 4960 | total++; // EOM counts as one byte |
| 4961 | count--; |
| 4962 | goto end; |
| 4963 | } |
| 4964 | } |
| 4965 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4966 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4967 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 4968 | mbuf = br_tail(h2c->mbuf); |
| 4969 | retry: |
| 4970 | if (!h2_get_buf(h2c, mbuf)) { |
| 4971 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4972 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4973 | goto end; |
| 4974 | } |
| 4975 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4976 | /* Perform some optimizations to reduce the number of buffer copies. |
| 4977 | * First, if the mux's buffer is empty and the htx area contains |
| 4978 | * exactly one data block of the same size as the requested count, and |
| 4979 | * this count fits within the frame size, the stream's window size, and |
| 4980 | * the connection's window size, then it's possible to simply swap the |
| 4981 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 4982 | * length to match the entire DATA HTX block in the middle. In this |
| 4983 | * case we perform a true zero-copy operation from end-to-end. This is |
| 4984 | * the situation that happens all the time with large files. Second, if |
| 4985 | * this is not possible, but the mux's output buffer is empty, we still |
| 4986 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 4987 | * making the intermediary buffer's area point to the output buffer's |
| 4988 | * area. In this case we want to skip the HTX header to make sure that |
| 4989 | * copies remain aligned and that this operation remains possible all |
| 4990 | * the time. This goes for headers, data blocks and any data extracted |
| 4991 | * from the HTX blocks. |
| 4992 | */ |
| 4993 | if (unlikely(fsize == count && |
| 4994 | htx->used == 1 && type == HTX_BLK_DATA && |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 4995 | fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4996 | void *old_area = mbuf->area; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4997 | |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 4998 | if (b_data(mbuf)) { |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4999 | /* Too bad there are data left there. We're willing to memcpy/memmove |
| 5000 | * up to 1/4 of the buffer, which means that it's OK to copy a large |
| 5001 | * frame into a buffer containing few data if it needs to be realigned, |
| 5002 | * and that it's also OK to copy few data without realigning. Otherwise |
| 5003 | * 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] | 5004 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5005 | if (fsize + 9 <= b_room(mbuf) && |
| 5006 | (b_data(mbuf) <= b_size(mbuf) / 4 || |
| 5007 | (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5008 | goto copy; |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 5009 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5010 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5011 | goto retry; |
| 5012 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5013 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5014 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5015 | goto end; |
| 5016 | } |
| 5017 | |
| 5018 | /* map an H2 frame to the HTX block so that we can put the |
| 5019 | * frame header there. |
| 5020 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5021 | *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9); |
| 5022 | outbuf.area = b_head(mbuf); |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5023 | |
| 5024 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 5025 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 5026 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5027 | h2_set_frame_size(outbuf.area, fsize); |
| 5028 | |
| 5029 | /* update windows */ |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5030 | h2s->sws -= fsize; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5031 | h2c->mws -= fsize; |
| 5032 | |
| 5033 | /* and exchange with our old area */ |
| 5034 | buf->area = old_area; |
| 5035 | buf->data = buf->head = 0; |
| 5036 | total += fsize; |
| 5037 | goto end; |
| 5038 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 5039 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5040 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5041 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 5042 | |
| 5043 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5044 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 5045 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5046 | break; |
| 5047 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5048 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5049 | } |
| 5050 | |
| 5051 | if (outbuf.size < 9) { |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5052 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5053 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5054 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5055 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5056 | goto end; |
| 5057 | } |
| 5058 | |
| 5059 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 5060 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 5061 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5062 | outbuf.data = 9; |
| 5063 | |
| 5064 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 5065 | * the HTX buffer. We need to check this against the connection's and |
| 5066 | * the stream's send windows, and to ensure that this fits in the max |
| 5067 | * frame size and in the buffer's available space minus 9 bytes (for |
| 5068 | * the frame header). The connection's flow control is applied last so |
| 5069 | * that we can use a separate list of streams which are immediately |
| 5070 | * unblocked on window opening. Note: we don't implement padding. |
| 5071 | */ |
| 5072 | |
| 5073 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 5074 | * empty frame, thus we force it to zero here. |
| 5075 | */ |
| 5076 | if (type == HTX_BLK_EOM) |
| 5077 | bsize = fsize = 0; |
| 5078 | |
| 5079 | if (!fsize) |
| 5080 | goto send_empty; |
| 5081 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5082 | if (h2s_mws(h2s) <= 0) { |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5083 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 5084 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 5085 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5086 | goto end; |
| 5087 | } |
| 5088 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5089 | if (fsize > count) |
| 5090 | fsize = count; |
| 5091 | |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5092 | if (fsize > h2s_mws(h2s)) |
| 5093 | fsize = h2s_mws(h2s); // >0 |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5094 | |
| 5095 | if (h2c->mfs && fsize > h2c->mfs) |
| 5096 | fsize = h2c->mfs; // >0 |
| 5097 | |
| 5098 | if (fsize + 9 > outbuf.size) { |
Willy Tarreau | 455d568 | 2019-05-24 19:42:18 +0200 | [diff] [blame] | 5099 | /* It doesn't fit at once. If it at least fits once split and |
| 5100 | * the amount of data to move is low, let's defragment the |
| 5101 | * buffer now. |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5102 | */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5103 | if (b_space_wraps(mbuf) && |
| 5104 | (fsize + 9 <= b_room(mbuf)) && |
| 5105 | b_data(mbuf) <= MAX_DATA_REALIGN) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5106 | goto realign_again; |
| 5107 | fsize = outbuf.size - 9; |
| 5108 | |
| 5109 | if (fsize <= 0) { |
| 5110 | /* no need to send an empty frame here */ |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5111 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5112 | goto retry; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5113 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5114 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5115 | goto end; |
| 5116 | } |
| 5117 | } |
| 5118 | |
| 5119 | if (h2c->mws <= 0) { |
| 5120 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 5121 | goto end; |
| 5122 | } |
| 5123 | |
| 5124 | if (fsize > h2c->mws) |
| 5125 | fsize = h2c->mws; |
| 5126 | |
| 5127 | /* now let's copy this this into the output buffer */ |
| 5128 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 5a9c875 | 2019-08-02 07:52:08 +0200 | [diff] [blame] | 5129 | h2s->sws -= fsize; |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 5130 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5131 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5132 | |
| 5133 | send_empty: |
| 5134 | /* update the frame's size */ |
| 5135 | h2_set_frame_size(outbuf.area, fsize); |
| 5136 | |
| 5137 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 5138 | * meeting EOM. We should optimize this later. |
| 5139 | */ |
| 5140 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 5141 | total++; // EOM counts as one byte |
| 5142 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5143 | es_now = 1; |
| 5144 | } |
| 5145 | |
| 5146 | if (es_now) |
| 5147 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 5148 | |
| 5149 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5150 | b_add(mbuf, fsize + 9); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5151 | |
| 5152 | /* consume incoming HTX block, including EOM */ |
| 5153 | total += fsize; |
| 5154 | if (fsize == bsize) { |
| 5155 | htx_remove_blk(htx, blk); |
| 5156 | if (fsize) |
| 5157 | goto new_frame; |
| 5158 | } else { |
| 5159 | /* we've truncated this block */ |
| 5160 | htx_cut_data_blk(htx, blk, fsize); |
| 5161 | } |
| 5162 | |
| 5163 | if (es_now) { |
| 5164 | if (h2s->st == H2_SS_OPEN) |
| 5165 | h2s->st = H2_SS_HLOC; |
| 5166 | else |
| 5167 | h2s_close(h2s); |
| 5168 | |
| 5169 | h2s->flags |= H2_SF_ES_SENT; |
| 5170 | } |
| 5171 | |
| 5172 | end: |
| 5173 | return total; |
| 5174 | } |
| 5175 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5176 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 5177 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 5178 | * processed. The caller must check the stream's status to detect any error |
| 5179 | * which might have happened subsequently to a successful send. The htx blocks |
| 5180 | * are automatically removed from the message. The htx message is assumed to be |
| 5181 | * valid since produced from the internal code. Processing stops when meeting |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5182 | * the EOM, which is *not* removed. All trailers are processed at once and sent |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5183 | * as a single frame. The ES flag is always set. |
| 5184 | */ |
| 5185 | static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx) |
| 5186 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 5187 | struct http_hdr list[global.tune.max_http_hdr]; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5188 | struct h2c *h2c = h2s->h2c; |
| 5189 | struct htx_blk *blk; |
| 5190 | struct htx_blk *blk_end; |
| 5191 | struct buffer outbuf; |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5192 | struct buffer *mbuf; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5193 | enum htx_blk_type type; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5194 | int ret = 0; |
| 5195 | int hdr; |
| 5196 | int idx; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5197 | |
| 5198 | if (h2c_mux_busy(h2c, h2s)) { |
| 5199 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 5200 | goto end; |
| 5201 | } |
| 5202 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5203 | /* determine the first block which must not be deleted, blk_end may |
| 5204 | * be NULL if all blocks have to be deleted. also get trailers. |
| 5205 | */ |
| 5206 | idx = htx_get_head(htx); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5207 | blk_end = NULL; |
| 5208 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5209 | hdr = 0; |
| 5210 | while (idx != -1) { |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5211 | blk = htx_get_blk(htx, idx); |
| 5212 | type = htx_get_blk_type(blk); |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5213 | idx = htx_get_next(htx, idx); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5214 | if (type == HTX_BLK_UNUSED) |
| 5215 | continue; |
| 5216 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5217 | if (type == HTX_BLK_EOT) { |
| 5218 | if (idx != -1) |
| 5219 | blk_end = blk; |
| 5220 | break; |
| 5221 | } |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5222 | if (type != HTX_BLK_TLR) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5223 | break; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5224 | |
| 5225 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 5226 | goto fail; |
| 5227 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5228 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 5229 | list[hdr].v = htx_get_blk_value(htx, blk); |
| 5230 | hdr++; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5231 | } |
| 5232 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5233 | /* marker for end of trailers */ |
| 5234 | list[hdr].n = ist(""); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5235 | |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5236 | mbuf = br_tail(h2c->mbuf); |
| 5237 | retry: |
| 5238 | if (!h2_get_buf(h2c, mbuf)) { |
| 5239 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 5240 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5241 | goto end; |
| 5242 | } |
| 5243 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5244 | chunk_reset(&outbuf); |
| 5245 | |
| 5246 | while (1) { |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5247 | outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0); |
| 5248 | if (outbuf.size >= 9 || !b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5249 | break; |
| 5250 | realign_again: |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5251 | b_slow_realign(mbuf, trash.area, b_data(mbuf)); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5252 | } |
| 5253 | |
| 5254 | if (outbuf.size < 9) |
| 5255 | goto full; |
| 5256 | |
| 5257 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 5258 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 5259 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5260 | outbuf.data = 9; |
| 5261 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5262 | /* encode all headers */ |
| 5263 | for (idx = 0; idx < hdr; idx++) { |
| 5264 | /* these ones do not exist in H2 or must not appear in |
| 5265 | * trailers and must be dropped. |
| 5266 | */ |
| 5267 | if (isteq(list[idx].n, ist("host")) || |
| 5268 | isteq(list[idx].n, ist("content-length")) || |
| 5269 | isteq(list[idx].n, ist("connection")) || |
| 5270 | isteq(list[idx].n, ist("proxy-connection")) || |
| 5271 | isteq(list[idx].n, ist("keep-alive")) || |
| 5272 | isteq(list[idx].n, ist("upgrade")) || |
| 5273 | isteq(list[idx].n, ist("te")) || |
| 5274 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 5275 | continue; |
| 5276 | |
| 5277 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 5278 | /* output full */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5279 | if (b_space_wraps(mbuf)) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5280 | goto realign_again; |
| 5281 | goto full; |
| 5282 | } |
| 5283 | } |
| 5284 | |
Willy Tarreau | 5121e5d | 2019-05-06 15:13:41 +0200 | [diff] [blame] | 5285 | if (outbuf.data == 9) { |
| 5286 | /* here we have a problem, we have nothing to emit (either we |
| 5287 | * received an empty trailers block followed or we removed its |
| 5288 | * contents above). Because of this we can't send a HEADERS |
| 5289 | * frame, so we have to cheat and instead send an empty DATA |
| 5290 | * frame conveying the ES flag. |
Willy Tarreau | 67b8cae | 2019-02-21 18:16:35 +0100 | [diff] [blame] | 5291 | */ |
| 5292 | outbuf.area[3] = H2_FT_DATA; |
| 5293 | outbuf.area[4] = H2_F_DATA_END_STREAM; |
| 5294 | } |
| 5295 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5296 | /* update the frame's size */ |
| 5297 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 5298 | |
| 5299 | /* commit the H2 response */ |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5300 | b_add(mbuf, outbuf.data); |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5301 | h2s->flags |= H2_SF_ES_SENT; |
| 5302 | |
| 5303 | if (h2s->st == H2_SS_OPEN) |
| 5304 | h2s->st = H2_SS_HLOC; |
| 5305 | else |
| 5306 | h2s_close(h2s); |
| 5307 | |
| 5308 | /* OK we could properly deliver the response */ |
| 5309 | done: |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5310 | /* remove all header blocks till the end and compute the corresponding size. */ |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5311 | ret = 0; |
| 5312 | idx = htx_get_head(htx); |
| 5313 | blk = htx_get_blk(htx, idx); |
| 5314 | while (blk != blk_end) { |
| 5315 | ret += htx_get_blksz(blk); |
| 5316 | blk = htx_remove_blk(htx, blk); |
| 5317 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5318 | |
| 5319 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) { |
| 5320 | ret += htx_get_blksz(blk_end); |
| 5321 | htx_remove_blk(htx, blk_end); |
| 5322 | } |
| 5323 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5324 | end: |
| 5325 | return ret; |
| 5326 | full: |
Willy Tarreau | 9c218e7 | 2019-05-26 10:08:28 +0200 | [diff] [blame] | 5327 | if ((mbuf = br_tail_add(h2c->mbuf)) != NULL) |
| 5328 | goto retry; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5329 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5330 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5331 | ret = 0; |
| 5332 | goto end; |
| 5333 | fail: |
| 5334 | /* unparsable HTX messages, too large ones to be produced in the local |
| 5335 | * list etc go here (unrecoverable errors). |
| 5336 | */ |
| 5337 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5338 | ret = 0; |
| 5339 | goto end; |
| 5340 | } |
| 5341 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5342 | /* Called from the upper layer, to subscribe to events, such as being able to send. |
| 5343 | * The <param> argument here is supposed to be a pointer to a wait_event struct |
| 5344 | * which will be passed to h2s->recv_wait or h2s->send_wait depending on the |
| 5345 | * event_type. The event_type must only be a combination of SUB_RETRY_RECV and |
| 5346 | * SUB_RETRY_SEND, other values will lead to -1 being returned. It always |
| 5347 | * returns 0 except for the error above. |
| 5348 | */ |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5349 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 5350 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5351 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5352 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5353 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5354 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5355 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5356 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5357 | BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV)); |
| 5358 | sw->events |= SUB_RETRY_RECV; |
| 5359 | h2s->recv_wait = sw; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5360 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5361 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5362 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5363 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5364 | BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND)); |
| 5365 | sw->events |= SUB_RETRY_SEND; |
| 5366 | h2s->send_wait = sw; |
| 5367 | if (!(h2s->flags & H2_SF_BLK_SFCTL) && |
| 5368 | !LIST_ADDED(&h2s->list)) { |
| 5369 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 5370 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 5371 | else |
| 5372 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 5373 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5374 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5375 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5376 | if (event_type != 0) |
| 5377 | return -1; |
| 5378 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5379 | } |
| 5380 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5381 | /* Called from the upper layer, to unsubscribe some events (undo h2_subscribe). |
| 5382 | * The <param> argument here is supposed to be a pointer to the same wait_event |
| 5383 | * struct that was passed to h2_subscribe() otherwise nothing will be changed. |
| 5384 | * It always returns zero. |
| 5385 | */ |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5386 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 5387 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5388 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5389 | struct h2s *h2s = cs->ctx; |
| 5390 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5391 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5392 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5393 | BUG_ON(h2s->recv_wait != sw); |
| 5394 | sw->events &= ~SUB_RETRY_RECV; |
| 5395 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5396 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5397 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5398 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5399 | BUG_ON(h2s->send_wait != sw); |
| 5400 | LIST_DEL(&h2s->list); |
| 5401 | LIST_INIT(&h2s->list); |
| 5402 | sw->events &= ~SUB_RETRY_SEND; |
| 5403 | /* We were about to send, make sure it does not happen */ |
| 5404 | if (LIST_ADDED(&h2s->sending_list) && |
| 5405 | h2s->send_wait != &h2s->wait_event) { |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 5406 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5407 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5408 | } |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5409 | h2s->send_wait = NULL; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5410 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5411 | return 0; |
| 5412 | } |
| 5413 | |
| 5414 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5415 | /* Called from the upper layer, to receive data */ |
| 5416 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 5417 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5418 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5419 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5420 | struct htx *h2s_htx = NULL; |
| 5421 | struct htx *buf_htx = NULL; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5422 | size_t ret = 0; |
| 5423 | |
| 5424 | /* transfer possibly pending data to the upper layer */ |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5425 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5426 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5427 | if (htx_is_empty(h2s_htx)) { |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5428 | /* Here htx_to_buf() will set buffer data to 0 because |
| 5429 | * the HTX is empty. |
| 5430 | */ |
| 5431 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5432 | goto end; |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5433 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5434 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5435 | ret = h2s_htx->data; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5436 | buf_htx = htx_from_buf(buf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5437 | |
Christopher Faulet | 2f6edc8 | 2019-05-15 10:07:59 +0200 | [diff] [blame] | 5438 | /* <buf> is empty and the message is small enough, swap the |
| 5439 | * buffers. */ |
| 5440 | if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) { |
| 5441 | htx_to_buf(buf_htx, buf); |
| 5442 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
| 5443 | b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf)); |
| 5444 | goto end; |
| 5445 | } |
| 5446 | |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5447 | htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM); |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5448 | |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 5449 | if (h2s_htx->flags & HTX_FL_PARSING_ERROR) { |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5450 | buf_htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | a61e97b | 2019-05-16 11:30:31 +0200 | [diff] [blame] | 5451 | if (htx_is_empty(buf_htx)) |
| 5452 | cs->flags |= CS_FL_EOI; |
| 5453 | } |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5454 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 5455 | 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] | 5456 | htx_to_buf(buf_htx, buf); |
| 5457 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Christopher Faulet | 156852b | 2019-05-16 11:29:13 +0200 | [diff] [blame] | 5458 | ret -= h2s_htx->data; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5459 | } |
| 5460 | else { |
| 5461 | ret = b_xfer(buf, &h2s->rxbuf, count); |
| 5462 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5463 | |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5464 | end: |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5465 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5466 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5467 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5468 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 5469 | if (h2s->flags & H2_SF_ES_RCVD) |
| 5470 | cs->flags |= CS_FL_EOI; |
Willy Tarreau | 76c8382 | 2019-06-15 09:55:50 +0200 | [diff] [blame] | 5471 | if (conn_xprt_read0_pending(h2c->conn) || h2s->st == H2_SS_CLOSED) |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5472 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5473 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5474 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5475 | if (b_size(&h2s->rxbuf)) { |
| 5476 | b_free(&h2s->rxbuf); |
| 5477 | offer_buffers(NULL, tasks_run_queue); |
| 5478 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5479 | } |
| 5480 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5481 | if (ret && h2c->dsi == h2s->id) { |
| 5482 | /* demux is blocking on this stream's buffer */ |
| 5483 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 5484 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5485 | } |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5486 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5487 | return ret; |
| 5488 | } |
| 5489 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5490 | /* stops all senders of this connection for example when the mux buffer is full. |
| 5491 | * They are moved from the sending_list to either fctl_list or send_list. |
| 5492 | */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5493 | static void h2_stop_senders(struct h2c *h2c) |
| 5494 | { |
| 5495 | struct h2s *h2s, *h2s_back; |
| 5496 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5497 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) { |
| 5498 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 86eded6 | 2019-06-14 14:47:49 +0200 | [diff] [blame] | 5499 | tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5500 | h2s->send_wait->events |= SUB_RETRY_SEND; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5501 | } |
| 5502 | } |
| 5503 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5504 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 5505 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 5506 | * flags may be updated on the conn_stream. |
| 5507 | */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 5508 | 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] | 5509 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5510 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5511 | size_t orig_count = count; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 5512 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5513 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5514 | struct htx *htx; |
| 5515 | struct htx_blk *blk; |
| 5516 | enum htx_blk_type btype; |
| 5517 | uint32_t bsize; |
| 5518 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5519 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5520 | /* If we were not just woken because we wanted to send but couldn't, |
| 5521 | * and there's somebody else that is waiting to send, do nothing, |
| 5522 | * we will subscribe later and be put at the end of the list |
| 5523 | */ |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 5524 | if (!LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5525 | (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) |
| 5526 | return 0; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5527 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5528 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5529 | /* We couldn't set it to NULL before, because we needed it in case |
| 5530 | * we had to cancel the tasklet |
| 5531 | */ |
| 5532 | h2s->send_wait = NULL; |
| 5533 | |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 5534 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 5535 | return 0; |
| 5536 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5537 | /* htx will be enough to decide if we're using HTX or legacy */ |
| 5538 | htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL; |
| 5539 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5540 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 5541 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 5542 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5543 | if (h2s->id == 0) { |
| 5544 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 5545 | |
| 5546 | if (id < 0) { |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5547 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5548 | return 0; |
| 5549 | } |
| 5550 | |
| 5551 | eb32_delete(&h2s->by_id); |
| 5552 | h2s->by_id.key = h2s->id = id; |
| 5553 | h2s->h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 5554 | h2s->h2c->nb_reserved--; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5555 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 5556 | } |
| 5557 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5558 | if (htx) { |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5559 | while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) && |
Willy Tarreau | c14999b | 2018-12-06 14:09:09 +0100 | [diff] [blame] | 5560 | count && !htx_is_empty(htx)) { |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5561 | idx = htx_get_head(htx); |
| 5562 | blk = htx_get_blk(htx, idx); |
| 5563 | btype = htx_get_blk_type(blk); |
| 5564 | bsize = htx_get_blksz(blk); |
| 5565 | |
| 5566 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5567 | case HTX_BLK_REQ_SL: |
| 5568 | /* start-line before headers */ |
| 5569 | ret = h2s_htx_bck_make_req_headers(h2s, htx); |
| 5570 | if (ret > 0) { |
| 5571 | total += ret; |
| 5572 | count -= ret; |
| 5573 | if (ret < bsize) |
| 5574 | goto done; |
| 5575 | } |
| 5576 | break; |
| 5577 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 5578 | case HTX_BLK_RES_SL: |
| 5579 | /* start-line before headers */ |
| 5580 | ret = h2s_htx_frt_make_resp_headers(h2s, htx); |
| 5581 | if (ret > 0) { |
| 5582 | total += ret; |
| 5583 | count -= ret; |
| 5584 | if (ret < bsize) |
| 5585 | goto done; |
| 5586 | } |
| 5587 | break; |
| 5588 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5589 | case HTX_BLK_DATA: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5590 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5591 | /* all these cause the emission of a DATA frame (possibly empty). |
| 5592 | * This EOM necessarily is one before trailers, as the EOM following |
| 5593 | * trailers would have been consumed by the trailers parser. |
| 5594 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5595 | ret = h2s_htx_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5596 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5597 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5598 | total += ret; |
| 5599 | count -= ret; |
| 5600 | if (ret < bsize) |
| 5601 | goto done; |
| 5602 | } |
| 5603 | break; |
| 5604 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5605 | case HTX_BLK_TLR: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 5606 | case HTX_BLK_EOT: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5607 | /* This is the first trailers block, all the subsequent ones AND |
| 5608 | * the EOM will be swallowed by the parser. |
| 5609 | */ |
| 5610 | ret = h2s_htx_make_trailers(h2s, htx); |
| 5611 | if (ret > 0) { |
| 5612 | total += ret; |
| 5613 | count -= ret; |
| 5614 | if (ret < bsize) |
| 5615 | goto done; |
| 5616 | } |
| 5617 | break; |
| 5618 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5619 | default: |
| 5620 | htx_remove_blk(htx, blk); |
| 5621 | total += bsize; |
| 5622 | count -= bsize; |
| 5623 | break; |
| 5624 | } |
| 5625 | } |
| 5626 | goto done; |
| 5627 | } |
| 5628 | |
| 5629 | /* legacy transfer mode */ |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5630 | while (h2s->h1m.state < H1_MSG_DONE && count) { |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 5631 | if (h2s->h1m.state <= H1_MSG_LAST_LF) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5632 | if (h2s->h2c->flags & H2_CF_IS_BACK) |
| 5633 | ret = -1; |
| 5634 | else |
| 5635 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5636 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5637 | else if (h2s->h1m.state < H1_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5638 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5639 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5640 | else if (h2s->h1m.state == H1_MSG_TRAILERS) { |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5641 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5642 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5643 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5644 | if (unlikely((int)ret <= 0)) { |
| 5645 | if ((int)ret < 0) |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5646 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5647 | break; |
| 5648 | } |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 5649 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5650 | total += count; |
| 5651 | count = 0; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5652 | h2s->h1m.state = H1_MSG_DONE; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5653 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 5654 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5655 | else { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5656 | cs_set_error(cs); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5657 | break; |
| 5658 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5659 | |
| 5660 | total += ret; |
| 5661 | count -= ret; |
| 5662 | |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5663 | if (h2s->st >= H2_SS_HLOC) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5664 | break; |
| 5665 | |
| 5666 | if (h2s->flags & H2_SF_BLK_ANY) |
| 5667 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5668 | } |
| 5669 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5670 | done: |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5671 | if (h2s->st >= H2_SS_HLOC) { |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5672 | /* trim any possibly pending data after we close (extra CR-LF, |
| 5673 | * unprocessed trailers, abnormal extra data, ...) |
| 5674 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5675 | total += count; |
| 5676 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5677 | } |
| 5678 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5679 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 5680 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5681 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 5682 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 5683 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5684 | } |
| 5685 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5686 | if (htx) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5687 | htx_to_buf(htx, buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5688 | } else { |
| 5689 | b_del(buf, total); |
| 5690 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5691 | |
| 5692 | /* The mux is full, cancel the pending tasks */ |
| 5693 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 5694 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 5695 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5696 | |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5697 | /* If we're running HTX, and we read the whole buffer, then pretend |
| 5698 | * we read exactly what the caller specified, as with HTX the caller |
| 5699 | * will always give the buffer size, instead of the amount of data |
| 5700 | * available. |
| 5701 | */ |
| 5702 | if (htx && !b_data(buf)) |
| 5703 | total = orig_count; |
| 5704 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5705 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5706 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5707 | tasklet_wakeup(h2s->h2c->wait_event.tasklet); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5708 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5709 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 5710 | /* If we're waiting for flow control, and we got a shutr on the |
| 5711 | * connection, we will never be unlocked, so add an error on |
| 5712 | * the conn_stream. |
| 5713 | */ |
| 5714 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 5715 | !b_data(&h2s->h2c->dbuf) && |
| 5716 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 5717 | if (cs->flags & CS_FL_EOS) |
| 5718 | cs->flags |= CS_FL_ERROR; |
| 5719 | else |
| 5720 | cs->flags |= CS_FL_ERR_PENDING; |
| 5721 | } |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5722 | if (total > 0) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5723 | /* Ok we managed to send something, leave the send_list */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5724 | LIST_DEL_INIT(&h2s->list); |
| 5725 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5726 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5727 | } |
| 5728 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5729 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5730 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5731 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5732 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5733 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5734 | struct eb32_node *node; |
| 5735 | int fctl_cnt = 0; |
| 5736 | int send_cnt = 0; |
| 5737 | int tree_cnt = 0; |
| 5738 | int orph_cnt = 0; |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5739 | struct buffer *hmbuf, *tmbuf; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5740 | |
| 5741 | if (!h2c) |
| 5742 | return; |
| 5743 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5744 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5745 | fctl_cnt++; |
| 5746 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5747 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5748 | send_cnt++; |
| 5749 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5750 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5751 | node = eb32_first(&h2c->streams_by_id); |
| 5752 | while (node) { |
| 5753 | h2s = container_of(node, struct h2s, by_id); |
| 5754 | tree_cnt++; |
| 5755 | if (!h2s->cs) |
| 5756 | orph_cnt++; |
| 5757 | node = eb32_next(node); |
| 5758 | } |
| 5759 | |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5760 | hmbuf = br_head(h2c->mbuf); |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5761 | tmbuf = br_tail(h2c->mbuf); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5762 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5763 | " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d" |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5764 | " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d" |
| 5765 | " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]", |
Willy Tarreau | 616ac81 | 2018-07-24 14:12:42 +0200 | [diff] [blame] | 5766 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5767 | 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] | 5768 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5769 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5770 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5771 | h2c->msi, |
Willy Tarreau | 60f6268 | 2019-05-26 11:32:27 +0200 | [diff] [blame] | 5772 | br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf), |
| 5773 | (unsigned int)b_data(hmbuf), b_orig(hmbuf), |
| 5774 | (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf), |
Willy Tarreau | bcc4595 | 2019-05-26 10:05:50 +0200 | [diff] [blame] | 5775 | (unsigned int)b_data(tmbuf), b_orig(tmbuf), |
| 5776 | (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf)); |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5777 | |
| 5778 | if (h2s) { |
| 5779 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5780 | h2s, h2s->id, h2s->flags, |
| 5781 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5782 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5783 | h2s->cs); |
| 5784 | if (h2s->cs) |
| 5785 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5786 | h2s->cs->flags, h2s->cs->data); |
| 5787 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5788 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5789 | |
| 5790 | /*******************************************************/ |
| 5791 | /* functions below are dedicated to the config parsers */ |
| 5792 | /*******************************************************/ |
| 5793 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5794 | /* config parser for global "tune.h2.header-table-size" */ |
| 5795 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5796 | struct proxy *defpx, const char *file, int line, |
| 5797 | char **err) |
| 5798 | { |
| 5799 | if (too_many_args(1, args, err, NULL)) |
| 5800 | return -1; |
| 5801 | |
| 5802 | h2_settings_header_table_size = atoi(args[1]); |
| 5803 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5804 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5805 | return -1; |
| 5806 | } |
| 5807 | return 0; |
| 5808 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5809 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5810 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5811 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5812 | struct proxy *defpx, const char *file, int line, |
| 5813 | char **err) |
| 5814 | { |
| 5815 | if (too_many_args(1, args, err, NULL)) |
| 5816 | return -1; |
| 5817 | |
| 5818 | h2_settings_initial_window_size = atoi(args[1]); |
| 5819 | if (h2_settings_initial_window_size < 0) { |
| 5820 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5821 | return -1; |
| 5822 | } |
| 5823 | return 0; |
| 5824 | } |
| 5825 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5826 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5827 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5828 | struct proxy *defpx, const char *file, int line, |
| 5829 | char **err) |
| 5830 | { |
| 5831 | if (too_many_args(1, args, err, NULL)) |
| 5832 | return -1; |
| 5833 | |
| 5834 | h2_settings_max_concurrent_streams = atoi(args[1]); |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 5835 | if ((int)h2_settings_max_concurrent_streams < 0) { |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5836 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5837 | return -1; |
| 5838 | } |
| 5839 | return 0; |
| 5840 | } |
| 5841 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5842 | /* config parser for global "tune.h2.max-frame-size" */ |
| 5843 | static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx, |
| 5844 | struct proxy *defpx, const char *file, int line, |
| 5845 | char **err) |
| 5846 | { |
| 5847 | if (too_many_args(1, args, err, NULL)) |
| 5848 | return -1; |
| 5849 | |
| 5850 | h2_settings_max_frame_size = atoi(args[1]); |
| 5851 | if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) { |
| 5852 | memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]); |
| 5853 | return -1; |
| 5854 | } |
| 5855 | return 0; |
| 5856 | } |
| 5857 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5858 | |
| 5859 | /****************************************/ |
| 5860 | /* MUX initialization and instanciation */ |
| 5861 | /***************************************/ |
| 5862 | |
| 5863 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5864 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5865 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5866 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5867 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5868 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5869 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5870 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5871 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5872 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5873 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5874 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5875 | .avail_streams = h2_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 5876 | .used_streams = h2_used_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5877 | .shutr = h2_shutr, |
| 5878 | .shutw = h2_shutw, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5879 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 5880 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5881 | .name = "H2", |
| 5882 | }; |
| 5883 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5884 | /* PROTO selection : this mux registers PROTO token "h2" */ |
| 5885 | static struct mux_proto_list mux_proto_h2 = |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5886 | { .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] | 5887 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5888 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 5889 | |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5890 | |
| 5891 | /* The mux operations */ |
| 5892 | static const struct mux_ops h2_htx_ops = { |
| 5893 | .init = h2_init, |
| 5894 | .wake = h2_wake, |
| 5895 | .snd_buf = h2_snd_buf, |
| 5896 | .rcv_buf = h2_rcv_buf, |
| 5897 | .subscribe = h2_subscribe, |
| 5898 | .unsubscribe = h2_unsubscribe, |
| 5899 | .attach = h2_attach, |
| 5900 | .get_first_cs = h2_get_first_cs, |
| 5901 | .detach = h2_detach, |
| 5902 | .destroy = h2_destroy, |
| 5903 | .avail_streams = h2_avail_streams, |
| 5904 | .used_streams = h2_used_streams, |
| 5905 | .shutr = h2_shutr, |
| 5906 | .shutw = h2_shutw, |
| 5907 | .show_fd = h2_show_fd, |
Christopher Faulet | 9f38f5a | 2019-04-03 09:53:32 +0200 | [diff] [blame] | 5908 | .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX, |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5909 | .name = "H2", |
| 5910 | }; |
| 5911 | |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5912 | static struct mux_proto_list mux_proto_h2_htx = |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5913 | { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops }; |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5914 | |
| 5915 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx); |
| 5916 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5917 | /* config keyword parsers */ |
| 5918 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5919 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5920 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5921 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5922 | { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5923 | { 0, NULL, NULL } |
| 5924 | }}; |
| 5925 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5926 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |