Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP/2 mux-demux for connections |
| 3 | * |
| 4 | * Copyright 2017 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <common/cfgparse.h> |
| 14 | #include <common/config.h> |
Willy Tarreau | afba57a | 2018-12-11 13:44:24 +0100 | [diff] [blame] | 15 | #include <common/h1.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 16 | #include <common/h2.h> |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 17 | #include <common/hpack-dec.h> |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 18 | #include <common/hpack-enc.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 19 | #include <common/hpack-tbl.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 20 | #include <common/htx.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 21 | #include <common/initcall.h> |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 22 | #include <common/net_helper.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 23 | #include <proto/connection.h> |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 24 | #include <proto/http_htx.h> |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 25 | #include <proto/session.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 26 | #include <proto/stream.h> |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 27 | #include <proto/stream_interface.h> |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 28 | #include <types/session.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 29 | #include <eb32tree.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 30 | |
| 31 | |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 32 | /* dummy streams returned for closed, error, refused, idle and states */ |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 33 | static const struct h2s *h2_closed_stream; |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 34 | static const struct h2s *h2_error_stream; |
Willy Tarreau | 8d0d58b | 2018-12-23 18:29:12 +0100 | [diff] [blame] | 35 | static const struct h2s *h2_refused_stream; |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 36 | static const struct h2s *h2_idle_stream; |
| 37 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 38 | /* Connection flags (32 bit), in h2c->flags */ |
| 39 | #define H2_CF_NONE 0x00000000 |
| 40 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 41 | /* Flags indicating why writing to the mux is blocked. */ |
| 42 | #define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer |
| 43 | #define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full |
| 44 | #define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above |
| 45 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 46 | /* Flags indicating why writing to the demux is blocked. |
| 47 | * The first two ones directly affect the ability for the mux to receive data |
| 48 | * from the connection. The other ones affect the mux's ability to demux |
| 49 | * received data. |
| 50 | */ |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 51 | #define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer |
| 52 | #define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 53 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 54 | #define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy |
| 55 | #define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer |
| 56 | #define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer |
| 57 | #define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 58 | #define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave |
| 59 | #define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 60 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 61 | /* other flags */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 62 | #define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent |
| 63 | #define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent |
| 64 | #define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake |
Willy Tarreau | b3fb56d | 2018-10-03 13:56:38 +0200 | [diff] [blame] | 65 | #define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 66 | #define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 67 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 68 | /* H2 connection state, in h2c->st0 */ |
| 69 | enum h2_cs { |
| 70 | H2_CS_PREFACE, // init done, waiting for connection preface |
| 71 | H2_CS_SETTINGS1, // preface OK, waiting for first settings frame |
| 72 | H2_CS_FRAME_H, // first settings frame ok, waiting for frame header |
| 73 | H2_CS_FRAME_P, // frame header OK, waiting for frame payload |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 74 | H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame |
| 75 | H2_CS_FRAME_E, // frame payload OK, trying to send RST frame |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 76 | H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP |
| 77 | H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP |
| 78 | H2_CS_ENTRIES // must be last |
| 79 | } __attribute__((packed)); |
| 80 | |
| 81 | /* H2 connection descriptor */ |
| 82 | struct h2c { |
| 83 | struct connection *conn; |
| 84 | |
| 85 | enum h2_cs st0; /* mux state */ |
| 86 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 87 | |
| 88 | /* 16 bit hole here */ |
| 89 | uint32_t flags; /* connection flags: H2_CF_* */ |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 90 | uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 91 | int32_t max_id; /* highest ID known on this connection, <0 before preface */ |
| 92 | uint32_t rcvd_c; /* newly received data to ACK for the connection */ |
| 93 | uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */ |
| 94 | |
| 95 | /* states for the demux direction */ |
| 96 | struct hpack_dht *ddht; /* demux dynamic header table */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 97 | struct buffer dbuf; /* demux buffer */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 98 | |
| 99 | int32_t dsi; /* demux stream ID (<0 = idle) */ |
| 100 | int32_t dfl; /* demux frame length (if dsi >= 0) */ |
| 101 | int8_t dft; /* demux frame type (if dsi >= 0) */ |
| 102 | int8_t dff; /* demux frame flags (if dsi >= 0) */ |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 103 | uint8_t dpl; /* demux pad length (part of dfl), init to 0 */ |
| 104 | /* 8 bit hole here */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 105 | int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */ |
| 106 | |
| 107 | /* states for the mux direction */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 108 | struct buffer mbuf; /* mux buffer */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 109 | int32_t msi; /* mux stream ID (<0 = idle) */ |
| 110 | int32_t mfl; /* mux frame length (if dsi >= 0) */ |
| 111 | int8_t mft; /* mux frame type (if dsi >= 0) */ |
| 112 | int8_t mff; /* mux frame flags (if dsi >= 0) */ |
| 113 | /* 16 bit hole here */ |
| 114 | int32_t miw; /* mux initial window size for all new streams */ |
| 115 | int32_t mws; /* mux window size. Can be negative. */ |
| 116 | int32_t mfs; /* mux's max frame size */ |
| 117 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 118 | int timeout; /* idle timeout duration in ticks */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 119 | int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */ |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 120 | unsigned int nb_streams; /* number of streams in the tree */ |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 121 | unsigned int nb_cs; /* number of attached conn_streams */ |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 122 | unsigned int nb_reserved; /* number of reserved streams */ |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 123 | unsigned int stream_cnt; /* total number of streams seen */ |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 124 | struct proxy *proxy; /* the proxy this connection was created for */ |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 125 | struct task *task; /* timeout management task */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 126 | struct eb_root streams_by_id; /* all active streams by their ID */ |
| 127 | struct list send_list; /* list of blocked streams requesting to send */ |
| 128 | struct list fctl_list; /* list of streams blocked by connection's fctl */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 129 | struct list sending_list; /* list of h2s scheduled to send data */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 130 | struct buffer_wait buf_wait; /* wait list for buffer allocations */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 131 | struct wait_event wait_event; /* To be used if we're waiting for I/Os */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 132 | }; |
| 133 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 134 | /* H2 stream state, in h2s->st */ |
| 135 | enum h2_ss { |
| 136 | H2_SS_IDLE = 0, // idle |
| 137 | H2_SS_RLOC, // reserved(local) |
| 138 | H2_SS_RREM, // reserved(remote) |
| 139 | H2_SS_OPEN, // open |
| 140 | H2_SS_HREM, // half-closed(remote) |
| 141 | H2_SS_HLOC, // half-closed(local) |
Willy Tarreau | 96060ba | 2017-10-16 18:34:34 +0200 | [diff] [blame] | 142 | H2_SS_ERROR, // an error needs to be sent using RST_STREAM |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 143 | H2_SS_CLOSED, // closed |
| 144 | H2_SS_ENTRIES // must be last |
| 145 | } __attribute__((packed)); |
| 146 | |
Willy Tarreau | 4c688eb | 2019-05-14 11:44:03 +0200 | [diff] [blame] | 147 | #define H2_SS_MASK(state) (1UL << (state)) |
| 148 | #define H2_SS_IDLE_BIT (1UL << H2_SS_IDLE) |
| 149 | #define H2_SS_RLOC_BIT (1UL << H2_SS_RLOC) |
| 150 | #define H2_SS_RREM_BIT (1UL << H2_SS_RREM) |
| 151 | #define H2_SS_OPEN_BIT (1UL << H2_SS_OPEN) |
| 152 | #define H2_SS_HREM_BIT (1UL << H2_SS_HREM) |
| 153 | #define H2_SS_HLOC_BIT (1UL << H2_SS_HLOC) |
| 154 | #define H2_SS_ERROR_BIT (1UL << H2_SS_ERROR) |
| 155 | #define H2_SS_CLOSED_BIT (1UL << H2_SS_CLOSED) |
| 156 | #define H2_SS_EOS_BITS (H2_SS_CLOSED_BIT|H2_SS_ERROR_BIT|H2_SS_HREM_BIT) |
| 157 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 158 | /* HTTP/2 stream flags (32 bit), in h2s->flags */ |
| 159 | #define H2_SF_NONE 0x00000000 |
| 160 | #define H2_SF_ES_RCVD 0x00000001 |
| 161 | #define H2_SF_ES_SENT 0x00000002 |
| 162 | |
| 163 | #define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM |
| 164 | #define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM |
| 165 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 166 | /* stream flags indicating the reason the stream is blocked */ |
| 167 | #define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient) |
| 168 | #define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux |
| 169 | #define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl |
| 170 | #define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl |
| 171 | #define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above |
| 172 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 173 | /* stream flags indicating how data is supposed to be sent */ |
| 174 | #define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length |
| 175 | #define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding |
| 176 | |
| 177 | /* step we're currently in when sending chunks. This is needed because we may |
| 178 | * have to transfer chunks as large as a full buffer so there's no room left |
| 179 | * for size nor crlf around. |
| 180 | */ |
| 181 | #define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size |
| 182 | #define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data |
| 183 | #define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data |
| 184 | |
| 185 | #define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size |
| 186 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 187 | #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] | 188 | #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] | 189 | |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 190 | #define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream |
| 191 | |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 192 | #define H2_SF_WANT_SHUTR 0x00008000 // a stream couldn't shutr() (mux full/busy) |
| 193 | #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] | 194 | #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] | 195 | |
| 196 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 197 | /* H2 stream descriptor, describing the stream as it appears in the H2C, and as |
| 198 | * it is being processed in the internal HTTP representation (H1 for now). |
| 199 | */ |
| 200 | struct h2s { |
| 201 | struct conn_stream *cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 202 | struct session *sess; |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 203 | struct h2c *h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 204 | struct h1m h1m; /* request or response parser state for H1 */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 205 | struct eb32_node by_id; /* place in h2c's streams_by_id */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 206 | int32_t id; /* stream ID */ |
| 207 | uint32_t flags; /* H2_SF_* */ |
| 208 | int mws; /* mux window size for this stream */ |
| 209 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 210 | enum h2_ss st; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 211 | uint16_t status; /* HTTP response status */ |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 212 | 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] | 213 | struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 214 | 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] | 215 | struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */ |
| 216 | 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] | 217 | 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] | 218 | 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] | 219 | }; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 220 | |
Willy Tarreau | c640514 | 2017-09-21 20:23:50 +0200 | [diff] [blame] | 221 | /* descriptor for an h2 frame header */ |
| 222 | struct h2_fh { |
| 223 | uint32_t len; /* length, host order, 24 bits */ |
| 224 | uint32_t sid; /* stream id, host order, 31 bits */ |
| 225 | uint8_t ft; /* frame type */ |
| 226 | uint8_t ff; /* frame flags */ |
| 227 | }; |
| 228 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 229 | /* the h2c connection pool */ |
| 230 | DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c)); |
| 231 | |
| 232 | /* the h2s stream pool */ |
| 233 | DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s)); |
| 234 | |
Willy Tarreau | dc57236 | 2018-12-12 08:08:05 +0100 | [diff] [blame] | 235 | /* The default connection window size is 65535, it may only be enlarged using |
| 236 | * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1, |
| 237 | * we'll pretend we already received the difference between the two to send |
| 238 | * an equivalent window update to enlarge it to 2G-1. |
| 239 | */ |
| 240 | #define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535) |
| 241 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 242 | /* a few settings from the global section */ |
| 243 | static int h2_settings_header_table_size = 4096; /* initial value */ |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 244 | static int h2_settings_initial_window_size = 65535; /* initial value */ |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 245 | static unsigned int h2_settings_max_concurrent_streams = 100; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 246 | static int h2_settings_max_frame_size = 0; /* unset */ |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 247 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 248 | /* a dmumy closed stream */ |
| 249 | static const struct h2s *h2_closed_stream = &(const struct h2s){ |
| 250 | .cs = NULL, |
| 251 | .h2c = NULL, |
| 252 | .st = H2_SS_CLOSED, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 253 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 254 | .flags = H2_SF_RST_RCVD, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 255 | .id = 0, |
| 256 | }; |
| 257 | |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 258 | /* a dmumy closed stream returning a PROTOCOL_ERROR error */ |
| 259 | static const struct h2s *h2_error_stream = &(const struct h2s){ |
| 260 | .cs = NULL, |
| 261 | .h2c = NULL, |
| 262 | .st = H2_SS_CLOSED, |
| 263 | .errcode = H2_ERR_PROTOCOL_ERROR, |
| 264 | .flags = 0, |
| 265 | .id = 0, |
| 266 | }; |
| 267 | |
Willy Tarreau | 8d0d58b | 2018-12-23 18:29:12 +0100 | [diff] [blame] | 268 | /* a dmumy closed stream returning a REFUSED_STREAM error */ |
| 269 | static const struct h2s *h2_refused_stream = &(const struct h2s){ |
| 270 | .cs = NULL, |
| 271 | .h2c = NULL, |
| 272 | .st = H2_SS_CLOSED, |
| 273 | .errcode = H2_ERR_REFUSED_STREAM, |
| 274 | .flags = 0, |
| 275 | .id = 0, |
| 276 | }; |
| 277 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 278 | /* and a dummy idle stream for use with any unannounced stream */ |
| 279 | static const struct h2s *h2_idle_stream = &(const struct h2s){ |
| 280 | .cs = NULL, |
| 281 | .h2c = NULL, |
| 282 | .st = H2_SS_IDLE, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 283 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 284 | .id = 0, |
| 285 | }; |
| 286 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 287 | 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] | 288 | static int h2_send(struct h2c *h2c); |
| 289 | static int h2_recv(struct h2c *h2c); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 290 | static int h2_process(struct h2c *h2c); |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 291 | 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] | 292 | 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] | 293 | 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] | 294 | static int h2_frt_transfer_data(struct h2s *h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 295 | 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] | 296 | 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] | 297 | static void h2s_alert(struct h2s *h2s); |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 298 | |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 299 | static __inline int |
| 300 | h2c_is_dead(struct h2c *h2c) |
| 301 | { |
| 302 | if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */ |
| 303 | ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */ |
| 304 | (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */ |
| 305 | (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */ |
| 306 | (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */ |
| 307 | (conn_xprt_read0_pending(h2c->conn) || |
| 308 | (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) |
| 309 | return 1; |
| 310 | |
| 311 | return 0; |
| 312 | |
| 313 | } |
| 314 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 315 | /*****************************************************/ |
| 316 | /* functions below are for dynamic buffer management */ |
| 317 | /*****************************************************/ |
| 318 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 319 | /* indicates whether or not the we may call the h2_recv() function to attempt |
| 320 | * to receive data into the buffer and/or demux pending data. The condition is |
| 321 | * a bit complex due to some API limits for now. The rules are the following : |
| 322 | * - if an error or a shutdown was detected on the connection and the buffer |
| 323 | * is empty, we must not attempt to receive |
| 324 | * - if the demux buf failed to be allocated, we must not try to receive and |
| 325 | * we know there is nothing pending |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 326 | * - if no flag indicates a blocking condition, we may attempt to receive, |
| 327 | * regardless of whether the demux buffer is full or not, so that only |
| 328 | * de demux part decides whether or not to block. This is needed because |
| 329 | * the connection API indeed prevents us from re-enabling receipt that is |
| 330 | * already enabled in a polled state, so we must always immediately stop |
| 331 | * as soon as the demux can't proceed so as never to hit an end of read |
| 332 | * with data pending in the buffers. |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 333 | * - otherwise must may not attempt |
| 334 | */ |
| 335 | static inline int h2_recv_allowed(const struct h2c *h2c) |
| 336 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 337 | if (b_data(&h2c->dbuf) == 0 && |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 338 | (h2c->st0 >= H2_CS_ERROR || |
| 339 | h2c->conn->flags & CO_FL_ERROR || |
| 340 | conn_xprt_read0_pending(h2c->conn))) |
| 341 | return 0; |
| 342 | |
| 343 | if (!(h2c->flags & H2_CF_DEM_DALLOC) && |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 344 | !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 345 | return 1; |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 350 | /* restarts reading on the connection if it was not enabled */ |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 351 | 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] | 352 | { |
| 353 | if (!h2_recv_allowed(h2c)) |
| 354 | return; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 355 | if ((!consider_buffer || !b_data(&h2c->dbuf)) |
| 356 | && (h2c->wait_event.events & SUB_RETRY_RECV)) |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 357 | return; |
| 358 | tasklet_wakeup(h2c->wait_event.task); |
| 359 | } |
| 360 | |
| 361 | |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 362 | /* returns true if the front connection has too many conn_streams attached */ |
| 363 | 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] | 364 | { |
Willy Tarreau | a875466 | 2018-12-23 20:43:58 +0100 | [diff] [blame] | 365 | return h2c->nb_cs > h2_settings_max_concurrent_streams; |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 366 | } |
| 367 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 368 | /* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c |
| 369 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 370 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 371 | * impossible to wake up and we prefer to be woken up later. |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 372 | */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 373 | static int h2_buf_available(void *target) |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 374 | { |
| 375 | struct h2c *h2c = target; |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 376 | struct h2s *h2s; |
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 | 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] | 379 | h2c->flags &= ~H2_CF_DEM_DALLOC; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 380 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 381 | return 1; |
| 382 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 383 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 384 | if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) { |
| 385 | h2c->flags &= ~H2_CF_MUX_MALLOC; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 386 | |
| 387 | if (h2c->flags & H2_CF_DEM_MROOM) { |
| 388 | h2c->flags &= ~H2_CF_DEM_MROOM; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 389 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 390 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 391 | return 1; |
| 392 | } |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 393 | |
| 394 | if ((h2c->flags & H2_CF_DEM_SALLOC) && |
| 395 | (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs && |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 396 | b_alloc_margin(&h2s->rxbuf, 0)) { |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 397 | h2c->flags &= ~H2_CF_DEM_SALLOC; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 398 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 399 | return 1; |
| 400 | } |
| 401 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 402 | return 0; |
| 403 | } |
| 404 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 405 | 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] | 406 | { |
| 407 | struct buffer *buf = NULL; |
| 408 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 409 | if (likely(!LIST_ADDED(&h2c->buf_wait.list)) && |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 410 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 411 | h2c->buf_wait.target = h2c; |
| 412 | h2c->buf_wait.wakeup_cb = h2_buf_available; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 413 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 414 | LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 415 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 416 | __conn_xprt_stop_recv(h2c->conn); |
| 417 | } |
| 418 | return buf; |
| 419 | } |
| 420 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 421 | static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 422 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 423 | if (bptr->size) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 424 | b_free(bptr); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 425 | offer_buffers(h2c->buf_wait.target, tasks_run_queue); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 429 | /* returns the number of allocatable outgoing streams for the connection taking |
| 430 | * the last_sid and the reserved ones into account. |
| 431 | */ |
| 432 | static inline int h2_streams_left(const struct h2c *h2c) |
| 433 | { |
| 434 | int ret; |
| 435 | |
| 436 | /* consider the number of outgoing streams we're allowed to create before |
| 437 | * reaching the last GOAWAY frame seen. max_id is the last assigned id, |
| 438 | * nb_reserved is the number of streams which don't yet have an ID. |
| 439 | */ |
| 440 | ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF; |
| 441 | ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1; |
| 442 | if (ret < 0) |
| 443 | ret = 0; |
| 444 | return ret; |
| 445 | } |
| 446 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 447 | /* returns the number of streams in use on a connection to figure if it's |
| 448 | * idle or not. We check nb_cs and not nb_streams as the caller will want |
| 449 | * to know if it was the last one after a detach(). |
| 450 | */ |
| 451 | static int h2_used_streams(struct connection *conn) |
| 452 | { |
| 453 | struct h2c *h2c = conn->ctx; |
| 454 | |
| 455 | return h2c->nb_cs; |
| 456 | } |
| 457 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 458 | /* returns the number of concurrent streams available on the connection */ |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 459 | static int h2_avail_streams(struct connection *conn) |
| 460 | { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 461 | struct server *srv = objt_server(conn->target); |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 462 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 463 | int ret1, ret2; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 464 | |
Willy Tarreau | 6afec46 | 2019-01-28 06:40:19 +0100 | [diff] [blame] | 465 | /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional |
| 466 | * streams on the connection. |
| 467 | */ |
| 468 | if (h2c->last_sid >= 0) |
| 469 | return 0; |
| 470 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 471 | /* note: may be negative if a SETTINGS frame changes the limit */ |
| 472 | ret1 = h2c->streams_limit - h2c->nb_streams; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 473 | |
| 474 | /* we must also consider the limit imposed by stream IDs */ |
| 475 | ret2 = h2_streams_left(h2c); |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 476 | ret1 = MIN(ret1, ret2); |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 477 | if (ret1 > 0 && srv && srv->max_reuse >= 0) { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 478 | ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0; |
| 479 | ret1 = MIN(ret1, ret2); |
| 480 | } |
| 481 | return ret1; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 482 | } |
| 483 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 484 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 485 | /*****************************************************************/ |
| 486 | /* functions below are dedicated to the mux setup and management */ |
| 487 | /*****************************************************************/ |
| 488 | |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 489 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 490 | * is already initialized before installing the mux, so we detect incoming |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 491 | * connections from the fact that the context is still NULL (even during mux |
| 492 | * upgrades). <input> is always used as Input buffer and may contain data. It is |
| 493 | * the caller responsibility to not reuse it anymore. Returns < 0 on error. |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 494 | */ |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 495 | static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess, |
| 496 | struct buffer *input) |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 497 | { |
| 498 | struct h2c *h2c; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 499 | struct task *t = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 500 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 501 | h2c = pool_alloc(pool_head_h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 502 | if (!h2c) |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 503 | goto fail_no_h2c; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 504 | |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 505 | if (conn_is_back(conn)) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 506 | h2c->flags = H2_CF_IS_BACK; |
| 507 | h2c->shut_timeout = h2c->timeout = prx->timeout.server; |
| 508 | if (tick_isset(prx->timeout.serverfin)) |
| 509 | h2c->shut_timeout = prx->timeout.serverfin; |
| 510 | } else { |
| 511 | h2c->flags = H2_CF_NONE; |
| 512 | h2c->shut_timeout = h2c->timeout = prx->timeout.client; |
| 513 | if (tick_isset(prx->timeout.clientfin)) |
| 514 | h2c->shut_timeout = prx->timeout.clientfin; |
| 515 | } |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 516 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 517 | h2c->proxy = prx; |
Willy Tarreau | 3340029 | 2017-11-05 11:23:40 +0100 | [diff] [blame] | 518 | h2c->task = NULL; |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 519 | if (tick_isset(h2c->timeout)) { |
| 520 | t = task_new(tid_bit); |
| 521 | if (!t) |
| 522 | goto fail; |
| 523 | |
| 524 | h2c->task = t; |
| 525 | t->process = h2_timeout_task; |
| 526 | t->context = h2c; |
| 527 | t->expire = tick_add(now_ms, h2c->timeout); |
| 528 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 529 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 530 | h2c->wait_event.task = tasklet_new(); |
| 531 | if (!h2c->wait_event.task) |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 532 | goto fail; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 533 | h2c->wait_event.task->process = h2_io_cb; |
| 534 | h2c->wait_event.task->context = h2c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 535 | h2c->wait_event.events = 0; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 536 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 537 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 538 | if (!h2c->ddht) |
| 539 | goto fail; |
| 540 | |
| 541 | /* Initialise the context. */ |
| 542 | h2c->st0 = H2_CS_PREFACE; |
| 543 | h2c->conn = conn; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 544 | h2c->streams_limit = h2_settings_max_concurrent_streams; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 545 | h2c->max_id = -1; |
| 546 | h2c->errcode = H2_ERR_NO_ERROR; |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 547 | h2c->rcvd_c = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 548 | h2c->rcvd_s = 0; |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 549 | h2c->nb_streams = 0; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 550 | h2c->nb_cs = 0; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 551 | h2c->nb_reserved = 0; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 552 | h2c->stream_cnt = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 553 | |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 554 | h2c->dbuf = *input; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 555 | h2c->dsi = -1; |
| 556 | h2c->msi = -1; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 557 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 558 | h2c->last_sid = -1; |
| 559 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 560 | h2c->mbuf = BUF_NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 561 | h2c->miw = 65535; /* mux initial window size */ |
| 562 | h2c->mws = 65535; /* mux window size */ |
| 563 | h2c->mfs = 16384; /* initial max frame size */ |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 564 | h2c->streams_by_id = EB_ROOT; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 565 | LIST_INIT(&h2c->send_list); |
| 566 | LIST_INIT(&h2c->fctl_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 567 | LIST_INIT(&h2c->sending_list); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 568 | LIST_INIT(&h2c->buf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 569 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 570 | if (t) |
| 571 | task_queue(t); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 572 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 573 | if (h2c->flags & H2_CF_IS_BACK) { |
| 574 | /* FIXME: this is temporary, for outgoing connections we need |
| 575 | * to immediately allocate a stream until the code is modified |
| 576 | * so that the caller calls ->attach(). For now the outgoing cs |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 577 | * is stored as conn->ctx by the caller. |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 578 | */ |
| 579 | struct h2s *h2s; |
| 580 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 581 | h2s = h2c_bck_stream_new(h2c, conn->ctx, sess); |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 582 | if (!h2s) |
| 583 | goto fail_stream; |
| 584 | } |
| 585 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 586 | conn->ctx = h2c; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 587 | |
Willy Tarreau | 0f38358 | 2018-10-03 14:22:21 +0200 | [diff] [blame] | 588 | /* prepare to read something */ |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 589 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 590 | return 0; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 591 | fail_stream: |
| 592 | hpack_dht_free(h2c->ddht); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 593 | fail: |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 594 | task_destroy(t); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 595 | if (h2c->wait_event.task) |
| 596 | tasklet_free(h2c->wait_event.task); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 597 | pool_free(pool_head_h2c, h2c); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 598 | fail_no_h2c: |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 599 | return -1; |
| 600 | } |
| 601 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 602 | /* returns the next allocatable outgoing stream ID for the H2 connection, or |
| 603 | * -1 if no more is allocatable. |
| 604 | */ |
| 605 | static inline int32_t h2c_get_next_sid(const struct h2c *h2c) |
| 606 | { |
| 607 | int32_t id = (h2c->max_id + 1) | 1; |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 608 | |
| 609 | if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid)) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 610 | id = -1; |
| 611 | return id; |
| 612 | } |
| 613 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 614 | /* returns the stream associated with id <id> or NULL if not found */ |
| 615 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 616 | { |
| 617 | struct eb32_node *node; |
| 618 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 619 | if (id == 0) |
| 620 | return (struct h2s *)h2_closed_stream; |
| 621 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 622 | if (id > h2c->max_id) |
| 623 | return (struct h2s *)h2_idle_stream; |
| 624 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 625 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 626 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 627 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 628 | |
| 629 | return container_of(node, struct h2s, by_id); |
| 630 | } |
| 631 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 632 | /* release function. This one should be called to free all resources allocated |
| 633 | * to the mux. |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 634 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 635 | static void h2_release(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 636 | { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 637 | struct connection *conn = NULL;; |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 638 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 639 | if (h2c) { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 640 | /* The connection must be aattached to this mux to be released */ |
| 641 | if (h2c->conn && h2c->conn->ctx == h2c) |
| 642 | conn = h2c->conn; |
| 643 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 644 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 645 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 646 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 647 | LIST_DEL(&h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 648 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 649 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 650 | h2_release_buf(h2c, &h2c->dbuf); |
| 651 | h2_release_buf(h2c, &h2c->mbuf); |
| 652 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 653 | if (h2c->task) { |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 654 | h2c->task->context = NULL; |
| 655 | task_wakeup(h2c->task, TASK_WOKEN_OTHER); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 656 | h2c->task = NULL; |
| 657 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 658 | if (h2c->wait_event.task) |
| 659 | tasklet_free(h2c->wait_event.task); |
Olivier Houchard | 0e07937 | 2019-04-15 17:51:16 +0200 | [diff] [blame] | 660 | if (h2c->wait_event.events != 0) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 661 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events, |
Olivier Houchard | 0e07937 | 2019-04-15 17:51:16 +0200 | [diff] [blame] | 662 | &h2c->wait_event); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 663 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 664 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 665 | } |
| 666 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 667 | if (conn) { |
| 668 | conn->mux = NULL; |
| 669 | conn->ctx = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 670 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 671 | conn_stop_tracking(conn); |
| 672 | conn_full_close(conn); |
| 673 | if (conn->destroy_cb) |
| 674 | conn->destroy_cb(conn); |
| 675 | conn_free(conn); |
| 676 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 680 | /******************************************************/ |
| 681 | /* functions below are for the H2 protocol processing */ |
| 682 | /******************************************************/ |
| 683 | |
| 684 | /* 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] | 685 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 686 | { |
| 687 | return h2s ? h2s->id : 0; |
| 688 | } |
| 689 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 690 | /* 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] | 691 | 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] | 692 | { |
| 693 | if (h2c->msi < 0) |
| 694 | return 0; |
| 695 | |
| 696 | if (h2c->msi == h2s_id(h2s)) |
| 697 | return 0; |
| 698 | |
| 699 | return 1; |
| 700 | } |
| 701 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 702 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 703 | 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] | 704 | { |
| 705 | h2c->errcode = err; |
| 706 | h2c->st0 = H2_CS_ERROR; |
| 707 | } |
| 708 | |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 709 | /* marks an error on the stream. It may also update an already closed stream |
| 710 | * (e.g. to report an error after an RST was received). |
| 711 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 712 | 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] | 713 | { |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 714 | if (h2s->id && h2s->st != H2_SS_ERROR) { |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 715 | h2s->errcode = err; |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 716 | if (h2s->st < H2_SS_ERROR) |
| 717 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 718 | if (h2s->cs) |
| 719 | cs_set_error(h2s->cs); |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 723 | /* attempt to notify the data layer of recv availability */ |
| 724 | static void __maybe_unused h2s_notify_recv(struct h2s *h2s) |
| 725 | { |
| 726 | struct wait_event *sw; |
| 727 | |
| 728 | if (h2s->recv_wait) { |
| 729 | sw = h2s->recv_wait; |
| 730 | sw->events &= ~SUB_RETRY_RECV; |
| 731 | tasklet_wakeup(sw->task); |
| 732 | h2s->recv_wait = NULL; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | /* attempt to notify the data layer of send availability */ |
| 737 | static void __maybe_unused h2s_notify_send(struct h2s *h2s) |
| 738 | { |
| 739 | struct wait_event *sw; |
| 740 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 741 | if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) { |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 742 | sw = h2s->send_wait; |
| 743 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fd1e96d | 2019-03-25 14:04:25 +0100 | [diff] [blame] | 744 | LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 745 | tasklet_wakeup(sw->task); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 749 | /* alerts the data layer, trying to wake it up by all means, following |
| 750 | * this sequence : |
| 751 | * - if the h2s' data layer is subscribed to recv, then it's woken up for recv |
| 752 | * - if its subscribed to send, then it's woken up for send |
| 753 | * - if it was subscribed to neither, its ->wake() callback is called |
| 754 | * It is safe to call this function with a closed stream which doesn't have a |
| 755 | * conn_stream anymore. |
| 756 | */ |
| 757 | static void __maybe_unused h2s_alert(struct h2s *h2s) |
| 758 | { |
| 759 | if (h2s->recv_wait || h2s->send_wait) { |
| 760 | h2s_notify_recv(h2s); |
| 761 | h2s_notify_send(h2s); |
| 762 | } |
| 763 | else if (h2s->cs && h2s->cs->data_cb->wake != NULL) |
| 764 | h2s->cs->data_cb->wake(h2s->cs); |
| 765 | } |
| 766 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 767 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 768 | 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] | 769 | { |
| 770 | uint8_t *out = frame; |
| 771 | |
| 772 | *out = len >> 16; |
| 773 | write_n16(out + 1, len); |
| 774 | } |
| 775 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 776 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 777 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 778 | * 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] | 779 | * available in the buffer's input prior to calling this function. The buffer |
| 780 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 781 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 782 | 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] | 783 | const struct buffer *b, int o) |
| 784 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 785 | 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] | 786 | } |
| 787 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 788 | 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] | 789 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 790 | 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] | 791 | } |
| 792 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 793 | 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] | 794 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 795 | 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] | 796 | } |
| 797 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 798 | 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] | 799 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 800 | 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] | 801 | } |
| 802 | |
| 803 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 804 | /* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>. |
| 805 | * The algorithm is not obvious. It turns out that H2 headers are neither |
| 806 | * aligned nor do they use regular sizes. And to add to the trouble, the buffer |
| 807 | * 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] | 808 | * |
| 809 | * b0 b1 b2 b3 b4 b5..b8 |
| 810 | * +----------+---------+--------+----+----+----------------------+ |
| 811 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 812 | * +----------+---------+--------+----+----+----------------------+ |
| 813 | * |
| 814 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 815 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 816 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 817 | * 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] | 818 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 819 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 820 | */ |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 821 | 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] | 822 | { |
| 823 | uint64_t w; |
| 824 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 825 | if (b_data(b) < o + 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 826 | return 0; |
| 827 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 828 | w = h2_get_n64(b, o + 1); |
| 829 | h->len = *(uint8_t*)b_peek(b, o) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 830 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 831 | h->ff = w >> 32; |
| 832 | h->ft = w >> 40; |
| 833 | h->len += w >> 48; |
| 834 | return 1; |
| 835 | } |
| 836 | |
| 837 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 838 | * h2_peek_frame_hdr() above. |
| 839 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 840 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 841 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 842 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 846 | 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] | 847 | { |
| 848 | int ret; |
| 849 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 850 | ret = h2_peek_frame_hdr(b, 0, h); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 851 | if (ret > 0) |
| 852 | h2_skip_frame_hdr(b); |
| 853 | return ret; |
| 854 | } |
| 855 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 856 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 857 | * its connection if the stream was not yet closed. Please use this exclusively |
| 858 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 859 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 860 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 861 | { |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 862 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 863 | h2s->h2c->nb_streams--; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 864 | if (!h2s->id) |
| 865 | h2s->h2c->nb_reserved--; |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 866 | if (h2s->cs) { |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 867 | if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf)) |
| 868 | h2s_notify_recv(h2s); |
| 869 | } |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 870 | } |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 871 | h2s->st = H2_SS_CLOSED; |
| 872 | } |
| 873 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 874 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 875 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 876 | { |
| 877 | h2s_close(h2s); |
| 878 | eb32_delete(&h2s->by_id); |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 879 | if (b_size(&h2s->rxbuf)) { |
| 880 | b_free(&h2s->rxbuf); |
| 881 | offer_buffers(NULL, tasks_run_queue); |
| 882 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 883 | if (h2s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 884 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 885 | if (h2s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 886 | h2s->recv_wait->events &= ~SUB_RETRY_RECV; |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 887 | /* There's no need to explicitly call unsubscribe here, the only |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 888 | * reference left would be in the h2c send_list/fctl_list, and if |
| 889 | * we're in it, we're getting out anyway |
| 890 | */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 891 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 892 | if (LIST_ADDED(&h2s->sending_list)) { |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 893 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
| 894 | LIST_DEL_INIT(&h2s->sending_list); |
| 895 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 896 | tasklet_free(h2s->wait_event.task); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 897 | pool_free(pool_head_h2s, h2s); |
| 898 | } |
| 899 | |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 900 | /* allocates a new stream <id> for connection <h2c> and adds it into h2c's |
| 901 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 902 | * causes of errors can be any failed memory allocation. The caller is |
| 903 | * responsible for checking if the connection may support an extra stream |
| 904 | * prior to calling this function. |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 905 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 906 | static struct h2s *h2s_new(struct h2c *h2c, int id) |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 907 | { |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 908 | struct h2s *h2s; |
| 909 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 910 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 911 | if (!h2s) |
| 912 | goto out; |
| 913 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 914 | h2s->wait_event.task = tasklet_new(); |
| 915 | if (!h2s->wait_event.task) { |
| 916 | pool_free(pool_head_h2s, h2s); |
| 917 | goto out; |
| 918 | } |
| 919 | h2s->send_wait = NULL; |
| 920 | h2s->recv_wait = NULL; |
| 921 | h2s->wait_event.task->process = h2_deferred_shut; |
| 922 | h2s->wait_event.task->context = h2s; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 923 | h2s->wait_event.events = 0; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 924 | LIST_INIT(&h2s->list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 925 | LIST_INIT(&h2s->sending_list); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 926 | h2s->h2c = h2c; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 927 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 928 | h2s->mws = h2c->miw; |
| 929 | h2s->flags = H2_SF_NONE; |
| 930 | h2s->errcode = H2_ERR_NO_ERROR; |
| 931 | h2s->st = H2_SS_IDLE; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 932 | h2s->status = 0; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 933 | h2s->body_len = 0; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 934 | h2s->rxbuf = BUF_NULL; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 935 | |
| 936 | if (h2c->flags & H2_CF_IS_BACK) { |
| 937 | h1m_init_req(&h2s->h1m); |
| 938 | h2s->h1m.err_pos = -1; // don't care about errors on the request path |
| 939 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 940 | } else { |
| 941 | h1m_init_res(&h2s->h1m); |
| 942 | h2s->h1m.err_pos = -1; // don't care about errors on the response path |
| 943 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 944 | } |
| 945 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 946 | h2s->by_id.key = h2s->id = id; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 947 | if (id > 0) |
| 948 | h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 949 | else |
| 950 | h2c->nb_reserved++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 951 | |
| 952 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 953 | h2c->nb_streams++; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 954 | h2c->stream_cnt++; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 955 | |
| 956 | return h2s; |
| 957 | |
| 958 | out_free_h2s: |
| 959 | pool_free(pool_head_h2s, h2s); |
| 960 | out: |
| 961 | return NULL; |
| 962 | } |
| 963 | |
| 964 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 965 | * case of memory allocation error. |
| 966 | */ |
| 967 | static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id) |
| 968 | { |
| 969 | struct session *sess = h2c->conn->owner; |
| 970 | struct conn_stream *cs; |
| 971 | struct h2s *h2s; |
| 972 | |
| 973 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 974 | goto out; |
| 975 | |
| 976 | h2s = h2s_new(h2c, id); |
| 977 | if (!h2s) |
| 978 | goto out; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 979 | |
| 980 | cs = cs_new(h2c->conn); |
| 981 | if (!cs) |
| 982 | goto out_close; |
| 983 | |
Olivier Houchard | 746fb77 | 2018-12-15 19:42:00 +0100 | [diff] [blame] | 984 | cs->flags |= CS_FL_NOT_FIRST; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 985 | h2s->cs = cs; |
| 986 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 987 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 988 | |
| 989 | if (stream_create_from_cs(cs) < 0) |
| 990 | goto out_free_cs; |
| 991 | |
Willy Tarreau | 590a051 | 2018-09-05 11:56:48 +0200 | [diff] [blame] | 992 | /* We want the accept date presented to the next stream to be the one |
| 993 | * we have now, the handshake time to be null (since the next stream |
| 994 | * is not delayed by a handshake), and the idle time to count since |
| 995 | * right now. |
| 996 | */ |
| 997 | sess->accept_date = date; |
| 998 | sess->tv_accept = now; |
| 999 | sess->t_handshake = 0; |
| 1000 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1001 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 1002 | if (h2_frt_has_too_many_cs(h2c)) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 1003 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1004 | return h2s; |
| 1005 | |
| 1006 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 1007 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1008 | cs_free(cs); |
| 1009 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 1010 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1011 | out: |
Willy Tarreau | 45efc07 | 2018-10-03 18:27:52 +0200 | [diff] [blame] | 1012 | sess_log(sess); |
| 1013 | return NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1014 | } |
| 1015 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1016 | /* allocates a new stream associated to conn_stream <cs> on the h2c connection |
| 1017 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 1018 | * possible stream ID was reached. |
| 1019 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1020 | 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] | 1021 | { |
| 1022 | struct h2s *h2s = NULL; |
| 1023 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 1024 | if (h2c->nb_streams >= h2c->streams_limit) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1025 | goto out; |
| 1026 | |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 1027 | if (h2_streams_left(h2c) < 1) |
| 1028 | goto out; |
| 1029 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1030 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 1031 | h2s = h2s_new(h2c, 0); |
| 1032 | if (!h2s) |
| 1033 | goto out; |
| 1034 | |
| 1035 | h2s->cs = cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1036 | h2s->sess = sess; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1037 | cs->ctx = h2s; |
| 1038 | h2c->nb_cs++; |
| 1039 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1040 | out: |
| 1041 | return h2s; |
| 1042 | } |
| 1043 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1044 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 1045 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 1046 | * the various settings codes. |
| 1047 | */ |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1048 | static int h2c_send_settings(struct h2c *h2c) |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1049 | { |
| 1050 | struct buffer *res; |
| 1051 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1052 | struct buffer buf; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1053 | int mfs; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1054 | int ret; |
| 1055 | |
| 1056 | if (h2c_mux_busy(h2c, NULL)) { |
| 1057 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1061 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1062 | if (!res) { |
| 1063 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1064 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
| 1068 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 1069 | chunk_memcpy(&buf, |
| 1070 | "\x00\x00\x00" /* length : 0 for now */ |
| 1071 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 1072 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 1073 | 9); |
| 1074 | |
Willy Tarreau | 0bbad6b | 2019-02-26 16:01:52 +0100 | [diff] [blame] | 1075 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1076 | /* send settings_enable_push=0 */ |
| 1077 | chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6); |
| 1078 | } |
| 1079 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1080 | if (h2_settings_header_table_size != 4096) { |
| 1081 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 1082 | |
| 1083 | write_n32(str + 2, h2_settings_header_table_size); |
| 1084 | chunk_memcat(&buf, str, 6); |
| 1085 | } |
| 1086 | |
| 1087 | if (h2_settings_initial_window_size != 65535) { |
| 1088 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 1089 | |
| 1090 | write_n32(str + 2, h2_settings_initial_window_size); |
| 1091 | chunk_memcat(&buf, str, 6); |
| 1092 | } |
| 1093 | |
| 1094 | if (h2_settings_max_concurrent_streams != 0) { |
| 1095 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 1096 | |
| 1097 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 1098 | * the protocol, so never send this value! |
| 1099 | */ |
| 1100 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 1101 | chunk_memcat(&buf, str, 6); |
| 1102 | } |
| 1103 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1104 | mfs = h2_settings_max_frame_size; |
| 1105 | if (mfs > global.tune.bufsize) |
| 1106 | mfs = global.tune.bufsize; |
| 1107 | |
| 1108 | if (!mfs) |
| 1109 | mfs = global.tune.bufsize; |
| 1110 | |
| 1111 | if (mfs != 16384) { |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1112 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 1113 | |
| 1114 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 1115 | * match bufsize - rewrite size, but at the moment it seems |
| 1116 | * that clients don't take care of it. |
| 1117 | */ |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1118 | write_n32(str + 2, mfs); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1119 | chunk_memcat(&buf, str, 6); |
| 1120 | } |
| 1121 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1122 | h2_set_frame_size(buf.area, buf.data - 9); |
| 1123 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1124 | if (unlikely(ret <= 0)) { |
| 1125 | if (!ret) { |
| 1126 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1127 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1128 | return 0; |
| 1129 | } |
| 1130 | else { |
| 1131 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1132 | return 0; |
| 1133 | } |
| 1134 | } |
| 1135 | return ret; |
| 1136 | } |
| 1137 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1138 | /* Try to receive a connection preface, then upon success try to send our |
| 1139 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1140 | * missing data. It may return an error in h2c. |
| 1141 | */ |
| 1142 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 1143 | { |
| 1144 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1145 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1146 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1147 | 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] | 1148 | |
| 1149 | if (unlikely(ret1 <= 0)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1150 | if (ret1 < 0) |
| 1151 | sess_log(h2c->conn->owner); |
| 1152 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1153 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 1154 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1155 | return 0; |
| 1156 | } |
| 1157 | |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1158 | ret2 = h2c_send_settings(h2c); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1159 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1160 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1161 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1162 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1163 | } |
| 1164 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1165 | /* Try to send a connection preface, then upon success try to send our |
| 1166 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1167 | * missing data. It may return an error in h2c. |
| 1168 | */ |
| 1169 | static int h2c_bck_send_preface(struct h2c *h2c) |
| 1170 | { |
| 1171 | struct buffer *res; |
| 1172 | |
| 1173 | if (h2c_mux_busy(h2c, NULL)) { |
| 1174 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | res = h2_get_buf(h2c, &h2c->mbuf); |
| 1179 | if (!res) { |
| 1180 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1181 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | if (!b_data(res)) { |
| 1186 | /* preface not yet sent */ |
| 1187 | b_istput(res, ist(H2_CONN_PREFACE)); |
| 1188 | } |
| 1189 | |
| 1190 | return h2c_send_settings(h2c); |
| 1191 | } |
| 1192 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1193 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 1194 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 1195 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 1196 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 1197 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 1198 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 1199 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 1200 | * case so that it is safe to use h2c_error() to report such errors. |
| 1201 | */ |
| 1202 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 1203 | { |
| 1204 | struct buffer *res; |
| 1205 | char str[17]; |
| 1206 | int ret; |
| 1207 | |
| 1208 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 1209 | return 1; // claim that it worked |
| 1210 | |
| 1211 | if (h2c_mux_busy(h2c, h2s)) { |
| 1212 | if (h2s) |
| 1213 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1214 | else |
| 1215 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1216 | return 0; |
| 1217 | } |
| 1218 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1219 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1220 | if (!res) { |
| 1221 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1222 | if (h2s) |
| 1223 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1224 | else |
| 1225 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1226 | return 0; |
| 1227 | } |
| 1228 | |
| 1229 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 1230 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 1231 | |
| 1232 | if (h2c->last_sid < 0) |
| 1233 | h2c->last_sid = h2c->max_id; |
| 1234 | |
| 1235 | write_n32(str + 9, h2c->last_sid); |
| 1236 | write_n32(str + 13, h2c->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1237 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1238 | if (unlikely(ret <= 0)) { |
| 1239 | if (!ret) { |
| 1240 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1241 | if (h2s) |
| 1242 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1243 | else |
| 1244 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1245 | return 0; |
| 1246 | } |
| 1247 | else { |
| 1248 | /* we cannot report this error using GOAWAY, so we mark |
| 1249 | * it and claim a success. |
| 1250 | */ |
| 1251 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1252 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 1253 | return 1; |
| 1254 | } |
| 1255 | } |
| 1256 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 1257 | return ret; |
| 1258 | } |
| 1259 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1260 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 1261 | * during mux operations. This stream must be valid and cannot be closed |
| 1262 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 1263 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 1264 | * not yet. |
| 1265 | * |
| 1266 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1267 | * to write the message, it subscribes the stream to future notifications. |
| 1268 | */ |
| 1269 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1270 | { |
| 1271 | struct buffer *res; |
| 1272 | char str[13]; |
| 1273 | int ret; |
| 1274 | |
| 1275 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 1276 | return 1; |
| 1277 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1278 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1279 | * RST_STREAM in response to a RST_STREAM frame. |
| 1280 | */ |
| 1281 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1282 | ret = 1; |
| 1283 | goto ignore; |
| 1284 | } |
| 1285 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1286 | if (h2c_mux_busy(h2c, h2s)) { |
| 1287 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1291 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1292 | if (!res) { |
| 1293 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1294 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | /* len: 4, type: 3, flags: none */ |
| 1299 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1300 | write_n32(str + 5, h2s->id); |
| 1301 | write_n32(str + 9, h2s->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1302 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1303 | |
| 1304 | if (unlikely(ret <= 0)) { |
| 1305 | if (!ret) { |
| 1306 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1307 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1308 | return 0; |
| 1309 | } |
| 1310 | else { |
| 1311 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1312 | return 0; |
| 1313 | } |
| 1314 | } |
| 1315 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1316 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1317 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1318 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1319 | return ret; |
| 1320 | } |
| 1321 | |
| 1322 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 1323 | * 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] | 1324 | * error code, even if the stream is one of the dummy ones, and will update |
| 1325 | * h2s->st to H2_SS_CLOSED if it was not yet. |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1326 | * |
| 1327 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1328 | * 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] | 1329 | * 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] | 1330 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1331 | */ |
| 1332 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1333 | { |
| 1334 | struct buffer *res; |
| 1335 | char str[13]; |
| 1336 | int ret; |
| 1337 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1338 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1339 | * RST_STREAM in response to a RST_STREAM frame. |
| 1340 | */ |
| 1341 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1342 | ret = 1; |
| 1343 | goto ignore; |
| 1344 | } |
| 1345 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1346 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1347 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1348 | return 0; |
| 1349 | } |
| 1350 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1351 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1352 | if (!res) { |
| 1353 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1354 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1355 | return 0; |
| 1356 | } |
| 1357 | |
| 1358 | /* len: 4, type: 3, flags: none */ |
| 1359 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1360 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1361 | write_n32(str + 5, h2c->dsi); |
Willy Tarreau | e6888ff | 2018-12-23 18:26:26 +0100 | [diff] [blame] | 1362 | write_n32(str + 9, h2s->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1363 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1364 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1365 | if (unlikely(ret <= 0)) { |
| 1366 | if (!ret) { |
| 1367 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1368 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1369 | return 0; |
| 1370 | } |
| 1371 | else { |
| 1372 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1373 | return 0; |
| 1374 | } |
| 1375 | } |
| 1376 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1377 | ignore: |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1378 | if (h2s->id) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1379 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1380 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1381 | } |
| 1382 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1383 | return ret; |
| 1384 | } |
| 1385 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1386 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 1387 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 1388 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 1389 | * on success or zero if nothing was done. In case of lack of room to write the |
| 1390 | * message, it subscribes the requesting stream to future notifications. |
| 1391 | */ |
| 1392 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 1393 | { |
| 1394 | struct h2c *h2c = h2s->h2c; |
| 1395 | struct buffer *res; |
| 1396 | char str[9]; |
| 1397 | int ret; |
| 1398 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1399 | 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] | 1400 | return 1; |
| 1401 | |
| 1402 | if (h2c_mux_busy(h2c, h2s)) { |
| 1403 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1404 | return 0; |
| 1405 | } |
| 1406 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1407 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1408 | if (!res) { |
| 1409 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1410 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1411 | return 0; |
| 1412 | } |
| 1413 | |
| 1414 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1415 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1416 | write_n32(str + 5, h2s->id); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1417 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1418 | if (likely(ret > 0)) { |
| 1419 | h2s->flags |= H2_SF_ES_SENT; |
| 1420 | } |
| 1421 | else if (!ret) { |
| 1422 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1423 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1424 | return 0; |
| 1425 | } |
| 1426 | else { |
| 1427 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1428 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1429 | } |
| 1430 | return ret; |
| 1431 | } |
| 1432 | |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1433 | /* 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] | 1434 | * 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] | 1435 | * is automatically updated accordingly. If the stream is orphaned, it is |
| 1436 | * destroyed. |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1437 | */ |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1438 | static void h2s_wake_one_stream(struct h2s *h2s) |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1439 | { |
| 1440 | if (!h2s->cs) { |
| 1441 | /* this stream was already orphaned */ |
| 1442 | h2s_destroy(h2s); |
| 1443 | return; |
| 1444 | } |
| 1445 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1446 | if (conn_xprt_read0_pending(h2s->h2c->conn)) { |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1447 | if (h2s->st == H2_SS_OPEN) |
| 1448 | h2s->st = H2_SS_HREM; |
| 1449 | else if (h2s->st == H2_SS_HLOC) |
| 1450 | h2s_close(h2s); |
| 1451 | } |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1452 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1453 | if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) || |
| 1454 | (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) { |
| 1455 | h2s->cs->flags |= CS_FL_ERR_PENDING; |
| 1456 | if (h2s->cs->flags & CS_FL_EOS) |
| 1457 | h2s->cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1458 | |
Willy Tarreau | aebbe5e | 2019-05-07 17:48:59 +0200 | [diff] [blame] | 1459 | if (h2s->st < H2_SS_ERROR) |
| 1460 | h2s->st = H2_SS_ERROR; |
| 1461 | } |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1462 | |
| 1463 | h2s_alert(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | /* wake the streams attached to the connection, whose id is greater than <last> |
| 1467 | * or unassigned. |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1468 | */ |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1469 | static void h2_wake_some_streams(struct h2c *h2c, int last) |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1470 | { |
| 1471 | struct eb32_node *node; |
| 1472 | struct h2s *h2s; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1473 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1474 | /* Wake all streams with ID > last */ |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1475 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1476 | while (node) { |
| 1477 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1478 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1479 | h2s_wake_one_stream(h2s); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1480 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1481 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1482 | /* Wake all streams with unassigned ID (ID == 0) */ |
| 1483 | node = eb32_lookup(&h2c->streams_by_id, 0); |
| 1484 | while (node) { |
| 1485 | h2s = container_of(node, struct h2s, by_id); |
| 1486 | if (h2s->id > 0) |
| 1487 | break; |
| 1488 | node = eb32_next(node); |
Willy Tarreau | 13b6c2e | 2019-05-07 17:26:05 +0200 | [diff] [blame] | 1489 | h2s_wake_one_stream(h2s); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1490 | } |
| 1491 | } |
| 1492 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1493 | /* Increase all streams' outgoing window size by the difference passed in |
| 1494 | * argument. This is needed upon receipt of the settings frame if the initial |
| 1495 | * window size is different. The difference may be negative and the resulting |
| 1496 | * window size as well, for the time it takes to receive some window updates. |
| 1497 | */ |
| 1498 | static void h2c_update_all_ws(struct h2c *h2c, int diff) |
| 1499 | { |
| 1500 | struct h2s *h2s; |
| 1501 | struct eb32_node *node; |
| 1502 | |
| 1503 | if (!diff) |
| 1504 | return; |
| 1505 | |
| 1506 | node = eb32_first(&h2c->streams_by_id); |
| 1507 | while (node) { |
| 1508 | h2s = container_of(node, struct h2s, by_id); |
| 1509 | h2s->mws += diff; |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1510 | |
| 1511 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1512 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 1513 | if (h2s->send_wait && !LIST_ADDED(&h2s->list)) |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1514 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1515 | } |
| 1516 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1517 | node = eb32_next(node); |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1522 | * 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] | 1523 | * return an error in h2c. The caller must have already verified frame length |
| 1524 | * and stream ID validity. Described in RFC7540#6.5. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1525 | */ |
| 1526 | static int h2c_handle_settings(struct h2c *h2c) |
| 1527 | { |
| 1528 | unsigned int offset; |
| 1529 | int error; |
| 1530 | |
| 1531 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1532 | if (h2c->dfl) { |
| 1533 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1534 | goto fail; |
| 1535 | } |
| 1536 | return 1; |
| 1537 | } |
| 1538 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1539 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1540 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1541 | return 0; |
| 1542 | |
| 1543 | /* parse the frame */ |
| 1544 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1545 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1546 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1547 | |
| 1548 | switch (type) { |
| 1549 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1550 | /* we need to update all existing streams with the |
| 1551 | * difference from the previous iws. |
| 1552 | */ |
| 1553 | if (arg < 0) { // RFC7540#6.5.2 |
| 1554 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1555 | goto fail; |
| 1556 | } |
| 1557 | h2c_update_all_ws(h2c, arg - h2c->miw); |
| 1558 | h2c->miw = arg; |
| 1559 | break; |
| 1560 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1561 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1562 | error = H2_ERR_PROTOCOL_ERROR; |
| 1563 | goto fail; |
| 1564 | } |
| 1565 | h2c->mfs = arg; |
| 1566 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1567 | case H2_SETTINGS_ENABLE_PUSH: |
| 1568 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1569 | error = H2_ERR_PROTOCOL_ERROR; |
| 1570 | goto fail; |
| 1571 | } |
| 1572 | break; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 1573 | case H2_SETTINGS_MAX_CONCURRENT_STREAMS: |
| 1574 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1575 | /* the limit is only for the backend; for the frontend it is our limit */ |
| 1576 | if ((unsigned int)arg > h2_settings_max_concurrent_streams) |
| 1577 | arg = h2_settings_max_concurrent_streams; |
| 1578 | h2c->streams_limit = arg; |
| 1579 | } |
| 1580 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | /* need to ACK this frame now */ |
| 1585 | h2c->st0 = H2_CS_FRAME_A; |
| 1586 | return 1; |
| 1587 | fail: |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1588 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1589 | h2c_error(h2c, error); |
| 1590 | return 0; |
| 1591 | } |
| 1592 | |
| 1593 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1594 | * success or one of the h2_status values. |
| 1595 | */ |
| 1596 | static int h2c_ack_settings(struct h2c *h2c) |
| 1597 | { |
| 1598 | struct buffer *res; |
| 1599 | char str[9]; |
| 1600 | int ret = -1; |
| 1601 | |
| 1602 | if (h2c_mux_busy(h2c, NULL)) { |
| 1603 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1604 | return 0; |
| 1605 | } |
| 1606 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1607 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1608 | if (!res) { |
| 1609 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1610 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1611 | return 0; |
| 1612 | } |
| 1613 | |
| 1614 | memcpy(str, |
| 1615 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1616 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1617 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1618 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1619 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1620 | if (unlikely(ret <= 0)) { |
| 1621 | if (!ret) { |
| 1622 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1623 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1624 | return 0; |
| 1625 | } |
| 1626 | else { |
| 1627 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1628 | return 0; |
| 1629 | } |
| 1630 | } |
| 1631 | return ret; |
| 1632 | } |
| 1633 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1634 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1635 | * 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] | 1636 | * missing data. The caller must have already verified frame length |
| 1637 | * and stream ID validity. |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1638 | */ |
| 1639 | static int h2c_handle_ping(struct h2c *h2c) |
| 1640 | { |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1641 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1642 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1643 | h2c->st0 = H2_CS_FRAME_A; |
| 1644 | return 1; |
| 1645 | } |
| 1646 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1647 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1648 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1649 | * error in h2c. |
| 1650 | */ |
| 1651 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1652 | { |
| 1653 | struct buffer *res; |
| 1654 | char str[13]; |
| 1655 | int ret = -1; |
| 1656 | |
| 1657 | if (h2c_mux_busy(h2c, NULL)) { |
| 1658 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1662 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1663 | if (!res) { |
| 1664 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1665 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1666 | return 0; |
| 1667 | } |
| 1668 | |
| 1669 | /* length: 4, type: 8, flags: none */ |
| 1670 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1671 | write_n32(str + 5, sid); |
| 1672 | write_n32(str + 9, increment); |
| 1673 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1674 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1675 | |
| 1676 | if (unlikely(ret <= 0)) { |
| 1677 | if (!ret) { |
| 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 | |
| 1690 | /* try to send pending window update for the connection. It's safe to call it |
| 1691 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1692 | * failure. It may return an error in h2c. |
| 1693 | */ |
| 1694 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1695 | { |
| 1696 | int ret = 1; |
| 1697 | |
| 1698 | if (h2c->rcvd_c <= 0) |
| 1699 | return 1; |
| 1700 | |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 1701 | if (!(h2c->flags & H2_CF_WINDOW_OPENED)) { |
| 1702 | /* increase the advertised connection window to 2G on |
| 1703 | * first update. |
| 1704 | */ |
| 1705 | h2c->flags |= H2_CF_WINDOW_OPENED; |
| 1706 | h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT; |
| 1707 | } |
| 1708 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1709 | /* send WU for the connection */ |
| 1710 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1711 | if (ret > 0) |
| 1712 | h2c->rcvd_c = 0; |
| 1713 | |
| 1714 | return ret; |
| 1715 | } |
| 1716 | |
| 1717 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1718 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1719 | * room or failure. It may return an error in h2c. |
| 1720 | */ |
| 1721 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1722 | { |
| 1723 | int ret = 1; |
| 1724 | |
| 1725 | if (h2c->rcvd_s <= 0) |
| 1726 | return 1; |
| 1727 | |
| 1728 | /* send WU for the stream */ |
| 1729 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1730 | if (ret > 0) |
| 1731 | h2c->rcvd_s = 0; |
| 1732 | |
| 1733 | return ret; |
| 1734 | } |
| 1735 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1736 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1737 | * success, 0 on missing data or one of the h2_status values. |
| 1738 | */ |
| 1739 | static int h2c_ack_ping(struct h2c *h2c) |
| 1740 | { |
| 1741 | struct buffer *res; |
| 1742 | char str[17]; |
| 1743 | int ret = -1; |
| 1744 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1745 | if (b_data(&h2c->dbuf) < 8) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1746 | return 0; |
| 1747 | |
| 1748 | if (h2c_mux_busy(h2c, NULL)) { |
| 1749 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1750 | return 0; |
| 1751 | } |
| 1752 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1753 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1754 | if (!res) { |
| 1755 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1756 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1757 | return 0; |
| 1758 | } |
| 1759 | |
| 1760 | memcpy(str, |
| 1761 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1762 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1763 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1764 | |
| 1765 | /* copy the original payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1766 | h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1767 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1768 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1769 | if (unlikely(ret <= 0)) { |
| 1770 | if (!ret) { |
| 1771 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1772 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1773 | return 0; |
| 1774 | } |
| 1775 | else { |
| 1776 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1777 | return 0; |
| 1778 | } |
| 1779 | } |
| 1780 | return ret; |
| 1781 | } |
| 1782 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1783 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1784 | * 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] | 1785 | * h2c or h2s. The caller must have already verified frame length and stream ID |
| 1786 | * validity. Described in RFC7540#6.9. |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1787 | */ |
| 1788 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1789 | { |
| 1790 | int32_t inc; |
| 1791 | int error; |
| 1792 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1793 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1794 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1795 | return 0; |
| 1796 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1797 | inc = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1798 | |
| 1799 | if (h2c->dsi != 0) { |
| 1800 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1801 | |
| 1802 | /* it's not an error to receive WU on a closed stream */ |
| 1803 | if (h2s->st == H2_SS_CLOSED) |
| 1804 | return 1; |
| 1805 | |
| 1806 | if (!inc) { |
| 1807 | error = H2_ERR_PROTOCOL_ERROR; |
| 1808 | goto strm_err; |
| 1809 | } |
| 1810 | |
| 1811 | if (h2s->mws >= 0 && h2s->mws + inc < 0) { |
| 1812 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1813 | goto strm_err; |
| 1814 | } |
| 1815 | |
| 1816 | h2s->mws += inc; |
| 1817 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1818 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 1819 | if (h2s->send_wait && !LIST_ADDED(&h2s->list)) |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 1820 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1821 | } |
| 1822 | } |
| 1823 | else { |
| 1824 | /* connection window update */ |
| 1825 | if (!inc) { |
| 1826 | error = H2_ERR_PROTOCOL_ERROR; |
| 1827 | goto conn_err; |
| 1828 | } |
| 1829 | |
| 1830 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1831 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1832 | goto conn_err; |
| 1833 | } |
| 1834 | |
| 1835 | h2c->mws += inc; |
| 1836 | } |
| 1837 | |
| 1838 | return 1; |
| 1839 | |
| 1840 | conn_err: |
| 1841 | h2c_error(h2c, error); |
| 1842 | return 0; |
| 1843 | |
| 1844 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 1845 | h2s_error(h2s, error); |
| 1846 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1847 | return 0; |
| 1848 | } |
| 1849 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1850 | /* 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] | 1851 | * the last ID. Returns > 0 on success or zero on missing data. The caller must |
| 1852 | * have already verified frame length and stream ID validity. Described in |
| 1853 | * RFC7540#6.8. |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1854 | */ |
| 1855 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1856 | { |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1857 | int last; |
| 1858 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1859 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1860 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1861 | return 0; |
| 1862 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1863 | last = h2_get_n32(&h2c->dbuf, 0); |
| 1864 | h2c->errcode = h2_get_n32(&h2c->dbuf, 4); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1865 | if (h2c->last_sid < 0) |
| 1866 | h2c->last_sid = last; |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 1867 | h2_wake_some_streams(h2c, last); |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1868 | return 1; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1869 | } |
| 1870 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1871 | /* 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] | 1872 | * invalid. Returns > 0 on success or zero on missing data. It may return an |
| 1873 | * error in h2c. The caller must have already verified frame length and stream |
| 1874 | * ID validity. Described in RFC7540#6.3. |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1875 | */ |
| 1876 | static int h2c_handle_priority(struct h2c *h2c) |
| 1877 | { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1878 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1879 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1880 | return 0; |
| 1881 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1882 | if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1883 | /* 7540#5.3 : can't depend on itself */ |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1884 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1885 | return 0; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1886 | } |
| 1887 | return 1; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1888 | } |
| 1889 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1890 | /* 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] | 1891 | * Returns > 0 on success or zero on missing data. The caller must have already |
| 1892 | * verified frame length and stream ID validity. Described in RFC7540#6.4. |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1893 | */ |
| 1894 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1895 | { |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1896 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1897 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1898 | return 0; |
| 1899 | |
| 1900 | /* late RST, already handled */ |
| 1901 | if (h2s->st == H2_SS_CLOSED) |
| 1902 | return 1; |
| 1903 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1904 | h2s->errcode = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1905 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1906 | |
| 1907 | if (h2s->cs) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 1908 | cs_set_error(h2s->cs); |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 1909 | h2s_alert(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | h2s->flags |= H2_SF_RST_RCVD; |
| 1913 | return 1; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1914 | } |
| 1915 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1916 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 1917 | * It may return an error in h2c or h2s. The caller must consider that the |
| 1918 | * return value is the new h2s in case one was allocated (most common case). |
| 1919 | * Described in RFC7540#6.2. Most of the |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1920 | * errors here are reported as connection errors since it's impossible to |
| 1921 | * recover from such errors after the compression context has been altered. |
| 1922 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1923 | 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] | 1924 | { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1925 | struct buffer rxbuf = BUF_NULL; |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1926 | unsigned long long body_len = 0; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1927 | uint32_t flags = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1928 | int error; |
| 1929 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1930 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1931 | return NULL; // empty buffer |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1932 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1933 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1934 | return NULL; // incomplete frame |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1935 | |
| 1936 | /* now either the frame is complete or the buffer is complete */ |
| 1937 | if (h2s->st != H2_SS_IDLE) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1938 | /* The stream exists/existed, this must be a trailers frame */ |
| 1939 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 1940 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len); |
| 1941 | /* unrecoverable error ? */ |
| 1942 | if (h2c->st0 >= H2_CS_ERROR) |
| 1943 | goto out; |
| 1944 | |
| 1945 | if (error == 0) |
| 1946 | goto out; // missing data |
| 1947 | |
| 1948 | if (error < 0) { |
| 1949 | /* Failed to decode this frame (e.g. too large request) |
| 1950 | * but the HPACK decompressor is still synchronized. |
| 1951 | */ |
| 1952 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 1953 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1954 | goto out; |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 1955 | } |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1956 | goto done; |
| 1957 | } |
Willy Tarreau | 1f03550 | 2019-01-30 11:44:07 +0100 | [diff] [blame] | 1958 | /* the connection was already killed by an RST, let's consume |
| 1959 | * the data and send another RST. |
| 1960 | */ |
| 1961 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
| 1962 | h2s = (struct h2s*)h2_error_stream; |
| 1963 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1964 | } |
| 1965 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 1966 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 1967 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1968 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1969 | goto conn_err; |
| 1970 | } |
Willy Tarreau | 415b1ee | 2019-01-02 13:59:43 +0100 | [diff] [blame] | 1971 | else if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 1972 | goto out; // IDLE but too many cs still present |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1973 | |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1974 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1975 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1976 | /* unrecoverable error ? */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1977 | if (h2c->st0 >= H2_CS_ERROR) |
| 1978 | goto out; |
| 1979 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1980 | if (error <= 0) { |
| 1981 | if (error == 0) |
| 1982 | goto out; // missing data |
| 1983 | |
| 1984 | /* Failed to decode this stream (e.g. too large request) |
| 1985 | * but the HPACK decompressor is still synchronized. |
| 1986 | */ |
| 1987 | h2s = (struct h2s*)h2_error_stream; |
| 1988 | goto send_rst; |
| 1989 | } |
| 1990 | |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1991 | /* 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] | 1992 | * positively from h2c_frt_stream_new(), the stream will report the error, |
| 1993 | * 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] | 1994 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 1995 | h2s = h2c_frt_stream_new(h2c, h2c->dsi); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1996 | if (!h2s) { |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 1997 | h2s = (struct h2s*)h2_refused_stream; |
| 1998 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | h2s->st = H2_SS_OPEN; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2002 | h2s->rxbuf = rxbuf; |
| 2003 | h2s->flags |= flags; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2004 | h2s->body_len = body_len; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2005 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 2006 | done: |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2007 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2008 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2009 | |
| 2010 | if (h2s->flags & H2_SF_ES_RCVD) { |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2011 | if (h2s->st == H2_SS_OPEN) |
| 2012 | h2s->st = H2_SS_HREM; |
| 2013 | else |
| 2014 | h2s_close(h2s); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2015 | } |
| 2016 | |
Willy Tarreau | 3a429f0 | 2019-01-03 11:41:50 +0100 | [diff] [blame] | 2017 | /* update the max stream ID if the request is being processed */ |
| 2018 | if (h2s->id > h2c->max_id) |
| 2019 | h2c->max_id = h2s->id; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2020 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2021 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2022 | |
| 2023 | conn_err: |
| 2024 | h2c_error(h2c, error); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2025 | goto out; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2026 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2027 | out: |
| 2028 | h2_release_buf(h2c, &rxbuf); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2029 | return NULL; |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2030 | |
| 2031 | send_rst: |
| 2032 | /* make the demux send an RST for the current stream. We may only |
| 2033 | * do this if we're certain that the HEADERS frame was properly |
| 2034 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2035 | */ |
| 2036 | h2_release_buf(h2c, &rxbuf); |
| 2037 | h2c->st0 = H2_CS_FRAME_E; |
| 2038 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2039 | } |
| 2040 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2041 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 2042 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 2043 | * errors here are reported as connection errors since it's impossible to |
| 2044 | * recover from such errors after the compression context has been altered. |
| 2045 | */ |
| 2046 | static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 2047 | { |
| 2048 | int error; |
| 2049 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2050 | if (!b_size(&h2c->dbuf)) |
| 2051 | return NULL; // empty buffer |
| 2052 | |
| 2053 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
| 2054 | return NULL; // incomplete frame |
| 2055 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2056 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2057 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2058 | /* unrecoverable error ? */ |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2059 | if (h2c->st0 >= H2_CS_ERROR) |
| 2060 | return NULL; |
| 2061 | |
Willy Tarreau | 08bb1d6 | 2019-01-30 16:55:48 +0100 | [diff] [blame] | 2062 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2063 | /* RFC7540#5.1 */ |
| 2064 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2065 | h2c->st0 = H2_CS_FRAME_E; |
| 2066 | return NULL; |
| 2067 | } |
| 2068 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2069 | if (error <= 0) { |
| 2070 | if (error == 0) |
| 2071 | return NULL; // missing data |
| 2072 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2073 | /* stream error : send RST_STREAM */ |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2074 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2075 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2076 | return NULL; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2077 | } |
| 2078 | |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2079 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2080 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2081 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2082 | 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] | 2083 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2084 | else if (h2s->flags & H2_SF_ES_RCVD) { |
| 2085 | if (h2s->st == H2_SS_OPEN) |
| 2086 | h2s->st = H2_SS_HREM; |
| 2087 | else if (h2s->st == H2_SS_HLOC) |
| 2088 | h2s_close(h2s); |
| 2089 | } |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2090 | |
| 2091 | return h2s; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2092 | } |
| 2093 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2094 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 2095 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 2096 | */ |
| 2097 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 2098 | { |
| 2099 | int error; |
| 2100 | |
| 2101 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 2102 | * to signal an end of stream (with the ES flag). |
| 2103 | */ |
| 2104 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2105 | if (!b_size(&h2c->dbuf) && h2c->dfl) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2106 | return 0; // empty buffer |
| 2107 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2108 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2109 | return 0; // incomplete frame |
| 2110 | |
| 2111 | /* now either the frame is complete or the buffer is complete */ |
| 2112 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2113 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2114 | /* RFC7540#6.1 */ |
| 2115 | error = H2_ERR_STREAM_CLOSED; |
| 2116 | goto strm_err; |
| 2117 | } |
| 2118 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2119 | if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) { |
| 2120 | /* RFC7540#8.1.2 */ |
| 2121 | error = H2_ERR_PROTOCOL_ERROR; |
| 2122 | goto strm_err; |
| 2123 | } |
| 2124 | |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 2125 | if (!h2_frt_transfer_data(h2s)) |
| 2126 | return 0; |
| 2127 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2128 | /* call the upper layers to process the frame, then let the upper layer |
| 2129 | * notify the stream about any change. |
| 2130 | */ |
| 2131 | if (!h2s->cs) { |
| 2132 | error = H2_ERR_STREAM_CLOSED; |
| 2133 | goto strm_err; |
| 2134 | } |
| 2135 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 2136 | if (h2c->st0 >= H2_CS_ERROR) |
| 2137 | return 0; |
| 2138 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2139 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2140 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2141 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | /* check for completion : the callee will change this to FRAME_A or |
| 2145 | * FRAME_H once done. |
| 2146 | */ |
| 2147 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2148 | return 0; |
| 2149 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2150 | /* last frame */ |
| 2151 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2152 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2153 | if (h2s->st == H2_SS_OPEN) |
| 2154 | h2s->st = H2_SS_HREM; |
| 2155 | else |
| 2156 | h2s_close(h2s); |
| 2157 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2158 | if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) { |
| 2159 | /* RFC7540#8.1.2 */ |
| 2160 | error = H2_ERR_PROTOCOL_ERROR; |
| 2161 | goto strm_err; |
| 2162 | } |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2163 | } |
| 2164 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2165 | return 1; |
| 2166 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2167 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 2168 | h2s_error(h2s, error); |
| 2169 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2170 | return 0; |
| 2171 | } |
| 2172 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2173 | /* process Rx frames to be demultiplexed */ |
| 2174 | static void h2_process_demux(struct h2c *h2c) |
| 2175 | { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2176 | struct h2s *h2s = NULL, *tmp_h2s; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2177 | struct h2_fh hdr; |
| 2178 | unsigned int padlen = 0; |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 2179 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2180 | if (h2c->st0 >= H2_CS_ERROR) |
| 2181 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2182 | |
| 2183 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2184 | if (h2c->st0 == H2_CS_PREFACE) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2185 | if (h2c->flags & H2_CF_IS_BACK) |
| 2186 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2187 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 2188 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2189 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2190 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2191 | sess_log(h2c->conn->owner); |
| 2192 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2193 | goto fail; |
| 2194 | } |
| 2195 | |
| 2196 | h2c->max_id = 0; |
| 2197 | h2c->st0 = H2_CS_SETTINGS1; |
| 2198 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2199 | |
| 2200 | if (h2c->st0 == H2_CS_SETTINGS1) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2201 | /* ensure that what is pending is a valid SETTINGS frame |
| 2202 | * without an ACK. |
| 2203 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2204 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2205 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2206 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2207 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2208 | sess_log(h2c->conn->owner); |
| 2209 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2210 | goto fail; |
| 2211 | } |
| 2212 | |
| 2213 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 2214 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2215 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2216 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2217 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2218 | goto fail; |
| 2219 | } |
| 2220 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2221 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2222 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2223 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2224 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2225 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2226 | goto fail; |
| 2227 | } |
| 2228 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2229 | /* that's OK, switch to FRAME_P to process it. This is |
| 2230 | * a SETTINGS frame whose header has already been |
| 2231 | * deleted above. |
| 2232 | */ |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2233 | padlen = 0; |
| 2234 | goto new_frame; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2235 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2236 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2237 | |
| 2238 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2239 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2240 | int ret = 0; |
| 2241 | |
| 2242 | if (h2c->st0 >= H2_CS_ERROR) |
| 2243 | break; |
| 2244 | |
| 2245 | if (h2c->st0 == H2_CS_FRAME_H) { |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 2246 | if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2247 | break; |
| 2248 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2249 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2250 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2251 | if (!h2c->nb_streams) { |
| 2252 | /* only log if no other stream can report the error */ |
| 2253 | sess_log(h2c->conn->owner); |
| 2254 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2255 | break; |
| 2256 | } |
| 2257 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2258 | if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) { |
| 2259 | /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA), |
| 2260 | * we read the pad length and drop it from the remaining |
| 2261 | * payload (one byte + the 9 remaining ones = 10 total |
| 2262 | * removed), so we have a frame payload starting after the |
| 2263 | * pad len. Flow controlled frames (DATA) also count the |
| 2264 | * padlen in the flow control, so it must be adjusted. |
| 2265 | */ |
| 2266 | if (hdr.len < 1) { |
| 2267 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2268 | sess_log(h2c->conn->owner); |
| 2269 | goto fail; |
| 2270 | } |
| 2271 | hdr.len--; |
| 2272 | |
| 2273 | if (b_data(&h2c->dbuf) < 10) |
| 2274 | break; // missing padlen |
| 2275 | |
| 2276 | padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9); |
| 2277 | |
| 2278 | if (padlen > hdr.len) { |
| 2279 | /* RFC7540#6.1 : pad length = length of |
| 2280 | * frame payload or greater => error. |
| 2281 | */ |
| 2282 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2283 | sess_log(h2c->conn->owner); |
| 2284 | goto fail; |
| 2285 | } |
| 2286 | |
| 2287 | if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) { |
| 2288 | h2c->rcvd_c++; |
| 2289 | h2c->rcvd_s++; |
| 2290 | } |
| 2291 | b_del(&h2c->dbuf, 1); |
| 2292 | } |
| 2293 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2294 | |
| 2295 | new_frame: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2296 | h2c->dfl = hdr.len; |
| 2297 | h2c->dsi = hdr.sid; |
| 2298 | h2c->dft = hdr.ft; |
| 2299 | h2c->dff = hdr.ff; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2300 | h2c->dpl = padlen; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2301 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2302 | |
| 2303 | /* check for minimum basic frame format validity */ |
| 2304 | ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize); |
| 2305 | if (ret != H2_ERR_NO_ERROR) { |
| 2306 | h2c_error(h2c, ret); |
| 2307 | sess_log(h2c->conn->owner); |
| 2308 | goto fail; |
| 2309 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2310 | } |
| 2311 | |
| 2312 | /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2313 | tmp_h2s = h2c_st_by_id(h2c, h2c->dsi); |
| 2314 | |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2315 | if (tmp_h2s != h2s && h2s && h2s->cs && |
| 2316 | (b_data(&h2s->rxbuf) || |
Willy Tarreau | 99ad1b3 | 2019-05-14 11:46:28 +0200 | [diff] [blame] | 2317 | (H2_SS_MASK(h2s->st) & H2_SS_EOS_BITS) || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2318 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2319 | (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] | 2320 | /* we may have to signal the upper layers */ |
| 2321 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2322 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2323 | } |
| 2324 | h2s = tmp_h2s; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2325 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 2326 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2327 | goto strm_err; |
| 2328 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2329 | if (h2s->st == H2_SS_IDLE && |
| 2330 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 2331 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 2332 | * this state MUST be treated as a connection error |
| 2333 | */ |
| 2334 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2335 | if (!h2c->nb_streams) { |
| 2336 | /* only log if no other stream can report the error */ |
| 2337 | sess_log(h2c->conn->owner); |
| 2338 | } |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2339 | break; |
| 2340 | } |
| 2341 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2342 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 2343 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 2344 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2345 | * this state MUST be treated as a stream error. |
| 2346 | * 6.2, 6.6 and 6.10 further mandate that HEADERS/ |
| 2347 | * PUSH_PROMISE/CONTINUATION cause connection errors. |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2348 | */ |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2349 | if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) |
| 2350 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2351 | else |
| 2352 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2353 | goto strm_err; |
| 2354 | } |
| 2355 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2356 | /* Below the management of frames received in closed state is a |
| 2357 | * bit hackish because the spec makes strong differences between |
| 2358 | * streams closed by receiving RST, sending RST, and seeing ES |
| 2359 | * in both directions. In addition to this, the creation of a |
| 2360 | * new stream reusing the identifier of a closed one will be |
| 2361 | * detected here. Given that we cannot keep track of all closed |
| 2362 | * streams forever, we consider that unknown closed streams were |
| 2363 | * closed on RST received, which allows us to respond with an |
| 2364 | * RST without breaking the connection (eg: to abort a transfer). |
| 2365 | * Some frames have to be silently ignored as well. |
| 2366 | */ |
| 2367 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
Willy Tarreau | 3ad5d31 | 2019-01-29 18:33:26 +0100 | [diff] [blame] | 2368 | 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] | 2369 | /* #5.1.1: The identifier of a newly |
| 2370 | * established stream MUST be numerically |
| 2371 | * greater than all streams that the initiating |
| 2372 | * endpoint has opened or reserved. This |
| 2373 | * governs streams that are opened using a |
| 2374 | * HEADERS frame and streams that are reserved |
| 2375 | * using PUSH_PROMISE. An endpoint that |
| 2376 | * receives an unexpected stream identifier |
| 2377 | * MUST respond with a connection error. |
| 2378 | */ |
| 2379 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2380 | goto strm_err; |
| 2381 | } |
| 2382 | |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2383 | 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] | 2384 | /* RFC7540#5.1:closed: an endpoint that |
| 2385 | * receives any frame other than PRIORITY after |
| 2386 | * receiving a RST_STREAM MUST treat that as a |
| 2387 | * stream error of type STREAM_CLOSED. |
| 2388 | * |
| 2389 | * Note that old streams fall into this category |
| 2390 | * and will lead to an RST being sent. |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2391 | * |
| 2392 | * However, we cannot generalize this to all frame types. Those |
| 2393 | * carrying compression state must still be processed before |
| 2394 | * being dropped or we'll desynchronize the decoder. This can |
| 2395 | * happen with request trailers received after sending an |
| 2396 | * RST_STREAM, or with header/trailers responses received after |
| 2397 | * sending RST_STREAM (aborted stream). |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2398 | */ |
| 2399 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2400 | h2c->st0 = H2_CS_FRAME_E; |
| 2401 | goto strm_err; |
| 2402 | } |
| 2403 | |
| 2404 | /* RFC7540#5.1:closed: if this state is reached as a |
| 2405 | * result of sending a RST_STREAM frame, the peer that |
| 2406 | * receives the RST_STREAM might have already sent |
| 2407 | * frames on the stream that cannot be withdrawn. An |
| 2408 | * endpoint MUST ignore frames that it receives on |
| 2409 | * closed streams after it has sent a RST_STREAM |
| 2410 | * frame. An endpoint MAY choose to limit the period |
| 2411 | * over which it ignores frames and treat frames that |
| 2412 | * arrive after this time as being in error. |
| 2413 | */ |
Willy Tarreau | 24ff1f8 | 2019-01-30 19:20:09 +0100 | [diff] [blame] | 2414 | if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2415 | /* RFC7540#5.1:closed: any frame other than |
| 2416 | * PRIO/WU/RST in this state MUST be treated as |
| 2417 | * a connection error |
| 2418 | */ |
| 2419 | if (h2c->dft != H2_FT_RST_STREAM && |
| 2420 | h2c->dft != H2_FT_PRIORITY && |
| 2421 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 2422 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2423 | goto strm_err; |
| 2424 | } |
| 2425 | } |
| 2426 | } |
| 2427 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2428 | #if 0 |
| 2429 | // problem below: it is not possible to completely ignore such |
| 2430 | // streams as we need to maintain the compression state as well |
| 2431 | // and for this we need to completely process these frames (eg: |
| 2432 | // HEADERS frames) as well as counting DATA frames to emit |
| 2433 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 2434 | // This is a typical case of layer violation where the |
| 2435 | // transported contents are critical to the connection's |
| 2436 | // validity and must be ignored at the same time :-( |
| 2437 | |
| 2438 | /* graceful shutdown, ignore streams whose ID is higher than |
| 2439 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 2440 | */ |
| 2441 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2442 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2443 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2444 | h2c->dfl -= ret; |
| 2445 | ret = h2c->dfl == 0; |
| 2446 | goto strm_err; |
| 2447 | } |
| 2448 | #endif |
| 2449 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2450 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 2451 | case H2_FT_SETTINGS: |
| 2452 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2453 | ret = h2c_handle_settings(h2c); |
| 2454 | |
| 2455 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2456 | ret = h2c_ack_settings(h2c); |
| 2457 | break; |
| 2458 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 2459 | case H2_FT_PING: |
| 2460 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2461 | ret = h2c_handle_ping(h2c); |
| 2462 | |
| 2463 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2464 | ret = h2c_ack_ping(h2c); |
| 2465 | break; |
| 2466 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 2467 | case H2_FT_WINDOW_UPDATE: |
| 2468 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2469 | ret = h2c_handle_window_update(h2c, h2s); |
| 2470 | break; |
| 2471 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2472 | case H2_FT_CONTINUATION: |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2473 | /* RFC7540#6.10: CONTINUATION may only be preceeded by |
| 2474 | * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These |
| 2475 | * frames' parsers consume all following CONTINUATION |
| 2476 | * frames so this one is out of sequence. |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2477 | */ |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2478 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2479 | sess_log(h2c->conn->owner); |
| 2480 | goto fail; |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2481 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2482 | case H2_FT_HEADERS: |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2483 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2484 | if (h2c->flags & H2_CF_IS_BACK) |
| 2485 | tmp_h2s = h2c_bck_handle_headers(h2c, h2s); |
| 2486 | else |
| 2487 | tmp_h2s = h2c_frt_handle_headers(h2c, h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2488 | if (tmp_h2s) { |
| 2489 | h2s = tmp_h2s; |
| 2490 | ret = 1; |
| 2491 | } |
| 2492 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2493 | break; |
| 2494 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2495 | case H2_FT_DATA: |
| 2496 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2497 | ret = h2c_frt_handle_data(h2c, h2s); |
| 2498 | |
| 2499 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2500 | ret = h2c_send_strm_wu(h2c); |
| 2501 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2502 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 2503 | case H2_FT_PRIORITY: |
| 2504 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2505 | ret = h2c_handle_priority(h2c); |
| 2506 | break; |
| 2507 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2508 | case H2_FT_RST_STREAM: |
| 2509 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2510 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 2511 | break; |
| 2512 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 2513 | case H2_FT_GOAWAY: |
| 2514 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2515 | ret = h2c_handle_goaway(h2c); |
| 2516 | break; |
| 2517 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2518 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2519 | default: |
| 2520 | /* drop frames that we ignore. They may be larger than |
| 2521 | * the buffer so we drain all of their contents until |
| 2522 | * we reach the end. |
| 2523 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2524 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2525 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2526 | h2c->dfl -= ret; |
| 2527 | ret = h2c->dfl == 0; |
| 2528 | } |
| 2529 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2530 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2531 | /* We may have to send an RST if not done yet */ |
| 2532 | if (h2s->st == H2_SS_ERROR) |
| 2533 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2534 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2535 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2536 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2537 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2538 | /* error or missing data condition met above ? */ |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2539 | if (ret <= 0) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2540 | break; |
| 2541 | |
| 2542 | if (h2c->st0 != H2_CS_FRAME_H) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2543 | b_del(&h2c->dbuf, h2c->dfl); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2544 | h2c->st0 = H2_CS_FRAME_H; |
| 2545 | } |
| 2546 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2547 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2548 | if (h2c->rcvd_c > 0 && |
| 2549 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2550 | h2c_send_conn_wu(h2c); |
| 2551 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2552 | fail: |
| 2553 | /* we can go here on missing data, blocked response or error */ |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2554 | if (h2s && h2s->cs && |
| 2555 | (b_data(&h2s->rxbuf) || |
Willy Tarreau | 99ad1b3 | 2019-05-14 11:46:28 +0200 | [diff] [blame] | 2556 | (H2_SS_MASK(h2s->st) & H2_SS_EOS_BITS) || |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 2557 | (h2s->flags & H2_SF_ES_RCVD) || |
| 2558 | (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] | 2559 | /* we may have to signal the upper layers */ |
| 2560 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2561 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2562 | } |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2563 | |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 2564 | h2c_restart_reading(h2c, 0); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2565 | } |
| 2566 | |
| 2567 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2568 | * the end. |
| 2569 | */ |
| 2570 | static int h2_process_mux(struct h2c *h2c) |
| 2571 | { |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2572 | struct h2s *h2s, *h2s_back; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2573 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2574 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2575 | if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) { |
| 2576 | if (unlikely(h2c_bck_send_preface(h2c) <= 0)) { |
| 2577 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2578 | if (h2c->st0 == H2_CS_ERROR) { |
| 2579 | h2c->st0 = H2_CS_ERROR2; |
| 2580 | sess_log(h2c->conn->owner); |
| 2581 | } |
| 2582 | goto fail; |
| 2583 | } |
| 2584 | h2c->st0 = H2_CS_SETTINGS1; |
| 2585 | } |
| 2586 | /* need to wait for the other side */ |
Willy Tarreau | 75a930a | 2018-12-12 08:03:58 +0100 | [diff] [blame] | 2587 | if (h2c->st0 < H2_CS_FRAME_H) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2588 | return 1; |
| 2589 | } |
| 2590 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2591 | /* start by sending possibly pending window updates */ |
| 2592 | if (h2c->rcvd_c > 0 && |
| 2593 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2594 | h2c_send_conn_wu(h2c) < 0) |
| 2595 | goto fail; |
| 2596 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2597 | /* First we always process the flow control list because the streams |
| 2598 | * waiting there were already elected for immediate emission but were |
| 2599 | * blocked just on this. |
| 2600 | */ |
| 2601 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2602 | list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2603 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2604 | h2c->st0 >= H2_CS_ERROR) |
| 2605 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2606 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2607 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2608 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2609 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2610 | h2s->flags &= ~H2_SF_BLK_ANY; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2611 | /* For some reason, the upper layer failed to subsribe again, |
| 2612 | * so remove it from the send_list |
| 2613 | */ |
| 2614 | if (!h2s->send_wait) { |
| 2615 | LIST_DEL_INIT(&h2s->list); |
| 2616 | continue; |
| 2617 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2618 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2619 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2620 | tasklet_wakeup(h2s->send_wait->task); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2621 | } |
| 2622 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2623 | list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2624 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2625 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2626 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2627 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2628 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2629 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2630 | /* For some reason, the upper layer failed to subsribe again, |
| 2631 | * so remove it from the send_list |
| 2632 | */ |
| 2633 | if (!h2s->send_wait) { |
| 2634 | LIST_DEL_INIT(&h2s->list); |
| 2635 | continue; |
| 2636 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2637 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2638 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2639 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2640 | tasklet_wakeup(h2s->send_wait->task); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2641 | } |
| 2642 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2643 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2644 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2645 | if (h2c->st0 == H2_CS_ERROR) { |
| 2646 | if (h2c->max_id >= 0) { |
| 2647 | h2c_send_goaway_error(h2c, NULL); |
| 2648 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2649 | return 0; |
| 2650 | } |
| 2651 | |
| 2652 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2653 | } |
| 2654 | return 1; |
| 2655 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2656 | return (1); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2657 | } |
| 2658 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2659 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2660 | /* Attempt to read data, and subscribe if none available. |
| 2661 | * The function returns 1 if data has been received, otherwise zero. |
| 2662 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2663 | static int h2_recv(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2664 | { |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2665 | struct connection *conn = h2c->conn; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2666 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2667 | int max; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2668 | size_t ret; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2669 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2670 | if (h2c->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2671 | return (b_data(&h2c->dbuf)); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2672 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2673 | if (!h2_recv_allowed(h2c)) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2674 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2675 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2676 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2677 | if (!buf) { |
| 2678 | h2c->flags |= H2_CF_DEM_DALLOC; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2679 | return 0; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2680 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2681 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2682 | do { |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 2683 | b_realign_if_empty(buf); |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2684 | if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
| 2685 | /* HTX in use : try to pre-align the buffer like the |
| 2686 | * rxbufs will be to optimize memory copies. We'll make |
| 2687 | * sure that the frame header lands at the end of the |
| 2688 | * HTX block to alias it upon recv. We cannot use the |
| 2689 | * head because rcv_buf() will realign the buffer if |
| 2690 | * it's empty. Thus we cheat and pretend we already |
| 2691 | * have a few bytes there. |
| 2692 | */ |
| 2693 | max = buf_room_for_htx_data(buf) + 9; |
Willy Tarreau | c0960d1 | 2018-12-14 10:59:15 +0100 | [diff] [blame] | 2694 | buf->head = sizeof(struct htx) - 9; |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2695 | } |
| 2696 | else |
| 2697 | max = b_room(buf); |
| 2698 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2699 | if (max) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2700 | ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2701 | else |
| 2702 | ret = 0; |
| 2703 | } while (ret > 0); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2704 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2705 | if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2706 | 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] | 2707 | |
Olivier Houchard | a1411e6 | 2018-08-17 18:42:48 +0200 | [diff] [blame] | 2708 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2709 | h2_release_buf(h2c, &h2c->dbuf); |
Olivier Houchard | 4667773 | 2018-11-29 17:06:17 +0100 | [diff] [blame] | 2710 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2711 | } |
| 2712 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2713 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2714 | h2c->flags |= H2_CF_DEM_DFULL; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2715 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2716 | } |
| 2717 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2718 | /* Try to send data if possible. |
| 2719 | * The function returns 1 if data have been sent, otherwise zero. |
| 2720 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2721 | static int h2_send(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2722 | { |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2723 | struct connection *conn = h2c->conn; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2724 | int done; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2725 | int sent = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2726 | |
| 2727 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 2728 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2729 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2730 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2731 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2732 | /* a handshake was requested */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2733 | goto schedule; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2734 | } |
| 2735 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2736 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2737 | * pending streams into the existing buffer until it's reportedly full |
| 2738 | * or the end of send requests is reached. Then it tries to send this |
| 2739 | * buffer's contents out, marks it not full if at least one byte could |
| 2740 | * be sent, and tries again. |
| 2741 | * |
| 2742 | * The snd_buf() function normally takes a "flags" argument which may |
| 2743 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2744 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2745 | * connection is streaming lots of data (used to increase TLS record |
| 2746 | * size at the expense of latency). The former can be sent any time |
| 2747 | * there's a buffer full flag, as it indicates at least one stream |
| 2748 | * attempted to send and failed so there are pending data. An |
| 2749 | * alternative would be to set it as long as there's an active stream |
| 2750 | * but that would be problematic for ACKs until we have an absolute |
| 2751 | * guarantee that all waiters have at least one byte to send. The |
| 2752 | * latter should possibly not be set for now. |
| 2753 | */ |
| 2754 | |
| 2755 | done = 0; |
| 2756 | while (!done) { |
| 2757 | unsigned int flags = 0; |
| 2758 | |
| 2759 | /* fill as much as we can into the current buffer */ |
| 2760 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2761 | done = h2_process_mux(h2c); |
| 2762 | |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2763 | if (h2c->flags & H2_CF_MUX_MALLOC) |
| 2764 | break; |
| 2765 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2766 | if (conn->flags & CO_FL_ERROR) |
| 2767 | break; |
| 2768 | |
| 2769 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2770 | flags |= CO_SFL_MSG_MORE; |
| 2771 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2772 | if (b_data(&h2c->mbuf)) { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2773 | int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h2c->mbuf, b_data(&h2c->mbuf), flags); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2774 | if (!ret) |
| 2775 | break; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2776 | sent = 1; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2777 | b_del(&h2c->mbuf, ret); |
| 2778 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2779 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2780 | |
| 2781 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2782 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2783 | } |
| 2784 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2785 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2786 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2787 | b_reset(&h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2788 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2789 | /* We're not full anymore, so we can wake any task that are waiting |
| 2790 | * for us. |
| 2791 | */ |
| 2792 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2793 | struct h2s *h2s; |
| 2794 | |
| 2795 | list_for_each_entry(h2s, &h2c->send_list, list) { |
| 2796 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2797 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2798 | |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 2799 | if (LIST_ADDED(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2800 | continue; |
| 2801 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2802 | /* For some reason, the upper layer failed to subsribe again, |
| 2803 | * so remove it from the send_list |
| 2804 | */ |
| 2805 | if (!h2s->send_wait) { |
| 2806 | LIST_DEL_INIT(&h2s->list); |
| 2807 | continue; |
| 2808 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2809 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2810 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2811 | tasklet_wakeup(h2s->send_wait->task); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2812 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 2813 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2814 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2815 | /* We're done, no more to send */ |
| 2816 | if (!b_data(&h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2817 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2818 | schedule: |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2819 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2820 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event); |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2821 | return sent; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2822 | } |
| 2823 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 2824 | /* this is the tasklet referenced in h2c->wait_event.task */ |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2825 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2826 | { |
| 2827 | struct h2c *h2c = ctx; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2828 | int ret = 0; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2829 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2830 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2831 | ret = h2_send(h2c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2832 | if (!(h2c->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2833 | ret |= h2_recv(h2c); |
Willy Tarreau | cef5c8e | 2018-12-18 10:29:54 +0100 | [diff] [blame] | 2834 | if (ret || b_data(&h2c->dbuf)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2835 | h2_process(h2c); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2836 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2837 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2838 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2839 | /* callback called on any event by the connection handler. |
| 2840 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2841 | * destruction of the connection (which normally doesn not happen in h2). |
| 2842 | */ |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2843 | static int h2_process(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2844 | { |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2845 | struct connection *conn = h2c->conn; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2846 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2847 | if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2848 | h2_process_demux(h2c); |
| 2849 | |
| 2850 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2851 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2852 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2853 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2854 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 2855 | } |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2856 | h2_send(h2c); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2857 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 2858 | if (unlikely(h2c->proxy->state == PR_STSTOPPED)) { |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2859 | /* frontend is stopping, reload likely in progress, let's try |
| 2860 | * to announce a graceful shutdown if not yet done. We don't |
| 2861 | * care if it fails, it will be tried again later. |
| 2862 | */ |
| 2863 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 2864 | if (h2c->last_sid < 0) |
| 2865 | h2c->last_sid = (1U << 31) - 1; |
| 2866 | h2c_send_goaway_error(h2c, NULL); |
| 2867 | } |
| 2868 | } |
| 2869 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2870 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2871 | * If we received early data, and the handshake is done, wake |
| 2872 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2873 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2874 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 2875 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 2876 | struct eb32_node *node; |
| 2877 | struct h2s *h2s; |
| 2878 | |
| 2879 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 2880 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 2881 | |
| 2882 | while (node) { |
| 2883 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | fde287c | 2018-12-19 18:33:16 +0100 | [diff] [blame] | 2884 | if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2885 | h2s_notify_recv(h2s); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2886 | node = eb32_next(node); |
| 2887 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2888 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2889 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 2890 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 2891 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 2892 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 2893 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 2894 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2895 | |
| 2896 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2897 | /* no more stream, kill the connection now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2898 | h2_release(h2c); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2899 | return -1; |
| 2900 | } |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2901 | } |
| 2902 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2903 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2904 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2905 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2906 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 2907 | h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 2908 | (h2c->st0 != H2_CS_ERROR && |
| 2909 | !b_data(&h2c->mbuf) && |
| 2910 | (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && |
| 2911 | ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list)))) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2912 | h2_release_buf(h2c, &h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2913 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2914 | if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2915 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2916 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2917 | task_queue(h2c->task); |
| 2918 | } |
| 2919 | else |
| 2920 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2921 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2922 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2923 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2924 | return 0; |
| 2925 | } |
| 2926 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 2927 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2928 | static int h2_wake(struct connection *conn) |
| 2929 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2930 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2931 | |
| 2932 | return (h2_process(h2c)); |
| 2933 | } |
| 2934 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2935 | /* Connection timeout management. The principle is that if there's no receipt |
| 2936 | * nor sending for a certain amount of time, the connection is closed. If the |
| 2937 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 2938 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 2939 | * GOAWAY frame. |
| 2940 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2941 | 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] | 2942 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2943 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2944 | int expired = tick_is_expired(t->expire, now_ms); |
| 2945 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2946 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2947 | return t; |
| 2948 | |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 2949 | task_destroy(t); |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2950 | |
| 2951 | if (!h2c) { |
| 2952 | /* resources were already deleted */ |
| 2953 | return NULL; |
| 2954 | } |
| 2955 | |
| 2956 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2957 | h2c_error(h2c, H2_ERR_NO_ERROR); |
Willy Tarreau | 2348291 | 2019-05-07 15:23:14 +0200 | [diff] [blame] | 2958 | h2_wake_some_streams(h2c, 0); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2959 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2960 | if (b_data(&h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2961 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 2962 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2963 | } |
| 2964 | |
| 2965 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2966 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2967 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 2968 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2969 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2970 | if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2971 | int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, &h2c->mbuf, b_data(&h2c->mbuf), 0); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2972 | if (ret > 0) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2973 | b_del(&h2c->mbuf, ret); |
| 2974 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2975 | } |
| 2976 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2977 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2978 | /* either we can release everything now or it will be done later once |
| 2979 | * the last stream closes. |
| 2980 | */ |
| 2981 | if (eb_is_empty(&h2c->streams_by_id)) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2982 | h2_release(h2c); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2983 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2984 | return NULL; |
| 2985 | } |
| 2986 | |
| 2987 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2988 | /*******************************************/ |
| 2989 | /* functions below are used by the streams */ |
| 2990 | /*******************************************/ |
| 2991 | |
| 2992 | /* |
| 2993 | * Attach a new stream to a connection |
| 2994 | * (Used for outgoing connections) |
| 2995 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2996 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2997 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2998 | struct conn_stream *cs; |
| 2999 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3000 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3001 | |
| 3002 | cs = cs_new(conn); |
| 3003 | if (!cs) |
| 3004 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3005 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 3006 | if (!h2s) { |
| 3007 | cs_free(cs); |
| 3008 | return NULL; |
| 3009 | } |
| 3010 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3011 | } |
| 3012 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3013 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 3014 | * We have to scan because we may have some orphan streams. It might be |
| 3015 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 3016 | * orphans. |
| 3017 | */ |
| 3018 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 3019 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3020 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3021 | struct h2s *h2s; |
| 3022 | struct eb32_node *node; |
| 3023 | |
| 3024 | node = eb32_first(&h2c->streams_by_id); |
| 3025 | while (node) { |
| 3026 | h2s = container_of(node, struct h2s, by_id); |
| 3027 | if (h2s->cs) |
| 3028 | return h2s->cs; |
| 3029 | node = eb32_next(node); |
| 3030 | } |
| 3031 | return NULL; |
| 3032 | } |
| 3033 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3034 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3035 | * Destroy the mux and the associated connection, if it is no longer used |
| 3036 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3037 | static void h2_destroy(void *ctx) |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3038 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3039 | struct h2c *h2c = ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3040 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 3041 | 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] | 3042 | h2_release(h2c); |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3043 | } |
| 3044 | |
| 3045 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3046 | * Detach the stream from the connection and possibly release the connection. |
| 3047 | */ |
| 3048 | static void h2_detach(struct conn_stream *cs) |
| 3049 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3050 | struct h2s *h2s = cs->ctx; |
| 3051 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3052 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3053 | |
| 3054 | cs->ctx = NULL; |
| 3055 | if (!h2s) |
| 3056 | return; |
| 3057 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3058 | /* 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] | 3059 | if (LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3060 | h2s->send_wait != &h2s->wait_event) { |
| 3061 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
| 3062 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d9986ed | 2019-05-09 13:24:08 +0200 | [diff] [blame] | 3063 | /* |
| 3064 | * At this point, the stream_interface is supposed to have called |
| 3065 | * h2_unsubscribe(), so the only way there's still a |
| 3066 | * subscription that came from the stream_interface (as we |
| 3067 | * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(), |
| 3068 | * without the stream_interface involved) is that we subscribed |
| 3069 | * for sending, we woke the tasklet up and removed the |
| 3070 | * SUB_RETRY_SEND flag, so the stream_interface would not |
| 3071 | * know it has to unsubscribe for send, but the tasklet hasn't |
| 3072 | * run yet. Make sure to handle that by explicitely setting |
| 3073 | * send_wait to NULL, as nothing else will do it for us. |
| 3074 | */ |
| 3075 | h2s->send_wait = NULL; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3076 | } |
| 3077 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3078 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3079 | h2c = h2s->h2c; |
| 3080 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 3081 | h2c->nb_cs--; |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 3082 | if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY && |
| 3083 | !h2_frt_has_too_many_cs(h2c)) { |
| 3084 | /* frontend connection was blocking new streams creation */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3085 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3086 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3087 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3088 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3089 | /* this stream may be blocked waiting for some data to leave (possibly |
| 3090 | * an ES or RST frame), so orphan it in this case. |
| 3091 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 3092 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 3093 | (h2c->st0 < H2_CS_ERROR) && |
Olivier Houchard | 16ff261 | 2019-03-21 15:48:46 +0100 | [diff] [blame] | 3094 | (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] | 3095 | return; |
| 3096 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3097 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 3098 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 3099 | /* unblock the connection if it was blocked on this |
| 3100 | * stream. |
| 3101 | */ |
| 3102 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 3103 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3104 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3105 | } |
| 3106 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 3107 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3108 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3109 | if (h2c->flags & H2_CF_IS_BACK && |
| 3110 | (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3111 | if (!(h2c->conn->flags & |
| 3112 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 3113 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3114 | h2c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3115 | if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) { |
| 3116 | h2c->conn->owner = NULL; |
| 3117 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3118 | if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn)) |
| 3119 | /* The server doesn't want it, let's kill the connection right away */ |
| 3120 | h2c->conn->mux->destroy(h2c->conn); |
| 3121 | return; |
| 3122 | } |
| 3123 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3124 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3125 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3126 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 3127 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 3128 | return; |
| 3129 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3130 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3131 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3132 | h2c->conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3133 | if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3134 | struct server *srv = objt_server(h2c->conn->target); |
| 3135 | |
| 3136 | if (srv) { |
| 3137 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 3138 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 3139 | else |
| 3140 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 3141 | } |
| 3142 | |
| 3143 | } |
| 3144 | } |
| 3145 | } |
| 3146 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3147 | /* We don't want to close right now unless we're removing the |
| 3148 | * last stream, and either the connection is in error, or it |
| 3149 | * reached the ID already specified in a GOAWAY frame received |
| 3150 | * or sent (as seen by last_sid >= 0). |
| 3151 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3152 | if (h2c_is_dead(h2c)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3153 | /* no more stream will come, kill it now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3154 | h2_release(h2c); |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3155 | } |
| 3156 | else if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3157 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3158 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3159 | task_queue(h2c->task); |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 3160 | } |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3161 | else |
| 3162 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3163 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3164 | } |
| 3165 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3166 | /* Performs a synchronous or asynchronous shutr(). */ |
| 3167 | static void h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3168 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3169 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3170 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3171 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3172 | if (h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3173 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3174 | |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3175 | /* a connstream may require us to immediately kill the whole connection |
| 3176 | * for example because of a "tcp-request content reject" rule that is |
| 3177 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3178 | * close the connection. |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3179 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3180 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3181 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3182 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3183 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3184 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3185 | else if (!(h2s->flags & H2_SF_HEADERS_SENT)) { |
| 3186 | /* Nothing was never sent for this stream, so reset with |
| 3187 | * REFUSED_STREAM error to let the client retry the |
| 3188 | * request. |
| 3189 | */ |
| 3190 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3191 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3192 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3193 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3194 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3195 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3196 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3197 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3198 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3199 | h2s_close(h2s); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3200 | done: |
| 3201 | h2s->flags &= ~H2_SF_WANT_SHUTR; |
| 3202 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3203 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3204 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3205 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3206 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3207 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3208 | h2s->send_wait = sw; |
| 3209 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3210 | h2s->send_wait = sw; |
| 3211 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3212 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3213 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3214 | /* Let the handler know we want shutr */ |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3215 | h2s->flags |= H2_SF_WANT_SHUTR; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3216 | return; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3217 | } |
| 3218 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3219 | /* Performs a synchronous or asynchronous shutw(). */ |
| 3220 | static void h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3221 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3222 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3223 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3224 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3225 | if (h2s->st == H2_SS_CLOSED) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3226 | goto done; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3227 | |
Willy Tarreau | f983d00 | 2019-05-14 10:40:21 +0200 | [diff] [blame] | 3228 | if (h2s->st != H2_SS_HLOC && h2s->st != H2_SS_ERROR && |
| 3229 | (h2s->flags & H2_SF_HEADERS_SENT)) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3230 | /* we can cleanly close using an empty data frame only after headers */ |
| 3231 | |
| 3232 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 3233 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3234 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3235 | |
| 3236 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3237 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3238 | else |
| 3239 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3240 | } else { |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3241 | /* a connstream may require us to immediately kill the whole connection |
| 3242 | * for example because of a "tcp-request content reject" rule that is |
| 3243 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3244 | * close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3245 | */ |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3246 | if ((h2s->flags & H2_SF_KILL_CONN) && |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3247 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3248 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3249 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3250 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3251 | else { |
| 3252 | /* Nothing was never sent for this stream, so reset with |
| 3253 | * REFUSED_STREAM error to let the client retry the |
| 3254 | * request. |
| 3255 | */ |
| 3256 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3257 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3258 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3259 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3260 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3261 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3262 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3263 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3264 | } |
| 3265 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3266 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3267 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3268 | done: |
| 3269 | h2s->flags &= ~H2_SF_WANT_SHUTW; |
| 3270 | return; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3271 | |
| 3272 | add_to_list: |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 3273 | if (!LIST_ADDED(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3274 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3275 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3276 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3277 | h2s->send_wait = sw; |
| 3278 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3279 | h2s->send_wait = sw; |
| 3280 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3281 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3282 | } |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3283 | /* let the handler know we want to shutw */ |
| 3284 | h2s->flags |= H2_SF_WANT_SHUTW; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3285 | return; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3286 | } |
| 3287 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3288 | /* This is the tasklet referenced in h2s->wait_event.task, it is used for |
| 3289 | * deferred shutdowns when the h2_detach() was done but the mux buffer was full |
| 3290 | * and prevented the last frame from being emitted. |
| 3291 | */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3292 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3293 | { |
| 3294 | struct h2s *h2s = ctx; |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3295 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3296 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3297 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3298 | if (h2s->flags & H2_SF_WANT_SHUTW) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3299 | h2_do_shutw(h2s); |
| 3300 | |
Willy Tarreau | 2c249eb | 2019-05-13 18:06:17 +0200 | [diff] [blame] | 3301 | if (h2s->flags & H2_SF_WANT_SHUTR) |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3302 | h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3303 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3304 | if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) { |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3305 | /* We're done trying to send, remove ourself from the send_list */ |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3306 | LIST_DEL_INIT(&h2s->list); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3307 | |
Willy Tarreau | 88bdba3 | 2019-05-13 18:17:53 +0200 | [diff] [blame] | 3308 | if (!h2s->cs) { |
| 3309 | h2s_destroy(h2s); |
| 3310 | if (h2c_is_dead(h2c)) |
| 3311 | h2_release(h2c); |
| 3312 | } |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3313 | } |
| 3314 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3315 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3316 | } |
| 3317 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3318 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3319 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3320 | { |
| 3321 | struct h2s *h2s = cs->ctx; |
| 3322 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3323 | if (cs->flags & CS_FL_KILL_CONN) |
| 3324 | h2s->flags |= H2_SF_KILL_CONN; |
| 3325 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3326 | if (!mode) |
| 3327 | return; |
| 3328 | |
| 3329 | h2_do_shutr(h2s); |
| 3330 | } |
| 3331 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3332 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3333 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3334 | { |
| 3335 | struct h2s *h2s = cs->ctx; |
| 3336 | |
Willy Tarreau | 3cf69fe | 2019-05-14 10:44:40 +0200 | [diff] [blame] | 3337 | if (cs->flags & CS_FL_KILL_CONN) |
| 3338 | h2s->flags |= H2_SF_KILL_CONN; |
| 3339 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3340 | h2_do_shutw(h2s); |
| 3341 | } |
| 3342 | |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3343 | /* 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] | 3344 | * HTX request or response depending on the connection's side. Returns a |
| 3345 | * positive value on success, a negative value on failure, or 0 if it couldn't |
| 3346 | * proceed. May report connection errors in h2c->errcode if the frame is |
| 3347 | * non-decodable and the connection unrecoverable. In absence of connection |
| 3348 | * 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] | 3349 | * |
| 3350 | * The function may fold CONTINUATION frames into the initial HEADERS frame |
| 3351 | * by removing padding and next frame header, then moving the CONTINUATION |
| 3352 | * frame's payload and adjusting h2c->dfl to match the new aggregated frame, |
| 3353 | * leaving a hole between the main frame and the beginning of the next one. |
| 3354 | * The possibly remaining incomplete or next frame at the end may be moved |
| 3355 | * if the aggregated frame is not deleted, in order to fill the hole. Wrapped |
| 3356 | * HEADERS frames are unwrapped into a temporary buffer before decoding. |
| 3357 | * |
| 3358 | * A buffer at the beginning of processing may look like this : |
| 3359 | * |
| 3360 | * ,---.---------.-----.--------------.--------------.------.---. |
| 3361 | * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///| |
| 3362 | * `---^---------^-----^--------------^--------------^------^---' |
| 3363 | * | | <-----> | | |
| 3364 | * area | dpl | wrap |
| 3365 | * |<--------------> | |
| 3366 | * | dfl | |
| 3367 | * |<-------------------------------------------------->| |
| 3368 | * head data |
| 3369 | * |
| 3370 | * Padding is automatically overwritten when folding, participating to the |
| 3371 | * hole size after dfl : |
| 3372 | * |
| 3373 | * ,---.------------------------.-----.--------------.------.---. |
| 3374 | * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///| |
| 3375 | * `---^------------------------^-----^--------------^------^---' |
| 3376 | * | | <-----> | | |
| 3377 | * area | hole | wrap |
| 3378 | * |<-----------------------> | |
| 3379 | * | dfl | |
| 3380 | * |<-------------------------------------------------->| |
| 3381 | * head data |
| 3382 | * |
| 3383 | * Please note that the HEADERS frame is always deprived from its PADLEN byte |
| 3384 | * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY |
| 3385 | * bit. |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3386 | * |
| 3387 | * The <flags> field must point to either the stream's flags or to a copy of it |
| 3388 | * so that the function can update the following flags : |
| 3389 | * - H2_SF_DATA_CLEN when content-length is seen |
| 3390 | * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion |
| 3391 | * - H2_SF_HEADERS_RCVD once the frame is successfully decoded |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3392 | * |
| 3393 | * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to |
| 3394 | * decoding, in order to detect if we're dealing with a headers or a trailers |
| 3395 | * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3396 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3397 | 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] | 3398 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3399 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3400 | struct buffer *tmp = get_trash_chunk(); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3401 | struct http_hdr list[MAX_HTTP_HDR * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3402 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3403 | unsigned int msgf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3404 | struct htx *htx = NULL; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3405 | int flen; // header frame len |
| 3406 | int hole = 0; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3407 | int ret = 0; |
| 3408 | int outlen; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3409 | int wrap; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3410 | int try = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3411 | |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3412 | next_frame: |
| 3413 | if (b_data(&h2c->dbuf) - hole < h2c->dfl) |
| 3414 | goto leave; // incomplete input frame |
| 3415 | |
| 3416 | /* No END_HEADERS means there's one or more CONTINUATION frames. In |
| 3417 | * this case, we'll try to paste it immediately after the initial |
| 3418 | * HEADERS frame payload and kill any possible padding. The initial |
| 3419 | * frame's length will be increased to represent the concatenation |
| 3420 | * of the two frames. The next frame is read from position <tlen> |
| 3421 | * and written at position <flen> (minus padding if some is present). |
| 3422 | */ |
| 3423 | if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) { |
| 3424 | struct h2_fh hdr; |
| 3425 | int clen; // CONTINUATION frame's payload length |
| 3426 | |
| 3427 | if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) { |
| 3428 | /* no more data, the buffer may be full, either due to |
| 3429 | * too large a frame or because of too large a hole that |
| 3430 | * we're going to compact at the end. |
| 3431 | */ |
| 3432 | goto leave; |
| 3433 | } |
| 3434 | |
| 3435 | if (hdr.ft != H2_FT_CONTINUATION) { |
| 3436 | /* RFC7540#6.10: frame of unexpected type */ |
| 3437 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3438 | goto fail; |
| 3439 | } |
| 3440 | |
| 3441 | if (hdr.sid != h2c->dsi) { |
| 3442 | /* RFC7540#6.10: frame of different stream */ |
| 3443 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3444 | goto fail; |
| 3445 | } |
| 3446 | |
| 3447 | if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { |
| 3448 | /* RFC7540#4.2: invalid frame length */ |
| 3449 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3450 | goto fail; |
| 3451 | } |
| 3452 | |
| 3453 | /* detect when we must stop aggragating frames */ |
| 3454 | h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS; |
| 3455 | |
| 3456 | /* Take as much as we can of the CONTINUATION frame's payload */ |
| 3457 | clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9); |
| 3458 | if (clen > hdr.len) |
| 3459 | clen = hdr.len; |
| 3460 | |
| 3461 | /* Move the frame's payload over the padding, hole and frame |
| 3462 | * header. At least one of hole or dpl is null (see diagrams |
| 3463 | * above). The hole moves after the new aggragated frame. |
| 3464 | */ |
| 3465 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9)); |
| 3466 | h2c->dfl += clen - h2c->dpl; |
| 3467 | hole += h2c->dpl + 9; |
| 3468 | h2c->dpl = 0; |
| 3469 | goto next_frame; |
| 3470 | } |
| 3471 | |
| 3472 | flen = h2c->dfl - h2c->dpl; |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3473 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3474 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3475 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3476 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3477 | copy = alloc_trash_chunk(); |
| 3478 | if (!copy) { |
| 3479 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3480 | goto fail; |
| 3481 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3482 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3483 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3484 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3485 | } |
| 3486 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3487 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3488 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3489 | if (read_n32(hdrs) == h2c->dsi) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3490 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3491 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3492 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3493 | } |
| 3494 | |
Willy Tarreau | a01f45e | 2018-12-31 07:41:24 +0100 | [diff] [blame] | 3495 | if (flen < 5) { |
| 3496 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3497 | goto fail; |
| 3498 | } |
| 3499 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3500 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3501 | flen -= 5; |
| 3502 | } |
| 3503 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3504 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3505 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3506 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3507 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3508 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3509 | /* we can't retry a failed decompression operation so we must be very |
| 3510 | * careful not to take any risks. In practice the output buffer is |
| 3511 | * always empty except maybe for trailers, in which case we simply have |
| 3512 | * to wait for the upper layer to finish consuming what is available. |
| 3513 | */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3514 | |
| 3515 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3516 | htx = htx_from_buf(rxbuf); |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3517 | if (!htx_is_empty(htx)) { |
| 3518 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3519 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3520 | } |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3521 | } else { |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3522 | if (b_data(rxbuf)) { |
| 3523 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3524 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3525 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3526 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3527 | rxbuf->head = 0; |
| 3528 | try = b_size(rxbuf); |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3529 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3530 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3531 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3532 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3533 | sizeof(list)/sizeof(list[0]), tmp); |
| 3534 | if (outlen < 0) { |
| 3535 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3536 | goto fail; |
| 3537 | } |
| 3538 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3539 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3540 | * the parser's state to commit these changes and allow us to later |
| 3541 | * fail solely on the stream if needed. |
| 3542 | */ |
| 3543 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3544 | h2c->dfl = hole = 0; |
| 3545 | h2c->st0 = H2_CS_FRAME_H; |
| 3546 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3547 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3548 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3549 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3550 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3551 | goto trailers; |
| 3552 | |
| 3553 | /* This is the first HEADERS frame so it's a headers block */ |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3554 | if (htx) { |
| 3555 | /* HTX mode */ |
| 3556 | if (h2c->flags & H2_CF_IS_BACK) |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3557 | outlen = h2_make_htx_response(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3558 | else |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3559 | outlen = h2_make_htx_request(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3560 | } else { |
| 3561 | /* HTTP/1 mode */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3562 | 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] | 3563 | if (outlen > 0) |
| 3564 | b_add(rxbuf, outlen); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3565 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3566 | |
| 3567 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3568 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3569 | goto fail; |
| 3570 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3571 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3572 | if (msgf & H2_MSGF_BODY) { |
| 3573 | /* a payload is present */ |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3574 | if (msgf & H2_MSGF_BODY_CL) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3575 | *flags |= H2_SF_DATA_CLEN; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3576 | if (htx) |
| 3577 | htx->extra = *body_len; |
| 3578 | } |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3579 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3580 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3581 | } |
| 3582 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3583 | done: |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3584 | /* indicate that a HEADERS frame was received for this stream, except |
| 3585 | * for 1xx responses. For 1xx responses, another HEADERS frame is |
| 3586 | * expected. |
| 3587 | */ |
| 3588 | if (!(msgf & H2_MSGF_RSP_1XX)) |
| 3589 | *flags |= H2_SF_HEADERS_RCVD; |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3590 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3591 | if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3592 | /* Mark the end of message, either using EOM in HTX or with the |
| 3593 | * trailing CRLF after the end of trailers. Note that DATA_CHNK |
Willy Tarreau | 2135f91 | 2019-05-07 11:17:32 +0200 | [diff] [blame] | 3594 | * is not set during headers with END_STREAM. For HTX trailers, |
| 3595 | * we must not leave an HTX trailers block not followed by an |
| 3596 | * EOM block, the two must be atomic. Thus if we fail to emit |
| 3597 | * 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] | 3598 | */ |
| 3599 | if (htx) { |
Willy Tarreau | 2135f91 | 2019-05-07 11:17:32 +0200 | [diff] [blame] | 3600 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3601 | struct htx_blk *tail = htx_get_tail_blk(htx); |
| 3602 | |
| 3603 | if (tail && htx_get_blk_type(tail) == HTX_BLK_TLR) |
| 3604 | htx_remove_blk(htx, tail); |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3605 | goto fail; |
Willy Tarreau | 2135f91 | 2019-05-07 11:17:32 +0200 | [diff] [blame] | 3606 | } |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3607 | } |
| 3608 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3609 | if (!b_putblk(rxbuf, "\r\n", 2)) |
| 3610 | goto fail; |
| 3611 | } |
| 3612 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3613 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3614 | /* success */ |
| 3615 | ret = 1; |
| 3616 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3617 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3618 | /* 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] | 3619 | * move the remaining data over it. |
| 3620 | */ |
| 3621 | if (hole) { |
| 3622 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3623 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3624 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3625 | b_sub(&h2c->dbuf, hole); |
| 3626 | } |
| 3627 | |
Willy Tarreau | 97215ca | 2019-04-29 10:20:21 +0200 | [diff] [blame] | 3628 | if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) { |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3629 | /* too large frames */ |
| 3630 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3631 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3632 | } |
| 3633 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3634 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3635 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3636 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3637 | return ret; |
| 3638 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3639 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3640 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3641 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3642 | |
| 3643 | trailers: |
| 3644 | /* This is the last HEADERS frame hence a trailer */ |
| 3645 | |
| 3646 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3647 | /* It's a trailer but it's missing ES flag */ |
| 3648 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3649 | goto fail; |
| 3650 | } |
| 3651 | |
| 3652 | /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD |
| 3653 | * block, and when using chunks we must send the 0 CRLF marker. For |
| 3654 | * other modes, the trailers are silently dropped. |
| 3655 | */ |
| 3656 | if (htx) { |
| 3657 | if (!htx_add_endof(htx, HTX_BLK_EOD)) |
| 3658 | goto fail; |
Willy Tarreau | 5255f28 | 2019-01-03 18:41:05 +0100 | [diff] [blame] | 3659 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3660 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3661 | } |
| 3662 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3663 | /* Legacy mode with chunked encoding : we must finalize the |
| 3664 | * data block message emit the trailing CRLF */ |
| 3665 | if (!b_putblk(rxbuf, "0\r\n", 3)) |
| 3666 | goto fail; |
Willy Tarreau | e2b05cc | 2019-01-03 16:18:34 +0100 | [diff] [blame] | 3667 | |
| 3668 | outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try); |
| 3669 | if (outlen > 0) |
| 3670 | b_add(rxbuf, outlen); |
| 3671 | else |
| 3672 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3673 | } |
| 3674 | |
| 3675 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3676 | } |
| 3677 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3678 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 3679 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 3680 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 3681 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 3682 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 3683 | * 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] | 3684 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3685 | * send the current frame, 0 if it couldn't complete, in which case |
| 3686 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3687 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3688 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3689 | * have checked the frame header and ensured that the frame was complete or the |
| 3690 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3691 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3692 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3693 | { |
| 3694 | struct h2c *h2c = h2s->h2c; |
| 3695 | int block1, block2; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3696 | unsigned int flen = 0; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3697 | unsigned int chklen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3698 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3699 | struct buffer *csbuf; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3700 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3701 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3702 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3703 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3704 | if (!csbuf) { |
| 3705 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3706 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3707 | } |
| 3708 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3709 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3710 | flen = h2c->dfl - h2c->dpl; |
Olivier Houchard | 2f30883 | 2018-12-19 15:53:53 +0100 | [diff] [blame] | 3711 | if (h2c->proxy->options2 & PR_O2_USE_HTX) |
| 3712 | htx = htx_from_buf(csbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3713 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3714 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3715 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3716 | if (flen > b_data(&h2c->dbuf)) { |
| 3717 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3718 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3719 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3720 | } |
| 3721 | |
Willy Tarreau | a9b7796 | 2019-01-31 07:23:00 +0100 | [diff] [blame] | 3722 | if (htx) { |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3723 | block1 = htx_free_data_space(htx); |
| 3724 | if (!block1) { |
| 3725 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3726 | goto fail; |
| 3727 | } |
| 3728 | if (flen > block1) |
| 3729 | flen = block1; |
| 3730 | |
| 3731 | /* here, flen is the max we can copy into the output buffer */ |
| 3732 | block1 = b_contig_data(&h2c->dbuf, 0); |
| 3733 | if (flen > block1) |
| 3734 | flen = block1; |
| 3735 | |
| 3736 | if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) { |
| 3737 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3738 | goto fail; |
| 3739 | } |
| 3740 | |
| 3741 | b_del(&h2c->dbuf, flen); |
| 3742 | h2c->dfl -= flen; |
| 3743 | h2c->rcvd_c += flen; |
| 3744 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3745 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3746 | if (h2s->flags & H2_SF_DATA_CLEN) { |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3747 | h2s->body_len -= flen; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3748 | htx->extra = h2s->body_len; |
| 3749 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3750 | goto try_again; |
| 3751 | } |
| 3752 | else if (unlikely(b_space_wraps(csbuf))) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3753 | /* it doesn't fit and the buffer is fragmented, |
| 3754 | * so let's defragment it and try again. |
| 3755 | */ |
| 3756 | b_slow_realign(csbuf, trash.area, 0); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3757 | } |
| 3758 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3759 | /* chunked-encoding requires more room */ |
| 3760 | if (h2s->flags & H2_SF_DATA_CHNK) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3761 | chklen = MIN(flen, b_room(csbuf)); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3762 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 3763 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 3764 | (chklen < 1048576) ? 4 : 8; |
| 3765 | chklen += 4; // CRLF, CRLF |
| 3766 | } |
| 3767 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3768 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3769 | if (flen + chklen > b_room(csbuf)) { |
| 3770 | if (chklen >= b_room(csbuf)) { |
| 3771 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3772 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3773 | } |
| 3774 | flen = b_room(csbuf) - chklen; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3778 | /* emit the chunk size */ |
| 3779 | unsigned int chksz = flen; |
| 3780 | char str[10]; |
| 3781 | char *beg; |
| 3782 | |
| 3783 | beg = str + sizeof(str); |
| 3784 | *--beg = '\n'; |
| 3785 | *--beg = '\r'; |
| 3786 | do { |
| 3787 | *--beg = hextab[chksz & 0xF]; |
| 3788 | } while (chksz >>= 4); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3789 | b_putblk(csbuf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3790 | } |
| 3791 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3792 | /* Block1 is the length of the first block before the buffer wraps, |
| 3793 | * block2 is the optional second block to reach the end of the frame. |
| 3794 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3795 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3796 | if (block1 > flen) |
| 3797 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3798 | block2 = flen - block1; |
| 3799 | |
| 3800 | if (block1) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3801 | b_putblk(csbuf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3802 | |
| 3803 | if (block2) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3804 | b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3805 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3806 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3807 | /* emit the CRLF */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3808 | b_putblk(csbuf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3809 | } |
| 3810 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3811 | /* now mark the input data as consumed (will be deleted from the buffer |
| 3812 | * by the caller when seeing FRAME_A after sending the window update). |
| 3813 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3814 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3815 | h2c->dfl -= flen; |
| 3816 | h2c->rcvd_c += flen; |
| 3817 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 3818 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3819 | if (h2s->flags & H2_SF_DATA_CLEN) |
| 3820 | h2s->body_len -= flen; |
| 3821 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3822 | if (h2c->dfl > h2c->dpl) { |
| 3823 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3824 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3825 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3826 | } |
| 3827 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3828 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3829 | /* here we're done with the frame, all the payload (except padding) was |
| 3830 | * transferred. |
| 3831 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3832 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3833 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 3834 | if (htx) { |
| 3835 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3836 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3837 | goto fail; |
| 3838 | } |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3839 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3840 | else if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3841 | /* emit the trailing 0 CRLF CRLF */ |
| 3842 | if (b_room(csbuf) < 5) { |
| 3843 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3844 | goto fail; |
| 3845 | } |
| 3846 | chklen += 5; |
| 3847 | b_putblk(csbuf, "0\r\n\r\n", 5); |
| 3848 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3849 | } |
| 3850 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 3851 | h2c->rcvd_c += h2c->dpl; |
| 3852 | h2c->rcvd_s += h2c->dpl; |
| 3853 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3854 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3855 | if (htx) |
| 3856 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3857 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3858 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3859 | if (htx) |
| 3860 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3861 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3862 | } |
| 3863 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3864 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 3865 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 3866 | * number of bytes sent. The caller must check the stream's status to detect |
| 3867 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3868 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3869 | 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] | 3870 | { |
| 3871 | struct http_hdr list[MAX_HTTP_HDR]; |
| 3872 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3873 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3874 | struct buffer outbuf; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3875 | union h1_sl sl; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3876 | int es_now = 0; |
| 3877 | int ret = 0; |
| 3878 | int hdr; |
| 3879 | |
| 3880 | if (h2c_mux_busy(h2c, h2s)) { |
| 3881 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3882 | return 0; |
| 3883 | } |
| 3884 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3885 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3886 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3887 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3888 | return 0; |
| 3889 | } |
| 3890 | |
| 3891 | /* First, try to parse the H1 response and index it into <list>. |
| 3892 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 3893 | * block does not wrap and we can safely read it this way without |
| 3894 | * having to realign the buffer. |
| 3895 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3896 | 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] | 3897 | list, sizeof(list)/sizeof(list[0]), h1m, &sl); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3898 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 3899 | /* incomplete or invalid response, this is abnormal coming from |
| 3900 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 3901 | * so that won't be fixed, raise an error now. |
| 3902 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3903 | * FIXME: we should instead add the ability to only return a |
| 3904 | * 502 bad gateway. But in theory this is not supposed to |
| 3905 | * happen. |
| 3906 | */ |
| 3907 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3908 | ret = 0; |
| 3909 | goto end; |
| 3910 | } |
| 3911 | |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3912 | h2s->status = sl.st.status; |
Willy Tarreau | db72da0 | 2018-09-13 11:52:20 +0200 | [diff] [blame] | 3913 | |
| 3914 | /* certain statuses have no body or an empty one, regardless of |
| 3915 | * what the headers say. |
| 3916 | */ |
| 3917 | if (sl.st.status >= 100 && sl.st.status < 200) { |
| 3918 | h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK); |
| 3919 | h1m->curr_len = h1m->body_len = 0; |
| 3920 | } |
| 3921 | else if (sl.st.status == 204 || sl.st.status == 304) { |
| 3922 | /* no contents, claim c-len is present and set to zero */ |
| 3923 | h1m->flags &= ~H1_MF_CHNK; |
| 3924 | h1m->flags |= H1_MF_CLEN; |
| 3925 | h1m->curr_len = h1m->body_len = 0; |
| 3926 | } |
| 3927 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3928 | chunk_reset(&outbuf); |
| 3929 | |
| 3930 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3931 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3932 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3933 | outbuf.data = 0; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3934 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3935 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3936 | break; |
| 3937 | realign_again: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3938 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3939 | } |
| 3940 | |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3941 | if (outbuf.size < 9) |
| 3942 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3943 | |
| 3944 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3945 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 3946 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3947 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3948 | |
| 3949 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3950 | if (unlikely(list[0].v.len != 3)) { |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 3951 | /* this is an unparsable response */ |
| 3952 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3953 | ret = 0; |
| 3954 | goto end; |
| 3955 | } |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3956 | |
| 3957 | if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3958 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3959 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3960 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | /* encode all headers, stop at empty name */ |
| 3964 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 3965 | /* these ones do not exist in H2 and must be dropped. */ |
| 3966 | if (isteq(list[hdr].n, ist("connection")) || |
| 3967 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 3968 | isteq(list[hdr].n, ist("keep-alive")) || |
| 3969 | isteq(list[hdr].n, ist("upgrade")) || |
| 3970 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3971 | continue; |
| 3972 | |
| 3973 | if (isteq(list[hdr].n, ist(""))) |
| 3974 | break; // end |
| 3975 | |
| 3976 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 3977 | /* output full */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3978 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3979 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3980 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3981 | } |
| 3982 | } |
| 3983 | |
| 3984 | /* we may need to add END_STREAM */ |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 3985 | 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] | 3986 | es_now = 1; |
| 3987 | |
| 3988 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3989 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3990 | |
| 3991 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3992 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3993 | |
| 3994 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3995 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3996 | |
| 3997 | /* commit the H2 response */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3998 | b_add(&h2c->mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3999 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4000 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4001 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4002 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4003 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4004 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4005 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4006 | h2s->flags |= H2_SF_ES_SENT; |
| 4007 | if (h2s->st == H2_SS_OPEN) |
| 4008 | h2s->st = H2_SS_HLOC; |
| 4009 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4010 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4011 | } |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 4012 | else if (h2s->status >= 100 && h2s->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4013 | /* 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] | 4014 | h1m_init_res(h1m); |
Willy Tarreau | 9b8cd1f | 2018-09-12 09:24:38 +0200 | [diff] [blame] | 4015 | 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] | 4016 | h2s->h1m.flags |= H1_MF_TOLOWER; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4017 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 4018 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 4019 | |
| 4020 | /* 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] | 4021 | |
| 4022 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4023 | //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] | 4024 | return ret; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4025 | full: |
| 4026 | h1m_init_res(h1m); |
| 4027 | h1m->err_pos = -1; // don't care about errors on the response path |
| 4028 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4029 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4030 | ret = 0; |
| 4031 | goto end; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4032 | } |
| 4033 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4034 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 4035 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 4036 | * the number of bytes sent. The caller must check the stream's status to |
| 4037 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4038 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4039 | 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] | 4040 | { |
| 4041 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4042 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4043 | struct buffer outbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4044 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 4045 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4046 | int es_now = 0; |
| 4047 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4048 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4049 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4050 | |
| 4051 | if (h2c_mux_busy(h2c, h2s)) { |
| 4052 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4053 | goto end; |
| 4054 | } |
| 4055 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 4056 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4057 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4058 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4059 | goto end; |
| 4060 | } |
| 4061 | |
| 4062 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4063 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4064 | goto end; |
| 4065 | |
| 4066 | chunk_reset(&outbuf); |
| 4067 | |
| 4068 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4069 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4070 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4071 | outbuf.data = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4072 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4073 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4074 | break; |
| 4075 | realign_again: |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4076 | /* If there are pending data in the output buffer, and we have |
| 4077 | * less than 1/4 of the mbuf's size and everything fits, we'll |
| 4078 | * still perform a copy anyway. Otherwise we'll pretend the mbuf |
| 4079 | * is full and wait, to save some slow realign calls. |
| 4080 | */ |
| 4081 | if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) { |
| 4082 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4083 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4084 | goto end; |
| 4085 | } |
| 4086 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4087 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4088 | } |
| 4089 | |
| 4090 | if (outbuf.size < 9) { |
| 4091 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4092 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4093 | goto end; |
| 4094 | } |
| 4095 | |
| 4096 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4097 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4098 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4099 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4100 | |
| 4101 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 4102 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4103 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 4104 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4105 | break; |
| 4106 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4107 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4108 | if ((long long)size > h1m->curr_len) |
| 4109 | size = h1m->curr_len; |
| 4110 | break; |
| 4111 | default: /* te:chunked : parse chunks */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4112 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 4113 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4114 | if (!ret) |
| 4115 | goto end; |
| 4116 | |
| 4117 | if (ret < 0) { |
| 4118 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4119 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4120 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4121 | goto end; |
| 4122 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4123 | max -= ret; |
| 4124 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4125 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4126 | h1m->state = H1_MSG_CHUNK_SIZE; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4127 | } |
| 4128 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4129 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4130 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 4131 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4132 | if (!ret) |
| 4133 | goto end; |
| 4134 | |
| 4135 | if (ret < 0) { |
| 4136 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4137 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4138 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4139 | goto end; |
| 4140 | } |
| 4141 | |
| 4142 | size = chunk; |
| 4143 | h1m->curr_len = chunk; |
| 4144 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4145 | max -= ret; |
| 4146 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4147 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4148 | h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4149 | if (!size) |
| 4150 | goto send_empty; |
| 4151 | } |
| 4152 | |
| 4153 | /* in MSG_DATA state, continue below */ |
| 4154 | size = h1m->curr_len; |
| 4155 | break; |
| 4156 | } |
| 4157 | |
| 4158 | /* we have in <size> the exact number of bytes we need to copy from |
| 4159 | * the H1 buffer. We need to check this against the connection's and |
| 4160 | * the stream's send windows, and to ensure that this fits in the max |
| 4161 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4162 | * the frame header). The connection's flow control is applied last so |
| 4163 | * that we can use a separate list of streams which are immediately |
| 4164 | * unblocked on window opening. Note: we don't implement padding. |
| 4165 | */ |
| 4166 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4167 | if (size > max) |
| 4168 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4169 | |
| 4170 | if (size > h2s->mws) |
| 4171 | size = h2s->mws; |
| 4172 | |
| 4173 | if (size <= 0) { |
| 4174 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 4175 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 4176 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4177 | goto end; |
| 4178 | } |
| 4179 | |
| 4180 | if (h2c->mfs && size > h2c->mfs) |
| 4181 | size = h2c->mfs; |
| 4182 | |
| 4183 | if (size + 9 > outbuf.size) { |
| 4184 | /* we have an opportunity for enlarging the too small |
| 4185 | * available space, let's try. |
| 4186 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4187 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4188 | goto realign_again; |
| 4189 | size = outbuf.size - 9; |
| 4190 | } |
| 4191 | |
| 4192 | if (size <= 0) { |
| 4193 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4194 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4195 | goto end; |
| 4196 | } |
| 4197 | |
| 4198 | if (size > h2c->mws) |
| 4199 | size = h2c->mws; |
| 4200 | |
| 4201 | if (size <= 0) { |
| 4202 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4203 | goto end; |
| 4204 | } |
| 4205 | |
| 4206 | /* copy whatever we can */ |
| 4207 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4208 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4209 | if (ret == 1) |
| 4210 | len2 = 0; |
| 4211 | |
| 4212 | if (!ret || len1 + len2 < size) { |
| 4213 | /* FIXME: must normally never happen */ |
| 4214 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4215 | goto end; |
| 4216 | } |
| 4217 | |
| 4218 | /* limit len1/len2 to size */ |
| 4219 | if (len1 + len2 > size) { |
| 4220 | int sub = len1 + len2 - size; |
| 4221 | |
| 4222 | if (len2 > sub) |
| 4223 | len2 -= sub; |
| 4224 | else { |
| 4225 | sub -= len2; |
| 4226 | len2 = 0; |
| 4227 | len1 -= sub; |
| 4228 | } |
| 4229 | } |
| 4230 | |
| 4231 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4232 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4233 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4234 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4235 | |
| 4236 | send_empty: |
| 4237 | /* we may need to add END_STREAM */ |
| 4238 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 4239 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4240 | * |
| 4241 | * FIXME: what we do here is not correct because we send end_stream |
| 4242 | * before knowing if we'll have to send a HEADERS frame for the |
| 4243 | * trailers. More importantly we're not consuming the trailing CRLF |
| 4244 | * after the end of trailers, so it will be left to the caller to |
| 4245 | * eat it. The right way to do it would be to measure trailers here |
| 4246 | * and to send ES only if there are no trailers. |
| 4247 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4248 | */ |
| 4249 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4250 | !h1m->curr_len || h1m->state >= H1_MSG_DONE) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4251 | es_now = 1; |
| 4252 | |
| 4253 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4254 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4255 | |
| 4256 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4257 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4258 | |
| 4259 | /* commit the H2 response */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4260 | b_add(&h2c->mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4261 | |
| 4262 | /* consume incoming H1 response */ |
| 4263 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4264 | max -= size; |
| 4265 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4266 | total += size; |
| 4267 | h1m->curr_len -= size; |
| 4268 | h2s->mws -= size; |
| 4269 | h2c->mws -= size; |
| 4270 | |
| 4271 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4272 | h1m->state = H1_MSG_CHUNK_CRLF; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4273 | goto new_frame; |
| 4274 | } |
| 4275 | } |
| 4276 | |
| 4277 | if (es_now) { |
| 4278 | if (h2s->st == H2_SS_OPEN) |
| 4279 | h2s->st = H2_SS_HLOC; |
| 4280 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4281 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4282 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4283 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 4284 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4285 | total += max; |
| 4286 | ofs += max; |
| 4287 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4288 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4289 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4290 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4291 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4292 | h2s->flags |= H2_SF_ES_SENT; |
| 4293 | } |
| 4294 | |
| 4295 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4296 | trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%u in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) data=%u", h2c->st0, h2s->id, size+9, (unsigned int)total, h1m_state_str(h1m->state), h1m->err_pos, h1m_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4297 | return total; |
| 4298 | } |
| 4299 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4300 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 4301 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4302 | * must check the stream's status to detect any error which might have happened |
| 4303 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4304 | * from the message. The htx message is assumed to be valid since produced from |
| 4305 | * the internal code, hence it contains a start line, an optional series of |
| 4306 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4307 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4308 | */ |
| 4309 | static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
| 4310 | { |
| 4311 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4312 | struct h2c *h2c = h2s->h2c; |
| 4313 | struct htx_blk *blk; |
| 4314 | struct htx_blk *blk_end; |
| 4315 | struct buffer outbuf; |
| 4316 | struct htx_sl *sl; |
| 4317 | enum htx_blk_type type; |
| 4318 | int es_now = 0; |
| 4319 | int ret = 0; |
| 4320 | int hdr; |
| 4321 | int idx; |
| 4322 | |
| 4323 | if (h2c_mux_busy(h2c, h2s)) { |
| 4324 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4325 | return 0; |
| 4326 | } |
| 4327 | |
| 4328 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4329 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4330 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4331 | return 0; |
| 4332 | } |
| 4333 | |
| 4334 | /* determine the first block which must not be deleted, blk_end may |
| 4335 | * be NULL if all blocks have to be deleted. |
| 4336 | */ |
| 4337 | idx = htx_get_head(htx); |
| 4338 | blk_end = NULL; |
| 4339 | while (idx != -1) { |
| 4340 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4341 | idx = htx_get_next(htx, idx); |
| 4342 | if (type == HTX_BLK_EOH) { |
| 4343 | if (idx != -1) |
| 4344 | blk_end = htx_get_blk(htx, idx); |
| 4345 | break; |
| 4346 | } |
| 4347 | } |
| 4348 | |
| 4349 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4350 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4351 | ALREADY_CHECKED(sl); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4352 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4353 | if (h2s->status < 100 || h2s->status > 999) |
| 4354 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4355 | |
| 4356 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4357 | hdr = 0; |
| 4358 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4359 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4360 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4361 | blk = htx_get_blk(htx, idx); |
| 4362 | type = htx_get_blk_type(blk); |
| 4363 | |
| 4364 | if (type == HTX_BLK_UNUSED) |
| 4365 | continue; |
| 4366 | |
| 4367 | if (type != HTX_BLK_HDR) |
| 4368 | break; |
| 4369 | |
| 4370 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4371 | goto fail; |
| 4372 | |
| 4373 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4374 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4375 | hdr++; |
| 4376 | } |
| 4377 | |
| 4378 | /* marker for end of headers */ |
| 4379 | list[hdr].n = ist(""); |
| 4380 | |
| 4381 | if (h2s->status == 204 || h2s->status == 304) { |
| 4382 | /* no contents, claim c-len is present and set to zero */ |
| 4383 | es_now = 1; |
| 4384 | } |
| 4385 | |
| 4386 | chunk_reset(&outbuf); |
| 4387 | |
| 4388 | while (1) { |
| 4389 | outbuf.area = b_tail(&h2c->mbuf); |
| 4390 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4391 | outbuf.data = 0; |
| 4392 | |
| 4393 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4394 | break; |
| 4395 | realign_again: |
| 4396 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4397 | } |
| 4398 | |
| 4399 | if (outbuf.size < 9) |
| 4400 | goto full; |
| 4401 | |
| 4402 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4403 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4404 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4405 | outbuf.data = 9; |
| 4406 | |
| 4407 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4408 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4409 | if (b_space_wraps(&h2c->mbuf)) |
| 4410 | goto realign_again; |
| 4411 | goto full; |
| 4412 | } |
| 4413 | |
| 4414 | /* encode all headers, stop at empty name */ |
| 4415 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4416 | /* these ones do not exist in H2 and must be dropped. */ |
| 4417 | if (isteq(list[hdr].n, ist("connection")) || |
| 4418 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4419 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4420 | isteq(list[hdr].n, ist("upgrade")) || |
| 4421 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4422 | continue; |
| 4423 | |
| 4424 | if (isteq(list[hdr].n, ist(""))) |
| 4425 | break; // end |
| 4426 | |
| 4427 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4428 | /* output full */ |
| 4429 | if (b_space_wraps(&h2c->mbuf)) |
| 4430 | goto realign_again; |
| 4431 | goto full; |
| 4432 | } |
| 4433 | } |
| 4434 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4435 | /* we may need to add END_STREAM except for 1xx responses. |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4436 | * FIXME: we should also set it when we know for sure that the |
| 4437 | * content-length is zero as well as on 204/304 |
| 4438 | */ |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4439 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM && |
| 4440 | (h2s->status >= 200 || h2s->status == 101)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4441 | es_now = 1; |
| 4442 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4443 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4444 | es_now = 1; |
| 4445 | |
| 4446 | /* update the frame's size */ |
| 4447 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4448 | |
| 4449 | if (es_now) |
| 4450 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4451 | |
| 4452 | /* commit the H2 response */ |
| 4453 | b_add(&h2c->mbuf, outbuf.data); |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4454 | |
| 4455 | /* indicates the HEADERS frame was sent, except for 1xx responses. For |
| 4456 | * 1xx responses, another HEADERS frame is expected. |
| 4457 | */ |
| 4458 | if (h2s->status >= 200 || h2s->status == 101) |
| 4459 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4460 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4461 | if (es_now) { |
| 4462 | h2s->flags |= H2_SF_ES_SENT; |
| 4463 | if (h2s->st == H2_SS_OPEN) |
| 4464 | h2s->st = H2_SS_HLOC; |
| 4465 | else |
| 4466 | h2s_close(h2s); |
| 4467 | } |
| 4468 | |
| 4469 | /* OK we could properly deliver the response */ |
| 4470 | |
| 4471 | /* remove all header blocks including the EOH and compute the |
| 4472 | * corresponding size. |
| 4473 | * |
| 4474 | * FIXME: We should remove everything when es_now is set. |
| 4475 | */ |
| 4476 | ret = 0; |
| 4477 | idx = htx_get_head(htx); |
| 4478 | blk = htx_get_blk(htx, idx); |
| 4479 | while (blk != blk_end) { |
| 4480 | ret += htx_get_blksz(blk); |
| 4481 | blk = htx_remove_blk(htx, blk); |
| 4482 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4483 | |
| 4484 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4485 | htx_remove_blk(htx, blk_end); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4486 | end: |
| 4487 | return ret; |
| 4488 | full: |
| 4489 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4490 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4491 | ret = 0; |
| 4492 | goto end; |
| 4493 | fail: |
| 4494 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4495 | * list etc go here (unrecoverable errors). |
| 4496 | */ |
| 4497 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4498 | ret = 0; |
| 4499 | goto end; |
| 4500 | } |
| 4501 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4502 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4503 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4504 | * must check the stream's status to detect any error which might have happened |
| 4505 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4506 | * from the message. The htx message is assumed to be valid since produced from |
| 4507 | * the internal code, hence it contains a start line, an optional series of |
| 4508 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4509 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4510 | */ |
| 4511 | static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
| 4512 | { |
| 4513 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4514 | struct h2c *h2c = h2s->h2c; |
| 4515 | struct htx_blk *blk; |
| 4516 | struct htx_blk *blk_end; |
| 4517 | struct buffer outbuf; |
| 4518 | struct htx_sl *sl; |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4519 | struct ist meth, path, auth; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4520 | enum htx_blk_type type; |
| 4521 | int es_now = 0; |
| 4522 | int ret = 0; |
| 4523 | int hdr; |
| 4524 | int idx; |
| 4525 | |
| 4526 | if (h2c_mux_busy(h2c, h2s)) { |
| 4527 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4528 | return 0; |
| 4529 | } |
| 4530 | |
| 4531 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4532 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4533 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4534 | return 0; |
| 4535 | } |
| 4536 | |
| 4537 | /* determine the first block which must not be deleted, blk_end may |
| 4538 | * be NULL if all blocks have to be deleted. |
| 4539 | */ |
| 4540 | idx = htx_get_head(htx); |
| 4541 | blk_end = NULL; |
| 4542 | while (idx != -1) { |
| 4543 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4544 | idx = htx_get_next(htx, idx); |
| 4545 | if (type == HTX_BLK_EOH) { |
| 4546 | if (idx != -1) |
| 4547 | blk_end = htx_get_blk(htx, idx); |
| 4548 | break; |
| 4549 | } |
| 4550 | } |
| 4551 | |
| 4552 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4553 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4554 | ALREADY_CHECKED(sl); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4555 | meth = htx_sl_req_meth(sl); |
| 4556 | path = htx_sl_req_uri(sl); |
| 4557 | |
| 4558 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4559 | hdr = 0; |
| 4560 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4561 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4562 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4563 | blk = htx_get_blk(htx, idx); |
| 4564 | type = htx_get_blk_type(blk); |
| 4565 | |
| 4566 | if (type == HTX_BLK_UNUSED) |
| 4567 | continue; |
| 4568 | |
| 4569 | if (type != HTX_BLK_HDR) |
| 4570 | break; |
| 4571 | |
| 4572 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4573 | goto fail; |
| 4574 | |
| 4575 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4576 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4577 | hdr++; |
| 4578 | } |
| 4579 | |
| 4580 | /* marker for end of headers */ |
| 4581 | list[hdr].n = ist(""); |
| 4582 | |
| 4583 | chunk_reset(&outbuf); |
| 4584 | |
| 4585 | while (1) { |
| 4586 | outbuf.area = b_tail(&h2c->mbuf); |
| 4587 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4588 | outbuf.data = 0; |
| 4589 | |
| 4590 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4591 | break; |
| 4592 | realign_again: |
| 4593 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4594 | } |
| 4595 | |
| 4596 | if (outbuf.size < 9) |
| 4597 | goto full; |
| 4598 | |
| 4599 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4600 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4601 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4602 | outbuf.data = 9; |
| 4603 | |
| 4604 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4605 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4606 | if (b_space_wraps(&h2c->mbuf)) |
| 4607 | goto realign_again; |
| 4608 | goto full; |
| 4609 | } |
| 4610 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4611 | /* RFC7540 #8.3: the CONNECT method must have : |
| 4612 | * - :authority set to the URI part (host:port) |
| 4613 | * - :method set to CONNECT |
| 4614 | * - :scheme and :path omitted |
| 4615 | */ |
| 4616 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 4617 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
| 4618 | if (!hpack_encode_scheme(&outbuf, ist("https"))) { |
| 4619 | /* output full */ |
| 4620 | if (b_space_wraps(&h2c->mbuf)) |
| 4621 | goto realign_again; |
| 4622 | goto full; |
| 4623 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4624 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4625 | /* encode the path, which necessarily is the second one */ |
| 4626 | if (!hpack_encode_path(&outbuf, path)) { |
| 4627 | /* output full */ |
| 4628 | if (b_space_wraps(&h2c->mbuf)) |
| 4629 | goto realign_again; |
| 4630 | goto full; |
| 4631 | } |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4632 | |
| 4633 | /* look for the Host header and place it in :authority */ |
| 4634 | auth = ist2(NULL, 0); |
| 4635 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4636 | if (isteq(list[hdr].n, ist(""))) |
| 4637 | break; // end |
| 4638 | |
| 4639 | if (isteq(list[hdr].n, ist("host"))) { |
| 4640 | auth = list[hdr].v; |
| 4641 | break; |
| 4642 | } |
| 4643 | } |
| 4644 | } |
| 4645 | else { |
| 4646 | /* for CONNECT, :authority is taken from the path */ |
| 4647 | auth = path; |
| 4648 | } |
| 4649 | |
| 4650 | if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) { |
| 4651 | /* output full */ |
| 4652 | if (b_space_wraps(&h2c->mbuf)) |
| 4653 | goto realign_again; |
| 4654 | goto full; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4655 | } |
| 4656 | |
| 4657 | /* encode all headers, stop at empty name */ |
| 4658 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4659 | /* these ones do not exist in H2 and must be dropped. */ |
| 4660 | if (isteq(list[hdr].n, ist("connection")) || |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4661 | isteq(list[hdr].n, ist("host")) || |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4662 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4663 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4664 | isteq(list[hdr].n, ist("upgrade")) || |
| 4665 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4666 | continue; |
| 4667 | |
| 4668 | if (isteq(list[hdr].n, ist(""))) |
| 4669 | break; // end |
| 4670 | |
| 4671 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4672 | /* output full */ |
| 4673 | if (b_space_wraps(&h2c->mbuf)) |
| 4674 | goto realign_again; |
| 4675 | goto full; |
| 4676 | } |
| 4677 | } |
| 4678 | |
| 4679 | /* we may need to add END_STREAM if we have no body : |
| 4680 | * - request already closed, or : |
| 4681 | * - no transfer-encoding, and : |
| 4682 | * - no content-length or content-length:0 |
| 4683 | * Fixme: this doesn't take into account CONNECT requests. |
| 4684 | */ |
| 4685 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4686 | es_now = 1; |
| 4687 | |
| 4688 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4689 | es_now = 1; |
| 4690 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4691 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4692 | es_now = 1; |
| 4693 | |
| 4694 | /* update the frame's size */ |
| 4695 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4696 | |
| 4697 | if (es_now) |
| 4698 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4699 | |
| 4700 | /* commit the H2 response */ |
| 4701 | b_add(&h2c->mbuf, outbuf.data); |
| 4702 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4703 | h2s->st = H2_SS_OPEN; |
| 4704 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4705 | if (es_now) { |
| 4706 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4707 | h2s->flags |= H2_SF_ES_SENT; |
| 4708 | h2s->st = H2_SS_HLOC; |
| 4709 | } |
| 4710 | |
| 4711 | /* remove all header blocks including the EOH and compute the |
| 4712 | * corresponding size. |
| 4713 | * |
| 4714 | * FIXME: We should remove everything when es_now is set. |
| 4715 | */ |
| 4716 | ret = 0; |
| 4717 | idx = htx_get_head(htx); |
| 4718 | blk = htx_get_blk(htx, idx); |
| 4719 | while (blk != blk_end) { |
| 4720 | ret += htx_get_blksz(blk); |
| 4721 | blk = htx_remove_blk(htx, blk); |
| 4722 | } |
| 4723 | |
| 4724 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4725 | htx_remove_blk(htx, blk_end); |
| 4726 | |
| 4727 | end: |
| 4728 | return ret; |
| 4729 | full: |
| 4730 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4731 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4732 | ret = 0; |
| 4733 | goto end; |
| 4734 | fail: |
| 4735 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4736 | * list etc go here (unrecoverable errors). |
| 4737 | */ |
| 4738 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4739 | ret = 0; |
| 4740 | goto end; |
| 4741 | } |
| 4742 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4743 | /* 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] | 4744 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 4745 | * caller must check the stream's status to detect any error which might have |
| 4746 | * happened subsequently to a successful send. Returns the number of data bytes |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4747 | * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte. |
| 4748 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4749 | 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] | 4750 | { |
| 4751 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4752 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4753 | struct buffer outbuf; |
| 4754 | size_t total = 0; |
| 4755 | int es_now = 0; |
| 4756 | int bsize; /* htx block size */ |
| 4757 | int fsize; /* h2 frame size */ |
| 4758 | struct htx_blk *blk; |
| 4759 | enum htx_blk_type type; |
| 4760 | int idx; |
| 4761 | |
| 4762 | if (h2c_mux_busy(h2c, h2s)) { |
| 4763 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4764 | goto end; |
| 4765 | } |
| 4766 | |
| 4767 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4768 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4769 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4770 | goto end; |
| 4771 | } |
| 4772 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4773 | htx = htx_from_buf(buf); |
| 4774 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4775 | /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However, |
| 4776 | * while looping, we can meet an HTX_BLK_EOM block that we'll leave to |
| 4777 | * the caller to handle. |
| 4778 | */ |
| 4779 | |
| 4780 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4781 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4782 | goto end; |
| 4783 | |
| 4784 | idx = htx_get_head(htx); |
| 4785 | blk = htx_get_blk(htx, idx); |
| 4786 | type = htx_get_blk_type(blk); // DATA or EOD or EOM |
| 4787 | bsize = htx_get_blksz(blk); |
| 4788 | fsize = bsize; |
| 4789 | |
| 4790 | if (type == HTX_BLK_EOD) { |
| 4791 | /* if we have an EOD, we're dealing with chunked data. We may |
| 4792 | * have a set of trailers after us that the caller will want to |
| 4793 | * deal with. Let's simply remove the EOD and return. |
| 4794 | */ |
| 4795 | htx_remove_blk(htx, blk); |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4796 | total++; // EOD counts as one byte |
| 4797 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4798 | goto end; |
| 4799 | } |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 4800 | else if (type == HTX_BLK_EOM) { |
| 4801 | if (h2s->flags & H2_SF_ES_SENT) { |
| 4802 | /* ES already sent */ |
| 4803 | htx_remove_blk(htx, blk); |
| 4804 | total++; // EOM counts as one byte |
| 4805 | count--; |
| 4806 | goto end; |
| 4807 | } |
| 4808 | } |
| 4809 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4810 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4811 | |
| 4812 | /* Perform some optimizations to reduce the number of buffer copies. |
| 4813 | * First, if the mux's buffer is empty and the htx area contains |
| 4814 | * exactly one data block of the same size as the requested count, and |
| 4815 | * this count fits within the frame size, the stream's window size, and |
| 4816 | * the connection's window size, then it's possible to simply swap the |
| 4817 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 4818 | * length to match the entire DATA HTX block in the middle. In this |
| 4819 | * case we perform a true zero-copy operation from end-to-end. This is |
| 4820 | * the situation that happens all the time with large files. Second, if |
| 4821 | * this is not possible, but the mux's output buffer is empty, we still |
| 4822 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 4823 | * making the intermediary buffer's area point to the output buffer's |
| 4824 | * area. In this case we want to skip the HTX header to make sure that |
| 4825 | * copies remain aligned and that this operation remains possible all |
| 4826 | * the time. This goes for headers, data blocks and any data extracted |
| 4827 | * from the HTX blocks. |
| 4828 | */ |
| 4829 | if (unlikely(fsize == count && |
| 4830 | htx->used == 1 && type == HTX_BLK_DATA && |
| 4831 | fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
| 4832 | void *old_area = h2c->mbuf.area; |
| 4833 | |
| 4834 | if (b_data(&h2c->mbuf)) { |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4835 | /* Too bad there are data left there. We're willing to memcpy/memmove |
| 4836 | * up to 1/4 of the buffer, which means that it's OK to copy a large |
| 4837 | * frame into a buffer containing few data if it needs to be realigned, |
| 4838 | * and that it's also OK to copy few data without realigning. Otherwise |
| 4839 | * 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] | 4840 | */ |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4841 | if (fsize + 9 <= b_room(&h2c->mbuf) && |
| 4842 | (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 || |
| 4843 | (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf)))) |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4844 | goto copy; |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4845 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4846 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4847 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4848 | goto end; |
| 4849 | } |
| 4850 | |
| 4851 | /* map an H2 frame to the HTX block so that we can put the |
| 4852 | * frame header there. |
| 4853 | */ |
| 4854 | h2c->mbuf.area = buf->area; |
Olivier Houchard | 84cca66 | 2018-12-14 16:28:08 +0100 | [diff] [blame] | 4855 | h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4856 | h2c->mbuf.data = fsize + 9; |
| 4857 | outbuf.area = b_head(&h2c->mbuf); |
| 4858 | |
| 4859 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 4860 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4861 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4862 | h2_set_frame_size(outbuf.area, fsize); |
| 4863 | |
| 4864 | /* update windows */ |
| 4865 | h2s->mws -= fsize; |
| 4866 | h2c->mws -= fsize; |
| 4867 | |
| 4868 | /* and exchange with our old area */ |
| 4869 | buf->area = old_area; |
| 4870 | buf->data = buf->head = 0; |
| 4871 | total += fsize; |
| 4872 | goto end; |
| 4873 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4874 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4875 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4876 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 4877 | |
| 4878 | while (1) { |
| 4879 | outbuf.area = b_tail(&h2c->mbuf); |
| 4880 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4881 | outbuf.data = 0; |
| 4882 | |
| 4883 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4884 | break; |
| 4885 | realign_again: |
| 4886 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4887 | } |
| 4888 | |
| 4889 | if (outbuf.size < 9) { |
| 4890 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4891 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4892 | goto end; |
| 4893 | } |
| 4894 | |
| 4895 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 4896 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4897 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4898 | outbuf.data = 9; |
| 4899 | |
| 4900 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 4901 | * the HTX buffer. We need to check this against the connection's and |
| 4902 | * the stream's send windows, and to ensure that this fits in the max |
| 4903 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4904 | * the frame header). The connection's flow control is applied last so |
| 4905 | * that we can use a separate list of streams which are immediately |
| 4906 | * unblocked on window opening. Note: we don't implement padding. |
| 4907 | */ |
| 4908 | |
| 4909 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 4910 | * empty frame, thus we force it to zero here. |
| 4911 | */ |
| 4912 | if (type == HTX_BLK_EOM) |
| 4913 | bsize = fsize = 0; |
| 4914 | |
| 4915 | if (!fsize) |
| 4916 | goto send_empty; |
| 4917 | |
| 4918 | if (h2s->mws <= 0) { |
| 4919 | h2s->flags |= H2_SF_BLK_SFCTL; |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 4920 | if (LIST_ADDED(&h2s->list)) |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 4921 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4922 | goto end; |
| 4923 | } |
| 4924 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4925 | if (fsize > count) |
| 4926 | fsize = count; |
| 4927 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4928 | if (fsize > h2s->mws) |
| 4929 | fsize = h2s->mws; // >0 |
| 4930 | |
| 4931 | if (h2c->mfs && fsize > h2c->mfs) |
| 4932 | fsize = h2c->mfs; // >0 |
| 4933 | |
| 4934 | if (fsize + 9 > outbuf.size) { |
| 4935 | /* we have an opportunity for enlarging the too small |
| 4936 | * available space, let's try. |
| 4937 | * FIXME: is this really interesting to do? Maybe we'll |
| 4938 | * spend lots of time realigning instead of using two |
| 4939 | * frames. |
| 4940 | */ |
| 4941 | if (b_space_wraps(&h2c->mbuf)) |
| 4942 | goto realign_again; |
| 4943 | fsize = outbuf.size - 9; |
| 4944 | |
| 4945 | if (fsize <= 0) { |
| 4946 | /* no need to send an empty frame here */ |
| 4947 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4948 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4949 | goto end; |
| 4950 | } |
| 4951 | } |
| 4952 | |
| 4953 | if (h2c->mws <= 0) { |
| 4954 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4955 | goto end; |
| 4956 | } |
| 4957 | |
| 4958 | if (fsize > h2c->mws) |
| 4959 | fsize = h2c->mws; |
| 4960 | |
| 4961 | /* now let's copy this this into the output buffer */ |
| 4962 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 4963 | h2s->mws -= fsize; |
| 4964 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4965 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4966 | |
| 4967 | send_empty: |
| 4968 | /* update the frame's size */ |
| 4969 | h2_set_frame_size(outbuf.area, fsize); |
| 4970 | |
| 4971 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 4972 | * meeting EOM. We should optimize this later. |
| 4973 | */ |
| 4974 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4975 | total++; // EOM counts as one byte |
| 4976 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4977 | es_now = 1; |
| 4978 | } |
| 4979 | |
| 4980 | if (es_now) |
| 4981 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 4982 | |
| 4983 | /* commit the H2 response */ |
| 4984 | b_add(&h2c->mbuf, fsize + 9); |
| 4985 | |
| 4986 | /* consume incoming HTX block, including EOM */ |
| 4987 | total += fsize; |
| 4988 | if (fsize == bsize) { |
| 4989 | htx_remove_blk(htx, blk); |
| 4990 | if (fsize) |
| 4991 | goto new_frame; |
| 4992 | } else { |
| 4993 | /* we've truncated this block */ |
| 4994 | htx_cut_data_blk(htx, blk, fsize); |
| 4995 | } |
| 4996 | |
| 4997 | if (es_now) { |
| 4998 | if (h2s->st == H2_SS_OPEN) |
| 4999 | h2s->st = H2_SS_HLOC; |
| 5000 | else |
| 5001 | h2s_close(h2s); |
| 5002 | |
| 5003 | h2s->flags |= H2_SF_ES_SENT; |
| 5004 | } |
| 5005 | |
| 5006 | end: |
| 5007 | return total; |
| 5008 | } |
| 5009 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5010 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 5011 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 5012 | * processed. The caller must check the stream's status to detect any error |
| 5013 | * which might have happened subsequently to a successful send. The htx blocks |
| 5014 | * are automatically removed from the message. The htx message is assumed to be |
| 5015 | * valid since produced from the internal code. Processing stops when meeting |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5016 | * 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] | 5017 | * as a single frame. The ES flag is always set. |
| 5018 | */ |
| 5019 | static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx) |
| 5020 | { |
| 5021 | struct http_hdr list[MAX_HTTP_HDR]; |
| 5022 | struct h2c *h2c = h2s->h2c; |
| 5023 | struct htx_blk *blk; |
| 5024 | struct htx_blk *blk_end; |
| 5025 | struct buffer outbuf; |
| 5026 | struct h1m h1m; |
| 5027 | enum htx_blk_type type; |
| 5028 | uint32_t size; |
| 5029 | int ret = 0; |
| 5030 | int hdr; |
| 5031 | int idx; |
| 5032 | void *start; |
| 5033 | |
| 5034 | if (h2c_mux_busy(h2c, h2s)) { |
| 5035 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 5036 | goto end; |
| 5037 | } |
| 5038 | |
| 5039 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 5040 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 5041 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5042 | goto end; |
| 5043 | } |
| 5044 | |
| 5045 | /* The principle is that we parse each and every trailers block using |
| 5046 | * the H1 headers parser, and append it to the list. We don't proceed |
| 5047 | * until EOM is met. blk_end will point to the EOM block. |
| 5048 | */ |
| 5049 | hdr = 0; |
| 5050 | memset(list, 0, sizeof(list)); |
| 5051 | blk_end = NULL; |
| 5052 | |
| 5053 | for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) { |
| 5054 | blk = htx_get_blk(htx, idx); |
| 5055 | type = htx_get_blk_type(blk); |
| 5056 | |
| 5057 | if (type == HTX_BLK_UNUSED) |
| 5058 | continue; |
| 5059 | |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5060 | if (type != HTX_BLK_TLR) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5061 | break; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5062 | |
| 5063 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 5064 | goto fail; |
| 5065 | |
| 5066 | size = htx_get_blksz(blk); |
| 5067 | start = htx_get_blk_ptr(htx, blk); |
| 5068 | |
| 5069 | h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER; |
| 5070 | h1m.err_pos = 0; |
| 5071 | ret = h1_headers_to_hdr_list(start, start + size, |
| 5072 | list + hdr, sizeof(list)/sizeof(list[0]) - hdr, |
| 5073 | &h1m, NULL); |
| 5074 | if (ret < 0) |
| 5075 | goto fail; |
| 5076 | |
| 5077 | /* ret == 0 if an incomplete trailers block was found (missing |
| 5078 | * empty line), or > 0 if it was found. We have to continue on |
| 5079 | * incomplete messages because the trailers block might be |
| 5080 | * incomplete. |
| 5081 | */ |
| 5082 | |
| 5083 | /* search the new end */ |
| 5084 | while (hdr <= sizeof(list)/sizeof(list[0])) { |
| 5085 | if (!list[hdr].n.len) |
| 5086 | break; |
| 5087 | hdr++; |
| 5088 | } |
| 5089 | } |
| 5090 | |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5091 | if (list[hdr].n.len != 0) |
| 5092 | goto fail; // empty trailer not found: internal error |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5093 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5094 | chunk_reset(&outbuf); |
| 5095 | |
| 5096 | while (1) { |
| 5097 | outbuf.area = b_tail(&h2c->mbuf); |
| 5098 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 5099 | outbuf.data = 0; |
| 5100 | |
| 5101 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 5102 | break; |
| 5103 | realign_again: |
| 5104 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 5105 | } |
| 5106 | |
| 5107 | if (outbuf.size < 9) |
| 5108 | goto full; |
| 5109 | |
| 5110 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 5111 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 5112 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5113 | outbuf.data = 9; |
| 5114 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5115 | /* encode all headers */ |
| 5116 | for (idx = 0; idx < hdr; idx++) { |
| 5117 | /* these ones do not exist in H2 or must not appear in |
| 5118 | * trailers and must be dropped. |
| 5119 | */ |
| 5120 | if (isteq(list[idx].n, ist("host")) || |
| 5121 | isteq(list[idx].n, ist("content-length")) || |
| 5122 | isteq(list[idx].n, ist("connection")) || |
| 5123 | isteq(list[idx].n, ist("proxy-connection")) || |
| 5124 | isteq(list[idx].n, ist("keep-alive")) || |
| 5125 | isteq(list[idx].n, ist("upgrade")) || |
| 5126 | isteq(list[idx].n, ist("te")) || |
| 5127 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 5128 | continue; |
| 5129 | |
| 5130 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 5131 | /* output full */ |
| 5132 | if (b_space_wraps(&h2c->mbuf)) |
| 5133 | goto realign_again; |
| 5134 | goto full; |
| 5135 | } |
| 5136 | } |
| 5137 | |
Willy Tarreau | 5121e5d | 2019-05-06 15:13:41 +0200 | [diff] [blame] | 5138 | if (outbuf.data == 9) { |
| 5139 | /* here we have a problem, we have nothing to emit (either we |
| 5140 | * received an empty trailers block followed or we removed its |
| 5141 | * contents above). Because of this we can't send a HEADERS |
| 5142 | * frame, so we have to cheat and instead send an empty DATA |
| 5143 | * frame conveying the ES flag. |
Willy Tarreau | 67b8cae | 2019-02-21 18:16:35 +0100 | [diff] [blame] | 5144 | */ |
| 5145 | outbuf.area[3] = H2_FT_DATA; |
| 5146 | outbuf.area[4] = H2_F_DATA_END_STREAM; |
| 5147 | } |
| 5148 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5149 | /* update the frame's size */ |
| 5150 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 5151 | |
| 5152 | /* commit the H2 response */ |
| 5153 | b_add(&h2c->mbuf, outbuf.data); |
| 5154 | h2s->flags |= H2_SF_ES_SENT; |
| 5155 | |
| 5156 | if (h2s->st == H2_SS_OPEN) |
| 5157 | h2s->st = H2_SS_HLOC; |
| 5158 | else |
| 5159 | h2s_close(h2s); |
| 5160 | |
| 5161 | /* OK we could properly deliver the response */ |
| 5162 | done: |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5163 | /* remove all header blocks till the end and compute the corresponding size. */ |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5164 | ret = 0; |
| 5165 | idx = htx_get_head(htx); |
| 5166 | blk = htx_get_blk(htx, idx); |
| 5167 | while (blk != blk_end) { |
| 5168 | ret += htx_get_blksz(blk); |
| 5169 | blk = htx_remove_blk(htx, blk); |
| 5170 | } |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5171 | end: |
| 5172 | return ret; |
| 5173 | full: |
| 5174 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5175 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5176 | ret = 0; |
| 5177 | goto end; |
| 5178 | fail: |
| 5179 | /* unparsable HTX messages, too large ones to be produced in the local |
| 5180 | * list etc go here (unrecoverable errors). |
| 5181 | */ |
| 5182 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5183 | ret = 0; |
| 5184 | goto end; |
| 5185 | } |
| 5186 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5187 | /* Called from the upper layer, to subscribe to events, such as being able to send. |
| 5188 | * The <param> argument here is supposed to be a pointer to a wait_event struct |
| 5189 | * which will be passed to h2s->recv_wait or h2s->send_wait depending on the |
| 5190 | * event_type. The event_type must only be a combination of SUB_RETRY_RECV and |
| 5191 | * SUB_RETRY_SEND, other values will lead to -1 being returned. It always |
| 5192 | * returns 0 except for the error above. |
| 5193 | */ |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5194 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 5195 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5196 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5197 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5198 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5199 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5200 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5201 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5202 | BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV)); |
| 5203 | sw->events |= SUB_RETRY_RECV; |
| 5204 | h2s->recv_wait = sw; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5205 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5206 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5207 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5208 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5209 | BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND)); |
| 5210 | sw->events |= SUB_RETRY_SEND; |
| 5211 | h2s->send_wait = sw; |
| 5212 | if (!(h2s->flags & H2_SF_BLK_SFCTL) && |
| 5213 | !LIST_ADDED(&h2s->list)) { |
| 5214 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 5215 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 5216 | else |
| 5217 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 5218 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5219 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5220 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5221 | if (event_type != 0) |
| 5222 | return -1; |
| 5223 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5224 | } |
| 5225 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5226 | /* Called from the upper layer, to unsubscribe some events (undo h2_subscribe). |
| 5227 | * The <param> argument here is supposed to be a pointer to the same wait_event |
| 5228 | * struct that was passed to h2_subscribe() otherwise nothing will be changed. |
| 5229 | * It always returns zero. |
| 5230 | */ |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5231 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 5232 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5233 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5234 | struct h2s *h2s = cs->ctx; |
| 5235 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5236 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5237 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5238 | BUG_ON(h2s->recv_wait != sw); |
| 5239 | sw->events &= ~SUB_RETRY_RECV; |
| 5240 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5241 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5242 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5243 | sw = param; |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5244 | BUG_ON(h2s->send_wait != sw); |
| 5245 | LIST_DEL(&h2s->list); |
| 5246 | LIST_INIT(&h2s->list); |
| 5247 | sw->events &= ~SUB_RETRY_SEND; |
| 5248 | /* We were about to send, make sure it does not happen */ |
| 5249 | if (LIST_ADDED(&h2s->sending_list) && |
| 5250 | h2s->send_wait != &h2s->wait_event) { |
| 5251 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
| 5252 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5253 | } |
Olivier Houchard | f833815 | 2019-05-14 17:50:32 +0200 | [diff] [blame] | 5254 | h2s->send_wait = NULL; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5255 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5256 | return 0; |
| 5257 | } |
| 5258 | |
| 5259 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5260 | /* Called from the upper layer, to receive data */ |
| 5261 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 5262 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5263 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5264 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5265 | struct htx *h2s_htx = NULL; |
| 5266 | struct htx *buf_htx = NULL; |
| 5267 | struct htx_ret htx_ret; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5268 | size_t ret = 0; |
| 5269 | |
| 5270 | /* transfer possibly pending data to the upper layer */ |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5271 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
| 5272 | /* in HTX mode we ignore the count argument */ |
| 5273 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5274 | if (htx_is_empty(h2s_htx)) { |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5275 | /* Here htx_to_buf() will set buffer data to 0 because |
| 5276 | * the HTX is empty. |
| 5277 | */ |
| 5278 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5279 | goto end; |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5280 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5281 | |
| 5282 | buf_htx = htx_from_buf(buf); |
Christopher Faulet | a413e95 | 2019-01-21 11:49:37 +0100 | [diff] [blame] | 5283 | count = htx_free_data_space(buf_htx); |
| 5284 | if (flags & CO_RFL_KEEP_RSV) { |
| 5285 | if (count <= global.tune.maxrewrite) |
| 5286 | goto end; |
| 5287 | count -= global.tune.maxrewrite; |
| 5288 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5289 | |
Willy Tarreau | 0c22fa7 | 2018-12-04 15:21:35 +0100 | [diff] [blame] | 5290 | htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM); |
Willy Tarreau | 7196dd6 | 2019-03-05 10:51:11 +0100 | [diff] [blame] | 5291 | |
| 5292 | if (h2s_htx->flags & HTX_FL_PARSING_ERROR) |
| 5293 | buf_htx->flags |= HTX_FL_PARSING_ERROR; |
| 5294 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 5295 | 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] | 5296 | htx_to_buf(buf_htx, buf); |
| 5297 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5298 | ret = htx_ret.ret; |
| 5299 | } |
| 5300 | else { |
| 5301 | ret = b_xfer(buf, &h2s->rxbuf, count); |
| 5302 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5303 | |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5304 | end: |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5305 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5306 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5307 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5308 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Christopher Faulet | fa922f0 | 2019-05-07 10:55:17 +0200 | [diff] [blame] | 5309 | if (h2s->flags & H2_SF_ES_RCVD) |
| 5310 | cs->flags |= CS_FL_EOI; |
Willy Tarreau | 99ad1b3 | 2019-05-14 11:46:28 +0200 | [diff] [blame] | 5311 | if (H2_SS_MASK(h2s->st) & H2_SS_EOS_BITS) |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5312 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5313 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5314 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5315 | if (b_size(&h2s->rxbuf)) { |
| 5316 | b_free(&h2s->rxbuf); |
| 5317 | offer_buffers(NULL, tasks_run_queue); |
| 5318 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5319 | } |
| 5320 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5321 | if (ret && h2c->dsi == h2s->id) { |
| 5322 | /* demux is blocking on this stream's buffer */ |
| 5323 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 5324 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5325 | } |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5326 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5327 | return ret; |
| 5328 | } |
| 5329 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5330 | /* stops all senders of this connection for example when the mux buffer is full. |
| 5331 | * They are moved from the sending_list to either fctl_list or send_list. |
| 5332 | */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5333 | static void h2_stop_senders(struct h2c *h2c) |
| 5334 | { |
| 5335 | struct h2s *h2s, *h2s_back; |
| 5336 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5337 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) { |
| 5338 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | e73256f | 2019-03-25 18:02:54 +0100 | [diff] [blame] | 5339 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5340 | h2s->send_wait->events |= SUB_RETRY_SEND; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5341 | } |
| 5342 | } |
| 5343 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5344 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 5345 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 5346 | * flags may be updated on the conn_stream. |
| 5347 | */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 5348 | 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] | 5349 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5350 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5351 | size_t orig_count = count; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 5352 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5353 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5354 | struct htx *htx; |
| 5355 | struct htx_blk *blk; |
| 5356 | enum htx_blk_type btype; |
| 5357 | uint32_t bsize; |
| 5358 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5359 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5360 | /* If we were not just woken because we wanted to send but couldn't, |
| 5361 | * and there's somebody else that is waiting to send, do nothing, |
| 5362 | * we will subscribe later and be put at the end of the list |
| 5363 | */ |
Willy Tarreau | c234ae3 | 2019-05-13 17:56:11 +0200 | [diff] [blame] | 5364 | if (!LIST_ADDED(&h2s->sending_list) && |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5365 | (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) |
| 5366 | return 0; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5367 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5368 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5369 | /* We couldn't set it to NULL before, because we needed it in case |
| 5370 | * we had to cancel the tasklet |
| 5371 | */ |
| 5372 | h2s->send_wait = NULL; |
| 5373 | |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 5374 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 5375 | return 0; |
| 5376 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5377 | /* htx will be enough to decide if we're using HTX or legacy */ |
| 5378 | htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL; |
| 5379 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5380 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 5381 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 5382 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5383 | if (h2s->id == 0) { |
| 5384 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 5385 | |
| 5386 | if (id < 0) { |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5387 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5388 | return 0; |
| 5389 | } |
| 5390 | |
| 5391 | eb32_delete(&h2s->by_id); |
| 5392 | h2s->by_id.key = h2s->id = id; |
| 5393 | h2s->h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 5394 | h2s->h2c->nb_reserved--; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5395 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 5396 | } |
| 5397 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5398 | if (htx) { |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5399 | while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) && |
Willy Tarreau | c14999b | 2018-12-06 14:09:09 +0100 | [diff] [blame] | 5400 | count && !htx_is_empty(htx)) { |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5401 | idx = htx_get_head(htx); |
| 5402 | blk = htx_get_blk(htx, idx); |
| 5403 | btype = htx_get_blk_type(blk); |
| 5404 | bsize = htx_get_blksz(blk); |
| 5405 | |
| 5406 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5407 | case HTX_BLK_REQ_SL: |
| 5408 | /* start-line before headers */ |
| 5409 | ret = h2s_htx_bck_make_req_headers(h2s, htx); |
| 5410 | if (ret > 0) { |
| 5411 | total += ret; |
| 5412 | count -= ret; |
| 5413 | if (ret < bsize) |
| 5414 | goto done; |
| 5415 | } |
| 5416 | break; |
| 5417 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 5418 | case HTX_BLK_RES_SL: |
| 5419 | /* start-line before headers */ |
| 5420 | ret = h2s_htx_frt_make_resp_headers(h2s, htx); |
| 5421 | if (ret > 0) { |
| 5422 | total += ret; |
| 5423 | count -= ret; |
| 5424 | if (ret < bsize) |
| 5425 | goto done; |
| 5426 | } |
| 5427 | break; |
| 5428 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5429 | case HTX_BLK_DATA: |
| 5430 | case HTX_BLK_EOD: |
| 5431 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5432 | /* all these cause the emission of a DATA frame (possibly empty). |
| 5433 | * This EOM necessarily is one before trailers, as the EOM following |
| 5434 | * trailers would have been consumed by the trailers parser. |
| 5435 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5436 | ret = h2s_htx_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5437 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5438 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5439 | total += ret; |
| 5440 | count -= ret; |
| 5441 | if (ret < bsize) |
| 5442 | goto done; |
| 5443 | } |
| 5444 | break; |
| 5445 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5446 | case HTX_BLK_TLR: |
| 5447 | /* This is the first trailers block, all the subsequent ones AND |
| 5448 | * the EOM will be swallowed by the parser. |
| 5449 | */ |
| 5450 | ret = h2s_htx_make_trailers(h2s, htx); |
| 5451 | if (ret > 0) { |
| 5452 | total += ret; |
| 5453 | count -= ret; |
| 5454 | if (ret < bsize) |
| 5455 | goto done; |
| 5456 | } |
| 5457 | break; |
| 5458 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5459 | default: |
| 5460 | htx_remove_blk(htx, blk); |
| 5461 | total += bsize; |
| 5462 | count -= bsize; |
| 5463 | break; |
| 5464 | } |
| 5465 | } |
| 5466 | goto done; |
| 5467 | } |
| 5468 | |
| 5469 | /* legacy transfer mode */ |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5470 | while (h2s->h1m.state < H1_MSG_DONE && count) { |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 5471 | if (h2s->h1m.state <= H1_MSG_LAST_LF) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5472 | if (h2s->h2c->flags & H2_CF_IS_BACK) |
| 5473 | ret = -1; |
| 5474 | else |
| 5475 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5476 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5477 | else if (h2s->h1m.state < H1_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5478 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5479 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5480 | else if (h2s->h1m.state == H1_MSG_TRAILERS) { |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5481 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5482 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5483 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5484 | if (unlikely((int)ret <= 0)) { |
| 5485 | if ((int)ret < 0) |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5486 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5487 | break; |
| 5488 | } |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 5489 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5490 | total += count; |
| 5491 | count = 0; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5492 | h2s->h1m.state = H1_MSG_DONE; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5493 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 5494 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5495 | else { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5496 | cs_set_error(cs); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5497 | break; |
| 5498 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5499 | |
| 5500 | total += ret; |
| 5501 | count -= ret; |
| 5502 | |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5503 | if (h2s->st >= H2_SS_HLOC) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5504 | break; |
| 5505 | |
| 5506 | if (h2s->flags & H2_SF_BLK_ANY) |
| 5507 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5508 | } |
| 5509 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5510 | done: |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5511 | if (h2s->st >= H2_SS_HLOC) { |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5512 | /* trim any possibly pending data after we close (extra CR-LF, |
| 5513 | * unprocessed trailers, abnormal extra data, ...) |
| 5514 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5515 | total += count; |
| 5516 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5517 | } |
| 5518 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5519 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 5520 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5521 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 5522 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 5523 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5524 | } |
| 5525 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5526 | if (htx) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5527 | htx_to_buf(htx, buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5528 | } else { |
| 5529 | b_del(buf, total); |
| 5530 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5531 | |
| 5532 | /* The mux is full, cancel the pending tasks */ |
| 5533 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 5534 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 5535 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5536 | |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5537 | /* If we're running HTX, and we read the whole buffer, then pretend |
| 5538 | * we read exactly what the caller specified, as with HTX the caller |
| 5539 | * will always give the buffer size, instead of the amount of data |
| 5540 | * available. |
| 5541 | */ |
| 5542 | if (htx && !b_data(buf)) |
| 5543 | total = orig_count; |
| 5544 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5545 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5546 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5547 | tasklet_wakeup(h2s->h2c->wait_event.task); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5548 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5549 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 5550 | /* If we're waiting for flow control, and we got a shutr on the |
| 5551 | * connection, we will never be unlocked, so add an error on |
| 5552 | * the conn_stream. |
| 5553 | */ |
| 5554 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 5555 | !b_data(&h2s->h2c->dbuf) && |
| 5556 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 5557 | if (cs->flags & CS_FL_EOS) |
| 5558 | cs->flags |= CS_FL_ERROR; |
| 5559 | else |
| 5560 | cs->flags |= CS_FL_ERR_PENDING; |
| 5561 | } |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5562 | if (total > 0) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5563 | /* Ok we managed to send something, leave the send_list */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5564 | LIST_DEL_INIT(&h2s->list); |
| 5565 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5566 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5567 | } |
| 5568 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5569 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5570 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5571 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5572 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5573 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5574 | struct eb32_node *node; |
| 5575 | int fctl_cnt = 0; |
| 5576 | int send_cnt = 0; |
| 5577 | int tree_cnt = 0; |
| 5578 | int orph_cnt = 0; |
| 5579 | |
| 5580 | if (!h2c) |
| 5581 | return; |
| 5582 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5583 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5584 | fctl_cnt++; |
| 5585 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5586 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5587 | send_cnt++; |
| 5588 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5589 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5590 | node = eb32_first(&h2c->streams_by_id); |
| 5591 | while (node) { |
| 5592 | h2s = container_of(node, struct h2s, by_id); |
| 5593 | tree_cnt++; |
| 5594 | if (!h2s->cs) |
| 5595 | orph_cnt++; |
| 5596 | node = eb32_next(node); |
| 5597 | } |
| 5598 | |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5599 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5600 | " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d" |
| 5601 | " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d .mbuf=%u@%p+%u/%u", |
Willy Tarreau | 616ac81 | 2018-07-24 14:12:42 +0200 | [diff] [blame] | 5602 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5603 | 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] | 5604 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5605 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5606 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5607 | h2c->msi, |
| 5608 | (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf), |
| 5609 | (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf)); |
| 5610 | |
| 5611 | if (h2s) { |
| 5612 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5613 | h2s, h2s->id, h2s->flags, |
| 5614 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5615 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5616 | h2s->cs); |
| 5617 | if (h2s->cs) |
| 5618 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5619 | h2s->cs->flags, h2s->cs->data); |
| 5620 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5621 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5622 | |
| 5623 | /*******************************************************/ |
| 5624 | /* functions below are dedicated to the config parsers */ |
| 5625 | /*******************************************************/ |
| 5626 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5627 | /* config parser for global "tune.h2.header-table-size" */ |
| 5628 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5629 | struct proxy *defpx, const char *file, int line, |
| 5630 | char **err) |
| 5631 | { |
| 5632 | if (too_many_args(1, args, err, NULL)) |
| 5633 | return -1; |
| 5634 | |
| 5635 | h2_settings_header_table_size = atoi(args[1]); |
| 5636 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5637 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5638 | return -1; |
| 5639 | } |
| 5640 | return 0; |
| 5641 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5642 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5643 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5644 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5645 | struct proxy *defpx, const char *file, int line, |
| 5646 | char **err) |
| 5647 | { |
| 5648 | if (too_many_args(1, args, err, NULL)) |
| 5649 | return -1; |
| 5650 | |
| 5651 | h2_settings_initial_window_size = atoi(args[1]); |
| 5652 | if (h2_settings_initial_window_size < 0) { |
| 5653 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5654 | return -1; |
| 5655 | } |
| 5656 | return 0; |
| 5657 | } |
| 5658 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5659 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5660 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5661 | struct proxy *defpx, const char *file, int line, |
| 5662 | char **err) |
| 5663 | { |
| 5664 | if (too_many_args(1, args, err, NULL)) |
| 5665 | return -1; |
| 5666 | |
| 5667 | h2_settings_max_concurrent_streams = atoi(args[1]); |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 5668 | if ((int)h2_settings_max_concurrent_streams < 0) { |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5669 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5670 | return -1; |
| 5671 | } |
| 5672 | return 0; |
| 5673 | } |
| 5674 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5675 | /* config parser for global "tune.h2.max-frame-size" */ |
| 5676 | static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx, |
| 5677 | struct proxy *defpx, const char *file, int line, |
| 5678 | char **err) |
| 5679 | { |
| 5680 | if (too_many_args(1, args, err, NULL)) |
| 5681 | return -1; |
| 5682 | |
| 5683 | h2_settings_max_frame_size = atoi(args[1]); |
| 5684 | if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) { |
| 5685 | memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]); |
| 5686 | return -1; |
| 5687 | } |
| 5688 | return 0; |
| 5689 | } |
| 5690 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5691 | |
| 5692 | /****************************************/ |
| 5693 | /* MUX initialization and instanciation */ |
| 5694 | /***************************************/ |
| 5695 | |
| 5696 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5697 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5698 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5699 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5700 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5701 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5702 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5703 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5704 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5705 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5706 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5707 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5708 | .avail_streams = h2_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 5709 | .used_streams = h2_used_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5710 | .shutr = h2_shutr, |
| 5711 | .shutw = h2_shutw, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5712 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 5713 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5714 | .name = "H2", |
| 5715 | }; |
| 5716 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5717 | /* PROTO selection : this mux registers PROTO token "h2" */ |
| 5718 | static struct mux_proto_list mux_proto_h2 = |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5719 | { .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] | 5720 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5721 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 5722 | |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5723 | |
| 5724 | /* The mux operations */ |
| 5725 | static const struct mux_ops h2_htx_ops = { |
| 5726 | .init = h2_init, |
| 5727 | .wake = h2_wake, |
| 5728 | .snd_buf = h2_snd_buf, |
| 5729 | .rcv_buf = h2_rcv_buf, |
| 5730 | .subscribe = h2_subscribe, |
| 5731 | .unsubscribe = h2_unsubscribe, |
| 5732 | .attach = h2_attach, |
| 5733 | .get_first_cs = h2_get_first_cs, |
| 5734 | .detach = h2_detach, |
| 5735 | .destroy = h2_destroy, |
| 5736 | .avail_streams = h2_avail_streams, |
| 5737 | .used_streams = h2_used_streams, |
| 5738 | .shutr = h2_shutr, |
| 5739 | .shutw = h2_shutw, |
| 5740 | .show_fd = h2_show_fd, |
Christopher Faulet | 9f38f5a | 2019-04-03 09:53:32 +0200 | [diff] [blame] | 5741 | .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX, |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5742 | .name = "H2", |
| 5743 | }; |
| 5744 | |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5745 | static struct mux_proto_list mux_proto_h2_htx = |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5746 | { .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] | 5747 | |
| 5748 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx); |
| 5749 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5750 | /* config keyword parsers */ |
| 5751 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5752 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5753 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5754 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5755 | { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5756 | { 0, NULL, NULL } |
| 5757 | }}; |
| 5758 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5759 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |