Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP/2 mux-demux for connections |
| 3 | * |
| 4 | * Copyright 2017 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <common/cfgparse.h> |
| 14 | #include <common/config.h> |
Willy Tarreau | afba57a | 2018-12-11 13:44:24 +0100 | [diff] [blame] | 15 | #include <common/h1.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 16 | #include <common/h2.h> |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 17 | #include <common/hpack-dec.h> |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 18 | #include <common/hpack-enc.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 19 | #include <common/hpack-tbl.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 20 | #include <common/htx.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 21 | #include <common/initcall.h> |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 22 | #include <common/net_helper.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 23 | #include <proto/connection.h> |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 24 | #include <proto/http_htx.h> |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 25 | #include <proto/session.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 26 | #include <proto/stream.h> |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 27 | #include <proto/stream_interface.h> |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 28 | #include <types/session.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 29 | #include <eb32tree.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 30 | |
| 31 | |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 32 | /* dummy streams returned for closed, error, refused, idle and states */ |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 33 | static const struct h2s *h2_closed_stream; |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 34 | static const struct h2s *h2_error_stream; |
Willy Tarreau | 8d0d58b | 2018-12-23 18:29:12 +0100 | [diff] [blame] | 35 | static const struct h2s *h2_refused_stream; |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 36 | static const struct h2s *h2_idle_stream; |
| 37 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 38 | /* Connection flags (32 bit), in h2c->flags */ |
| 39 | #define H2_CF_NONE 0x00000000 |
| 40 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 41 | /* Flags indicating why writing to the mux is blocked. */ |
| 42 | #define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer |
| 43 | #define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full |
| 44 | #define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above |
| 45 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 46 | /* Flags indicating why writing to the demux is blocked. |
| 47 | * The first two ones directly affect the ability for the mux to receive data |
| 48 | * from the connection. The other ones affect the mux's ability to demux |
| 49 | * received data. |
| 50 | */ |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 51 | #define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer |
| 52 | #define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 53 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 54 | #define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy |
| 55 | #define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer |
| 56 | #define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer |
| 57 | #define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 58 | #define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave |
| 59 | #define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 60 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 61 | /* other flags */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 62 | #define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent |
| 63 | #define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent |
| 64 | #define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake |
Willy Tarreau | b3fb56d | 2018-10-03 13:56:38 +0200 | [diff] [blame] | 65 | #define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 66 | #define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 67 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 68 | /* H2 connection state, in h2c->st0 */ |
| 69 | enum h2_cs { |
| 70 | H2_CS_PREFACE, // init done, waiting for connection preface |
| 71 | H2_CS_SETTINGS1, // preface OK, waiting for first settings frame |
| 72 | H2_CS_FRAME_H, // first settings frame ok, waiting for frame header |
| 73 | H2_CS_FRAME_P, // frame header OK, waiting for frame payload |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 74 | H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame |
| 75 | H2_CS_FRAME_E, // frame payload OK, trying to send RST frame |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 76 | H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP |
| 77 | H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP |
| 78 | H2_CS_ENTRIES // must be last |
| 79 | } __attribute__((packed)); |
| 80 | |
| 81 | /* H2 connection descriptor */ |
| 82 | struct h2c { |
| 83 | struct connection *conn; |
| 84 | |
| 85 | enum h2_cs st0; /* mux state */ |
| 86 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 87 | |
| 88 | /* 16 bit hole here */ |
| 89 | uint32_t flags; /* connection flags: H2_CF_* */ |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 90 | uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 91 | int32_t max_id; /* highest ID known on this connection, <0 before preface */ |
| 92 | uint32_t rcvd_c; /* newly received data to ACK for the connection */ |
| 93 | uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */ |
| 94 | |
| 95 | /* states for the demux direction */ |
| 96 | struct hpack_dht *ddht; /* demux dynamic header table */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 97 | struct buffer dbuf; /* demux buffer */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 98 | |
| 99 | int32_t dsi; /* demux stream ID (<0 = idle) */ |
| 100 | int32_t dfl; /* demux frame length (if dsi >= 0) */ |
| 101 | int8_t dft; /* demux frame type (if dsi >= 0) */ |
| 102 | int8_t dff; /* demux frame flags (if dsi >= 0) */ |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 103 | uint8_t dpl; /* demux pad length (part of dfl), init to 0 */ |
| 104 | /* 8 bit hole here */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 105 | int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */ |
| 106 | |
| 107 | /* states for the mux direction */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 108 | struct buffer mbuf; /* mux buffer */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 109 | int32_t msi; /* mux stream ID (<0 = idle) */ |
| 110 | int32_t mfl; /* mux frame length (if dsi >= 0) */ |
| 111 | int8_t mft; /* mux frame type (if dsi >= 0) */ |
| 112 | int8_t mff; /* mux frame flags (if dsi >= 0) */ |
| 113 | /* 16 bit hole here */ |
| 114 | int32_t miw; /* mux initial window size for all new streams */ |
| 115 | int32_t mws; /* mux window size. Can be negative. */ |
| 116 | int32_t mfs; /* mux's max frame size */ |
| 117 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 118 | int timeout; /* idle timeout duration in ticks */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 119 | int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */ |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 120 | unsigned int nb_streams; /* number of streams in the tree */ |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 121 | unsigned int nb_cs; /* number of attached conn_streams */ |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 122 | unsigned int nb_reserved; /* number of reserved streams */ |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 123 | unsigned int stream_cnt; /* total number of streams seen */ |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 124 | struct proxy *proxy; /* the proxy this connection was created for */ |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 125 | struct task *task; /* timeout management task */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 126 | struct eb_root streams_by_id; /* all active streams by their ID */ |
| 127 | struct list send_list; /* list of blocked streams requesting to send */ |
| 128 | struct list fctl_list; /* list of streams blocked by connection's fctl */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 129 | struct list sending_list; /* list of h2s scheduled to send data */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 130 | struct buffer_wait buf_wait; /* wait list for buffer allocations */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 131 | struct wait_event wait_event; /* To be used if we're waiting for I/Os */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 132 | }; |
| 133 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 134 | /* H2 stream state, in h2s->st */ |
| 135 | enum h2_ss { |
| 136 | H2_SS_IDLE = 0, // idle |
| 137 | H2_SS_RLOC, // reserved(local) |
| 138 | H2_SS_RREM, // reserved(remote) |
| 139 | H2_SS_OPEN, // open |
| 140 | H2_SS_HREM, // half-closed(remote) |
| 141 | H2_SS_HLOC, // half-closed(local) |
Willy Tarreau | 96060ba | 2017-10-16 18:34:34 +0200 | [diff] [blame] | 142 | H2_SS_ERROR, // an error needs to be sent using RST_STREAM |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 143 | H2_SS_CLOSED, // closed |
| 144 | H2_SS_ENTRIES // must be last |
| 145 | } __attribute__((packed)); |
| 146 | |
| 147 | /* HTTP/2 stream flags (32 bit), in h2s->flags */ |
| 148 | #define H2_SF_NONE 0x00000000 |
| 149 | #define H2_SF_ES_RCVD 0x00000001 |
| 150 | #define H2_SF_ES_SENT 0x00000002 |
| 151 | |
| 152 | #define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM |
| 153 | #define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM |
| 154 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 155 | /* stream flags indicating the reason the stream is blocked */ |
| 156 | #define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient) |
| 157 | #define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux |
| 158 | #define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl |
| 159 | #define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl |
| 160 | #define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above |
| 161 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 162 | /* stream flags indicating how data is supposed to be sent */ |
| 163 | #define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length |
| 164 | #define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding |
| 165 | |
| 166 | /* step we're currently in when sending chunks. This is needed because we may |
| 167 | * have to transfer chunks as large as a full buffer so there's no room left |
| 168 | * for size nor crlf around. |
| 169 | */ |
| 170 | #define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size |
| 171 | #define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data |
| 172 | #define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data |
| 173 | |
| 174 | #define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size |
| 175 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 176 | #define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 177 | #define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 178 | |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 179 | #define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream |
| 180 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 181 | /* H2 stream descriptor, describing the stream as it appears in the H2C, and as |
| 182 | * it is being processed in the internal HTTP representation (H1 for now). |
| 183 | */ |
| 184 | struct h2s { |
| 185 | struct conn_stream *cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 186 | struct session *sess; |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 187 | struct h2c *h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 188 | struct h1m h1m; /* request or response parser state for H1 */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 189 | struct eb32_node by_id; /* place in h2c's streams_by_id */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 190 | int32_t id; /* stream ID */ |
| 191 | uint32_t flags; /* H2_SF_* */ |
| 192 | int mws; /* mux window size for this stream */ |
| 193 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 194 | enum h2_ss st; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 195 | uint16_t status; /* HTTP response status */ |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 196 | unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */ |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 197 | struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 198 | struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */ |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 199 | struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */ |
| 200 | struct wait_event *send_wait; /* send wait_event the conn_stream associated is waiting on (via h2_subscribe) */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 201 | struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 202 | struct list sending_list; /* To be used when adding in h2c->sending_list */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 203 | }; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 204 | |
Willy Tarreau | c640514 | 2017-09-21 20:23:50 +0200 | [diff] [blame] | 205 | /* descriptor for an h2 frame header */ |
| 206 | struct h2_fh { |
| 207 | uint32_t len; /* length, host order, 24 bits */ |
| 208 | uint32_t sid; /* stream id, host order, 31 bits */ |
| 209 | uint8_t ft; /* frame type */ |
| 210 | uint8_t ff; /* frame flags */ |
| 211 | }; |
| 212 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 213 | /* the h2c connection pool */ |
| 214 | DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c)); |
| 215 | |
| 216 | /* the h2s stream pool */ |
| 217 | DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s)); |
| 218 | |
Willy Tarreau | dc57236 | 2018-12-12 08:08:05 +0100 | [diff] [blame] | 219 | /* The default connection window size is 65535, it may only be enlarged using |
| 220 | * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1, |
| 221 | * we'll pretend we already received the difference between the two to send |
| 222 | * an equivalent window update to enlarge it to 2G-1. |
| 223 | */ |
| 224 | #define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535) |
| 225 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 226 | /* a few settings from the global section */ |
| 227 | static int h2_settings_header_table_size = 4096; /* initial value */ |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 228 | static int h2_settings_initial_window_size = 65535; /* initial value */ |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 229 | static unsigned int h2_settings_max_concurrent_streams = 100; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 230 | static int h2_settings_max_frame_size = 0; /* unset */ |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 231 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 232 | /* a dmumy closed stream */ |
| 233 | static const struct h2s *h2_closed_stream = &(const struct h2s){ |
| 234 | .cs = NULL, |
| 235 | .h2c = NULL, |
| 236 | .st = H2_SS_CLOSED, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 237 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 238 | .flags = H2_SF_RST_RCVD, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 239 | .id = 0, |
| 240 | }; |
| 241 | |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 242 | /* a dmumy closed stream returning a PROTOCOL_ERROR error */ |
| 243 | static const struct h2s *h2_error_stream = &(const struct h2s){ |
| 244 | .cs = NULL, |
| 245 | .h2c = NULL, |
| 246 | .st = H2_SS_CLOSED, |
| 247 | .errcode = H2_ERR_PROTOCOL_ERROR, |
| 248 | .flags = 0, |
| 249 | .id = 0, |
| 250 | }; |
| 251 | |
Willy Tarreau | 8d0d58b | 2018-12-23 18:29:12 +0100 | [diff] [blame] | 252 | /* a dmumy closed stream returning a REFUSED_STREAM error */ |
| 253 | static const struct h2s *h2_refused_stream = &(const struct h2s){ |
| 254 | .cs = NULL, |
| 255 | .h2c = NULL, |
| 256 | .st = H2_SS_CLOSED, |
| 257 | .errcode = H2_ERR_REFUSED_STREAM, |
| 258 | .flags = 0, |
| 259 | .id = 0, |
| 260 | }; |
| 261 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 262 | /* and a dummy idle stream for use with any unannounced stream */ |
| 263 | static const struct h2s *h2_idle_stream = &(const struct h2s){ |
| 264 | .cs = NULL, |
| 265 | .h2c = NULL, |
| 266 | .st = H2_SS_IDLE, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 267 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 268 | .id = 0, |
| 269 | }; |
| 270 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 271 | static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state); |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 272 | static int h2_send(struct h2c *h2c); |
| 273 | static int h2_recv(struct h2c *h2c); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 274 | static int h2_process(struct h2c *h2c); |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 275 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state); |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 276 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id); |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 277 | static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len); |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 278 | static int h2_frt_transfer_data(struct h2s *h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 279 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state); |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 280 | static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess); |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 281 | static void h2s_alert(struct h2s *h2s); |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 282 | |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 283 | static __inline int |
| 284 | h2c_is_dead(struct h2c *h2c) |
| 285 | { |
| 286 | if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */ |
| 287 | ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */ |
| 288 | (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */ |
| 289 | (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */ |
| 290 | (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */ |
| 291 | (conn_xprt_read0_pending(h2c->conn) || |
| 292 | (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) |
| 293 | return 1; |
| 294 | |
| 295 | return 0; |
| 296 | |
| 297 | } |
| 298 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 299 | /*****************************************************/ |
| 300 | /* functions below are for dynamic buffer management */ |
| 301 | /*****************************************************/ |
| 302 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 303 | /* indicates whether or not the we may call the h2_recv() function to attempt |
| 304 | * to receive data into the buffer and/or demux pending data. The condition is |
| 305 | * a bit complex due to some API limits for now. The rules are the following : |
| 306 | * - if an error or a shutdown was detected on the connection and the buffer |
| 307 | * is empty, we must not attempt to receive |
| 308 | * - if the demux buf failed to be allocated, we must not try to receive and |
| 309 | * we know there is nothing pending |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 310 | * - if no flag indicates a blocking condition, we may attempt to receive, |
| 311 | * regardless of whether the demux buffer is full or not, so that only |
| 312 | * de demux part decides whether or not to block. This is needed because |
| 313 | * the connection API indeed prevents us from re-enabling receipt that is |
| 314 | * already enabled in a polled state, so we must always immediately stop |
| 315 | * as soon as the demux can't proceed so as never to hit an end of read |
| 316 | * with data pending in the buffers. |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 317 | * - otherwise must may not attempt |
| 318 | */ |
| 319 | static inline int h2_recv_allowed(const struct h2c *h2c) |
| 320 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 321 | if (b_data(&h2c->dbuf) == 0 && |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 322 | (h2c->st0 >= H2_CS_ERROR || |
| 323 | h2c->conn->flags & CO_FL_ERROR || |
| 324 | conn_xprt_read0_pending(h2c->conn))) |
| 325 | return 0; |
| 326 | |
| 327 | if (!(h2c->flags & H2_CF_DEM_DALLOC) && |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 328 | !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 329 | return 1; |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 334 | /* restarts reading on the connection if it was not enabled */ |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 335 | 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] | 336 | { |
| 337 | if (!h2_recv_allowed(h2c)) |
| 338 | return; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 339 | if ((!consider_buffer || !b_data(&h2c->dbuf)) |
| 340 | && (h2c->wait_event.events & SUB_RETRY_RECV)) |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 341 | return; |
| 342 | tasklet_wakeup(h2c->wait_event.task); |
| 343 | } |
| 344 | |
| 345 | |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 346 | /* returns true if the front connection has too many conn_streams attached */ |
| 347 | 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] | 348 | { |
Willy Tarreau | a875466 | 2018-12-23 20:43:58 +0100 | [diff] [blame] | 349 | return h2c->nb_cs > h2_settings_max_concurrent_streams; |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 350 | } |
| 351 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 352 | /* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c |
| 353 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 354 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 355 | * impossible to wake up and we prefer to be woken up later. |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 356 | */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 357 | static int h2_buf_available(void *target) |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 358 | { |
| 359 | struct h2c *h2c = target; |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 360 | struct h2s *h2s; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 361 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 362 | 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] | 363 | h2c->flags &= ~H2_CF_DEM_DALLOC; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 364 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 365 | return 1; |
| 366 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 367 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 368 | if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) { |
| 369 | h2c->flags &= ~H2_CF_MUX_MALLOC; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 370 | |
| 371 | if (h2c->flags & H2_CF_DEM_MROOM) { |
| 372 | h2c->flags &= ~H2_CF_DEM_MROOM; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 373 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 374 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 375 | return 1; |
| 376 | } |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 377 | |
| 378 | if ((h2c->flags & H2_CF_DEM_SALLOC) && |
| 379 | (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs && |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 380 | b_alloc_margin(&h2s->rxbuf, 0)) { |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 381 | h2c->flags &= ~H2_CF_DEM_SALLOC; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 382 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 383 | return 1; |
| 384 | } |
| 385 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 386 | return 0; |
| 387 | } |
| 388 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 389 | 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] | 390 | { |
| 391 | struct buffer *buf = NULL; |
| 392 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 393 | if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) && |
| 394 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 395 | h2c->buf_wait.target = h2c; |
| 396 | h2c->buf_wait.wakeup_cb = h2_buf_available; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 397 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 398 | LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 399 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 400 | __conn_xprt_stop_recv(h2c->conn); |
| 401 | } |
| 402 | return buf; |
| 403 | } |
| 404 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 405 | static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 406 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 407 | if (bptr->size) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 408 | b_free(bptr); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 409 | offer_buffers(h2c->buf_wait.target, tasks_run_queue); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 413 | /* returns the number of allocatable outgoing streams for the connection taking |
| 414 | * the last_sid and the reserved ones into account. |
| 415 | */ |
| 416 | static inline int h2_streams_left(const struct h2c *h2c) |
| 417 | { |
| 418 | int ret; |
| 419 | |
| 420 | /* consider the number of outgoing streams we're allowed to create before |
| 421 | * reaching the last GOAWAY frame seen. max_id is the last assigned id, |
| 422 | * nb_reserved is the number of streams which don't yet have an ID. |
| 423 | */ |
| 424 | ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF; |
| 425 | ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1; |
| 426 | if (ret < 0) |
| 427 | ret = 0; |
| 428 | return ret; |
| 429 | } |
| 430 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 431 | /* returns the number of streams in use on a connection to figure if it's |
| 432 | * idle or not. We check nb_cs and not nb_streams as the caller will want |
| 433 | * to know if it was the last one after a detach(). |
| 434 | */ |
| 435 | static int h2_used_streams(struct connection *conn) |
| 436 | { |
| 437 | struct h2c *h2c = conn->ctx; |
| 438 | |
| 439 | return h2c->nb_cs; |
| 440 | } |
| 441 | |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 442 | /* returns the number of concurrent streams available on the connection */ |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 443 | static int h2_avail_streams(struct connection *conn) |
| 444 | { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 445 | struct server *srv = objt_server(conn->target); |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 446 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 447 | int ret1, ret2; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 448 | |
Willy Tarreau | 6afec46 | 2019-01-28 06:40:19 +0100 | [diff] [blame] | 449 | /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional |
| 450 | * streams on the connection. |
| 451 | */ |
| 452 | if (h2c->last_sid >= 0) |
| 453 | return 0; |
| 454 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 455 | /* note: may be negative if a SETTINGS frame changes the limit */ |
| 456 | ret1 = h2c->streams_limit - h2c->nb_streams; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 457 | |
| 458 | /* we must also consider the limit imposed by stream IDs */ |
| 459 | ret2 = h2_streams_left(h2c); |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 460 | ret1 = MIN(ret1, ret2); |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 461 | if (ret1 > 0 && srv && srv->max_reuse >= 0) { |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 462 | ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0; |
| 463 | ret1 = MIN(ret1, ret2); |
| 464 | } |
| 465 | return ret1; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 466 | } |
| 467 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 468 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 469 | /*****************************************************************/ |
| 470 | /* functions below are dedicated to the mux setup and management */ |
| 471 | /*****************************************************************/ |
| 472 | |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 473 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 474 | * is already initialized before installing the mux, so we detect incoming |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 475 | * connections from the fact that the context is still NULL (even during mux |
| 476 | * upgrades). <input> is always used as Input buffer and may contain data. It is |
| 477 | * the caller responsibility to not reuse it anymore. Returns < 0 on error. |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 478 | */ |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 479 | static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess, |
| 480 | struct buffer *input) |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 481 | { |
| 482 | struct h2c *h2c; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 483 | struct task *t = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 484 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 485 | h2c = pool_alloc(pool_head_h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 486 | if (!h2c) |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 487 | goto fail_no_h2c; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 488 | |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 489 | if (conn_is_back(conn)) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 490 | h2c->flags = H2_CF_IS_BACK; |
| 491 | h2c->shut_timeout = h2c->timeout = prx->timeout.server; |
| 492 | if (tick_isset(prx->timeout.serverfin)) |
| 493 | h2c->shut_timeout = prx->timeout.serverfin; |
| 494 | } else { |
| 495 | h2c->flags = H2_CF_NONE; |
| 496 | h2c->shut_timeout = h2c->timeout = prx->timeout.client; |
| 497 | if (tick_isset(prx->timeout.clientfin)) |
| 498 | h2c->shut_timeout = prx->timeout.clientfin; |
| 499 | } |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 500 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 501 | h2c->proxy = prx; |
Willy Tarreau | 3340029 | 2017-11-05 11:23:40 +0100 | [diff] [blame] | 502 | h2c->task = NULL; |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 503 | if (tick_isset(h2c->timeout)) { |
| 504 | t = task_new(tid_bit); |
| 505 | if (!t) |
| 506 | goto fail; |
| 507 | |
| 508 | h2c->task = t; |
| 509 | t->process = h2_timeout_task; |
| 510 | t->context = h2c; |
| 511 | t->expire = tick_add(now_ms, h2c->timeout); |
| 512 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 513 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 514 | h2c->wait_event.task = tasklet_new(); |
| 515 | if (!h2c->wait_event.task) |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 516 | goto fail; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 517 | h2c->wait_event.task->process = h2_io_cb; |
| 518 | h2c->wait_event.task->context = h2c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 519 | h2c->wait_event.events = 0; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 520 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 521 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 522 | if (!h2c->ddht) |
| 523 | goto fail; |
| 524 | |
| 525 | /* Initialise the context. */ |
| 526 | h2c->st0 = H2_CS_PREFACE; |
| 527 | h2c->conn = conn; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 528 | h2c->streams_limit = h2_settings_max_concurrent_streams; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 529 | h2c->max_id = -1; |
| 530 | h2c->errcode = H2_ERR_NO_ERROR; |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 531 | h2c->rcvd_c = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 532 | h2c->rcvd_s = 0; |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 533 | h2c->nb_streams = 0; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 534 | h2c->nb_cs = 0; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 535 | h2c->nb_reserved = 0; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 536 | h2c->stream_cnt = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 537 | |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 538 | h2c->dbuf = *input; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 539 | h2c->dsi = -1; |
| 540 | h2c->msi = -1; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 541 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 542 | h2c->last_sid = -1; |
| 543 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 544 | h2c->mbuf = BUF_NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 545 | h2c->miw = 65535; /* mux initial window size */ |
| 546 | h2c->mws = 65535; /* mux window size */ |
| 547 | h2c->mfs = 16384; /* initial max frame size */ |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 548 | h2c->streams_by_id = EB_ROOT; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 549 | LIST_INIT(&h2c->send_list); |
| 550 | LIST_INIT(&h2c->fctl_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 551 | LIST_INIT(&h2c->sending_list); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 552 | LIST_INIT(&h2c->buf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 553 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 554 | if (t) |
| 555 | task_queue(t); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 556 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 557 | if (h2c->flags & H2_CF_IS_BACK) { |
| 558 | /* FIXME: this is temporary, for outgoing connections we need |
| 559 | * to immediately allocate a stream until the code is modified |
| 560 | * so that the caller calls ->attach(). For now the outgoing cs |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 561 | * is stored as conn->ctx by the caller. |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 562 | */ |
| 563 | struct h2s *h2s; |
| 564 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 565 | h2s = h2c_bck_stream_new(h2c, conn->ctx, sess); |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 566 | if (!h2s) |
| 567 | goto fail_stream; |
| 568 | } |
| 569 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 570 | conn->ctx = h2c; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 571 | |
Willy Tarreau | 0f38358 | 2018-10-03 14:22:21 +0200 | [diff] [blame] | 572 | /* prepare to read something */ |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 573 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 574 | return 0; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 575 | fail_stream: |
| 576 | hpack_dht_free(h2c->ddht); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 577 | fail: |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 578 | task_destroy(t); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 579 | if (h2c->wait_event.task) |
| 580 | tasklet_free(h2c->wait_event.task); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 581 | pool_free(pool_head_h2c, h2c); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 582 | fail_no_h2c: |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 583 | return -1; |
| 584 | } |
| 585 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 586 | /* returns the next allocatable outgoing stream ID for the H2 connection, or |
| 587 | * -1 if no more is allocatable. |
| 588 | */ |
| 589 | static inline int32_t h2c_get_next_sid(const struct h2c *h2c) |
| 590 | { |
| 591 | int32_t id = (h2c->max_id + 1) | 1; |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 592 | |
| 593 | if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid)) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 594 | id = -1; |
| 595 | return id; |
| 596 | } |
| 597 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 598 | /* returns the stream associated with id <id> or NULL if not found */ |
| 599 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 600 | { |
| 601 | struct eb32_node *node; |
| 602 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 603 | if (id == 0) |
| 604 | return (struct h2s *)h2_closed_stream; |
| 605 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 606 | if (id > h2c->max_id) |
| 607 | return (struct h2s *)h2_idle_stream; |
| 608 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 609 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 610 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 611 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 612 | |
| 613 | return container_of(node, struct h2s, by_id); |
| 614 | } |
| 615 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 616 | /* release function. This one should be called to free all resources allocated |
| 617 | * to the mux. |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 618 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 619 | static void h2_release(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 620 | { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 621 | struct connection *conn = NULL;; |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 622 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 623 | if (h2c) { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 624 | /* The connection must be aattached to this mux to be released */ |
| 625 | if (h2c->conn && h2c->conn->ctx == h2c) |
| 626 | conn = h2c->conn; |
| 627 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 628 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 629 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 630 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 631 | LIST_DEL(&h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 632 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 633 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 634 | h2_release_buf(h2c, &h2c->dbuf); |
| 635 | h2_release_buf(h2c, &h2c->mbuf); |
| 636 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 637 | if (h2c->task) { |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 638 | h2c->task->context = NULL; |
| 639 | task_wakeup(h2c->task, TASK_WOKEN_OTHER); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 640 | h2c->task = NULL; |
| 641 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 642 | if (h2c->wait_event.task) |
| 643 | tasklet_free(h2c->wait_event.task); |
Olivier Houchard | 0e07937 | 2019-04-15 17:51:16 +0200 | [diff] [blame] | 644 | if (h2c->wait_event.events != 0) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 645 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events, |
Olivier Houchard | 0e07937 | 2019-04-15 17:51:16 +0200 | [diff] [blame] | 646 | &h2c->wait_event); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 647 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 648 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 649 | } |
| 650 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 651 | if (conn) { |
| 652 | conn->mux = NULL; |
| 653 | conn->ctx = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 654 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 655 | conn_stop_tracking(conn); |
| 656 | conn_full_close(conn); |
| 657 | if (conn->destroy_cb) |
| 658 | conn->destroy_cb(conn); |
| 659 | conn_free(conn); |
| 660 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 664 | /******************************************************/ |
| 665 | /* functions below are for the H2 protocol processing */ |
| 666 | /******************************************************/ |
| 667 | |
| 668 | /* 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] | 669 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 670 | { |
| 671 | return h2s ? h2s->id : 0; |
| 672 | } |
| 673 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 674 | /* 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] | 675 | 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] | 676 | { |
| 677 | if (h2c->msi < 0) |
| 678 | return 0; |
| 679 | |
| 680 | if (h2c->msi == h2s_id(h2s)) |
| 681 | return 0; |
| 682 | |
| 683 | return 1; |
| 684 | } |
| 685 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 686 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 687 | 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] | 688 | { |
| 689 | h2c->errcode = err; |
| 690 | h2c->st0 = H2_CS_ERROR; |
| 691 | } |
| 692 | |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 693 | /* marks an error on the stream. It may also update an already closed stream |
| 694 | * (e.g. to report an error after an RST was received). |
| 695 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 696 | 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] | 697 | { |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 698 | if (h2s->id && h2s->st != H2_SS_ERROR) { |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 699 | h2s->errcode = err; |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 700 | if (h2s->st < H2_SS_ERROR) |
| 701 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 702 | if (h2s->cs) |
| 703 | cs_set_error(h2s->cs); |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 707 | /* attempt to notify the data layer of recv availability */ |
| 708 | static void __maybe_unused h2s_notify_recv(struct h2s *h2s) |
| 709 | { |
| 710 | struct wait_event *sw; |
| 711 | |
| 712 | if (h2s->recv_wait) { |
| 713 | sw = h2s->recv_wait; |
| 714 | sw->events &= ~SUB_RETRY_RECV; |
| 715 | tasklet_wakeup(sw->task); |
| 716 | h2s->recv_wait = NULL; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | /* attempt to notify the data layer of send availability */ |
| 721 | static void __maybe_unused h2s_notify_send(struct h2s *h2s) |
| 722 | { |
| 723 | struct wait_event *sw; |
| 724 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 725 | if (h2s->send_wait && LIST_ISEMPTY(&h2s->sending_list)) { |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 726 | sw = h2s->send_wait; |
| 727 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fd1e96d | 2019-03-25 14:04:25 +0100 | [diff] [blame] | 728 | LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 729 | tasklet_wakeup(sw->task); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 733 | /* alerts the data layer, trying to wake it up by all means, following |
| 734 | * this sequence : |
| 735 | * - if the h2s' data layer is subscribed to recv, then it's woken up for recv |
| 736 | * - if its subscribed to send, then it's woken up for send |
| 737 | * - if it was subscribed to neither, its ->wake() callback is called |
| 738 | * It is safe to call this function with a closed stream which doesn't have a |
| 739 | * conn_stream anymore. |
| 740 | */ |
| 741 | static void __maybe_unused h2s_alert(struct h2s *h2s) |
| 742 | { |
| 743 | if (h2s->recv_wait || h2s->send_wait) { |
| 744 | h2s_notify_recv(h2s); |
| 745 | h2s_notify_send(h2s); |
| 746 | } |
| 747 | else if (h2s->cs && h2s->cs->data_cb->wake != NULL) |
| 748 | h2s->cs->data_cb->wake(h2s->cs); |
| 749 | } |
| 750 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 751 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 752 | 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] | 753 | { |
| 754 | uint8_t *out = frame; |
| 755 | |
| 756 | *out = len >> 16; |
| 757 | write_n16(out + 1, len); |
| 758 | } |
| 759 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 760 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 761 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 762 | * 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] | 763 | * available in the buffer's input prior to calling this function. The buffer |
| 764 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 765 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 766 | 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] | 767 | const struct buffer *b, int o) |
| 768 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 769 | 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] | 770 | } |
| 771 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 772 | 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] | 773 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 774 | 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] | 775 | } |
| 776 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 777 | 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] | 778 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 779 | 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] | 780 | } |
| 781 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 782 | 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] | 783 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 784 | 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] | 785 | } |
| 786 | |
| 787 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 788 | /* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>. |
| 789 | * The algorithm is not obvious. It turns out that H2 headers are neither |
| 790 | * aligned nor do they use regular sizes. And to add to the trouble, the buffer |
| 791 | * 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] | 792 | * |
| 793 | * b0 b1 b2 b3 b4 b5..b8 |
| 794 | * +----------+---------+--------+----+----+----------------------+ |
| 795 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 796 | * +----------+---------+--------+----+----+----------------------+ |
| 797 | * |
| 798 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 799 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 800 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 801 | * 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] | 802 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 803 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 804 | */ |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 805 | 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] | 806 | { |
| 807 | uint64_t w; |
| 808 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 809 | if (b_data(b) < o + 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 810 | return 0; |
| 811 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 812 | w = h2_get_n64(b, o + 1); |
| 813 | h->len = *(uint8_t*)b_peek(b, o) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 814 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 815 | h->ff = w >> 32; |
| 816 | h->ft = w >> 40; |
| 817 | h->len += w >> 48; |
| 818 | return 1; |
| 819 | } |
| 820 | |
| 821 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 822 | * h2_peek_frame_hdr() above. |
| 823 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 824 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 825 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 826 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 830 | 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] | 831 | { |
| 832 | int ret; |
| 833 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 834 | ret = h2_peek_frame_hdr(b, 0, h); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 835 | if (ret > 0) |
| 836 | h2_skip_frame_hdr(b); |
| 837 | return ret; |
| 838 | } |
| 839 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 840 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 841 | * its connection if the stream was not yet closed. Please use this exclusively |
| 842 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 843 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 844 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 845 | { |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 846 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 847 | h2s->h2c->nb_streams--; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 848 | if (!h2s->id) |
| 849 | h2s->h2c->nb_reserved--; |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 850 | if (h2s->cs) { |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 851 | h2s->cs->flags |= CS_FL_REOS; |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 852 | if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf)) |
| 853 | h2s_notify_recv(h2s); |
| 854 | } |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 855 | } |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 856 | h2s->st = H2_SS_CLOSED; |
| 857 | } |
| 858 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 859 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 860 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 861 | { |
| 862 | h2s_close(h2s); |
| 863 | eb32_delete(&h2s->by_id); |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 864 | if (b_size(&h2s->rxbuf)) { |
| 865 | b_free(&h2s->rxbuf); |
| 866 | offer_buffers(NULL, tasks_run_queue); |
| 867 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 868 | if (h2s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 869 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 870 | if (h2s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 871 | h2s->recv_wait->events &= ~SUB_RETRY_RECV; |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 872 | /* There's no need to explicitly call unsubscribe here, the only |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 873 | * reference left would be in the h2c send_list/fctl_list, and if |
| 874 | * we're in it, we're getting out anyway |
| 875 | */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 876 | LIST_DEL_INIT(&h2s->list); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 877 | if (!LIST_ISEMPTY(&h2s->sending_list)) { |
| 878 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
| 879 | LIST_DEL_INIT(&h2s->sending_list); |
| 880 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 881 | tasklet_free(h2s->wait_event.task); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 882 | pool_free(pool_head_h2s, h2s); |
| 883 | } |
| 884 | |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 885 | /* allocates a new stream <id> for connection <h2c> and adds it into h2c's |
| 886 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 887 | * causes of errors can be any failed memory allocation. The caller is |
| 888 | * responsible for checking if the connection may support an extra stream |
| 889 | * prior to calling this function. |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 890 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 891 | static struct h2s *h2s_new(struct h2c *h2c, int id) |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 892 | { |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 893 | struct h2s *h2s; |
| 894 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 895 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 896 | if (!h2s) |
| 897 | goto out; |
| 898 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 899 | h2s->wait_event.task = tasklet_new(); |
| 900 | if (!h2s->wait_event.task) { |
| 901 | pool_free(pool_head_h2s, h2s); |
| 902 | goto out; |
| 903 | } |
| 904 | h2s->send_wait = NULL; |
| 905 | h2s->recv_wait = NULL; |
| 906 | h2s->wait_event.task->process = h2_deferred_shut; |
| 907 | h2s->wait_event.task->context = h2s; |
| 908 | h2s->wait_event.handle = NULL; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 909 | h2s->wait_event.events = 0; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 910 | LIST_INIT(&h2s->list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 911 | LIST_INIT(&h2s->sending_list); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 912 | h2s->h2c = h2c; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 913 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 914 | h2s->mws = h2c->miw; |
| 915 | h2s->flags = H2_SF_NONE; |
| 916 | h2s->errcode = H2_ERR_NO_ERROR; |
| 917 | h2s->st = H2_SS_IDLE; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 918 | h2s->status = 0; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 919 | h2s->body_len = 0; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 920 | h2s->rxbuf = BUF_NULL; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 921 | |
| 922 | if (h2c->flags & H2_CF_IS_BACK) { |
| 923 | h1m_init_req(&h2s->h1m); |
| 924 | h2s->h1m.err_pos = -1; // don't care about errors on the request path |
| 925 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 926 | } else { |
| 927 | h1m_init_res(&h2s->h1m); |
| 928 | h2s->h1m.err_pos = -1; // don't care about errors on the response path |
| 929 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 930 | } |
| 931 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 932 | h2s->by_id.key = h2s->id = id; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 933 | if (id > 0) |
| 934 | h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 935 | else |
| 936 | h2c->nb_reserved++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 937 | |
| 938 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 939 | h2c->nb_streams++; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 940 | h2c->stream_cnt++; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 941 | |
| 942 | return h2s; |
| 943 | |
| 944 | out_free_h2s: |
| 945 | pool_free(pool_head_h2s, h2s); |
| 946 | out: |
| 947 | return NULL; |
| 948 | } |
| 949 | |
| 950 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 951 | * case of memory allocation error. |
| 952 | */ |
| 953 | static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id) |
| 954 | { |
| 955 | struct session *sess = h2c->conn->owner; |
| 956 | struct conn_stream *cs; |
| 957 | struct h2s *h2s; |
| 958 | |
| 959 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 960 | goto out; |
| 961 | |
| 962 | h2s = h2s_new(h2c, id); |
| 963 | if (!h2s) |
| 964 | goto out; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 965 | |
| 966 | cs = cs_new(h2c->conn); |
| 967 | if (!cs) |
| 968 | goto out_close; |
| 969 | |
Olivier Houchard | 746fb77 | 2018-12-15 19:42:00 +0100 | [diff] [blame] | 970 | cs->flags |= CS_FL_NOT_FIRST; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 971 | h2s->cs = cs; |
| 972 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 973 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 974 | |
| 975 | if (stream_create_from_cs(cs) < 0) |
| 976 | goto out_free_cs; |
| 977 | |
Willy Tarreau | 590a051 | 2018-09-05 11:56:48 +0200 | [diff] [blame] | 978 | /* We want the accept date presented to the next stream to be the one |
| 979 | * we have now, the handshake time to be null (since the next stream |
| 980 | * is not delayed by a handshake), and the idle time to count since |
| 981 | * right now. |
| 982 | */ |
| 983 | sess->accept_date = date; |
| 984 | sess->tv_accept = now; |
| 985 | sess->t_handshake = 0; |
| 986 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 987 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 988 | if (h2_frt_has_too_many_cs(h2c)) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 989 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 990 | return h2s; |
| 991 | |
| 992 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 993 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 994 | cs_free(cs); |
| 995 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 996 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 997 | out: |
Willy Tarreau | 45efc07 | 2018-10-03 18:27:52 +0200 | [diff] [blame] | 998 | sess_log(sess); |
| 999 | return NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1000 | } |
| 1001 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1002 | /* allocates a new stream associated to conn_stream <cs> on the h2c connection |
| 1003 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 1004 | * possible stream ID was reached. |
| 1005 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1006 | 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] | 1007 | { |
| 1008 | struct h2s *h2s = NULL; |
| 1009 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 1010 | if (h2c->nb_streams >= h2c->streams_limit) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1011 | goto out; |
| 1012 | |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 1013 | if (h2_streams_left(h2c) < 1) |
| 1014 | goto out; |
| 1015 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1016 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 1017 | h2s = h2s_new(h2c, 0); |
| 1018 | if (!h2s) |
| 1019 | goto out; |
| 1020 | |
| 1021 | h2s->cs = cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1022 | h2s->sess = sess; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1023 | cs->ctx = h2s; |
| 1024 | h2c->nb_cs++; |
| 1025 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1026 | out: |
| 1027 | return h2s; |
| 1028 | } |
| 1029 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1030 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 1031 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 1032 | * the various settings codes. |
| 1033 | */ |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1034 | static int h2c_send_settings(struct h2c *h2c) |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1035 | { |
| 1036 | struct buffer *res; |
| 1037 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1038 | struct buffer buf; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1039 | int mfs; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1040 | int ret; |
| 1041 | |
| 1042 | if (h2c_mux_busy(h2c, NULL)) { |
| 1043 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1044 | return 0; |
| 1045 | } |
| 1046 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1047 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1048 | if (!res) { |
| 1049 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1050 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 1055 | chunk_memcpy(&buf, |
| 1056 | "\x00\x00\x00" /* length : 0 for now */ |
| 1057 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 1058 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 1059 | 9); |
| 1060 | |
Willy Tarreau | 0bbad6b | 2019-02-26 16:01:52 +0100 | [diff] [blame] | 1061 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1062 | /* send settings_enable_push=0 */ |
| 1063 | chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6); |
| 1064 | } |
| 1065 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1066 | if (h2_settings_header_table_size != 4096) { |
| 1067 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 1068 | |
| 1069 | write_n32(str + 2, h2_settings_header_table_size); |
| 1070 | chunk_memcat(&buf, str, 6); |
| 1071 | } |
| 1072 | |
| 1073 | if (h2_settings_initial_window_size != 65535) { |
| 1074 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 1075 | |
| 1076 | write_n32(str + 2, h2_settings_initial_window_size); |
| 1077 | chunk_memcat(&buf, str, 6); |
| 1078 | } |
| 1079 | |
| 1080 | if (h2_settings_max_concurrent_streams != 0) { |
| 1081 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 1082 | |
| 1083 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 1084 | * the protocol, so never send this value! |
| 1085 | */ |
| 1086 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 1087 | chunk_memcat(&buf, str, 6); |
| 1088 | } |
| 1089 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1090 | mfs = h2_settings_max_frame_size; |
| 1091 | if (mfs > global.tune.bufsize) |
| 1092 | mfs = global.tune.bufsize; |
| 1093 | |
| 1094 | if (!mfs) |
| 1095 | mfs = global.tune.bufsize; |
| 1096 | |
| 1097 | if (mfs != 16384) { |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1098 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 1099 | |
| 1100 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 1101 | * match bufsize - rewrite size, but at the moment it seems |
| 1102 | * that clients don't take care of it. |
| 1103 | */ |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1104 | write_n32(str + 2, mfs); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1105 | chunk_memcat(&buf, str, 6); |
| 1106 | } |
| 1107 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1108 | h2_set_frame_size(buf.area, buf.data - 9); |
| 1109 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1110 | if (unlikely(ret <= 0)) { |
| 1111 | if (!ret) { |
| 1112 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1113 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1114 | return 0; |
| 1115 | } |
| 1116 | else { |
| 1117 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1118 | return 0; |
| 1119 | } |
| 1120 | } |
| 1121 | return ret; |
| 1122 | } |
| 1123 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1124 | /* Try to receive a connection preface, then upon success try to send our |
| 1125 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1126 | * missing data. It may return an error in h2c. |
| 1127 | */ |
| 1128 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 1129 | { |
| 1130 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1131 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1132 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1133 | 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] | 1134 | |
| 1135 | if (unlikely(ret1 <= 0)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1136 | if (ret1 < 0) |
| 1137 | sess_log(h2c->conn->owner); |
| 1138 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1139 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 1140 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1141 | return 0; |
| 1142 | } |
| 1143 | |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1144 | ret2 = h2c_send_settings(h2c); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1145 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1146 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1147 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1148 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1149 | } |
| 1150 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1151 | /* Try to send a connection preface, then upon success try to send our |
| 1152 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1153 | * missing data. It may return an error in h2c. |
| 1154 | */ |
| 1155 | static int h2c_bck_send_preface(struct h2c *h2c) |
| 1156 | { |
| 1157 | struct buffer *res; |
| 1158 | |
| 1159 | if (h2c_mux_busy(h2c, NULL)) { |
| 1160 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
| 1164 | res = h2_get_buf(h2c, &h2c->mbuf); |
| 1165 | if (!res) { |
| 1166 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1167 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1168 | return 0; |
| 1169 | } |
| 1170 | |
| 1171 | if (!b_data(res)) { |
| 1172 | /* preface not yet sent */ |
| 1173 | b_istput(res, ist(H2_CONN_PREFACE)); |
| 1174 | } |
| 1175 | |
| 1176 | return h2c_send_settings(h2c); |
| 1177 | } |
| 1178 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1179 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 1180 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 1181 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 1182 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 1183 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 1184 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 1185 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 1186 | * case so that it is safe to use h2c_error() to report such errors. |
| 1187 | */ |
| 1188 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 1189 | { |
| 1190 | struct buffer *res; |
| 1191 | char str[17]; |
| 1192 | int ret; |
| 1193 | |
| 1194 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 1195 | return 1; // claim that it worked |
| 1196 | |
| 1197 | if (h2c_mux_busy(h2c, h2s)) { |
| 1198 | if (h2s) |
| 1199 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1200 | else |
| 1201 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1205 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1206 | if (!res) { |
| 1207 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1208 | if (h2s) |
| 1209 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1210 | else |
| 1211 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 1216 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 1217 | |
| 1218 | if (h2c->last_sid < 0) |
| 1219 | h2c->last_sid = h2c->max_id; |
| 1220 | |
| 1221 | write_n32(str + 9, h2c->last_sid); |
| 1222 | write_n32(str + 13, h2c->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1223 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1224 | if (unlikely(ret <= 0)) { |
| 1225 | if (!ret) { |
| 1226 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1227 | if (h2s) |
| 1228 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1229 | else |
| 1230 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1231 | return 0; |
| 1232 | } |
| 1233 | else { |
| 1234 | /* we cannot report this error using GOAWAY, so we mark |
| 1235 | * it and claim a success. |
| 1236 | */ |
| 1237 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1238 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 1239 | return 1; |
| 1240 | } |
| 1241 | } |
| 1242 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 1243 | return ret; |
| 1244 | } |
| 1245 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1246 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 1247 | * during mux operations. This stream must be valid and cannot be closed |
| 1248 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 1249 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 1250 | * not yet. |
| 1251 | * |
| 1252 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1253 | * to write the message, it subscribes the stream to future notifications. |
| 1254 | */ |
| 1255 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1256 | { |
| 1257 | struct buffer *res; |
| 1258 | char str[13]; |
| 1259 | int ret; |
| 1260 | |
| 1261 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 1262 | return 1; |
| 1263 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1264 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1265 | * RST_STREAM in response to a RST_STREAM frame. |
| 1266 | */ |
| 1267 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1268 | ret = 1; |
| 1269 | goto ignore; |
| 1270 | } |
| 1271 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1272 | if (h2c_mux_busy(h2c, h2s)) { |
| 1273 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1277 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1278 | if (!res) { |
| 1279 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1280 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
| 1284 | /* len: 4, type: 3, flags: none */ |
| 1285 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1286 | write_n32(str + 5, h2s->id); |
| 1287 | write_n32(str + 9, h2s->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1288 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1289 | |
| 1290 | if (unlikely(ret <= 0)) { |
| 1291 | if (!ret) { |
| 1292 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1293 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1294 | return 0; |
| 1295 | } |
| 1296 | else { |
| 1297 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1298 | return 0; |
| 1299 | } |
| 1300 | } |
| 1301 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1302 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1303 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1304 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1305 | return ret; |
| 1306 | } |
| 1307 | |
| 1308 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 1309 | * 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] | 1310 | * error code, even if the stream is one of the dummy ones, and will update |
| 1311 | * h2s->st to H2_SS_CLOSED if it was not yet. |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1312 | * |
| 1313 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1314 | * 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] | 1315 | * 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] | 1316 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1317 | */ |
| 1318 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1319 | { |
| 1320 | struct buffer *res; |
| 1321 | char str[13]; |
| 1322 | int ret; |
| 1323 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1324 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1325 | * RST_STREAM in response to a RST_STREAM frame. |
| 1326 | */ |
| 1327 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1328 | ret = 1; |
| 1329 | goto ignore; |
| 1330 | } |
| 1331 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1332 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1333 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1334 | return 0; |
| 1335 | } |
| 1336 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1337 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1338 | if (!res) { |
| 1339 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1340 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1341 | return 0; |
| 1342 | } |
| 1343 | |
| 1344 | /* len: 4, type: 3, flags: none */ |
| 1345 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1346 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1347 | write_n32(str + 5, h2c->dsi); |
Willy Tarreau | e6888ff | 2018-12-23 18:26:26 +0100 | [diff] [blame] | 1348 | write_n32(str + 9, h2s->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1349 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1350 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1351 | if (unlikely(ret <= 0)) { |
| 1352 | if (!ret) { |
| 1353 | h2c->flags |= H2_CF_MUX_MFULL; |
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 | else { |
| 1358 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1359 | return 0; |
| 1360 | } |
| 1361 | } |
| 1362 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1363 | ignore: |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1364 | if (h2s->id) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1365 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1366 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1367 | } |
| 1368 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1369 | return ret; |
| 1370 | } |
| 1371 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1372 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 1373 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 1374 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 1375 | * on success or zero if nothing was done. In case of lack of room to write the |
| 1376 | * message, it subscribes the requesting stream to future notifications. |
| 1377 | */ |
| 1378 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 1379 | { |
| 1380 | struct h2c *h2c = h2s->h2c; |
| 1381 | struct buffer *res; |
| 1382 | char str[9]; |
| 1383 | int ret; |
| 1384 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1385 | 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] | 1386 | return 1; |
| 1387 | |
| 1388 | if (h2c_mux_busy(h2c, h2s)) { |
| 1389 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1390 | return 0; |
| 1391 | } |
| 1392 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1393 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1394 | if (!res) { |
| 1395 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1396 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1397 | return 0; |
| 1398 | } |
| 1399 | |
| 1400 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1401 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1402 | write_n32(str + 5, h2s->id); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1403 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1404 | if (likely(ret > 0)) { |
| 1405 | h2s->flags |= H2_SF_ES_SENT; |
| 1406 | } |
| 1407 | else if (!ret) { |
| 1408 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1409 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1410 | return 0; |
| 1411 | } |
| 1412 | else { |
| 1413 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1414 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1415 | } |
| 1416 | return ret; |
| 1417 | } |
| 1418 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1419 | /* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags> |
| 1420 | * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close |
| 1421 | * connection. The stream's state is automatically updated accordingly. If the |
| 1422 | * stream is orphaned, it is destroyed. |
| 1423 | */ |
| 1424 | static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags) |
| 1425 | { |
| 1426 | if (!h2s->cs) { |
| 1427 | /* this stream was already orphaned */ |
| 1428 | h2s_destroy(h2s); |
| 1429 | return; |
| 1430 | } |
| 1431 | |
| 1432 | h2s->cs->flags |= flags; |
| 1433 | if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS)) |
| 1434 | h2s->cs->flags |= CS_FL_ERROR; |
| 1435 | |
| 1436 | h2s_alert(h2s); |
| 1437 | |
| 1438 | if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR) |
| 1439 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 1440 | else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN) |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1441 | h2s->st = H2_SS_HREM; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 1442 | else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC) |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1443 | h2s_close(h2s); |
| 1444 | } |
| 1445 | |
| 1446 | /* wake the streams attached to the connection, whose id is greater than <last> |
| 1447 | * or unassigned. |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1448 | */ |
| 1449 | static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags) |
| 1450 | { |
| 1451 | struct eb32_node *node; |
| 1452 | struct h2s *h2s; |
| 1453 | |
| 1454 | if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR) |
Willy Tarreau | a851935 | 2018-12-18 16:44:28 +0100 | [diff] [blame] | 1455 | flags |= CS_FL_ERR_PENDING; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1456 | |
| 1457 | if (conn_xprt_read0_pending(h2c->conn)) |
Christopher Faulet | 87a8f35 | 2019-03-22 14:51:36 +0100 | [diff] [blame] | 1458 | flags |= CS_FL_REOS; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1459 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1460 | /* Wake all streams with ID > last */ |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1461 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1462 | while (node) { |
| 1463 | h2s = container_of(node, struct h2s, by_id); |
| 1464 | if (h2s->id <= last) |
| 1465 | break; |
| 1466 | node = eb32_next(node); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1467 | h2s_wake_one_stream(h2s, flags); |
| 1468 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1469 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1470 | /* Wake all streams with unassigned ID (ID == 0) */ |
| 1471 | node = eb32_lookup(&h2c->streams_by_id, 0); |
| 1472 | while (node) { |
| 1473 | h2s = container_of(node, struct h2s, by_id); |
| 1474 | if (h2s->id > 0) |
| 1475 | break; |
| 1476 | node = eb32_next(node); |
| 1477 | h2s_wake_one_stream(h2s, flags); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1478 | } |
| 1479 | } |
| 1480 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1481 | /* Increase all streams' outgoing window size by the difference passed in |
| 1482 | * argument. This is needed upon receipt of the settings frame if the initial |
| 1483 | * window size is different. The difference may be negative and the resulting |
| 1484 | * window size as well, for the time it takes to receive some window updates. |
| 1485 | */ |
| 1486 | static void h2c_update_all_ws(struct h2c *h2c, int diff) |
| 1487 | { |
| 1488 | struct h2s *h2s; |
| 1489 | struct eb32_node *node; |
| 1490 | |
| 1491 | if (!diff) |
| 1492 | return; |
| 1493 | |
| 1494 | node = eb32_first(&h2c->streams_by_id); |
| 1495 | while (node) { |
| 1496 | h2s = container_of(node, struct h2s, by_id); |
| 1497 | h2s->mws += diff; |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1498 | |
| 1499 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1500 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | 4087346 | 2019-05-13 08:05:28 +0200 | [diff] [blame] | 1501 | if (h2s->send_wait && LIST_ISEMPTY(&h2s->list)) |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1502 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1503 | } |
| 1504 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1505 | node = eb32_next(node); |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1510 | * 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] | 1511 | * return an error in h2c. The caller must have already verified frame length |
| 1512 | * and stream ID validity. Described in RFC7540#6.5. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1513 | */ |
| 1514 | static int h2c_handle_settings(struct h2c *h2c) |
| 1515 | { |
| 1516 | unsigned int offset; |
| 1517 | int error; |
| 1518 | |
| 1519 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1520 | if (h2c->dfl) { |
| 1521 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1522 | goto fail; |
| 1523 | } |
| 1524 | return 1; |
| 1525 | } |
| 1526 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1527 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1528 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1529 | return 0; |
| 1530 | |
| 1531 | /* parse the frame */ |
| 1532 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1533 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1534 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1535 | |
| 1536 | switch (type) { |
| 1537 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1538 | /* we need to update all existing streams with the |
| 1539 | * difference from the previous iws. |
| 1540 | */ |
| 1541 | if (arg < 0) { // RFC7540#6.5.2 |
| 1542 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1543 | goto fail; |
| 1544 | } |
| 1545 | h2c_update_all_ws(h2c, arg - h2c->miw); |
| 1546 | h2c->miw = arg; |
| 1547 | break; |
| 1548 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1549 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1550 | error = H2_ERR_PROTOCOL_ERROR; |
| 1551 | goto fail; |
| 1552 | } |
| 1553 | h2c->mfs = arg; |
| 1554 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1555 | case H2_SETTINGS_ENABLE_PUSH: |
| 1556 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1557 | error = H2_ERR_PROTOCOL_ERROR; |
| 1558 | goto fail; |
| 1559 | } |
| 1560 | break; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 1561 | case H2_SETTINGS_MAX_CONCURRENT_STREAMS: |
| 1562 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1563 | /* the limit is only for the backend; for the frontend it is our limit */ |
| 1564 | if ((unsigned int)arg > h2_settings_max_concurrent_streams) |
| 1565 | arg = h2_settings_max_concurrent_streams; |
| 1566 | h2c->streams_limit = arg; |
| 1567 | } |
| 1568 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | /* need to ACK this frame now */ |
| 1573 | h2c->st0 = H2_CS_FRAME_A; |
| 1574 | return 1; |
| 1575 | fail: |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1576 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1577 | h2c_error(h2c, error); |
| 1578 | return 0; |
| 1579 | } |
| 1580 | |
| 1581 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1582 | * success or one of the h2_status values. |
| 1583 | */ |
| 1584 | static int h2c_ack_settings(struct h2c *h2c) |
| 1585 | { |
| 1586 | struct buffer *res; |
| 1587 | char str[9]; |
| 1588 | int ret = -1; |
| 1589 | |
| 1590 | if (h2c_mux_busy(h2c, NULL)) { |
| 1591 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1592 | return 0; |
| 1593 | } |
| 1594 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1595 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1596 | if (!res) { |
| 1597 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1598 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1599 | return 0; |
| 1600 | } |
| 1601 | |
| 1602 | memcpy(str, |
| 1603 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1604 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1605 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1606 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1607 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1608 | if (unlikely(ret <= 0)) { |
| 1609 | if (!ret) { |
| 1610 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1611 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1612 | return 0; |
| 1613 | } |
| 1614 | else { |
| 1615 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1616 | return 0; |
| 1617 | } |
| 1618 | } |
| 1619 | return ret; |
| 1620 | } |
| 1621 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1622 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1623 | * 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] | 1624 | * missing data. The caller must have already verified frame length |
| 1625 | * and stream ID validity. |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1626 | */ |
| 1627 | static int h2c_handle_ping(struct h2c *h2c) |
| 1628 | { |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1629 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1630 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1631 | h2c->st0 = H2_CS_FRAME_A; |
| 1632 | return 1; |
| 1633 | } |
| 1634 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1635 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1636 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1637 | * error in h2c. |
| 1638 | */ |
| 1639 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1640 | { |
| 1641 | struct buffer *res; |
| 1642 | char str[13]; |
| 1643 | int ret = -1; |
| 1644 | |
| 1645 | if (h2c_mux_busy(h2c, NULL)) { |
| 1646 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1647 | return 0; |
| 1648 | } |
| 1649 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1650 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1651 | if (!res) { |
| 1652 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1653 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1654 | return 0; |
| 1655 | } |
| 1656 | |
| 1657 | /* length: 4, type: 8, flags: none */ |
| 1658 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1659 | write_n32(str + 5, sid); |
| 1660 | write_n32(str + 9, increment); |
| 1661 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1662 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1663 | |
| 1664 | if (unlikely(ret <= 0)) { |
| 1665 | if (!ret) { |
| 1666 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1667 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1668 | return 0; |
| 1669 | } |
| 1670 | else { |
| 1671 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1672 | return 0; |
| 1673 | } |
| 1674 | } |
| 1675 | return ret; |
| 1676 | } |
| 1677 | |
| 1678 | /* try to send pending window update for the connection. It's safe to call it |
| 1679 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1680 | * failure. It may return an error in h2c. |
| 1681 | */ |
| 1682 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1683 | { |
| 1684 | int ret = 1; |
| 1685 | |
| 1686 | if (h2c->rcvd_c <= 0) |
| 1687 | return 1; |
| 1688 | |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 1689 | if (!(h2c->flags & H2_CF_WINDOW_OPENED)) { |
| 1690 | /* increase the advertised connection window to 2G on |
| 1691 | * first update. |
| 1692 | */ |
| 1693 | h2c->flags |= H2_CF_WINDOW_OPENED; |
| 1694 | h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT; |
| 1695 | } |
| 1696 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1697 | /* send WU for the connection */ |
| 1698 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1699 | if (ret > 0) |
| 1700 | h2c->rcvd_c = 0; |
| 1701 | |
| 1702 | return ret; |
| 1703 | } |
| 1704 | |
| 1705 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1706 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1707 | * room or failure. It may return an error in h2c. |
| 1708 | */ |
| 1709 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1710 | { |
| 1711 | int ret = 1; |
| 1712 | |
| 1713 | if (h2c->rcvd_s <= 0) |
| 1714 | return 1; |
| 1715 | |
| 1716 | /* send WU for the stream */ |
| 1717 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1718 | if (ret > 0) |
| 1719 | h2c->rcvd_s = 0; |
| 1720 | |
| 1721 | return ret; |
| 1722 | } |
| 1723 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1724 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1725 | * success, 0 on missing data or one of the h2_status values. |
| 1726 | */ |
| 1727 | static int h2c_ack_ping(struct h2c *h2c) |
| 1728 | { |
| 1729 | struct buffer *res; |
| 1730 | char str[17]; |
| 1731 | int ret = -1; |
| 1732 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1733 | if (b_data(&h2c->dbuf) < 8) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1734 | return 0; |
| 1735 | |
| 1736 | if (h2c_mux_busy(h2c, NULL)) { |
| 1737 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1738 | return 0; |
| 1739 | } |
| 1740 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1741 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1742 | if (!res) { |
| 1743 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1744 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1745 | return 0; |
| 1746 | } |
| 1747 | |
| 1748 | memcpy(str, |
| 1749 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1750 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1751 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1752 | |
| 1753 | /* copy the original payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1754 | h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1755 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1756 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1757 | if (unlikely(ret <= 0)) { |
| 1758 | if (!ret) { |
| 1759 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1760 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1761 | return 0; |
| 1762 | } |
| 1763 | else { |
| 1764 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1765 | return 0; |
| 1766 | } |
| 1767 | } |
| 1768 | return ret; |
| 1769 | } |
| 1770 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1771 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1772 | * 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] | 1773 | * h2c or h2s. The caller must have already verified frame length and stream ID |
| 1774 | * validity. Described in RFC7540#6.9. |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1775 | */ |
| 1776 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1777 | { |
| 1778 | int32_t inc; |
| 1779 | int error; |
| 1780 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1781 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1782 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1783 | return 0; |
| 1784 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1785 | inc = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1786 | |
| 1787 | if (h2c->dsi != 0) { |
| 1788 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1789 | |
| 1790 | /* it's not an error to receive WU on a closed stream */ |
| 1791 | if (h2s->st == H2_SS_CLOSED) |
| 1792 | return 1; |
| 1793 | |
| 1794 | if (!inc) { |
| 1795 | error = H2_ERR_PROTOCOL_ERROR; |
| 1796 | goto strm_err; |
| 1797 | } |
| 1798 | |
| 1799 | if (h2s->mws >= 0 && h2s->mws + inc < 0) { |
| 1800 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1801 | goto strm_err; |
| 1802 | } |
| 1803 | |
| 1804 | h2s->mws += inc; |
| 1805 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1806 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Willy Tarreau | 4087346 | 2019-05-13 08:05:28 +0200 | [diff] [blame] | 1807 | if (h2s->send_wait && LIST_ISEMPTY(&h2s->list)) |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 1808 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1809 | } |
| 1810 | } |
| 1811 | else { |
| 1812 | /* connection window update */ |
| 1813 | if (!inc) { |
| 1814 | error = H2_ERR_PROTOCOL_ERROR; |
| 1815 | goto conn_err; |
| 1816 | } |
| 1817 | |
| 1818 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1819 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1820 | goto conn_err; |
| 1821 | } |
| 1822 | |
| 1823 | h2c->mws += inc; |
| 1824 | } |
| 1825 | |
| 1826 | return 1; |
| 1827 | |
| 1828 | conn_err: |
| 1829 | h2c_error(h2c, error); |
| 1830 | return 0; |
| 1831 | |
| 1832 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 1833 | h2s_error(h2s, error); |
| 1834 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1835 | return 0; |
| 1836 | } |
| 1837 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1838 | /* 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] | 1839 | * the last ID. Returns > 0 on success or zero on missing data. The caller must |
| 1840 | * have already verified frame length and stream ID validity. Described in |
| 1841 | * RFC7540#6.8. |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1842 | */ |
| 1843 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1844 | { |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1845 | int last; |
| 1846 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1847 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1848 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1849 | return 0; |
| 1850 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1851 | last = h2_get_n32(&h2c->dbuf, 0); |
| 1852 | h2c->errcode = h2_get_n32(&h2c->dbuf, 4); |
Olivier Houchard | 9117780 | 2018-12-19 14:49:39 +0100 | [diff] [blame] | 1853 | h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1854 | if (h2c->last_sid < 0) |
| 1855 | h2c->last_sid = last; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1856 | return 1; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1857 | } |
| 1858 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1859 | /* 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] | 1860 | * invalid. Returns > 0 on success or zero on missing data. It may return an |
| 1861 | * error in h2c. The caller must have already verified frame length and stream |
| 1862 | * ID validity. Described in RFC7540#6.3. |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1863 | */ |
| 1864 | static int h2c_handle_priority(struct h2c *h2c) |
| 1865 | { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1866 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1867 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1868 | return 0; |
| 1869 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1870 | if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1871 | /* 7540#5.3 : can't depend on itself */ |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1872 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1873 | return 0; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1874 | } |
| 1875 | return 1; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1876 | } |
| 1877 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1878 | /* 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] | 1879 | * Returns > 0 on success or zero on missing data. The caller must have already |
| 1880 | * verified frame length and stream ID validity. Described in RFC7540#6.4. |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1881 | */ |
| 1882 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1883 | { |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1884 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1885 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1886 | return 0; |
| 1887 | |
| 1888 | /* late RST, already handled */ |
| 1889 | if (h2s->st == H2_SS_CLOSED) |
| 1890 | return 1; |
| 1891 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1892 | h2s->errcode = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1893 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1894 | |
| 1895 | if (h2s->cs) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 1896 | cs_set_error(h2s->cs); |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 1897 | h2s_alert(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | h2s->flags |= H2_SF_RST_RCVD; |
| 1901 | return 1; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1902 | } |
| 1903 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1904 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 1905 | * It may return an error in h2c or h2s. The caller must consider that the |
| 1906 | * return value is the new h2s in case one was allocated (most common case). |
| 1907 | * Described in RFC7540#6.2. Most of the |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1908 | * errors here are reported as connection errors since it's impossible to |
| 1909 | * recover from such errors after the compression context has been altered. |
| 1910 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1911 | 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] | 1912 | { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1913 | struct buffer rxbuf = BUF_NULL; |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1914 | unsigned long long body_len = 0; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1915 | uint32_t flags = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1916 | int error; |
| 1917 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1918 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1919 | return NULL; // empty buffer |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1920 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1921 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1922 | return NULL; // incomplete frame |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1923 | |
| 1924 | /* now either the frame is complete or the buffer is complete */ |
| 1925 | if (h2s->st != H2_SS_IDLE) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1926 | /* The stream exists/existed, this must be a trailers frame */ |
| 1927 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 1928 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len); |
| 1929 | /* unrecoverable error ? */ |
| 1930 | if (h2c->st0 >= H2_CS_ERROR) |
| 1931 | goto out; |
| 1932 | |
| 1933 | if (error == 0) |
| 1934 | goto out; // missing data |
| 1935 | |
| 1936 | if (error < 0) { |
| 1937 | /* Failed to decode this frame (e.g. too large request) |
| 1938 | * but the HPACK decompressor is still synchronized. |
| 1939 | */ |
| 1940 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 1941 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1942 | goto out; |
Willy Tarreau | aab1a60 | 2019-05-06 11:12:18 +0200 | [diff] [blame] | 1943 | } |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1944 | goto done; |
| 1945 | } |
Willy Tarreau | 1f03550 | 2019-01-30 11:44:07 +0100 | [diff] [blame] | 1946 | /* the connection was already killed by an RST, let's consume |
| 1947 | * the data and send another RST. |
| 1948 | */ |
| 1949 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
| 1950 | h2s = (struct h2s*)h2_error_stream; |
| 1951 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1952 | } |
| 1953 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 1954 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 1955 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1956 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1957 | goto conn_err; |
| 1958 | } |
Willy Tarreau | 415b1ee | 2019-01-02 13:59:43 +0100 | [diff] [blame] | 1959 | else if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 1960 | goto out; // IDLE but too many cs still present |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1961 | |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1962 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1963 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1964 | /* unrecoverable error ? */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1965 | if (h2c->st0 >= H2_CS_ERROR) |
| 1966 | goto out; |
| 1967 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1968 | if (error <= 0) { |
| 1969 | if (error == 0) |
| 1970 | goto out; // missing data |
| 1971 | |
| 1972 | /* Failed to decode this stream (e.g. too large request) |
| 1973 | * but the HPACK decompressor is still synchronized. |
| 1974 | */ |
| 1975 | h2s = (struct h2s*)h2_error_stream; |
| 1976 | goto send_rst; |
| 1977 | } |
| 1978 | |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1979 | /* 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] | 1980 | * positively from h2c_frt_stream_new(), the stream will report the error, |
| 1981 | * 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] | 1982 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 1983 | h2s = h2c_frt_stream_new(h2c, h2c->dsi); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1984 | if (!h2s) { |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 1985 | h2s = (struct h2s*)h2_refused_stream; |
| 1986 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | h2s->st = H2_SS_OPEN; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1990 | h2s->rxbuf = rxbuf; |
| 1991 | h2s->flags |= flags; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 1992 | h2s->body_len = body_len; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1993 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1994 | done: |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1995 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1996 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1997 | |
| 1998 | if (h2s->flags & H2_SF_ES_RCVD) { |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 1999 | if (h2s->cs) |
| 2000 | h2s->cs->flags |= CS_FL_EOI; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2001 | if (h2s->st == H2_SS_OPEN) |
| 2002 | h2s->st = H2_SS_HREM; |
| 2003 | else |
| 2004 | h2s_close(h2s); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2005 | } |
| 2006 | |
Willy Tarreau | 3a429f0 | 2019-01-03 11:41:50 +0100 | [diff] [blame] | 2007 | /* update the max stream ID if the request is being processed */ |
| 2008 | if (h2s->id > h2c->max_id) |
| 2009 | h2c->max_id = h2s->id; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2010 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2011 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2012 | |
| 2013 | conn_err: |
| 2014 | h2c_error(h2c, error); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2015 | goto out; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2016 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2017 | out: |
| 2018 | h2_release_buf(h2c, &rxbuf); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2019 | return NULL; |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2020 | |
| 2021 | send_rst: |
| 2022 | /* make the demux send an RST for the current stream. We may only |
| 2023 | * do this if we're certain that the HEADERS frame was properly |
| 2024 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2025 | */ |
| 2026 | h2_release_buf(h2c, &rxbuf); |
| 2027 | h2c->st0 = H2_CS_FRAME_E; |
| 2028 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2029 | } |
| 2030 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2031 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 2032 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 2033 | * errors here are reported as connection errors since it's impossible to |
| 2034 | * recover from such errors after the compression context has been altered. |
| 2035 | */ |
| 2036 | static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 2037 | { |
| 2038 | int error; |
| 2039 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2040 | if (!b_size(&h2c->dbuf)) |
| 2041 | return NULL; // empty buffer |
| 2042 | |
| 2043 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
| 2044 | return NULL; // incomplete frame |
| 2045 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2046 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2047 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2048 | /* unrecoverable error ? */ |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2049 | if (h2c->st0 >= H2_CS_ERROR) |
| 2050 | return NULL; |
| 2051 | |
Willy Tarreau | 08bb1d6 | 2019-01-30 16:55:48 +0100 | [diff] [blame] | 2052 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2053 | /* RFC7540#5.1 */ |
| 2054 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2055 | h2c->st0 = H2_CS_FRAME_E; |
| 2056 | return NULL; |
| 2057 | } |
| 2058 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2059 | if (error <= 0) { |
| 2060 | if (error == 0) |
| 2061 | return NULL; // missing data |
| 2062 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2063 | /* stream error : send RST_STREAM */ |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2064 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2065 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2066 | return NULL; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2067 | } |
| 2068 | |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2069 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 2070 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2071 | if (h2s->cs) |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2072 | h2s->cs->flags |= CS_FL_EOI; |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2073 | } |
| 2074 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2075 | 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] | 2076 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2077 | else if (h2s->cs && (h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN) |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2078 | h2s->st = H2_SS_HREM; |
Willy Tarreau | 201fe40 | 2019-05-07 18:10:10 +0200 | [diff] [blame] | 2079 | else if ((!h2s->cs || h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC) |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2080 | h2s_close(h2s); |
| 2081 | |
| 2082 | return h2s; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2083 | } |
| 2084 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2085 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 2086 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 2087 | */ |
| 2088 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 2089 | { |
| 2090 | int error; |
| 2091 | |
| 2092 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 2093 | * to signal an end of stream (with the ES flag). |
| 2094 | */ |
| 2095 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2096 | if (!b_size(&h2c->dbuf) && h2c->dfl) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2097 | return 0; // empty buffer |
| 2098 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2099 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2100 | return 0; // incomplete frame |
| 2101 | |
| 2102 | /* now either the frame is complete or the buffer is complete */ |
| 2103 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2104 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2105 | /* RFC7540#6.1 */ |
| 2106 | error = H2_ERR_STREAM_CLOSED; |
| 2107 | goto strm_err; |
| 2108 | } |
| 2109 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2110 | if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) { |
| 2111 | /* RFC7540#8.1.2 */ |
| 2112 | error = H2_ERR_PROTOCOL_ERROR; |
| 2113 | goto strm_err; |
| 2114 | } |
| 2115 | |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 2116 | if (!h2_frt_transfer_data(h2s)) |
| 2117 | return 0; |
| 2118 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2119 | /* call the upper layers to process the frame, then let the upper layer |
| 2120 | * notify the stream about any change. |
| 2121 | */ |
| 2122 | if (!h2s->cs) { |
| 2123 | error = H2_ERR_STREAM_CLOSED; |
| 2124 | goto strm_err; |
| 2125 | } |
| 2126 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 2127 | if (h2c->st0 >= H2_CS_ERROR) |
| 2128 | return 0; |
| 2129 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2130 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2131 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2132 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | /* check for completion : the callee will change this to FRAME_A or |
| 2136 | * FRAME_H once done. |
| 2137 | */ |
| 2138 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2139 | return 0; |
| 2140 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2141 | /* last frame */ |
| 2142 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2143 | if (h2s->cs) |
| 2144 | h2s->cs->flags |= CS_FL_EOI; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2145 | if (h2s->st == H2_SS_OPEN) |
| 2146 | h2s->st = H2_SS_HREM; |
| 2147 | else |
| 2148 | h2s_close(h2s); |
| 2149 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2150 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2151 | |
| 2152 | if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) { |
| 2153 | /* RFC7540#8.1.2 */ |
| 2154 | error = H2_ERR_PROTOCOL_ERROR; |
| 2155 | goto strm_err; |
| 2156 | } |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2157 | } |
| 2158 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2159 | return 1; |
| 2160 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2161 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 2162 | h2s_error(h2s, error); |
| 2163 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2164 | return 0; |
| 2165 | } |
| 2166 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2167 | /* process Rx frames to be demultiplexed */ |
| 2168 | static void h2_process_demux(struct h2c *h2c) |
| 2169 | { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2170 | struct h2s *h2s = NULL, *tmp_h2s; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2171 | struct h2_fh hdr; |
| 2172 | unsigned int padlen = 0; |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 2173 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2174 | if (h2c->st0 >= H2_CS_ERROR) |
| 2175 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2176 | |
| 2177 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2178 | if (h2c->st0 == H2_CS_PREFACE) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2179 | if (h2c->flags & H2_CF_IS_BACK) |
| 2180 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2181 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 2182 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2183 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2184 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2185 | sess_log(h2c->conn->owner); |
| 2186 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2187 | goto fail; |
| 2188 | } |
| 2189 | |
| 2190 | h2c->max_id = 0; |
| 2191 | h2c->st0 = H2_CS_SETTINGS1; |
| 2192 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2193 | |
| 2194 | if (h2c->st0 == H2_CS_SETTINGS1) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2195 | /* ensure that what is pending is a valid SETTINGS frame |
| 2196 | * without an ACK. |
| 2197 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2198 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2199 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2200 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2201 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2202 | sess_log(h2c->conn->owner); |
| 2203 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2204 | goto fail; |
| 2205 | } |
| 2206 | |
| 2207 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 2208 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2209 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2210 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2211 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2212 | goto fail; |
| 2213 | } |
| 2214 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2215 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2216 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2217 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2218 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2219 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2220 | goto fail; |
| 2221 | } |
| 2222 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2223 | /* that's OK, switch to FRAME_P to process it. This is |
| 2224 | * a SETTINGS frame whose header has already been |
| 2225 | * deleted above. |
| 2226 | */ |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2227 | padlen = 0; |
| 2228 | goto new_frame; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2229 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2230 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2231 | |
| 2232 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2233 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2234 | int ret = 0; |
| 2235 | |
| 2236 | if (h2c->st0 >= H2_CS_ERROR) |
| 2237 | break; |
| 2238 | |
| 2239 | if (h2c->st0 == H2_CS_FRAME_H) { |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 2240 | if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2241 | break; |
| 2242 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2243 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2244 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2245 | if (!h2c->nb_streams) { |
| 2246 | /* only log if no other stream can report the error */ |
| 2247 | sess_log(h2c->conn->owner); |
| 2248 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2249 | break; |
| 2250 | } |
| 2251 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2252 | if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) { |
| 2253 | /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA), |
| 2254 | * we read the pad length and drop it from the remaining |
| 2255 | * payload (one byte + the 9 remaining ones = 10 total |
| 2256 | * removed), so we have a frame payload starting after the |
| 2257 | * pad len. Flow controlled frames (DATA) also count the |
| 2258 | * padlen in the flow control, so it must be adjusted. |
| 2259 | */ |
| 2260 | if (hdr.len < 1) { |
| 2261 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2262 | sess_log(h2c->conn->owner); |
| 2263 | goto fail; |
| 2264 | } |
| 2265 | hdr.len--; |
| 2266 | |
| 2267 | if (b_data(&h2c->dbuf) < 10) |
| 2268 | break; // missing padlen |
| 2269 | |
| 2270 | padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9); |
| 2271 | |
| 2272 | if (padlen > hdr.len) { |
| 2273 | /* RFC7540#6.1 : pad length = length of |
| 2274 | * frame payload or greater => error. |
| 2275 | */ |
| 2276 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2277 | sess_log(h2c->conn->owner); |
| 2278 | goto fail; |
| 2279 | } |
| 2280 | |
| 2281 | if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) { |
| 2282 | h2c->rcvd_c++; |
| 2283 | h2c->rcvd_s++; |
| 2284 | } |
| 2285 | b_del(&h2c->dbuf, 1); |
| 2286 | } |
| 2287 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2288 | |
| 2289 | new_frame: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2290 | h2c->dfl = hdr.len; |
| 2291 | h2c->dsi = hdr.sid; |
| 2292 | h2c->dft = hdr.ft; |
| 2293 | h2c->dff = hdr.ff; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2294 | h2c->dpl = padlen; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2295 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2296 | |
| 2297 | /* check for minimum basic frame format validity */ |
| 2298 | ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize); |
| 2299 | if (ret != H2_ERR_NO_ERROR) { |
| 2300 | h2c_error(h2c, ret); |
| 2301 | sess_log(h2c->conn->owner); |
| 2302 | goto fail; |
| 2303 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2307 | tmp_h2s = h2c_st_by_id(h2c, h2c->dsi); |
| 2308 | |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2309 | if (tmp_h2s != h2s && h2s && h2s->cs && |
| 2310 | (b_data(&h2s->rxbuf) || |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2311 | (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS|CS_FL_EOI)))) { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2312 | /* we may have to signal the upper layers */ |
| 2313 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2314 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2315 | } |
| 2316 | h2s = tmp_h2s; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2317 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 2318 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2319 | goto strm_err; |
| 2320 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2321 | if (h2s->st == H2_SS_IDLE && |
| 2322 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 2323 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 2324 | * this state MUST be treated as a connection error |
| 2325 | */ |
| 2326 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2327 | if (!h2c->nb_streams) { |
| 2328 | /* only log if no other stream can report the error */ |
| 2329 | sess_log(h2c->conn->owner); |
| 2330 | } |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2331 | break; |
| 2332 | } |
| 2333 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2334 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 2335 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 2336 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2337 | * this state MUST be treated as a stream error. |
| 2338 | * 6.2, 6.6 and 6.10 further mandate that HEADERS/ |
| 2339 | * PUSH_PROMISE/CONTINUATION cause connection errors. |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2340 | */ |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2341 | if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) |
| 2342 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2343 | else |
| 2344 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2345 | goto strm_err; |
| 2346 | } |
| 2347 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2348 | /* Below the management of frames received in closed state is a |
| 2349 | * bit hackish because the spec makes strong differences between |
| 2350 | * streams closed by receiving RST, sending RST, and seeing ES |
| 2351 | * in both directions. In addition to this, the creation of a |
| 2352 | * new stream reusing the identifier of a closed one will be |
| 2353 | * detected here. Given that we cannot keep track of all closed |
| 2354 | * streams forever, we consider that unknown closed streams were |
| 2355 | * closed on RST received, which allows us to respond with an |
| 2356 | * RST without breaking the connection (eg: to abort a transfer). |
| 2357 | * Some frames have to be silently ignored as well. |
| 2358 | */ |
| 2359 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
Willy Tarreau | 3ad5d31 | 2019-01-29 18:33:26 +0100 | [diff] [blame] | 2360 | 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] | 2361 | /* #5.1.1: The identifier of a newly |
| 2362 | * established stream MUST be numerically |
| 2363 | * greater than all streams that the initiating |
| 2364 | * endpoint has opened or reserved. This |
| 2365 | * governs streams that are opened using a |
| 2366 | * HEADERS frame and streams that are reserved |
| 2367 | * using PUSH_PROMISE. An endpoint that |
| 2368 | * receives an unexpected stream identifier |
| 2369 | * MUST respond with a connection error. |
| 2370 | */ |
| 2371 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2372 | goto strm_err; |
| 2373 | } |
| 2374 | |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2375 | 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] | 2376 | /* RFC7540#5.1:closed: an endpoint that |
| 2377 | * receives any frame other than PRIORITY after |
| 2378 | * receiving a RST_STREAM MUST treat that as a |
| 2379 | * stream error of type STREAM_CLOSED. |
| 2380 | * |
| 2381 | * Note that old streams fall into this category |
| 2382 | * and will lead to an RST being sent. |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2383 | * |
| 2384 | * However, we cannot generalize this to all frame types. Those |
| 2385 | * carrying compression state must still be processed before |
| 2386 | * being dropped or we'll desynchronize the decoder. This can |
| 2387 | * happen with request trailers received after sending an |
| 2388 | * RST_STREAM, or with header/trailers responses received after |
| 2389 | * sending RST_STREAM (aborted stream). |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2390 | */ |
| 2391 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2392 | h2c->st0 = H2_CS_FRAME_E; |
| 2393 | goto strm_err; |
| 2394 | } |
| 2395 | |
| 2396 | /* RFC7540#5.1:closed: if this state is reached as a |
| 2397 | * result of sending a RST_STREAM frame, the peer that |
| 2398 | * receives the RST_STREAM might have already sent |
| 2399 | * frames on the stream that cannot be withdrawn. An |
| 2400 | * endpoint MUST ignore frames that it receives on |
| 2401 | * closed streams after it has sent a RST_STREAM |
| 2402 | * frame. An endpoint MAY choose to limit the period |
| 2403 | * over which it ignores frames and treat frames that |
| 2404 | * arrive after this time as being in error. |
| 2405 | */ |
Willy Tarreau | 24ff1f8 | 2019-01-30 19:20:09 +0100 | [diff] [blame] | 2406 | if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2407 | /* RFC7540#5.1:closed: any frame other than |
| 2408 | * PRIO/WU/RST in this state MUST be treated as |
| 2409 | * a connection error |
| 2410 | */ |
| 2411 | if (h2c->dft != H2_FT_RST_STREAM && |
| 2412 | h2c->dft != H2_FT_PRIORITY && |
| 2413 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 2414 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2415 | goto strm_err; |
| 2416 | } |
| 2417 | } |
| 2418 | } |
| 2419 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2420 | #if 0 |
| 2421 | // problem below: it is not possible to completely ignore such |
| 2422 | // streams as we need to maintain the compression state as well |
| 2423 | // and for this we need to completely process these frames (eg: |
| 2424 | // HEADERS frames) as well as counting DATA frames to emit |
| 2425 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 2426 | // This is a typical case of layer violation where the |
| 2427 | // transported contents are critical to the connection's |
| 2428 | // validity and must be ignored at the same time :-( |
| 2429 | |
| 2430 | /* graceful shutdown, ignore streams whose ID is higher than |
| 2431 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 2432 | */ |
| 2433 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2434 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2435 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2436 | h2c->dfl -= ret; |
| 2437 | ret = h2c->dfl == 0; |
| 2438 | goto strm_err; |
| 2439 | } |
| 2440 | #endif |
| 2441 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2442 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 2443 | case H2_FT_SETTINGS: |
| 2444 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2445 | ret = h2c_handle_settings(h2c); |
| 2446 | |
| 2447 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2448 | ret = h2c_ack_settings(h2c); |
| 2449 | break; |
| 2450 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 2451 | case H2_FT_PING: |
| 2452 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2453 | ret = h2c_handle_ping(h2c); |
| 2454 | |
| 2455 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2456 | ret = h2c_ack_ping(h2c); |
| 2457 | break; |
| 2458 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 2459 | case H2_FT_WINDOW_UPDATE: |
| 2460 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2461 | ret = h2c_handle_window_update(h2c, h2s); |
| 2462 | break; |
| 2463 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2464 | case H2_FT_CONTINUATION: |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2465 | /* RFC7540#6.10: CONTINUATION may only be preceeded by |
| 2466 | * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These |
| 2467 | * frames' parsers consume all following CONTINUATION |
| 2468 | * frames so this one is out of sequence. |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2469 | */ |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2470 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2471 | sess_log(h2c->conn->owner); |
| 2472 | goto fail; |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2473 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2474 | case H2_FT_HEADERS: |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2475 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2476 | if (h2c->flags & H2_CF_IS_BACK) |
| 2477 | tmp_h2s = h2c_bck_handle_headers(h2c, h2s); |
| 2478 | else |
| 2479 | tmp_h2s = h2c_frt_handle_headers(h2c, h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2480 | if (tmp_h2s) { |
| 2481 | h2s = tmp_h2s; |
| 2482 | ret = 1; |
| 2483 | } |
| 2484 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2485 | break; |
| 2486 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2487 | case H2_FT_DATA: |
| 2488 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2489 | ret = h2c_frt_handle_data(h2c, h2s); |
| 2490 | |
| 2491 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2492 | ret = h2c_send_strm_wu(h2c); |
| 2493 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2494 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 2495 | case H2_FT_PRIORITY: |
| 2496 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2497 | ret = h2c_handle_priority(h2c); |
| 2498 | break; |
| 2499 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2500 | case H2_FT_RST_STREAM: |
| 2501 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2502 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 2503 | break; |
| 2504 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 2505 | case H2_FT_GOAWAY: |
| 2506 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2507 | ret = h2c_handle_goaway(h2c); |
| 2508 | break; |
| 2509 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2510 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2511 | default: |
| 2512 | /* drop frames that we ignore. They may be larger than |
| 2513 | * the buffer so we drain all of their contents until |
| 2514 | * we reach the end. |
| 2515 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2516 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2517 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2518 | h2c->dfl -= ret; |
| 2519 | ret = h2c->dfl == 0; |
| 2520 | } |
| 2521 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2522 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2523 | /* We may have to send an RST if not done yet */ |
| 2524 | if (h2s->st == H2_SS_ERROR) |
| 2525 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2526 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2527 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2528 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2529 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2530 | /* error or missing data condition met above ? */ |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2531 | if (ret <= 0) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2532 | break; |
| 2533 | |
| 2534 | if (h2c->st0 != H2_CS_FRAME_H) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2535 | b_del(&h2c->dbuf, h2c->dfl); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2536 | h2c->st0 = H2_CS_FRAME_H; |
| 2537 | } |
| 2538 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2539 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2540 | if (h2c->rcvd_c > 0 && |
| 2541 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2542 | h2c_send_conn_wu(h2c); |
| 2543 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2544 | fail: |
| 2545 | /* we can go here on missing data, blocked response or error */ |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2546 | if (h2s && h2s->cs && |
| 2547 | (b_data(&h2s->rxbuf) || |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2548 | (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS|CS_FL_EOI)))) { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2549 | /* we may have to signal the upper layers */ |
| 2550 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2551 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2552 | } |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2553 | |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 2554 | h2c_restart_reading(h2c, 0); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2558 | * the end. |
| 2559 | */ |
| 2560 | static int h2_process_mux(struct h2c *h2c) |
| 2561 | { |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2562 | struct h2s *h2s, *h2s_back; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2563 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2564 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2565 | if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) { |
| 2566 | if (unlikely(h2c_bck_send_preface(h2c) <= 0)) { |
| 2567 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2568 | if (h2c->st0 == H2_CS_ERROR) { |
| 2569 | h2c->st0 = H2_CS_ERROR2; |
| 2570 | sess_log(h2c->conn->owner); |
| 2571 | } |
| 2572 | goto fail; |
| 2573 | } |
| 2574 | h2c->st0 = H2_CS_SETTINGS1; |
| 2575 | } |
| 2576 | /* need to wait for the other side */ |
Willy Tarreau | 75a930a | 2018-12-12 08:03:58 +0100 | [diff] [blame] | 2577 | if (h2c->st0 < H2_CS_FRAME_H) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2578 | return 1; |
| 2579 | } |
| 2580 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2581 | /* start by sending possibly pending window updates */ |
| 2582 | if (h2c->rcvd_c > 0 && |
| 2583 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2584 | h2c_send_conn_wu(h2c) < 0) |
| 2585 | goto fail; |
| 2586 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2587 | /* First we always process the flow control list because the streams |
| 2588 | * waiting there were already elected for immediate emission but were |
| 2589 | * blocked just on this. |
| 2590 | */ |
| 2591 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2592 | list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2593 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2594 | h2c->st0 >= H2_CS_ERROR) |
| 2595 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2596 | |
| 2597 | if (!LIST_ISEMPTY(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2598 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2599 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2600 | h2s->flags &= ~H2_SF_BLK_ANY; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2601 | /* For some reason, the upper layer failed to subsribe again, |
| 2602 | * so remove it from the send_list |
| 2603 | */ |
| 2604 | if (!h2s->send_wait) { |
| 2605 | LIST_DEL_INIT(&h2s->list); |
| 2606 | continue; |
| 2607 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2608 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2609 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2610 | tasklet_wakeup(h2s->send_wait->task); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2611 | } |
| 2612 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2613 | list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2614 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2615 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2616 | |
| 2617 | if (!LIST_ISEMPTY(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2618 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2619 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2620 | /* For some reason, the upper layer failed to subsribe again, |
| 2621 | * so remove it from the send_list |
| 2622 | */ |
| 2623 | if (!h2s->send_wait) { |
| 2624 | LIST_DEL_INIT(&h2s->list); |
| 2625 | continue; |
| 2626 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2627 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2628 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2629 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2630 | tasklet_wakeup(h2s->send_wait->task); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2631 | } |
| 2632 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2633 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2634 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2635 | if (h2c->st0 == H2_CS_ERROR) { |
| 2636 | if (h2c->max_id >= 0) { |
| 2637 | h2c_send_goaway_error(h2c, NULL); |
| 2638 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2639 | return 0; |
| 2640 | } |
| 2641 | |
| 2642 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2643 | } |
| 2644 | return 1; |
| 2645 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2646 | return (1); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2647 | } |
| 2648 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2649 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2650 | /* Attempt to read data, and subscribe if none available. |
| 2651 | * The function returns 1 if data has been received, otherwise zero. |
| 2652 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2653 | static int h2_recv(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2654 | { |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2655 | struct connection *conn = h2c->conn; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2656 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2657 | int max; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2658 | size_t ret; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2659 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2660 | if (h2c->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2661 | return (b_data(&h2c->dbuf)); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2662 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2663 | if (!h2_recv_allowed(h2c)) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2664 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2665 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2666 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2667 | if (!buf) { |
| 2668 | h2c->flags |= H2_CF_DEM_DALLOC; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2669 | return 0; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2670 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2671 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2672 | do { |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 2673 | b_realign_if_empty(buf); |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2674 | if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
| 2675 | /* HTX in use : try to pre-align the buffer like the |
| 2676 | * rxbufs will be to optimize memory copies. We'll make |
| 2677 | * sure that the frame header lands at the end of the |
| 2678 | * HTX block to alias it upon recv. We cannot use the |
| 2679 | * head because rcv_buf() will realign the buffer if |
| 2680 | * it's empty. Thus we cheat and pretend we already |
| 2681 | * have a few bytes there. |
| 2682 | */ |
| 2683 | max = buf_room_for_htx_data(buf) + 9; |
Willy Tarreau | c0960d1 | 2018-12-14 10:59:15 +0100 | [diff] [blame] | 2684 | buf->head = sizeof(struct htx) - 9; |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2685 | } |
| 2686 | else |
| 2687 | max = b_room(buf); |
| 2688 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2689 | if (max) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2690 | ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2691 | else |
| 2692 | ret = 0; |
| 2693 | } while (ret > 0); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2694 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2695 | if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2696 | 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] | 2697 | |
Olivier Houchard | a1411e6 | 2018-08-17 18:42:48 +0200 | [diff] [blame] | 2698 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2699 | h2_release_buf(h2c, &h2c->dbuf); |
Olivier Houchard | 4667773 | 2018-11-29 17:06:17 +0100 | [diff] [blame] | 2700 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2701 | } |
| 2702 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2703 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2704 | h2c->flags |= H2_CF_DEM_DFULL; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2705 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2706 | } |
| 2707 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2708 | /* Try to send data if possible. |
| 2709 | * The function returns 1 if data have been sent, otherwise zero. |
| 2710 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2711 | static int h2_send(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2712 | { |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2713 | struct connection *conn = h2c->conn; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2714 | int done; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2715 | int sent = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2716 | |
| 2717 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 2718 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2719 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2720 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2721 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2722 | /* a handshake was requested */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2723 | goto schedule; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2724 | } |
| 2725 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2726 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2727 | * pending streams into the existing buffer until it's reportedly full |
| 2728 | * or the end of send requests is reached. Then it tries to send this |
| 2729 | * buffer's contents out, marks it not full if at least one byte could |
| 2730 | * be sent, and tries again. |
| 2731 | * |
| 2732 | * The snd_buf() function normally takes a "flags" argument which may |
| 2733 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2734 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2735 | * connection is streaming lots of data (used to increase TLS record |
| 2736 | * size at the expense of latency). The former can be sent any time |
| 2737 | * there's a buffer full flag, as it indicates at least one stream |
| 2738 | * attempted to send and failed so there are pending data. An |
| 2739 | * alternative would be to set it as long as there's an active stream |
| 2740 | * but that would be problematic for ACKs until we have an absolute |
| 2741 | * guarantee that all waiters have at least one byte to send. The |
| 2742 | * latter should possibly not be set for now. |
| 2743 | */ |
| 2744 | |
| 2745 | done = 0; |
| 2746 | while (!done) { |
| 2747 | unsigned int flags = 0; |
| 2748 | |
| 2749 | /* fill as much as we can into the current buffer */ |
| 2750 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2751 | done = h2_process_mux(h2c); |
| 2752 | |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2753 | if (h2c->flags & H2_CF_MUX_MALLOC) |
| 2754 | break; |
| 2755 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2756 | if (conn->flags & CO_FL_ERROR) |
| 2757 | break; |
| 2758 | |
| 2759 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2760 | flags |= CO_SFL_MSG_MORE; |
| 2761 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2762 | if (b_data(&h2c->mbuf)) { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2763 | 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] | 2764 | if (!ret) |
| 2765 | break; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2766 | sent = 1; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2767 | b_del(&h2c->mbuf, ret); |
| 2768 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2769 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2770 | |
| 2771 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2772 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2773 | } |
| 2774 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2775 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2776 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2777 | b_reset(&h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2778 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2779 | /* We're not full anymore, so we can wake any task that are waiting |
| 2780 | * for us. |
| 2781 | */ |
| 2782 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2783 | struct h2s *h2s; |
| 2784 | |
| 2785 | list_for_each_entry(h2s, &h2c->send_list, list) { |
| 2786 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2787 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2788 | |
| 2789 | if (!LIST_ISEMPTY(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2790 | continue; |
| 2791 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2792 | /* For some reason, the upper layer failed to subsribe again, |
| 2793 | * so remove it from the send_list |
| 2794 | */ |
| 2795 | if (!h2s->send_wait) { |
| 2796 | LIST_DEL_INIT(&h2s->list); |
| 2797 | continue; |
| 2798 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2799 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2800 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2801 | tasklet_wakeup(h2s->send_wait->task); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2802 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 2803 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2804 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2805 | /* We're done, no more to send */ |
| 2806 | if (!b_data(&h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2807 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2808 | schedule: |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2809 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2810 | 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] | 2811 | return sent; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2812 | } |
| 2813 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 2814 | /* this is the tasklet referenced in h2c->wait_event.task */ |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2815 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2816 | { |
| 2817 | struct h2c *h2c = ctx; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2818 | int ret = 0; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2819 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2820 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2821 | ret = h2_send(h2c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2822 | if (!(h2c->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2823 | ret |= h2_recv(h2c); |
Willy Tarreau | cef5c8e | 2018-12-18 10:29:54 +0100 | [diff] [blame] | 2824 | if (ret || b_data(&h2c->dbuf)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2825 | h2_process(h2c); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2826 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2827 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2828 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2829 | /* callback called on any event by the connection handler. |
| 2830 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2831 | * destruction of the connection (which normally doesn not happen in h2). |
| 2832 | */ |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2833 | static int h2_process(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2834 | { |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2835 | struct connection *conn = h2c->conn; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2836 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2837 | if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2838 | h2_process_demux(h2c); |
| 2839 | |
| 2840 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2841 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2842 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2843 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2844 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 2845 | } |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2846 | h2_send(h2c); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2847 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 2848 | if (unlikely(h2c->proxy->state == PR_STSTOPPED)) { |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2849 | /* frontend is stopping, reload likely in progress, let's try |
| 2850 | * to announce a graceful shutdown if not yet done. We don't |
| 2851 | * care if it fails, it will be tried again later. |
| 2852 | */ |
| 2853 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 2854 | if (h2c->last_sid < 0) |
| 2855 | h2c->last_sid = (1U << 31) - 1; |
| 2856 | h2c_send_goaway_error(h2c, NULL); |
| 2857 | } |
| 2858 | } |
| 2859 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2860 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2861 | * If we received early data, and the handshake is done, wake |
| 2862 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2863 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2864 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 2865 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 2866 | struct eb32_node *node; |
| 2867 | struct h2s *h2s; |
| 2868 | |
| 2869 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 2870 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 2871 | |
| 2872 | while (node) { |
| 2873 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | fde287c | 2018-12-19 18:33:16 +0100 | [diff] [blame] | 2874 | if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2875 | h2s_notify_recv(h2s); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2876 | node = eb32_next(node); |
| 2877 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2878 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2879 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 2880 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 2881 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 2882 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 2883 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 2884 | h2_wake_some_streams(h2c, 0, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2885 | |
| 2886 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2887 | /* no more stream, kill the connection now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2888 | h2_release(h2c); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2889 | return -1; |
| 2890 | } |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2891 | } |
| 2892 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2893 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2894 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2895 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2896 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 2897 | h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 2898 | (h2c->st0 != H2_CS_ERROR && |
| 2899 | !b_data(&h2c->mbuf) && |
| 2900 | (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && |
| 2901 | ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list)))) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2902 | h2_release_buf(h2c, &h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2903 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2904 | if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2905 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2906 | 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] | 2907 | task_queue(h2c->task); |
| 2908 | } |
| 2909 | else |
| 2910 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2911 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2912 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2913 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2914 | return 0; |
| 2915 | } |
| 2916 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 2917 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2918 | static int h2_wake(struct connection *conn) |
| 2919 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2920 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2921 | |
| 2922 | return (h2_process(h2c)); |
| 2923 | } |
| 2924 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2925 | /* Connection timeout management. The principle is that if there's no receipt |
| 2926 | * nor sending for a certain amount of time, the connection is closed. If the |
| 2927 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 2928 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 2929 | * GOAWAY frame. |
| 2930 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2931 | 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] | 2932 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2933 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2934 | int expired = tick_is_expired(t->expire, now_ms); |
| 2935 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2936 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2937 | return t; |
| 2938 | |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 2939 | task_destroy(t); |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2940 | |
| 2941 | if (!h2c) { |
| 2942 | /* resources were already deleted */ |
| 2943 | return NULL; |
| 2944 | } |
| 2945 | |
| 2946 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2947 | h2c_error(h2c, H2_ERR_NO_ERROR); |
| 2948 | h2_wake_some_streams(h2c, 0, 0); |
| 2949 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2950 | if (b_data(&h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2951 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 2952 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2953 | } |
| 2954 | |
| 2955 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2956 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2957 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 2958 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2959 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2960 | 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] | 2961 | 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] | 2962 | if (ret > 0) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2963 | b_del(&h2c->mbuf, ret); |
| 2964 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2965 | } |
| 2966 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2967 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2968 | /* either we can release everything now or it will be done later once |
| 2969 | * the last stream closes. |
| 2970 | */ |
| 2971 | if (eb_is_empty(&h2c->streams_by_id)) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2972 | h2_release(h2c); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2973 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2974 | return NULL; |
| 2975 | } |
| 2976 | |
| 2977 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2978 | /*******************************************/ |
| 2979 | /* functions below are used by the streams */ |
| 2980 | /*******************************************/ |
| 2981 | |
| 2982 | /* |
| 2983 | * Attach a new stream to a connection |
| 2984 | * (Used for outgoing connections) |
| 2985 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2986 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2987 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2988 | struct conn_stream *cs; |
| 2989 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2990 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2991 | |
| 2992 | cs = cs_new(conn); |
| 2993 | if (!cs) |
| 2994 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2995 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2996 | if (!h2s) { |
| 2997 | cs_free(cs); |
| 2998 | return NULL; |
| 2999 | } |
| 3000 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3001 | } |
| 3002 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3003 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 3004 | * We have to scan because we may have some orphan streams. It might be |
| 3005 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 3006 | * orphans. |
| 3007 | */ |
| 3008 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 3009 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3010 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3011 | struct h2s *h2s; |
| 3012 | struct eb32_node *node; |
| 3013 | |
| 3014 | node = eb32_first(&h2c->streams_by_id); |
| 3015 | while (node) { |
| 3016 | h2s = container_of(node, struct h2s, by_id); |
| 3017 | if (h2s->cs) |
| 3018 | return h2s->cs; |
| 3019 | node = eb32_next(node); |
| 3020 | } |
| 3021 | return NULL; |
| 3022 | } |
| 3023 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3024 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3025 | * Destroy the mux and the associated connection, if it is no longer used |
| 3026 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3027 | static void h2_destroy(void *ctx) |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3028 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3029 | struct h2c *h2c = ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3030 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 3031 | 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] | 3032 | h2_release(h2c); |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3033 | } |
| 3034 | |
| 3035 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3036 | * Detach the stream from the connection and possibly release the connection. |
| 3037 | */ |
| 3038 | static void h2_detach(struct conn_stream *cs) |
| 3039 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3040 | struct h2s *h2s = cs->ctx; |
| 3041 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3042 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3043 | |
| 3044 | cs->ctx = NULL; |
| 3045 | if (!h2s) |
| 3046 | return; |
| 3047 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3048 | /* The stream is about to die, so no need to attempt to run its task */ |
| 3049 | if (!LIST_ISEMPTY(&h2s->sending_list) && |
| 3050 | h2s->send_wait != &h2s->wait_event) { |
| 3051 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
| 3052 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d9986ed | 2019-05-09 13:24:08 +0200 | [diff] [blame] | 3053 | /* |
| 3054 | * At this point, the stream_interface is supposed to have called |
| 3055 | * h2_unsubscribe(), so the only way there's still a |
| 3056 | * subscription that came from the stream_interface (as we |
| 3057 | * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(), |
| 3058 | * without the stream_interface involved) is that we subscribed |
| 3059 | * for sending, we woke the tasklet up and removed the |
| 3060 | * SUB_RETRY_SEND flag, so the stream_interface would not |
| 3061 | * know it has to unsubscribe for send, but the tasklet hasn't |
| 3062 | * run yet. Make sure to handle that by explicitely setting |
| 3063 | * send_wait to NULL, as nothing else will do it for us. |
| 3064 | */ |
| 3065 | h2s->send_wait = NULL; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3066 | } |
| 3067 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3068 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3069 | h2c = h2s->h2c; |
| 3070 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 3071 | h2c->nb_cs--; |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 3072 | if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY && |
| 3073 | !h2_frt_has_too_many_cs(h2c)) { |
| 3074 | /* frontend connection was blocking new streams creation */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3075 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3076 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3077 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3078 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3079 | /* this stream may be blocked waiting for some data to leave (possibly |
| 3080 | * an ES or RST frame), so orphan it in this case. |
| 3081 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 3082 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 3083 | (h2c->st0 < H2_CS_ERROR) && |
Olivier Houchard | 16ff261 | 2019-03-21 15:48:46 +0100 | [diff] [blame] | 3084 | (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] | 3085 | return; |
| 3086 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3087 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 3088 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 3089 | /* unblock the connection if it was blocked on this |
| 3090 | * stream. |
| 3091 | */ |
| 3092 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 3093 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3094 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3095 | } |
| 3096 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 3097 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3098 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3099 | if (h2c->flags & H2_CF_IS_BACK && |
| 3100 | (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3101 | if (!(h2c->conn->flags & |
| 3102 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 3103 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3104 | h2c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3105 | if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) { |
| 3106 | h2c->conn->owner = NULL; |
| 3107 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3108 | if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn)) |
| 3109 | /* The server doesn't want it, let's kill the connection right away */ |
| 3110 | h2c->conn->mux->destroy(h2c->conn); |
| 3111 | return; |
| 3112 | } |
| 3113 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3114 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3115 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3116 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 3117 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 3118 | return; |
| 3119 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3120 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3121 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3122 | h2c->conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 3123 | if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3124 | struct server *srv = objt_server(h2c->conn->target); |
| 3125 | |
| 3126 | if (srv) { |
| 3127 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 3128 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 3129 | else |
| 3130 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 3131 | } |
| 3132 | |
| 3133 | } |
| 3134 | } |
| 3135 | } |
| 3136 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3137 | /* We don't want to close right now unless we're removing the |
| 3138 | * last stream, and either the connection is in error, or it |
| 3139 | * reached the ID already specified in a GOAWAY frame received |
| 3140 | * or sent (as seen by last_sid >= 0). |
| 3141 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3142 | if (h2c_is_dead(h2c)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3143 | /* no more stream will come, kill it now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3144 | h2_release(h2c); |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3145 | } |
| 3146 | else if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3147 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3148 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3149 | task_queue(h2c->task); |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 3150 | } |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3151 | else |
| 3152 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3153 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3154 | } |
| 3155 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3156 | /* Performs a synchronous or asynchronous shutr(). |
| 3157 | * FIXME: guess what the return code tries to indicate! |
| 3158 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3159 | static int h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3160 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3161 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3162 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3163 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 3164 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3165 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3166 | |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3167 | /* a connstream may require us to immediately kill the whole connection |
| 3168 | * for example because of a "tcp-request content reject" rule that is |
| 3169 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3170 | * close the connection. |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3171 | */ |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3172 | if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) && |
| 3173 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3174 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3175 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3176 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3177 | else if (!(h2s->flags & H2_SF_HEADERS_SENT)) { |
| 3178 | /* Nothing was never sent for this stream, so reset with |
| 3179 | * REFUSED_STREAM error to let the client retry the |
| 3180 | * request. |
| 3181 | */ |
| 3182 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3183 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3184 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3185 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3186 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3187 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3188 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3189 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3190 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3191 | h2s_close(h2s); |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3192 | |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3193 | return 0; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3194 | add_to_list: |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3195 | if (LIST_ISEMPTY(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3196 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3197 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3198 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3199 | h2s->send_wait = sw; |
| 3200 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3201 | h2s->send_wait = sw; |
| 3202 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3203 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3204 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3205 | /* Let the handler know we want shutr */ |
| 3206 | sw->handle = (void *)((long)sw->handle | 1); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3207 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3208 | } |
| 3209 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3210 | /* Performs a synchronous or asynchronous shutw(). |
| 3211 | * FIXME: guess what the return code tries to indicate! |
| 3212 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3213 | static int h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3214 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3215 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3216 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3217 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 3218 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3219 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3220 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3221 | if (h2s->flags & H2_SF_HEADERS_SENT) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3222 | /* we can cleanly close using an empty data frame only after headers */ |
| 3223 | |
| 3224 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 3225 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3226 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3227 | |
| 3228 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3229 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3230 | else |
| 3231 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3232 | } else { |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3233 | /* a connstream may require us to immediately kill the whole connection |
| 3234 | * for example because of a "tcp-request content reject" rule that is |
| 3235 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3236 | * close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3237 | */ |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3238 | if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) && |
| 3239 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3240 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3241 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3242 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3243 | else { |
| 3244 | /* Nothing was never sent for this stream, so reset with |
| 3245 | * REFUSED_STREAM error to let the client retry the |
| 3246 | * request. |
| 3247 | */ |
| 3248 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3249 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3250 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3251 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3252 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3253 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3254 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3255 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3256 | } |
| 3257 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3258 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3259 | tasklet_wakeup(h2c->wait_event.task); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3260 | return 0; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3261 | |
| 3262 | add_to_list: |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3263 | if (LIST_ISEMPTY(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3264 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3265 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3266 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3267 | h2s->send_wait = sw; |
| 3268 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3269 | h2s->send_wait = sw; |
| 3270 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3271 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3272 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3273 | /* let the handler know we want to shutw */ |
| 3274 | sw->handle = (void *)((long)(sw->handle) | 2); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3275 | return 1; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3276 | } |
| 3277 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3278 | /* This is the tasklet referenced in h2s->wait_event.task, it is used for |
| 3279 | * deferred shutdowns when the h2_detach() was done but the mux buffer was full |
| 3280 | * and prevented the last frame from being emitted. |
| 3281 | */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3282 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3283 | { |
| 3284 | struct h2s *h2s = ctx; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3285 | long reason = (long)h2s->wait_event.handle; |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3286 | int ret = 0; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3287 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3288 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3289 | if (reason & 2) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3290 | ret |= h2_do_shutw(h2s); |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3291 | if (reason & 1) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3292 | ret |= h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3293 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3294 | if (!ret) { |
| 3295 | /* We're done trying to send, remove ourself from the send_list */ |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3296 | LIST_DEL_INIT(&h2s->list); |
| 3297 | } |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3298 | /* We're no longer trying to send anything, let's destroy the h2s */ |
Olivier Houchard | 01d4cb5 | 2019-03-25 13:25:02 +0100 | [diff] [blame] | 3299 | if (!ret && (h2s->cs == NULL)) { |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3300 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3301 | h2s_destroy(h2s); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3302 | |
| 3303 | if (h2c_is_dead(h2c)) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3304 | h2_release(h2c); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3305 | } |
| 3306 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3307 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3308 | } |
| 3309 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3310 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3311 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3312 | { |
| 3313 | struct h2s *h2s = cs->ctx; |
| 3314 | |
| 3315 | if (!mode) |
| 3316 | return; |
| 3317 | |
| 3318 | h2_do_shutr(h2s); |
| 3319 | } |
| 3320 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3321 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3322 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3323 | { |
| 3324 | struct h2s *h2s = cs->ctx; |
| 3325 | |
| 3326 | h2_do_shutw(h2s); |
| 3327 | } |
| 3328 | |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3329 | /* 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] | 3330 | * HTX request or response depending on the connection's side. Returns a |
| 3331 | * positive value on success, a negative value on failure, or 0 if it couldn't |
| 3332 | * proceed. May report connection errors in h2c->errcode if the frame is |
| 3333 | * non-decodable and the connection unrecoverable. In absence of connection |
| 3334 | * 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] | 3335 | * |
| 3336 | * The function may fold CONTINUATION frames into the initial HEADERS frame |
| 3337 | * by removing padding and next frame header, then moving the CONTINUATION |
| 3338 | * frame's payload and adjusting h2c->dfl to match the new aggregated frame, |
| 3339 | * leaving a hole between the main frame and the beginning of the next one. |
| 3340 | * The possibly remaining incomplete or next frame at the end may be moved |
| 3341 | * if the aggregated frame is not deleted, in order to fill the hole. Wrapped |
| 3342 | * HEADERS frames are unwrapped into a temporary buffer before decoding. |
| 3343 | * |
| 3344 | * A buffer at the beginning of processing may look like this : |
| 3345 | * |
| 3346 | * ,---.---------.-----.--------------.--------------.------.---. |
| 3347 | * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///| |
| 3348 | * `---^---------^-----^--------------^--------------^------^---' |
| 3349 | * | | <-----> | | |
| 3350 | * area | dpl | wrap |
| 3351 | * |<--------------> | |
| 3352 | * | dfl | |
| 3353 | * |<-------------------------------------------------->| |
| 3354 | * head data |
| 3355 | * |
| 3356 | * Padding is automatically overwritten when folding, participating to the |
| 3357 | * hole size after dfl : |
| 3358 | * |
| 3359 | * ,---.------------------------.-----.--------------.------.---. |
| 3360 | * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///| |
| 3361 | * `---^------------------------^-----^--------------^------^---' |
| 3362 | * | | <-----> | | |
| 3363 | * area | hole | wrap |
| 3364 | * |<-----------------------> | |
| 3365 | * | dfl | |
| 3366 | * |<-------------------------------------------------->| |
| 3367 | * head data |
| 3368 | * |
| 3369 | * Please note that the HEADERS frame is always deprived from its PADLEN byte |
| 3370 | * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY |
| 3371 | * bit. |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3372 | * |
| 3373 | * The <flags> field must point to either the stream's flags or to a copy of it |
| 3374 | * so that the function can update the following flags : |
| 3375 | * - H2_SF_DATA_CLEN when content-length is seen |
| 3376 | * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion |
| 3377 | * - H2_SF_HEADERS_RCVD once the frame is successfully decoded |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3378 | * |
| 3379 | * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to |
| 3380 | * decoding, in order to detect if we're dealing with a headers or a trailers |
| 3381 | * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3382 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3383 | 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] | 3384 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3385 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3386 | struct buffer *tmp = get_trash_chunk(); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3387 | struct http_hdr list[MAX_HTTP_HDR * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3388 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3389 | unsigned int msgf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3390 | struct htx *htx = NULL; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3391 | int flen; // header frame len |
| 3392 | int hole = 0; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3393 | int ret = 0; |
| 3394 | int outlen; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3395 | int wrap; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3396 | int try = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3397 | |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3398 | next_frame: |
| 3399 | if (b_data(&h2c->dbuf) - hole < h2c->dfl) |
| 3400 | goto leave; // incomplete input frame |
| 3401 | |
| 3402 | /* No END_HEADERS means there's one or more CONTINUATION frames. In |
| 3403 | * this case, we'll try to paste it immediately after the initial |
| 3404 | * HEADERS frame payload and kill any possible padding. The initial |
| 3405 | * frame's length will be increased to represent the concatenation |
| 3406 | * of the two frames. The next frame is read from position <tlen> |
| 3407 | * and written at position <flen> (minus padding if some is present). |
| 3408 | */ |
| 3409 | if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) { |
| 3410 | struct h2_fh hdr; |
| 3411 | int clen; // CONTINUATION frame's payload length |
| 3412 | |
| 3413 | if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) { |
| 3414 | /* no more data, the buffer may be full, either due to |
| 3415 | * too large a frame or because of too large a hole that |
| 3416 | * we're going to compact at the end. |
| 3417 | */ |
| 3418 | goto leave; |
| 3419 | } |
| 3420 | |
| 3421 | if (hdr.ft != H2_FT_CONTINUATION) { |
| 3422 | /* RFC7540#6.10: frame of unexpected type */ |
| 3423 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3424 | goto fail; |
| 3425 | } |
| 3426 | |
| 3427 | if (hdr.sid != h2c->dsi) { |
| 3428 | /* RFC7540#6.10: frame of different stream */ |
| 3429 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3430 | goto fail; |
| 3431 | } |
| 3432 | |
| 3433 | if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { |
| 3434 | /* RFC7540#4.2: invalid frame length */ |
| 3435 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3436 | goto fail; |
| 3437 | } |
| 3438 | |
| 3439 | /* detect when we must stop aggragating frames */ |
| 3440 | h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS; |
| 3441 | |
| 3442 | /* Take as much as we can of the CONTINUATION frame's payload */ |
| 3443 | clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9); |
| 3444 | if (clen > hdr.len) |
| 3445 | clen = hdr.len; |
| 3446 | |
| 3447 | /* Move the frame's payload over the padding, hole and frame |
| 3448 | * header. At least one of hole or dpl is null (see diagrams |
| 3449 | * above). The hole moves after the new aggragated frame. |
| 3450 | */ |
| 3451 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9)); |
| 3452 | h2c->dfl += clen - h2c->dpl; |
| 3453 | hole += h2c->dpl + 9; |
| 3454 | h2c->dpl = 0; |
| 3455 | goto next_frame; |
| 3456 | } |
| 3457 | |
| 3458 | flen = h2c->dfl - h2c->dpl; |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3459 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3460 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3461 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3462 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3463 | copy = alloc_trash_chunk(); |
| 3464 | if (!copy) { |
| 3465 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3466 | goto fail; |
| 3467 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3468 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3469 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3470 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3471 | } |
| 3472 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3473 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3474 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3475 | if (read_n32(hdrs) == h2c->dsi) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3476 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3477 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3478 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3479 | } |
| 3480 | |
Willy Tarreau | a01f45e | 2018-12-31 07:41:24 +0100 | [diff] [blame] | 3481 | if (flen < 5) { |
| 3482 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3483 | goto fail; |
| 3484 | } |
| 3485 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3486 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3487 | flen -= 5; |
| 3488 | } |
| 3489 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3490 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3491 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3492 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3493 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3494 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3495 | /* we can't retry a failed decompression operation so we must be very |
| 3496 | * careful not to take any risks. In practice the output buffer is |
| 3497 | * always empty except maybe for trailers, in which case we simply have |
| 3498 | * to wait for the upper layer to finish consuming what is available. |
| 3499 | */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3500 | |
| 3501 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3502 | htx = htx_from_buf(rxbuf); |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3503 | if (!htx_is_empty(htx)) { |
| 3504 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3505 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3506 | } |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3507 | } else { |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3508 | if (b_data(rxbuf)) { |
| 3509 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3510 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3511 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3512 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3513 | rxbuf->head = 0; |
| 3514 | try = b_size(rxbuf); |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3515 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3516 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3517 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3518 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3519 | sizeof(list)/sizeof(list[0]), tmp); |
| 3520 | if (outlen < 0) { |
| 3521 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3522 | goto fail; |
| 3523 | } |
| 3524 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3525 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3526 | * the parser's state to commit these changes and allow us to later |
| 3527 | * fail solely on the stream if needed. |
| 3528 | */ |
| 3529 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3530 | h2c->dfl = hole = 0; |
| 3531 | h2c->st0 = H2_CS_FRAME_H; |
| 3532 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3533 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3534 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3535 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3536 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3537 | goto trailers; |
| 3538 | |
| 3539 | /* This is the first HEADERS frame so it's a headers block */ |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3540 | if (htx) { |
| 3541 | /* HTX mode */ |
| 3542 | if (h2c->flags & H2_CF_IS_BACK) |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3543 | outlen = h2_make_htx_response(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3544 | else |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3545 | outlen = h2_make_htx_request(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3546 | } else { |
| 3547 | /* HTTP/1 mode */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3548 | 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] | 3549 | if (outlen > 0) |
| 3550 | b_add(rxbuf, outlen); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3551 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3552 | |
| 3553 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3554 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3555 | goto fail; |
| 3556 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3557 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3558 | if (msgf & H2_MSGF_BODY) { |
| 3559 | /* a payload is present */ |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3560 | if (msgf & H2_MSGF_BODY_CL) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3561 | *flags |= H2_SF_DATA_CLEN; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3562 | if (htx) |
| 3563 | htx->extra = *body_len; |
| 3564 | } |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3565 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3566 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3567 | } |
| 3568 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3569 | done: |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3570 | /* indicate that a HEADERS frame was received for this stream, except |
| 3571 | * for 1xx responses. For 1xx responses, another HEADERS frame is |
| 3572 | * expected. |
| 3573 | */ |
| 3574 | if (!(msgf & H2_MSGF_RSP_1XX)) |
| 3575 | *flags |= H2_SF_HEADERS_RCVD; |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3576 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3577 | 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] | 3578 | /* Mark the end of message, either using EOM in HTX or with the |
| 3579 | * trailing CRLF after the end of trailers. Note that DATA_CHNK |
Willy Tarreau | 2135f91 | 2019-05-07 11:17:32 +0200 | [diff] [blame] | 3580 | * is not set during headers with END_STREAM. For HTX trailers, |
| 3581 | * we must not leave an HTX trailers block not followed by an |
| 3582 | * EOM block, the two must be atomic. Thus if we fail to emit |
| 3583 | * 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] | 3584 | */ |
| 3585 | if (htx) { |
Willy Tarreau | 2135f91 | 2019-05-07 11:17:32 +0200 | [diff] [blame] | 3586 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3587 | struct htx_blk *tail = htx_get_tail_blk(htx); |
| 3588 | |
| 3589 | if (tail && htx_get_blk_type(tail) == HTX_BLK_TLR) |
| 3590 | htx_remove_blk(htx, tail); |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3591 | goto fail; |
Willy Tarreau | 2135f91 | 2019-05-07 11:17:32 +0200 | [diff] [blame] | 3592 | } |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3593 | } |
| 3594 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3595 | if (!b_putblk(rxbuf, "\r\n", 2)) |
| 3596 | goto fail; |
| 3597 | } |
| 3598 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3599 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3600 | /* success */ |
| 3601 | ret = 1; |
| 3602 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3603 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3604 | /* 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] | 3605 | * move the remaining data over it. |
| 3606 | */ |
| 3607 | if (hole) { |
| 3608 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3609 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3610 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3611 | b_sub(&h2c->dbuf, hole); |
| 3612 | } |
| 3613 | |
Willy Tarreau | 97215ca | 2019-04-29 10:20:21 +0200 | [diff] [blame] | 3614 | if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) { |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3615 | /* too large frames */ |
| 3616 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3617 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3618 | } |
| 3619 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3620 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3621 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3622 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3623 | return ret; |
| 3624 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3625 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3626 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3627 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3628 | |
| 3629 | trailers: |
| 3630 | /* This is the last HEADERS frame hence a trailer */ |
| 3631 | |
| 3632 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3633 | /* It's a trailer but it's missing ES flag */ |
| 3634 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3635 | goto fail; |
| 3636 | } |
| 3637 | |
| 3638 | /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD |
| 3639 | * block, and when using chunks we must send the 0 CRLF marker. For |
| 3640 | * other modes, the trailers are silently dropped. |
| 3641 | */ |
| 3642 | if (htx) { |
| 3643 | if (!htx_add_endof(htx, HTX_BLK_EOD)) |
| 3644 | goto fail; |
Willy Tarreau | 5255f28 | 2019-01-03 18:41:05 +0100 | [diff] [blame] | 3645 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3646 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3647 | } |
| 3648 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3649 | /* Legacy mode with chunked encoding : we must finalize the |
| 3650 | * data block message emit the trailing CRLF */ |
| 3651 | if (!b_putblk(rxbuf, "0\r\n", 3)) |
| 3652 | goto fail; |
Willy Tarreau | e2b05cc | 2019-01-03 16:18:34 +0100 | [diff] [blame] | 3653 | |
| 3654 | outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try); |
| 3655 | if (outlen > 0) |
| 3656 | b_add(rxbuf, outlen); |
| 3657 | else |
| 3658 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3659 | } |
| 3660 | |
| 3661 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3662 | } |
| 3663 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3664 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 3665 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 3666 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 3667 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 3668 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 3669 | * 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] | 3670 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3671 | * send the current frame, 0 if it couldn't complete, in which case |
| 3672 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3673 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3674 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3675 | * have checked the frame header and ensured that the frame was complete or the |
| 3676 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3677 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3678 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3679 | { |
| 3680 | struct h2c *h2c = h2s->h2c; |
| 3681 | int block1, block2; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3682 | unsigned int flen = 0; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3683 | unsigned int chklen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3684 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3685 | struct buffer *csbuf; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3686 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3687 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3688 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3689 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3690 | if (!csbuf) { |
| 3691 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3692 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3693 | } |
| 3694 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3695 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3696 | flen = h2c->dfl - h2c->dpl; |
Olivier Houchard | 2f30883 | 2018-12-19 15:53:53 +0100 | [diff] [blame] | 3697 | if (h2c->proxy->options2 & PR_O2_USE_HTX) |
| 3698 | htx = htx_from_buf(csbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3699 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3700 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3701 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3702 | if (flen > b_data(&h2c->dbuf)) { |
| 3703 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3704 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3705 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3706 | } |
| 3707 | |
Willy Tarreau | a9b7796 | 2019-01-31 07:23:00 +0100 | [diff] [blame] | 3708 | if (htx) { |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3709 | block1 = htx_free_data_space(htx); |
| 3710 | if (!block1) { |
| 3711 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3712 | goto fail; |
| 3713 | } |
| 3714 | if (flen > block1) |
| 3715 | flen = block1; |
| 3716 | |
| 3717 | /* here, flen is the max we can copy into the output buffer */ |
| 3718 | block1 = b_contig_data(&h2c->dbuf, 0); |
| 3719 | if (flen > block1) |
| 3720 | flen = block1; |
| 3721 | |
| 3722 | if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) { |
| 3723 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3724 | goto fail; |
| 3725 | } |
| 3726 | |
| 3727 | b_del(&h2c->dbuf, flen); |
| 3728 | h2c->dfl -= flen; |
| 3729 | h2c->rcvd_c += flen; |
| 3730 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3731 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3732 | if (h2s->flags & H2_SF_DATA_CLEN) { |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3733 | h2s->body_len -= flen; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3734 | htx->extra = h2s->body_len; |
| 3735 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3736 | goto try_again; |
| 3737 | } |
| 3738 | else if (unlikely(b_space_wraps(csbuf))) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3739 | /* it doesn't fit and the buffer is fragmented, |
| 3740 | * so let's defragment it and try again. |
| 3741 | */ |
| 3742 | b_slow_realign(csbuf, trash.area, 0); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3743 | } |
| 3744 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3745 | /* chunked-encoding requires more room */ |
| 3746 | if (h2s->flags & H2_SF_DATA_CHNK) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3747 | chklen = MIN(flen, b_room(csbuf)); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3748 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 3749 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 3750 | (chklen < 1048576) ? 4 : 8; |
| 3751 | chklen += 4; // CRLF, CRLF |
| 3752 | } |
| 3753 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3754 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3755 | if (flen + chklen > b_room(csbuf)) { |
| 3756 | if (chklen >= b_room(csbuf)) { |
| 3757 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3758 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3759 | } |
| 3760 | flen = b_room(csbuf) - chklen; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3761 | } |
| 3762 | |
| 3763 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3764 | /* emit the chunk size */ |
| 3765 | unsigned int chksz = flen; |
| 3766 | char str[10]; |
| 3767 | char *beg; |
| 3768 | |
| 3769 | beg = str + sizeof(str); |
| 3770 | *--beg = '\n'; |
| 3771 | *--beg = '\r'; |
| 3772 | do { |
| 3773 | *--beg = hextab[chksz & 0xF]; |
| 3774 | } while (chksz >>= 4); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3775 | b_putblk(csbuf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3776 | } |
| 3777 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3778 | /* Block1 is the length of the first block before the buffer wraps, |
| 3779 | * block2 is the optional second block to reach the end of the frame. |
| 3780 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3781 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3782 | if (block1 > flen) |
| 3783 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3784 | block2 = flen - block1; |
| 3785 | |
| 3786 | if (block1) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3787 | b_putblk(csbuf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3788 | |
| 3789 | if (block2) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3790 | b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3791 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3792 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3793 | /* emit the CRLF */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3794 | b_putblk(csbuf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3795 | } |
| 3796 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3797 | /* now mark the input data as consumed (will be deleted from the buffer |
| 3798 | * by the caller when seeing FRAME_A after sending the window update). |
| 3799 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3800 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3801 | h2c->dfl -= flen; |
| 3802 | h2c->rcvd_c += flen; |
| 3803 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 3804 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3805 | if (h2s->flags & H2_SF_DATA_CLEN) |
| 3806 | h2s->body_len -= flen; |
| 3807 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3808 | if (h2c->dfl > h2c->dpl) { |
| 3809 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3810 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3811 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3812 | } |
| 3813 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3814 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3815 | /* here we're done with the frame, all the payload (except padding) was |
| 3816 | * transferred. |
| 3817 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3818 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3819 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 3820 | if (htx) { |
| 3821 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3822 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3823 | goto fail; |
| 3824 | } |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3825 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3826 | else if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3827 | /* emit the trailing 0 CRLF CRLF */ |
| 3828 | if (b_room(csbuf) < 5) { |
| 3829 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3830 | goto fail; |
| 3831 | } |
| 3832 | chklen += 5; |
| 3833 | b_putblk(csbuf, "0\r\n\r\n", 5); |
| 3834 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3835 | } |
| 3836 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 3837 | h2c->rcvd_c += h2c->dpl; |
| 3838 | h2c->rcvd_s += h2c->dpl; |
| 3839 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3840 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3841 | if (htx) |
| 3842 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3843 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3844 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3845 | if (htx) |
| 3846 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3847 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3848 | } |
| 3849 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3850 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 3851 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 3852 | * number of bytes sent. The caller must check the stream's status to detect |
| 3853 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3854 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3855 | 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] | 3856 | { |
| 3857 | struct http_hdr list[MAX_HTTP_HDR]; |
| 3858 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3859 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3860 | struct buffer outbuf; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3861 | union h1_sl sl; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3862 | int es_now = 0; |
| 3863 | int ret = 0; |
| 3864 | int hdr; |
| 3865 | |
| 3866 | if (h2c_mux_busy(h2c, h2s)) { |
| 3867 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3868 | return 0; |
| 3869 | } |
| 3870 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3871 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3872 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3873 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3874 | return 0; |
| 3875 | } |
| 3876 | |
| 3877 | /* First, try to parse the H1 response and index it into <list>. |
| 3878 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 3879 | * block does not wrap and we can safely read it this way without |
| 3880 | * having to realign the buffer. |
| 3881 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3882 | 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] | 3883 | list, sizeof(list)/sizeof(list[0]), h1m, &sl); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3884 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 3885 | /* incomplete or invalid response, this is abnormal coming from |
| 3886 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 3887 | * so that won't be fixed, raise an error now. |
| 3888 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3889 | * FIXME: we should instead add the ability to only return a |
| 3890 | * 502 bad gateway. But in theory this is not supposed to |
| 3891 | * happen. |
| 3892 | */ |
| 3893 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3894 | ret = 0; |
| 3895 | goto end; |
| 3896 | } |
| 3897 | |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3898 | h2s->status = sl.st.status; |
Willy Tarreau | db72da0 | 2018-09-13 11:52:20 +0200 | [diff] [blame] | 3899 | |
| 3900 | /* certain statuses have no body or an empty one, regardless of |
| 3901 | * what the headers say. |
| 3902 | */ |
| 3903 | if (sl.st.status >= 100 && sl.st.status < 200) { |
| 3904 | h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK); |
| 3905 | h1m->curr_len = h1m->body_len = 0; |
| 3906 | } |
| 3907 | else if (sl.st.status == 204 || sl.st.status == 304) { |
| 3908 | /* no contents, claim c-len is present and set to zero */ |
| 3909 | h1m->flags &= ~H1_MF_CHNK; |
| 3910 | h1m->flags |= H1_MF_CLEN; |
| 3911 | h1m->curr_len = h1m->body_len = 0; |
| 3912 | } |
| 3913 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3914 | chunk_reset(&outbuf); |
| 3915 | |
| 3916 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3917 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3918 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3919 | outbuf.data = 0; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3920 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3921 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3922 | break; |
| 3923 | realign_again: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3924 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3925 | } |
| 3926 | |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3927 | if (outbuf.size < 9) |
| 3928 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3929 | |
| 3930 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3931 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 3932 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3933 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3934 | |
| 3935 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3936 | if (unlikely(list[0].v.len != 3)) { |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 3937 | /* this is an unparsable response */ |
| 3938 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3939 | ret = 0; |
| 3940 | goto end; |
| 3941 | } |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3942 | |
| 3943 | if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3944 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3945 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3946 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3947 | } |
| 3948 | |
| 3949 | /* encode all headers, stop at empty name */ |
| 3950 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 3951 | /* these ones do not exist in H2 and must be dropped. */ |
| 3952 | if (isteq(list[hdr].n, ist("connection")) || |
| 3953 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 3954 | isteq(list[hdr].n, ist("keep-alive")) || |
| 3955 | isteq(list[hdr].n, ist("upgrade")) || |
| 3956 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3957 | continue; |
| 3958 | |
| 3959 | if (isteq(list[hdr].n, ist(""))) |
| 3960 | break; // end |
| 3961 | |
| 3962 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 3963 | /* output full */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3964 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3965 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3966 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3967 | } |
| 3968 | } |
| 3969 | |
| 3970 | /* we may need to add END_STREAM */ |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 3971 | 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] | 3972 | es_now = 1; |
| 3973 | |
| 3974 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3975 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3976 | |
| 3977 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3978 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3979 | |
| 3980 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3981 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3982 | |
| 3983 | /* commit the H2 response */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3984 | b_add(&h2c->mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3985 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3986 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3987 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3988 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3989 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3990 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3991 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3992 | h2s->flags |= H2_SF_ES_SENT; |
| 3993 | if (h2s->st == H2_SS_OPEN) |
| 3994 | h2s->st = H2_SS_HLOC; |
| 3995 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3996 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3997 | } |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3998 | else if (h2s->status >= 100 && h2s->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3999 | /* 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] | 4000 | h1m_init_res(h1m); |
Willy Tarreau | 9b8cd1f | 2018-09-12 09:24:38 +0200 | [diff] [blame] | 4001 | 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] | 4002 | h2s->h1m.flags |= H1_MF_TOLOWER; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 4003 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 4004 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 4005 | |
| 4006 | /* 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] | 4007 | |
| 4008 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4009 | //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] | 4010 | return ret; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 4011 | full: |
| 4012 | h1m_init_res(h1m); |
| 4013 | h1m->err_pos = -1; // don't care about errors on the response path |
| 4014 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4015 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4016 | ret = 0; |
| 4017 | goto end; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4018 | } |
| 4019 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4020 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 4021 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 4022 | * the number of bytes sent. The caller must check the stream's status to |
| 4023 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4024 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4025 | 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] | 4026 | { |
| 4027 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4028 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4029 | struct buffer outbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4030 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 4031 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4032 | int es_now = 0; |
| 4033 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4034 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4035 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4036 | |
| 4037 | if (h2c_mux_busy(h2c, h2s)) { |
| 4038 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4039 | goto end; |
| 4040 | } |
| 4041 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 4042 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4043 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4044 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4045 | goto end; |
| 4046 | } |
| 4047 | |
| 4048 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4049 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4050 | goto end; |
| 4051 | |
| 4052 | chunk_reset(&outbuf); |
| 4053 | |
| 4054 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4055 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4056 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4057 | outbuf.data = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4058 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4059 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4060 | break; |
| 4061 | realign_again: |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4062 | /* If there are pending data in the output buffer, and we have |
| 4063 | * less than 1/4 of the mbuf's size and everything fits, we'll |
| 4064 | * still perform a copy anyway. Otherwise we'll pretend the mbuf |
| 4065 | * is full and wait, to save some slow realign calls. |
| 4066 | */ |
| 4067 | if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) { |
| 4068 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4069 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4070 | goto end; |
| 4071 | } |
| 4072 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4073 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4074 | } |
| 4075 | |
| 4076 | if (outbuf.size < 9) { |
| 4077 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4078 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4079 | goto end; |
| 4080 | } |
| 4081 | |
| 4082 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4083 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4084 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4085 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4086 | |
| 4087 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 4088 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4089 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 4090 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4091 | break; |
| 4092 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4093 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4094 | if ((long long)size > h1m->curr_len) |
| 4095 | size = h1m->curr_len; |
| 4096 | break; |
| 4097 | default: /* te:chunked : parse chunks */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4098 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 4099 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4100 | if (!ret) |
| 4101 | goto end; |
| 4102 | |
| 4103 | if (ret < 0) { |
| 4104 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4105 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4106 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4107 | goto end; |
| 4108 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4109 | max -= ret; |
| 4110 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4111 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4112 | h1m->state = H1_MSG_CHUNK_SIZE; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4113 | } |
| 4114 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4115 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4116 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 4117 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4118 | if (!ret) |
| 4119 | goto end; |
| 4120 | |
| 4121 | if (ret < 0) { |
| 4122 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4123 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4124 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4125 | goto end; |
| 4126 | } |
| 4127 | |
| 4128 | size = chunk; |
| 4129 | h1m->curr_len = chunk; |
| 4130 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4131 | max -= ret; |
| 4132 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4133 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4134 | h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4135 | if (!size) |
| 4136 | goto send_empty; |
| 4137 | } |
| 4138 | |
| 4139 | /* in MSG_DATA state, continue below */ |
| 4140 | size = h1m->curr_len; |
| 4141 | break; |
| 4142 | } |
| 4143 | |
| 4144 | /* we have in <size> the exact number of bytes we need to copy from |
| 4145 | * the H1 buffer. We need to check this against the connection's and |
| 4146 | * the stream's send windows, and to ensure that this fits in the max |
| 4147 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4148 | * the frame header). The connection's flow control is applied last so |
| 4149 | * that we can use a separate list of streams which are immediately |
| 4150 | * unblocked on window opening. Note: we don't implement padding. |
| 4151 | */ |
| 4152 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4153 | if (size > max) |
| 4154 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4155 | |
| 4156 | if (size > h2s->mws) |
| 4157 | size = h2s->mws; |
| 4158 | |
| 4159 | if (size <= 0) { |
| 4160 | h2s->flags |= H2_SF_BLK_SFCTL; |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 4161 | if (!LIST_ISEMPTY(&h2s->list)) |
| 4162 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4163 | goto end; |
| 4164 | } |
| 4165 | |
| 4166 | if (h2c->mfs && size > h2c->mfs) |
| 4167 | size = h2c->mfs; |
| 4168 | |
| 4169 | if (size + 9 > outbuf.size) { |
| 4170 | /* we have an opportunity for enlarging the too small |
| 4171 | * available space, let's try. |
| 4172 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4173 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4174 | goto realign_again; |
| 4175 | size = outbuf.size - 9; |
| 4176 | } |
| 4177 | |
| 4178 | if (size <= 0) { |
| 4179 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4180 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4181 | goto end; |
| 4182 | } |
| 4183 | |
| 4184 | if (size > h2c->mws) |
| 4185 | size = h2c->mws; |
| 4186 | |
| 4187 | if (size <= 0) { |
| 4188 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4189 | goto end; |
| 4190 | } |
| 4191 | |
| 4192 | /* copy whatever we can */ |
| 4193 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4194 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4195 | if (ret == 1) |
| 4196 | len2 = 0; |
| 4197 | |
| 4198 | if (!ret || len1 + len2 < size) { |
| 4199 | /* FIXME: must normally never happen */ |
| 4200 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4201 | goto end; |
| 4202 | } |
| 4203 | |
| 4204 | /* limit len1/len2 to size */ |
| 4205 | if (len1 + len2 > size) { |
| 4206 | int sub = len1 + len2 - size; |
| 4207 | |
| 4208 | if (len2 > sub) |
| 4209 | len2 -= sub; |
| 4210 | else { |
| 4211 | sub -= len2; |
| 4212 | len2 = 0; |
| 4213 | len1 -= sub; |
| 4214 | } |
| 4215 | } |
| 4216 | |
| 4217 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4218 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4219 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4220 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4221 | |
| 4222 | send_empty: |
| 4223 | /* we may need to add END_STREAM */ |
| 4224 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 4225 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4226 | * |
| 4227 | * FIXME: what we do here is not correct because we send end_stream |
| 4228 | * before knowing if we'll have to send a HEADERS frame for the |
| 4229 | * trailers. More importantly we're not consuming the trailing CRLF |
| 4230 | * after the end of trailers, so it will be left to the caller to |
| 4231 | * eat it. The right way to do it would be to measure trailers here |
| 4232 | * and to send ES only if there are no trailers. |
| 4233 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4234 | */ |
| 4235 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4236 | !h1m->curr_len || h1m->state >= H1_MSG_DONE) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4237 | es_now = 1; |
| 4238 | |
| 4239 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4240 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4241 | |
| 4242 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4243 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4244 | |
| 4245 | /* commit the H2 response */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4246 | b_add(&h2c->mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4247 | |
| 4248 | /* consume incoming H1 response */ |
| 4249 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4250 | max -= size; |
| 4251 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4252 | total += size; |
| 4253 | h1m->curr_len -= size; |
| 4254 | h2s->mws -= size; |
| 4255 | h2c->mws -= size; |
| 4256 | |
| 4257 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4258 | h1m->state = H1_MSG_CHUNK_CRLF; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4259 | goto new_frame; |
| 4260 | } |
| 4261 | } |
| 4262 | |
| 4263 | if (es_now) { |
| 4264 | if (h2s->st == H2_SS_OPEN) |
| 4265 | h2s->st = H2_SS_HLOC; |
| 4266 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4267 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4268 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4269 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 4270 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4271 | total += max; |
| 4272 | ofs += max; |
| 4273 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4274 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4275 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4276 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4277 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4278 | h2s->flags |= H2_SF_ES_SENT; |
| 4279 | } |
| 4280 | |
| 4281 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4282 | 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] | 4283 | return total; |
| 4284 | } |
| 4285 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4286 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 4287 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4288 | * must check the stream's status to detect any error which might have happened |
| 4289 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4290 | * from the message. The htx message is assumed to be valid since produced from |
| 4291 | * the internal code, hence it contains a start line, an optional series of |
| 4292 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4293 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4294 | */ |
| 4295 | static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
| 4296 | { |
| 4297 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4298 | struct h2c *h2c = h2s->h2c; |
| 4299 | struct htx_blk *blk; |
| 4300 | struct htx_blk *blk_end; |
| 4301 | struct buffer outbuf; |
| 4302 | struct htx_sl *sl; |
| 4303 | enum htx_blk_type type; |
| 4304 | int es_now = 0; |
| 4305 | int ret = 0; |
| 4306 | int hdr; |
| 4307 | int idx; |
| 4308 | |
| 4309 | if (h2c_mux_busy(h2c, h2s)) { |
| 4310 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4311 | return 0; |
| 4312 | } |
| 4313 | |
| 4314 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4315 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4316 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4317 | return 0; |
| 4318 | } |
| 4319 | |
| 4320 | /* determine the first block which must not be deleted, blk_end may |
| 4321 | * be NULL if all blocks have to be deleted. |
| 4322 | */ |
| 4323 | idx = htx_get_head(htx); |
| 4324 | blk_end = NULL; |
| 4325 | while (idx != -1) { |
| 4326 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4327 | idx = htx_get_next(htx, idx); |
| 4328 | if (type == HTX_BLK_EOH) { |
| 4329 | if (idx != -1) |
| 4330 | blk_end = htx_get_blk(htx, idx); |
| 4331 | break; |
| 4332 | } |
| 4333 | } |
| 4334 | |
| 4335 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4336 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4337 | ALREADY_CHECKED(sl); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4338 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4339 | if (h2s->status < 100 || h2s->status > 999) |
| 4340 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4341 | |
| 4342 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4343 | hdr = 0; |
| 4344 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4345 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4346 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4347 | blk = htx_get_blk(htx, idx); |
| 4348 | type = htx_get_blk_type(blk); |
| 4349 | |
| 4350 | if (type == HTX_BLK_UNUSED) |
| 4351 | continue; |
| 4352 | |
| 4353 | if (type != HTX_BLK_HDR) |
| 4354 | break; |
| 4355 | |
| 4356 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4357 | goto fail; |
| 4358 | |
| 4359 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4360 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4361 | hdr++; |
| 4362 | } |
| 4363 | |
| 4364 | /* marker for end of headers */ |
| 4365 | list[hdr].n = ist(""); |
| 4366 | |
| 4367 | if (h2s->status == 204 || h2s->status == 304) { |
| 4368 | /* no contents, claim c-len is present and set to zero */ |
| 4369 | es_now = 1; |
| 4370 | } |
| 4371 | |
| 4372 | chunk_reset(&outbuf); |
| 4373 | |
| 4374 | while (1) { |
| 4375 | outbuf.area = b_tail(&h2c->mbuf); |
| 4376 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4377 | outbuf.data = 0; |
| 4378 | |
| 4379 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4380 | break; |
| 4381 | realign_again: |
| 4382 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4383 | } |
| 4384 | |
| 4385 | if (outbuf.size < 9) |
| 4386 | goto full; |
| 4387 | |
| 4388 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4389 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4390 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4391 | outbuf.data = 9; |
| 4392 | |
| 4393 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4394 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4395 | if (b_space_wraps(&h2c->mbuf)) |
| 4396 | goto realign_again; |
| 4397 | goto full; |
| 4398 | } |
| 4399 | |
| 4400 | /* encode all headers, stop at empty name */ |
| 4401 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4402 | /* these ones do not exist in H2 and must be dropped. */ |
| 4403 | if (isteq(list[hdr].n, ist("connection")) || |
| 4404 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4405 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4406 | isteq(list[hdr].n, ist("upgrade")) || |
| 4407 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4408 | continue; |
| 4409 | |
| 4410 | if (isteq(list[hdr].n, ist(""))) |
| 4411 | break; // end |
| 4412 | |
| 4413 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4414 | /* output full */ |
| 4415 | if (b_space_wraps(&h2c->mbuf)) |
| 4416 | goto realign_again; |
| 4417 | goto full; |
| 4418 | } |
| 4419 | } |
| 4420 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4421 | /* we may need to add END_STREAM except for 1xx responses. |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4422 | * FIXME: we should also set it when we know for sure that the |
| 4423 | * content-length is zero as well as on 204/304 |
| 4424 | */ |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4425 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM && |
| 4426 | (h2s->status >= 200 || h2s->status == 101)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4427 | es_now = 1; |
| 4428 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4429 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4430 | es_now = 1; |
| 4431 | |
| 4432 | /* update the frame's size */ |
| 4433 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4434 | |
| 4435 | if (es_now) |
| 4436 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4437 | |
| 4438 | /* commit the H2 response */ |
| 4439 | b_add(&h2c->mbuf, outbuf.data); |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4440 | |
| 4441 | /* indicates the HEADERS frame was sent, except for 1xx responses. For |
| 4442 | * 1xx responses, another HEADERS frame is expected. |
| 4443 | */ |
| 4444 | if (h2s->status >= 200 || h2s->status == 101) |
| 4445 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4446 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4447 | if (es_now) { |
| 4448 | h2s->flags |= H2_SF_ES_SENT; |
| 4449 | if (h2s->st == H2_SS_OPEN) |
| 4450 | h2s->st = H2_SS_HLOC; |
| 4451 | else |
| 4452 | h2s_close(h2s); |
| 4453 | } |
| 4454 | |
| 4455 | /* OK we could properly deliver the response */ |
| 4456 | |
| 4457 | /* remove all header blocks including the EOH and compute the |
| 4458 | * corresponding size. |
| 4459 | * |
| 4460 | * FIXME: We should remove everything when es_now is set. |
| 4461 | */ |
| 4462 | ret = 0; |
| 4463 | idx = htx_get_head(htx); |
| 4464 | blk = htx_get_blk(htx, idx); |
| 4465 | while (blk != blk_end) { |
| 4466 | ret += htx_get_blksz(blk); |
| 4467 | blk = htx_remove_blk(htx, blk); |
| 4468 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4469 | |
| 4470 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4471 | htx_remove_blk(htx, blk_end); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4472 | end: |
| 4473 | return ret; |
| 4474 | full: |
| 4475 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4476 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4477 | ret = 0; |
| 4478 | goto end; |
| 4479 | fail: |
| 4480 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4481 | * list etc go here (unrecoverable errors). |
| 4482 | */ |
| 4483 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4484 | ret = 0; |
| 4485 | goto end; |
| 4486 | } |
| 4487 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4488 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4489 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4490 | * must check the stream's status to detect any error which might have happened |
| 4491 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4492 | * from the message. The htx message is assumed to be valid since produced from |
| 4493 | * the internal code, hence it contains a start line, an optional series of |
| 4494 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4495 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4496 | */ |
| 4497 | static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
| 4498 | { |
| 4499 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4500 | struct h2c *h2c = h2s->h2c; |
| 4501 | struct htx_blk *blk; |
| 4502 | struct htx_blk *blk_end; |
| 4503 | struct buffer outbuf; |
| 4504 | struct htx_sl *sl; |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4505 | struct ist meth, path, auth; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4506 | enum htx_blk_type type; |
| 4507 | int es_now = 0; |
| 4508 | int ret = 0; |
| 4509 | int hdr; |
| 4510 | int idx; |
| 4511 | |
| 4512 | if (h2c_mux_busy(h2c, h2s)) { |
| 4513 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4514 | return 0; |
| 4515 | } |
| 4516 | |
| 4517 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4518 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4519 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4520 | return 0; |
| 4521 | } |
| 4522 | |
| 4523 | /* determine the first block which must not be deleted, blk_end may |
| 4524 | * be NULL if all blocks have to be deleted. |
| 4525 | */ |
| 4526 | idx = htx_get_head(htx); |
| 4527 | blk_end = NULL; |
| 4528 | while (idx != -1) { |
| 4529 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4530 | idx = htx_get_next(htx, idx); |
| 4531 | if (type == HTX_BLK_EOH) { |
| 4532 | if (idx != -1) |
| 4533 | blk_end = htx_get_blk(htx, idx); |
| 4534 | break; |
| 4535 | } |
| 4536 | } |
| 4537 | |
| 4538 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4539 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4540 | ALREADY_CHECKED(sl); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4541 | meth = htx_sl_req_meth(sl); |
| 4542 | path = htx_sl_req_uri(sl); |
| 4543 | |
| 4544 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4545 | hdr = 0; |
| 4546 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4547 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4548 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4549 | blk = htx_get_blk(htx, idx); |
| 4550 | type = htx_get_blk_type(blk); |
| 4551 | |
| 4552 | if (type == HTX_BLK_UNUSED) |
| 4553 | continue; |
| 4554 | |
| 4555 | if (type != HTX_BLK_HDR) |
| 4556 | break; |
| 4557 | |
| 4558 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4559 | goto fail; |
| 4560 | |
| 4561 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4562 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4563 | hdr++; |
| 4564 | } |
| 4565 | |
| 4566 | /* marker for end of headers */ |
| 4567 | list[hdr].n = ist(""); |
| 4568 | |
| 4569 | chunk_reset(&outbuf); |
| 4570 | |
| 4571 | while (1) { |
| 4572 | outbuf.area = b_tail(&h2c->mbuf); |
| 4573 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4574 | outbuf.data = 0; |
| 4575 | |
| 4576 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4577 | break; |
| 4578 | realign_again: |
| 4579 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4580 | } |
| 4581 | |
| 4582 | if (outbuf.size < 9) |
| 4583 | goto full; |
| 4584 | |
| 4585 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4586 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4587 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4588 | outbuf.data = 9; |
| 4589 | |
| 4590 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4591 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4592 | if (b_space_wraps(&h2c->mbuf)) |
| 4593 | goto realign_again; |
| 4594 | goto full; |
| 4595 | } |
| 4596 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4597 | /* RFC7540 #8.3: the CONNECT method must have : |
| 4598 | * - :authority set to the URI part (host:port) |
| 4599 | * - :method set to CONNECT |
| 4600 | * - :scheme and :path omitted |
| 4601 | */ |
| 4602 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 4603 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
| 4604 | if (!hpack_encode_scheme(&outbuf, ist("https"))) { |
| 4605 | /* output full */ |
| 4606 | if (b_space_wraps(&h2c->mbuf)) |
| 4607 | goto realign_again; |
| 4608 | goto full; |
| 4609 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4610 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4611 | /* encode the path, which necessarily is the second one */ |
| 4612 | if (!hpack_encode_path(&outbuf, path)) { |
| 4613 | /* output full */ |
| 4614 | if (b_space_wraps(&h2c->mbuf)) |
| 4615 | goto realign_again; |
| 4616 | goto full; |
| 4617 | } |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4618 | |
| 4619 | /* look for the Host header and place it in :authority */ |
| 4620 | auth = ist2(NULL, 0); |
| 4621 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4622 | if (isteq(list[hdr].n, ist(""))) |
| 4623 | break; // end |
| 4624 | |
| 4625 | if (isteq(list[hdr].n, ist("host"))) { |
| 4626 | auth = list[hdr].v; |
| 4627 | break; |
| 4628 | } |
| 4629 | } |
| 4630 | } |
| 4631 | else { |
| 4632 | /* for CONNECT, :authority is taken from the path */ |
| 4633 | auth = path; |
| 4634 | } |
| 4635 | |
| 4636 | if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) { |
| 4637 | /* output full */ |
| 4638 | if (b_space_wraps(&h2c->mbuf)) |
| 4639 | goto realign_again; |
| 4640 | goto full; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4641 | } |
| 4642 | |
| 4643 | /* encode all headers, stop at empty name */ |
| 4644 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4645 | /* these ones do not exist in H2 and must be dropped. */ |
| 4646 | if (isteq(list[hdr].n, ist("connection")) || |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4647 | isteq(list[hdr].n, ist("host")) || |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4648 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4649 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4650 | isteq(list[hdr].n, ist("upgrade")) || |
| 4651 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4652 | continue; |
| 4653 | |
| 4654 | if (isteq(list[hdr].n, ist(""))) |
| 4655 | break; // end |
| 4656 | |
| 4657 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4658 | /* output full */ |
| 4659 | if (b_space_wraps(&h2c->mbuf)) |
| 4660 | goto realign_again; |
| 4661 | goto full; |
| 4662 | } |
| 4663 | } |
| 4664 | |
| 4665 | /* we may need to add END_STREAM if we have no body : |
| 4666 | * - request already closed, or : |
| 4667 | * - no transfer-encoding, and : |
| 4668 | * - no content-length or content-length:0 |
| 4669 | * Fixme: this doesn't take into account CONNECT requests. |
| 4670 | */ |
| 4671 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4672 | es_now = 1; |
| 4673 | |
| 4674 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4675 | es_now = 1; |
| 4676 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4677 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4678 | es_now = 1; |
| 4679 | |
| 4680 | /* update the frame's size */ |
| 4681 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4682 | |
| 4683 | if (es_now) |
| 4684 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4685 | |
| 4686 | /* commit the H2 response */ |
| 4687 | b_add(&h2c->mbuf, outbuf.data); |
| 4688 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4689 | h2s->st = H2_SS_OPEN; |
| 4690 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4691 | if (es_now) { |
| 4692 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4693 | h2s->flags |= H2_SF_ES_SENT; |
| 4694 | h2s->st = H2_SS_HLOC; |
| 4695 | } |
| 4696 | |
| 4697 | /* remove all header blocks including the EOH and compute the |
| 4698 | * corresponding size. |
| 4699 | * |
| 4700 | * FIXME: We should remove everything when es_now is set. |
| 4701 | */ |
| 4702 | ret = 0; |
| 4703 | idx = htx_get_head(htx); |
| 4704 | blk = htx_get_blk(htx, idx); |
| 4705 | while (blk != blk_end) { |
| 4706 | ret += htx_get_blksz(blk); |
| 4707 | blk = htx_remove_blk(htx, blk); |
| 4708 | } |
| 4709 | |
| 4710 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4711 | htx_remove_blk(htx, blk_end); |
| 4712 | |
| 4713 | end: |
| 4714 | return ret; |
| 4715 | full: |
| 4716 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4717 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4718 | ret = 0; |
| 4719 | goto end; |
| 4720 | fail: |
| 4721 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4722 | * list etc go here (unrecoverable errors). |
| 4723 | */ |
| 4724 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4725 | ret = 0; |
| 4726 | goto end; |
| 4727 | } |
| 4728 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4729 | /* 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] | 4730 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 4731 | * caller must check the stream's status to detect any error which might have |
| 4732 | * happened subsequently to a successful send. Returns the number of data bytes |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4733 | * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte. |
| 4734 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4735 | 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] | 4736 | { |
| 4737 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4738 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4739 | struct buffer outbuf; |
| 4740 | size_t total = 0; |
| 4741 | int es_now = 0; |
| 4742 | int bsize; /* htx block size */ |
| 4743 | int fsize; /* h2 frame size */ |
| 4744 | struct htx_blk *blk; |
| 4745 | enum htx_blk_type type; |
| 4746 | int idx; |
| 4747 | |
| 4748 | if (h2c_mux_busy(h2c, h2s)) { |
| 4749 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4750 | goto end; |
| 4751 | } |
| 4752 | |
| 4753 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4754 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4755 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4756 | goto end; |
| 4757 | } |
| 4758 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4759 | htx = htx_from_buf(buf); |
| 4760 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4761 | /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However, |
| 4762 | * while looping, we can meet an HTX_BLK_EOM block that we'll leave to |
| 4763 | * the caller to handle. |
| 4764 | */ |
| 4765 | |
| 4766 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4767 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4768 | goto end; |
| 4769 | |
| 4770 | idx = htx_get_head(htx); |
| 4771 | blk = htx_get_blk(htx, idx); |
| 4772 | type = htx_get_blk_type(blk); // DATA or EOD or EOM |
| 4773 | bsize = htx_get_blksz(blk); |
| 4774 | fsize = bsize; |
| 4775 | |
| 4776 | if (type == HTX_BLK_EOD) { |
| 4777 | /* if we have an EOD, we're dealing with chunked data. We may |
| 4778 | * have a set of trailers after us that the caller will want to |
| 4779 | * deal with. Let's simply remove the EOD and return. |
| 4780 | */ |
| 4781 | htx_remove_blk(htx, blk); |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4782 | total++; // EOD counts as one byte |
| 4783 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4784 | goto end; |
| 4785 | } |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 4786 | else if (type == HTX_BLK_EOM) { |
| 4787 | if (h2s->flags & H2_SF_ES_SENT) { |
| 4788 | /* ES already sent */ |
| 4789 | htx_remove_blk(htx, blk); |
| 4790 | total++; // EOM counts as one byte |
| 4791 | count--; |
| 4792 | goto end; |
| 4793 | } |
| 4794 | } |
| 4795 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4796 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4797 | |
| 4798 | /* Perform some optimizations to reduce the number of buffer copies. |
| 4799 | * First, if the mux's buffer is empty and the htx area contains |
| 4800 | * exactly one data block of the same size as the requested count, and |
| 4801 | * this count fits within the frame size, the stream's window size, and |
| 4802 | * the connection's window size, then it's possible to simply swap the |
| 4803 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 4804 | * length to match the entire DATA HTX block in the middle. In this |
| 4805 | * case we perform a true zero-copy operation from end-to-end. This is |
| 4806 | * the situation that happens all the time with large files. Second, if |
| 4807 | * this is not possible, but the mux's output buffer is empty, we still |
| 4808 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 4809 | * making the intermediary buffer's area point to the output buffer's |
| 4810 | * area. In this case we want to skip the HTX header to make sure that |
| 4811 | * copies remain aligned and that this operation remains possible all |
| 4812 | * the time. This goes for headers, data blocks and any data extracted |
| 4813 | * from the HTX blocks. |
| 4814 | */ |
| 4815 | if (unlikely(fsize == count && |
| 4816 | htx->used == 1 && type == HTX_BLK_DATA && |
| 4817 | fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
| 4818 | void *old_area = h2c->mbuf.area; |
| 4819 | |
| 4820 | if (b_data(&h2c->mbuf)) { |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4821 | /* Too bad there are data left there. We're willing to memcpy/memmove |
| 4822 | * up to 1/4 of the buffer, which means that it's OK to copy a large |
| 4823 | * frame into a buffer containing few data if it needs to be realigned, |
| 4824 | * and that it's also OK to copy few data without realigning. Otherwise |
| 4825 | * 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] | 4826 | */ |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4827 | if (fsize + 9 <= b_room(&h2c->mbuf) && |
| 4828 | (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 || |
| 4829 | (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] | 4830 | goto copy; |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4831 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4832 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4833 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4834 | goto end; |
| 4835 | } |
| 4836 | |
| 4837 | /* map an H2 frame to the HTX block so that we can put the |
| 4838 | * frame header there. |
| 4839 | */ |
| 4840 | h2c->mbuf.area = buf->area; |
Olivier Houchard | 84cca66 | 2018-12-14 16:28:08 +0100 | [diff] [blame] | 4841 | h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4842 | h2c->mbuf.data = fsize + 9; |
| 4843 | outbuf.area = b_head(&h2c->mbuf); |
| 4844 | |
| 4845 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 4846 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4847 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4848 | h2_set_frame_size(outbuf.area, fsize); |
| 4849 | |
| 4850 | /* update windows */ |
| 4851 | h2s->mws -= fsize; |
| 4852 | h2c->mws -= fsize; |
| 4853 | |
| 4854 | /* and exchange with our old area */ |
| 4855 | buf->area = old_area; |
| 4856 | buf->data = buf->head = 0; |
| 4857 | total += fsize; |
| 4858 | goto end; |
| 4859 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4860 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4861 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4862 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 4863 | |
| 4864 | while (1) { |
| 4865 | outbuf.area = b_tail(&h2c->mbuf); |
| 4866 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4867 | outbuf.data = 0; |
| 4868 | |
| 4869 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4870 | break; |
| 4871 | realign_again: |
| 4872 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4873 | } |
| 4874 | |
| 4875 | if (outbuf.size < 9) { |
| 4876 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4877 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4878 | goto end; |
| 4879 | } |
| 4880 | |
| 4881 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 4882 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4883 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4884 | outbuf.data = 9; |
| 4885 | |
| 4886 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 4887 | * the HTX buffer. We need to check this against the connection's and |
| 4888 | * the stream's send windows, and to ensure that this fits in the max |
| 4889 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4890 | * the frame header). The connection's flow control is applied last so |
| 4891 | * that we can use a separate list of streams which are immediately |
| 4892 | * unblocked on window opening. Note: we don't implement padding. |
| 4893 | */ |
| 4894 | |
| 4895 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 4896 | * empty frame, thus we force it to zero here. |
| 4897 | */ |
| 4898 | if (type == HTX_BLK_EOM) |
| 4899 | bsize = fsize = 0; |
| 4900 | |
| 4901 | if (!fsize) |
| 4902 | goto send_empty; |
| 4903 | |
| 4904 | if (h2s->mws <= 0) { |
| 4905 | h2s->flags |= H2_SF_BLK_SFCTL; |
Olivier Houchard | bfe2a83 | 2019-05-10 14:02:21 +0200 | [diff] [blame] | 4906 | if (!LIST_ISEMPTY(&h2s->list)) |
| 4907 | LIST_DEL_INIT(&h2s->list); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4908 | goto end; |
| 4909 | } |
| 4910 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4911 | if (fsize > count) |
| 4912 | fsize = count; |
| 4913 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4914 | if (fsize > h2s->mws) |
| 4915 | fsize = h2s->mws; // >0 |
| 4916 | |
| 4917 | if (h2c->mfs && fsize > h2c->mfs) |
| 4918 | fsize = h2c->mfs; // >0 |
| 4919 | |
| 4920 | if (fsize + 9 > outbuf.size) { |
| 4921 | /* we have an opportunity for enlarging the too small |
| 4922 | * available space, let's try. |
| 4923 | * FIXME: is this really interesting to do? Maybe we'll |
| 4924 | * spend lots of time realigning instead of using two |
| 4925 | * frames. |
| 4926 | */ |
| 4927 | if (b_space_wraps(&h2c->mbuf)) |
| 4928 | goto realign_again; |
| 4929 | fsize = outbuf.size - 9; |
| 4930 | |
| 4931 | if (fsize <= 0) { |
| 4932 | /* no need to send an empty frame here */ |
| 4933 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4934 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4935 | goto end; |
| 4936 | } |
| 4937 | } |
| 4938 | |
| 4939 | if (h2c->mws <= 0) { |
| 4940 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4941 | goto end; |
| 4942 | } |
| 4943 | |
| 4944 | if (fsize > h2c->mws) |
| 4945 | fsize = h2c->mws; |
| 4946 | |
| 4947 | /* now let's copy this this into the output buffer */ |
| 4948 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 4949 | h2s->mws -= fsize; |
| 4950 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4951 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4952 | |
| 4953 | send_empty: |
| 4954 | /* update the frame's size */ |
| 4955 | h2_set_frame_size(outbuf.area, fsize); |
| 4956 | |
| 4957 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 4958 | * meeting EOM. We should optimize this later. |
| 4959 | */ |
| 4960 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4961 | total++; // EOM counts as one byte |
| 4962 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4963 | es_now = 1; |
| 4964 | } |
| 4965 | |
| 4966 | if (es_now) |
| 4967 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 4968 | |
| 4969 | /* commit the H2 response */ |
| 4970 | b_add(&h2c->mbuf, fsize + 9); |
| 4971 | |
| 4972 | /* consume incoming HTX block, including EOM */ |
| 4973 | total += fsize; |
| 4974 | if (fsize == bsize) { |
| 4975 | htx_remove_blk(htx, blk); |
| 4976 | if (fsize) |
| 4977 | goto new_frame; |
| 4978 | } else { |
| 4979 | /* we've truncated this block */ |
| 4980 | htx_cut_data_blk(htx, blk, fsize); |
| 4981 | } |
| 4982 | |
| 4983 | if (es_now) { |
| 4984 | if (h2s->st == H2_SS_OPEN) |
| 4985 | h2s->st = H2_SS_HLOC; |
| 4986 | else |
| 4987 | h2s_close(h2s); |
| 4988 | |
| 4989 | h2s->flags |= H2_SF_ES_SENT; |
| 4990 | } |
| 4991 | |
| 4992 | end: |
| 4993 | return total; |
| 4994 | } |
| 4995 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4996 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 4997 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 4998 | * processed. The caller must check the stream's status to detect any error |
| 4999 | * which might have happened subsequently to a successful send. The htx blocks |
| 5000 | * are automatically removed from the message. The htx message is assumed to be |
| 5001 | * valid since produced from the internal code. Processing stops when meeting |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5002 | * 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] | 5003 | * as a single frame. The ES flag is always set. |
| 5004 | */ |
| 5005 | static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx) |
| 5006 | { |
| 5007 | struct http_hdr list[MAX_HTTP_HDR]; |
| 5008 | struct h2c *h2c = h2s->h2c; |
| 5009 | struct htx_blk *blk; |
| 5010 | struct htx_blk *blk_end; |
| 5011 | struct buffer outbuf; |
| 5012 | struct h1m h1m; |
| 5013 | enum htx_blk_type type; |
| 5014 | uint32_t size; |
| 5015 | int ret = 0; |
| 5016 | int hdr; |
| 5017 | int idx; |
| 5018 | void *start; |
| 5019 | |
| 5020 | if (h2c_mux_busy(h2c, h2s)) { |
| 5021 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 5022 | goto end; |
| 5023 | } |
| 5024 | |
| 5025 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 5026 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 5027 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5028 | goto end; |
| 5029 | } |
| 5030 | |
| 5031 | /* The principle is that we parse each and every trailers block using |
| 5032 | * the H1 headers parser, and append it to the list. We don't proceed |
| 5033 | * until EOM is met. blk_end will point to the EOM block. |
| 5034 | */ |
| 5035 | hdr = 0; |
| 5036 | memset(list, 0, sizeof(list)); |
| 5037 | blk_end = NULL; |
| 5038 | |
| 5039 | for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) { |
| 5040 | blk = htx_get_blk(htx, idx); |
| 5041 | type = htx_get_blk_type(blk); |
| 5042 | |
| 5043 | if (type == HTX_BLK_UNUSED) |
| 5044 | continue; |
| 5045 | |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5046 | if (type != HTX_BLK_TLR) |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5047 | break; |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5048 | |
| 5049 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 5050 | goto fail; |
| 5051 | |
| 5052 | size = htx_get_blksz(blk); |
| 5053 | start = htx_get_blk_ptr(htx, blk); |
| 5054 | |
| 5055 | h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER; |
| 5056 | h1m.err_pos = 0; |
| 5057 | ret = h1_headers_to_hdr_list(start, start + size, |
| 5058 | list + hdr, sizeof(list)/sizeof(list[0]) - hdr, |
| 5059 | &h1m, NULL); |
| 5060 | if (ret < 0) |
| 5061 | goto fail; |
| 5062 | |
| 5063 | /* ret == 0 if an incomplete trailers block was found (missing |
| 5064 | * empty line), or > 0 if it was found. We have to continue on |
| 5065 | * incomplete messages because the trailers block might be |
| 5066 | * incomplete. |
| 5067 | */ |
| 5068 | |
| 5069 | /* search the new end */ |
| 5070 | while (hdr <= sizeof(list)/sizeof(list[0])) { |
| 5071 | if (!list[hdr].n.len) |
| 5072 | break; |
| 5073 | hdr++; |
| 5074 | } |
| 5075 | } |
| 5076 | |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5077 | if (list[hdr].n.len != 0) |
| 5078 | goto fail; // empty trailer not found: internal error |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5079 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5080 | chunk_reset(&outbuf); |
| 5081 | |
| 5082 | while (1) { |
| 5083 | outbuf.area = b_tail(&h2c->mbuf); |
| 5084 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 5085 | outbuf.data = 0; |
| 5086 | |
| 5087 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 5088 | break; |
| 5089 | realign_again: |
| 5090 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 5091 | } |
| 5092 | |
| 5093 | if (outbuf.size < 9) |
| 5094 | goto full; |
| 5095 | |
| 5096 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 5097 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 5098 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5099 | outbuf.data = 9; |
| 5100 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5101 | /* encode all headers */ |
| 5102 | for (idx = 0; idx < hdr; idx++) { |
| 5103 | /* these ones do not exist in H2 or must not appear in |
| 5104 | * trailers and must be dropped. |
| 5105 | */ |
| 5106 | if (isteq(list[idx].n, ist("host")) || |
| 5107 | isteq(list[idx].n, ist("content-length")) || |
| 5108 | isteq(list[idx].n, ist("connection")) || |
| 5109 | isteq(list[idx].n, ist("proxy-connection")) || |
| 5110 | isteq(list[idx].n, ist("keep-alive")) || |
| 5111 | isteq(list[idx].n, ist("upgrade")) || |
| 5112 | isteq(list[idx].n, ist("te")) || |
| 5113 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 5114 | continue; |
| 5115 | |
| 5116 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 5117 | /* output full */ |
| 5118 | if (b_space_wraps(&h2c->mbuf)) |
| 5119 | goto realign_again; |
| 5120 | goto full; |
| 5121 | } |
| 5122 | } |
| 5123 | |
Willy Tarreau | 5121e5d | 2019-05-06 15:13:41 +0200 | [diff] [blame] | 5124 | if (outbuf.data == 9) { |
| 5125 | /* here we have a problem, we have nothing to emit (either we |
| 5126 | * received an empty trailers block followed or we removed its |
| 5127 | * contents above). Because of this we can't send a HEADERS |
| 5128 | * frame, so we have to cheat and instead send an empty DATA |
| 5129 | * frame conveying the ES flag. |
Willy Tarreau | 67b8cae | 2019-02-21 18:16:35 +0100 | [diff] [blame] | 5130 | */ |
| 5131 | outbuf.area[3] = H2_FT_DATA; |
| 5132 | outbuf.area[4] = H2_F_DATA_END_STREAM; |
| 5133 | } |
| 5134 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5135 | /* update the frame's size */ |
| 5136 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 5137 | |
| 5138 | /* commit the H2 response */ |
| 5139 | b_add(&h2c->mbuf, outbuf.data); |
| 5140 | h2s->flags |= H2_SF_ES_SENT; |
| 5141 | |
| 5142 | if (h2s->st == H2_SS_OPEN) |
| 5143 | h2s->st = H2_SS_HLOC; |
| 5144 | else |
| 5145 | h2s_close(h2s); |
| 5146 | |
| 5147 | /* OK we could properly deliver the response */ |
| 5148 | done: |
Willy Tarreau | fb07b3f | 2019-05-06 11:23:29 +0200 | [diff] [blame] | 5149 | /* remove all header blocks till the end and compute the corresponding size. */ |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5150 | ret = 0; |
| 5151 | idx = htx_get_head(htx); |
| 5152 | blk = htx_get_blk(htx, idx); |
| 5153 | while (blk != blk_end) { |
| 5154 | ret += htx_get_blksz(blk); |
| 5155 | blk = htx_remove_blk(htx, blk); |
| 5156 | } |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5157 | end: |
| 5158 | return ret; |
| 5159 | full: |
| 5160 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5161 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5162 | ret = 0; |
| 5163 | goto end; |
| 5164 | fail: |
| 5165 | /* unparsable HTX messages, too large ones to be produced in the local |
| 5166 | * list etc go here (unrecoverable errors). |
| 5167 | */ |
| 5168 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5169 | ret = 0; |
| 5170 | goto end; |
| 5171 | } |
| 5172 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5173 | /* Called from the upper layer, to subscribe to events, such as being able to send. |
| 5174 | * The <param> argument here is supposed to be a pointer to a wait_event struct |
| 5175 | * which will be passed to h2s->recv_wait or h2s->send_wait depending on the |
| 5176 | * event_type. The event_type must only be a combination of SUB_RETRY_RECV and |
| 5177 | * SUB_RETRY_SEND, other values will lead to -1 being returned. It always |
| 5178 | * returns 0 except for the error above. |
| 5179 | */ |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5180 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 5181 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5182 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5183 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5184 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5185 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5186 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5187 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5188 | if (!(sw->events & SUB_RETRY_RECV)) { |
| 5189 | sw->events |= SUB_RETRY_RECV; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 5190 | sw->handle = h2s; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5191 | h2s->recv_wait = sw; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5192 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5193 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5194 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5195 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5196 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5197 | if (!(sw->events & SUB_RETRY_SEND)) { |
| 5198 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 5199 | sw->handle = h2s; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5200 | h2s->send_wait = sw; |
Olivier Houchard | 9a0f559 | 2019-04-15 19:22:24 +0200 | [diff] [blame] | 5201 | if (!(h2s->flags & H2_SF_BLK_SFCTL) && |
| 5202 | LIST_ISEMPTY(&h2s->list)) { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5203 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 5204 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 5205 | else |
| 5206 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 5207 | } |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 5208 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5209 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5210 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5211 | if (event_type != 0) |
| 5212 | return -1; |
| 5213 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5214 | } |
| 5215 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5216 | /* Called from the upper layer, to unsubscribe some events (undo h2_subscribe). |
| 5217 | * The <param> argument here is supposed to be a pointer to the same wait_event |
| 5218 | * struct that was passed to h2_subscribe() otherwise nothing will be changed. |
| 5219 | * It always returns zero. |
| 5220 | */ |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5221 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 5222 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5223 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5224 | struct h2s *h2s = cs->ctx; |
| 5225 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5226 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5227 | sw = param; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5228 | if (h2s->recv_wait == sw) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5229 | sw->events &= ~SUB_RETRY_RECV; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5230 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5231 | } |
| 5232 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5233 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5234 | sw = param; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5235 | if (h2s->send_wait == sw) { |
| 5236 | LIST_DEL(&h2s->list); |
| 5237 | LIST_INIT(&h2s->list); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5238 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5239 | /* We were about to send, make sure it does not happen */ |
| 5240 | if (!LIST_ISEMPTY(&h2s->sending_list) && |
| 5241 | h2s->send_wait != &h2s->wait_event) { |
| 5242 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
| 5243 | LIST_DEL_INIT(&h2s->sending_list); |
| 5244 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5245 | h2s->send_wait = NULL; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5246 | |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5247 | } |
| 5248 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5249 | return 0; |
| 5250 | } |
| 5251 | |
| 5252 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5253 | /* Called from the upper layer, to receive data */ |
| 5254 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 5255 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5256 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5257 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5258 | struct htx *h2s_htx = NULL; |
| 5259 | struct htx *buf_htx = NULL; |
| 5260 | struct htx_ret htx_ret; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5261 | size_t ret = 0; |
| 5262 | |
| 5263 | /* transfer possibly pending data to the upper layer */ |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5264 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
| 5265 | /* in HTX mode we ignore the count argument */ |
| 5266 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5267 | if (htx_is_empty(h2s_htx)) { |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5268 | /* Here htx_to_buf() will set buffer data to 0 because |
| 5269 | * the HTX is empty. |
| 5270 | */ |
| 5271 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5272 | goto end; |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5273 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5274 | |
| 5275 | buf_htx = htx_from_buf(buf); |
Christopher Faulet | a413e95 | 2019-01-21 11:49:37 +0100 | [diff] [blame] | 5276 | count = htx_free_data_space(buf_htx); |
| 5277 | if (flags & CO_RFL_KEEP_RSV) { |
| 5278 | if (count <= global.tune.maxrewrite) |
| 5279 | goto end; |
| 5280 | count -= global.tune.maxrewrite; |
| 5281 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5282 | |
Willy Tarreau | 0c22fa7 | 2018-12-04 15:21:35 +0100 | [diff] [blame] | 5283 | 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] | 5284 | |
| 5285 | if (h2s_htx->flags & HTX_FL_PARSING_ERROR) |
| 5286 | buf_htx->flags |= HTX_FL_PARSING_ERROR; |
| 5287 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 5288 | 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] | 5289 | htx_to_buf(buf_htx, buf); |
| 5290 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5291 | ret = htx_ret.ret; |
| 5292 | } |
| 5293 | else { |
| 5294 | ret = b_xfer(buf, &h2s->rxbuf, count); |
| 5295 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5296 | |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5297 | end: |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5298 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5299 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5300 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5301 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5302 | if (cs->flags & CS_FL_REOS) |
| 5303 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5304 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5305 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5306 | if (b_size(&h2s->rxbuf)) { |
| 5307 | b_free(&h2s->rxbuf); |
| 5308 | offer_buffers(NULL, tasks_run_queue); |
| 5309 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5310 | } |
| 5311 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5312 | if (ret && h2c->dsi == h2s->id) { |
| 5313 | /* demux is blocking on this stream's buffer */ |
| 5314 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 5315 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5316 | } |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5317 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5318 | return ret; |
| 5319 | } |
| 5320 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5321 | /* stops all senders of this connection for example when the mux buffer is full. |
| 5322 | * They are moved from the sending_list to either fctl_list or send_list. |
| 5323 | */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5324 | static void h2_stop_senders(struct h2c *h2c) |
| 5325 | { |
| 5326 | struct h2s *h2s, *h2s_back; |
| 5327 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5328 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) { |
| 5329 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | e73256f | 2019-03-25 18:02:54 +0100 | [diff] [blame] | 5330 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5331 | h2s->send_wait->events |= SUB_RETRY_SEND; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5332 | } |
| 5333 | } |
| 5334 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5335 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 5336 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 5337 | * flags may be updated on the conn_stream. |
| 5338 | */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 5339 | 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] | 5340 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5341 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5342 | size_t orig_count = count; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 5343 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5344 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5345 | struct htx *htx; |
| 5346 | struct htx_blk *blk; |
| 5347 | enum htx_blk_type btype; |
| 5348 | uint32_t bsize; |
| 5349 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5350 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5351 | /* If we were not just woken because we wanted to send but couldn't, |
| 5352 | * and there's somebody else that is waiting to send, do nothing, |
| 5353 | * we will subscribe later and be put at the end of the list |
| 5354 | */ |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5355 | if (LIST_ISEMPTY(&h2s->sending_list) && |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5356 | (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) |
| 5357 | return 0; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5358 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5359 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5360 | /* We couldn't set it to NULL before, because we needed it in case |
| 5361 | * we had to cancel the tasklet |
| 5362 | */ |
| 5363 | h2s->send_wait = NULL; |
| 5364 | |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 5365 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 5366 | return 0; |
| 5367 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5368 | /* htx will be enough to decide if we're using HTX or legacy */ |
| 5369 | htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL; |
| 5370 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5371 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 5372 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 5373 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5374 | if (h2s->id == 0) { |
| 5375 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 5376 | |
| 5377 | if (id < 0) { |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5378 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5379 | return 0; |
| 5380 | } |
| 5381 | |
| 5382 | eb32_delete(&h2s->by_id); |
| 5383 | h2s->by_id.key = h2s->id = id; |
| 5384 | h2s->h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 5385 | h2s->h2c->nb_reserved--; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5386 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 5387 | } |
| 5388 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5389 | if (htx) { |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5390 | while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) && |
Willy Tarreau | c14999b | 2018-12-06 14:09:09 +0100 | [diff] [blame] | 5391 | count && !htx_is_empty(htx)) { |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5392 | idx = htx_get_head(htx); |
| 5393 | blk = htx_get_blk(htx, idx); |
| 5394 | btype = htx_get_blk_type(blk); |
| 5395 | bsize = htx_get_blksz(blk); |
| 5396 | |
| 5397 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5398 | case HTX_BLK_REQ_SL: |
| 5399 | /* start-line before headers */ |
| 5400 | ret = h2s_htx_bck_make_req_headers(h2s, htx); |
| 5401 | if (ret > 0) { |
| 5402 | total += ret; |
| 5403 | count -= ret; |
| 5404 | if (ret < bsize) |
| 5405 | goto done; |
| 5406 | } |
| 5407 | break; |
| 5408 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 5409 | case HTX_BLK_RES_SL: |
| 5410 | /* start-line before headers */ |
| 5411 | ret = h2s_htx_frt_make_resp_headers(h2s, htx); |
| 5412 | if (ret > 0) { |
| 5413 | total += ret; |
| 5414 | count -= ret; |
| 5415 | if (ret < bsize) |
| 5416 | goto done; |
| 5417 | } |
| 5418 | break; |
| 5419 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5420 | case HTX_BLK_DATA: |
| 5421 | case HTX_BLK_EOD: |
| 5422 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5423 | /* all these cause the emission of a DATA frame (possibly empty). |
| 5424 | * This EOM necessarily is one before trailers, as the EOM following |
| 5425 | * trailers would have been consumed by the trailers parser. |
| 5426 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5427 | ret = h2s_htx_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5428 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5429 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5430 | total += ret; |
| 5431 | count -= ret; |
| 5432 | if (ret < bsize) |
| 5433 | goto done; |
| 5434 | } |
| 5435 | break; |
| 5436 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5437 | case HTX_BLK_TLR: |
| 5438 | /* This is the first trailers block, all the subsequent ones AND |
| 5439 | * the EOM will be swallowed by the parser. |
| 5440 | */ |
| 5441 | ret = h2s_htx_make_trailers(h2s, htx); |
| 5442 | if (ret > 0) { |
| 5443 | total += ret; |
| 5444 | count -= ret; |
| 5445 | if (ret < bsize) |
| 5446 | goto done; |
| 5447 | } |
| 5448 | break; |
| 5449 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5450 | default: |
| 5451 | htx_remove_blk(htx, blk); |
| 5452 | total += bsize; |
| 5453 | count -= bsize; |
| 5454 | break; |
| 5455 | } |
| 5456 | } |
| 5457 | goto done; |
| 5458 | } |
| 5459 | |
| 5460 | /* legacy transfer mode */ |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5461 | while (h2s->h1m.state < H1_MSG_DONE && count) { |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 5462 | if (h2s->h1m.state <= H1_MSG_LAST_LF) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5463 | if (h2s->h2c->flags & H2_CF_IS_BACK) |
| 5464 | ret = -1; |
| 5465 | else |
| 5466 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5467 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5468 | else if (h2s->h1m.state < H1_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5469 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5470 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5471 | else if (h2s->h1m.state == H1_MSG_TRAILERS) { |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5472 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5473 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5474 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5475 | if (unlikely((int)ret <= 0)) { |
| 5476 | if ((int)ret < 0) |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5477 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5478 | break; |
| 5479 | } |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 5480 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5481 | total += count; |
| 5482 | count = 0; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5483 | h2s->h1m.state = H1_MSG_DONE; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5484 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 5485 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5486 | else { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5487 | cs_set_error(cs); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5488 | break; |
| 5489 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5490 | |
| 5491 | total += ret; |
| 5492 | count -= ret; |
| 5493 | |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5494 | if (h2s->st >= H2_SS_HLOC) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5495 | break; |
| 5496 | |
| 5497 | if (h2s->flags & H2_SF_BLK_ANY) |
| 5498 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5499 | } |
| 5500 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5501 | done: |
Willy Tarreau | 2b77848 | 2019-05-06 15:00:22 +0200 | [diff] [blame] | 5502 | if (h2s->st >= H2_SS_HLOC) { |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5503 | /* trim any possibly pending data after we close (extra CR-LF, |
| 5504 | * unprocessed trailers, abnormal extra data, ...) |
| 5505 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5506 | total += count; |
| 5507 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5508 | } |
| 5509 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5510 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 5511 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5512 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 5513 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 5514 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5515 | } |
| 5516 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5517 | if (htx) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5518 | htx_to_buf(htx, buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5519 | } else { |
| 5520 | b_del(buf, total); |
| 5521 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5522 | |
| 5523 | /* The mux is full, cancel the pending tasks */ |
| 5524 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 5525 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 5526 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5527 | |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5528 | /* If we're running HTX, and we read the whole buffer, then pretend |
| 5529 | * we read exactly what the caller specified, as with HTX the caller |
| 5530 | * will always give the buffer size, instead of the amount of data |
| 5531 | * available. |
| 5532 | */ |
| 5533 | if (htx && !b_data(buf)) |
| 5534 | total = orig_count; |
| 5535 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5536 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5537 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5538 | tasklet_wakeup(h2s->h2c->wait_event.task); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5539 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5540 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 5541 | /* If we're waiting for flow control, and we got a shutr on the |
| 5542 | * connection, we will never be unlocked, so add an error on |
| 5543 | * the conn_stream. |
| 5544 | */ |
| 5545 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 5546 | !b_data(&h2s->h2c->dbuf) && |
| 5547 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 5548 | if (cs->flags & CS_FL_EOS) |
| 5549 | cs->flags |= CS_FL_ERROR; |
| 5550 | else |
| 5551 | cs->flags |= CS_FL_ERR_PENDING; |
| 5552 | } |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5553 | if (total > 0) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5554 | /* Ok we managed to send something, leave the send_list */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5555 | LIST_DEL_INIT(&h2s->list); |
| 5556 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5557 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5558 | } |
| 5559 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5560 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5561 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5562 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5563 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5564 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5565 | struct eb32_node *node; |
| 5566 | int fctl_cnt = 0; |
| 5567 | int send_cnt = 0; |
| 5568 | int tree_cnt = 0; |
| 5569 | int orph_cnt = 0; |
| 5570 | |
| 5571 | if (!h2c) |
| 5572 | return; |
| 5573 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5574 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5575 | fctl_cnt++; |
| 5576 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5577 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5578 | send_cnt++; |
| 5579 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5580 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5581 | node = eb32_first(&h2c->streams_by_id); |
| 5582 | while (node) { |
| 5583 | h2s = container_of(node, struct h2s, by_id); |
| 5584 | tree_cnt++; |
| 5585 | if (!h2s->cs) |
| 5586 | orph_cnt++; |
| 5587 | node = eb32_next(node); |
| 5588 | } |
| 5589 | |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5590 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5591 | " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d" |
| 5592 | " .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] | 5593 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5594 | 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] | 5595 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5596 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5597 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5598 | h2c->msi, |
| 5599 | (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf), |
| 5600 | (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf)); |
| 5601 | |
| 5602 | if (h2s) { |
| 5603 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5604 | h2s, h2s->id, h2s->flags, |
| 5605 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5606 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5607 | h2s->cs); |
| 5608 | if (h2s->cs) |
| 5609 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5610 | h2s->cs->flags, h2s->cs->data); |
| 5611 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5612 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5613 | |
| 5614 | /*******************************************************/ |
| 5615 | /* functions below are dedicated to the config parsers */ |
| 5616 | /*******************************************************/ |
| 5617 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5618 | /* config parser for global "tune.h2.header-table-size" */ |
| 5619 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5620 | struct proxy *defpx, const char *file, int line, |
| 5621 | char **err) |
| 5622 | { |
| 5623 | if (too_many_args(1, args, err, NULL)) |
| 5624 | return -1; |
| 5625 | |
| 5626 | h2_settings_header_table_size = atoi(args[1]); |
| 5627 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5628 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5629 | return -1; |
| 5630 | } |
| 5631 | return 0; |
| 5632 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5633 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5634 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5635 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5636 | struct proxy *defpx, const char *file, int line, |
| 5637 | char **err) |
| 5638 | { |
| 5639 | if (too_many_args(1, args, err, NULL)) |
| 5640 | return -1; |
| 5641 | |
| 5642 | h2_settings_initial_window_size = atoi(args[1]); |
| 5643 | if (h2_settings_initial_window_size < 0) { |
| 5644 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5645 | return -1; |
| 5646 | } |
| 5647 | return 0; |
| 5648 | } |
| 5649 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5650 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5651 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5652 | struct proxy *defpx, const char *file, int line, |
| 5653 | char **err) |
| 5654 | { |
| 5655 | if (too_many_args(1, args, err, NULL)) |
| 5656 | return -1; |
| 5657 | |
| 5658 | h2_settings_max_concurrent_streams = atoi(args[1]); |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 5659 | if ((int)h2_settings_max_concurrent_streams < 0) { |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5660 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5661 | return -1; |
| 5662 | } |
| 5663 | return 0; |
| 5664 | } |
| 5665 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5666 | /* config parser for global "tune.h2.max-frame-size" */ |
| 5667 | static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx, |
| 5668 | struct proxy *defpx, const char *file, int line, |
| 5669 | char **err) |
| 5670 | { |
| 5671 | if (too_many_args(1, args, err, NULL)) |
| 5672 | return -1; |
| 5673 | |
| 5674 | h2_settings_max_frame_size = atoi(args[1]); |
| 5675 | if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) { |
| 5676 | memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]); |
| 5677 | return -1; |
| 5678 | } |
| 5679 | return 0; |
| 5680 | } |
| 5681 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5682 | |
| 5683 | /****************************************/ |
| 5684 | /* MUX initialization and instanciation */ |
| 5685 | /***************************************/ |
| 5686 | |
| 5687 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5688 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5689 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5690 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5691 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5692 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5693 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5694 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5695 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5696 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5697 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5698 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5699 | .avail_streams = h2_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 5700 | .used_streams = h2_used_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5701 | .shutr = h2_shutr, |
| 5702 | .shutw = h2_shutw, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5703 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 5704 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5705 | .name = "H2", |
| 5706 | }; |
| 5707 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5708 | /* PROTO selection : this mux registers PROTO token "h2" */ |
| 5709 | static struct mux_proto_list mux_proto_h2 = |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5710 | { .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] | 5711 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5712 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 5713 | |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5714 | |
| 5715 | /* The mux operations */ |
| 5716 | static const struct mux_ops h2_htx_ops = { |
| 5717 | .init = h2_init, |
| 5718 | .wake = h2_wake, |
| 5719 | .snd_buf = h2_snd_buf, |
| 5720 | .rcv_buf = h2_rcv_buf, |
| 5721 | .subscribe = h2_subscribe, |
| 5722 | .unsubscribe = h2_unsubscribe, |
| 5723 | .attach = h2_attach, |
| 5724 | .get_first_cs = h2_get_first_cs, |
| 5725 | .detach = h2_detach, |
| 5726 | .destroy = h2_destroy, |
| 5727 | .avail_streams = h2_avail_streams, |
| 5728 | .used_streams = h2_used_streams, |
| 5729 | .shutr = h2_shutr, |
| 5730 | .shutw = h2_shutw, |
| 5731 | .show_fd = h2_show_fd, |
Christopher Faulet | 9f38f5a | 2019-04-03 09:53:32 +0200 | [diff] [blame] | 5732 | .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX, |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5733 | .name = "H2", |
| 5734 | }; |
| 5735 | |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5736 | static struct mux_proto_list mux_proto_h2_htx = |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5737 | { .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] | 5738 | |
| 5739 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx); |
| 5740 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5741 | /* config keyword parsers */ |
| 5742 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5743 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5744 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5745 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5746 | { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5747 | { 0, NULL, NULL } |
| 5748 | }}; |
| 5749 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5750 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |