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 | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 578 | if (t) |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 579 | task_destroy(t); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 580 | if (h2c->wait_event.task) |
| 581 | tasklet_free(h2c->wait_event.task); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 582 | pool_free(pool_head_h2c, h2c); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 583 | fail_no_h2c: |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 584 | return -1; |
| 585 | } |
| 586 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 587 | /* returns the next allocatable outgoing stream ID for the H2 connection, or |
| 588 | * -1 if no more is allocatable. |
| 589 | */ |
| 590 | static inline int32_t h2c_get_next_sid(const struct h2c *h2c) |
| 591 | { |
| 592 | int32_t id = (h2c->max_id + 1) | 1; |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 593 | |
| 594 | if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid)) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 595 | id = -1; |
| 596 | return id; |
| 597 | } |
| 598 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 599 | /* returns the stream associated with id <id> or NULL if not found */ |
| 600 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 601 | { |
| 602 | struct eb32_node *node; |
| 603 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 604 | if (id == 0) |
| 605 | return (struct h2s *)h2_closed_stream; |
| 606 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 607 | if (id > h2c->max_id) |
| 608 | return (struct h2s *)h2_idle_stream; |
| 609 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 610 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 611 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 612 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 613 | |
| 614 | return container_of(node, struct h2s, by_id); |
| 615 | } |
| 616 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 617 | /* release function. This one should be called to free all resources allocated |
| 618 | * to the mux. |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 619 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 620 | static void h2_release(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 621 | { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 622 | struct connection *conn = NULL;; |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 623 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 624 | if (h2c) { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 625 | /* The connection must be aattached to this mux to be released */ |
| 626 | if (h2c->conn && h2c->conn->ctx == h2c) |
| 627 | conn = h2c->conn; |
| 628 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 629 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 630 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 631 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 632 | LIST_DEL(&h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 633 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 634 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 635 | h2_release_buf(h2c, &h2c->dbuf); |
| 636 | h2_release_buf(h2c, &h2c->mbuf); |
| 637 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 638 | if (h2c->task) { |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 639 | h2c->task->context = NULL; |
| 640 | task_wakeup(h2c->task, TASK_WOKEN_OTHER); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 641 | h2c->task = NULL; |
| 642 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 643 | if (h2c->wait_event.task) |
| 644 | tasklet_free(h2c->wait_event.task); |
Olivier Houchard | 0e07937 | 2019-04-15 17:51:16 +0200 | [diff] [blame] | 645 | if (h2c->wait_event.events != 0) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 646 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events, |
Olivier Houchard | 0e07937 | 2019-04-15 17:51:16 +0200 | [diff] [blame] | 647 | &h2c->wait_event); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 648 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 649 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 650 | } |
| 651 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 652 | if (conn) { |
| 653 | conn->mux = NULL; |
| 654 | conn->ctx = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 655 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 656 | conn_stop_tracking(conn); |
| 657 | conn_full_close(conn); |
| 658 | if (conn->destroy_cb) |
| 659 | conn->destroy_cb(conn); |
| 660 | conn_free(conn); |
| 661 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 665 | /******************************************************/ |
| 666 | /* functions below are for the H2 protocol processing */ |
| 667 | /******************************************************/ |
| 668 | |
| 669 | /* 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] | 670 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 671 | { |
| 672 | return h2s ? h2s->id : 0; |
| 673 | } |
| 674 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 675 | /* 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] | 676 | 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] | 677 | { |
| 678 | if (h2c->msi < 0) |
| 679 | return 0; |
| 680 | |
| 681 | if (h2c->msi == h2s_id(h2s)) |
| 682 | return 0; |
| 683 | |
| 684 | return 1; |
| 685 | } |
| 686 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 687 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 688 | 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] | 689 | { |
| 690 | h2c->errcode = err; |
| 691 | h2c->st0 = H2_CS_ERROR; |
| 692 | } |
| 693 | |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 694 | /* marks an error on the stream. It may also update an already closed stream |
| 695 | * (e.g. to report an error after an RST was received). |
| 696 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 697 | 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] | 698 | { |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 699 | if (h2s->id && h2s->st != H2_SS_ERROR) { |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 700 | h2s->errcode = err; |
Willy Tarreau | 175cebb | 2019-01-24 10:02:24 +0100 | [diff] [blame] | 701 | if (h2s->st < H2_SS_ERROR) |
| 702 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 703 | if (h2s->cs) |
| 704 | cs_set_error(h2s->cs); |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 708 | /* attempt to notify the data layer of recv availability */ |
| 709 | static void __maybe_unused h2s_notify_recv(struct h2s *h2s) |
| 710 | { |
| 711 | struct wait_event *sw; |
| 712 | |
| 713 | if (h2s->recv_wait) { |
| 714 | sw = h2s->recv_wait; |
| 715 | sw->events &= ~SUB_RETRY_RECV; |
| 716 | tasklet_wakeup(sw->task); |
| 717 | h2s->recv_wait = NULL; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | /* attempt to notify the data layer of send availability */ |
| 722 | static void __maybe_unused h2s_notify_send(struct h2s *h2s) |
| 723 | { |
| 724 | struct wait_event *sw; |
| 725 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 726 | if (h2s->send_wait && LIST_ISEMPTY(&h2s->sending_list)) { |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 727 | sw = h2s->send_wait; |
| 728 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fd1e96d | 2019-03-25 14:04:25 +0100 | [diff] [blame] | 729 | LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 730 | tasklet_wakeup(sw->task); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 734 | /* alerts the data layer, trying to wake it up by all means, following |
| 735 | * this sequence : |
| 736 | * - if the h2s' data layer is subscribed to recv, then it's woken up for recv |
| 737 | * - if its subscribed to send, then it's woken up for send |
| 738 | * - if it was subscribed to neither, its ->wake() callback is called |
| 739 | * It is safe to call this function with a closed stream which doesn't have a |
| 740 | * conn_stream anymore. |
| 741 | */ |
| 742 | static void __maybe_unused h2s_alert(struct h2s *h2s) |
| 743 | { |
| 744 | if (h2s->recv_wait || h2s->send_wait) { |
| 745 | h2s_notify_recv(h2s); |
| 746 | h2s_notify_send(h2s); |
| 747 | } |
| 748 | else if (h2s->cs && h2s->cs->data_cb->wake != NULL) |
| 749 | h2s->cs->data_cb->wake(h2s->cs); |
| 750 | } |
| 751 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 752 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 753 | 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] | 754 | { |
| 755 | uint8_t *out = frame; |
| 756 | |
| 757 | *out = len >> 16; |
| 758 | write_n16(out + 1, len); |
| 759 | } |
| 760 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 761 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 762 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 763 | * 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] | 764 | * available in the buffer's input prior to calling this function. The buffer |
| 765 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 766 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 767 | 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] | 768 | const struct buffer *b, int o) |
| 769 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 770 | 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] | 771 | } |
| 772 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 773 | 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] | 774 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 775 | 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] | 776 | } |
| 777 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 778 | 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] | 779 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 780 | 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] | 781 | } |
| 782 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 783 | 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] | 784 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 785 | 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] | 786 | } |
| 787 | |
| 788 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 789 | /* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>. |
| 790 | * The algorithm is not obvious. It turns out that H2 headers are neither |
| 791 | * aligned nor do they use regular sizes. And to add to the trouble, the buffer |
| 792 | * 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] | 793 | * |
| 794 | * b0 b1 b2 b3 b4 b5..b8 |
| 795 | * +----------+---------+--------+----+----+----------------------+ |
| 796 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 797 | * +----------+---------+--------+----+----+----------------------+ |
| 798 | * |
| 799 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 800 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 801 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 802 | * 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] | 803 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 804 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 805 | */ |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 806 | 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] | 807 | { |
| 808 | uint64_t w; |
| 809 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 810 | if (b_data(b) < o + 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 811 | return 0; |
| 812 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 813 | w = h2_get_n64(b, o + 1); |
| 814 | h->len = *(uint8_t*)b_peek(b, o) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 815 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 816 | h->ff = w >> 32; |
| 817 | h->ft = w >> 40; |
| 818 | h->len += w >> 48; |
| 819 | return 1; |
| 820 | } |
| 821 | |
| 822 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 823 | * h2_peek_frame_hdr() above. |
| 824 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 825 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 826 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 827 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 831 | 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] | 832 | { |
| 833 | int ret; |
| 834 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 835 | ret = h2_peek_frame_hdr(b, 0, h); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 836 | if (ret > 0) |
| 837 | h2_skip_frame_hdr(b); |
| 838 | return ret; |
| 839 | } |
| 840 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 841 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 842 | * its connection if the stream was not yet closed. Please use this exclusively |
| 843 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 844 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 845 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 846 | { |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 847 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 848 | h2s->h2c->nb_streams--; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 849 | if (!h2s->id) |
| 850 | h2s->h2c->nb_reserved--; |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 851 | if (h2s->cs) { |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 852 | h2s->cs->flags |= CS_FL_REOS; |
Willy Tarreau | a27db38 | 2019-03-25 18:13:16 +0100 | [diff] [blame] | 853 | if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf)) |
| 854 | h2s_notify_recv(h2s); |
| 855 | } |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 856 | } |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 857 | h2s->st = H2_SS_CLOSED; |
| 858 | } |
| 859 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 860 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 861 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 862 | { |
| 863 | h2s_close(h2s); |
| 864 | eb32_delete(&h2s->by_id); |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 865 | if (b_size(&h2s->rxbuf)) { |
| 866 | b_free(&h2s->rxbuf); |
| 867 | offer_buffers(NULL, tasks_run_queue); |
| 868 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 869 | if (h2s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 870 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 871 | if (h2s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 872 | h2s->recv_wait->events &= ~SUB_RETRY_RECV; |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 873 | /* There's no need to explicitly call unsubscribe here, the only |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 874 | * reference left would be in the h2c send_list/fctl_list, and if |
| 875 | * we're in it, we're getting out anyway |
| 876 | */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 877 | LIST_DEL_INIT(&h2s->list); |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 878 | if (!LIST_ISEMPTY(&h2s->sending_list)) { |
| 879 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
| 880 | LIST_DEL_INIT(&h2s->sending_list); |
| 881 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 882 | tasklet_free(h2s->wait_event.task); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 883 | pool_free(pool_head_h2s, h2s); |
| 884 | } |
| 885 | |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 886 | /* allocates a new stream <id> for connection <h2c> and adds it into h2c's |
| 887 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 888 | * causes of errors can be any failed memory allocation. The caller is |
| 889 | * responsible for checking if the connection may support an extra stream |
| 890 | * prior to calling this function. |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 891 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 892 | static struct h2s *h2s_new(struct h2c *h2c, int id) |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 893 | { |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 894 | struct h2s *h2s; |
| 895 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 896 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 897 | if (!h2s) |
| 898 | goto out; |
| 899 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 900 | h2s->wait_event.task = tasklet_new(); |
| 901 | if (!h2s->wait_event.task) { |
| 902 | pool_free(pool_head_h2s, h2s); |
| 903 | goto out; |
| 904 | } |
| 905 | h2s->send_wait = NULL; |
| 906 | h2s->recv_wait = NULL; |
| 907 | h2s->wait_event.task->process = h2_deferred_shut; |
| 908 | h2s->wait_event.task->context = h2s; |
| 909 | h2s->wait_event.handle = NULL; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 910 | h2s->wait_event.events = 0; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 911 | LIST_INIT(&h2s->list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 912 | LIST_INIT(&h2s->sending_list); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 913 | h2s->h2c = h2c; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 914 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 915 | h2s->mws = h2c->miw; |
| 916 | h2s->flags = H2_SF_NONE; |
| 917 | h2s->errcode = H2_ERR_NO_ERROR; |
| 918 | h2s->st = H2_SS_IDLE; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 919 | h2s->status = 0; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 920 | h2s->body_len = 0; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 921 | h2s->rxbuf = BUF_NULL; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 922 | |
| 923 | if (h2c->flags & H2_CF_IS_BACK) { |
| 924 | h1m_init_req(&h2s->h1m); |
| 925 | h2s->h1m.err_pos = -1; // don't care about errors on the request path |
| 926 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 927 | } else { |
| 928 | h1m_init_res(&h2s->h1m); |
| 929 | h2s->h1m.err_pos = -1; // don't care about errors on the response path |
| 930 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 931 | } |
| 932 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 933 | h2s->by_id.key = h2s->id = id; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 934 | if (id > 0) |
| 935 | h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 936 | else |
| 937 | h2c->nb_reserved++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 938 | |
| 939 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 940 | h2c->nb_streams++; |
Willy Tarreau | e9634bd | 2019-01-23 10:25:10 +0100 | [diff] [blame] | 941 | h2c->stream_cnt++; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 942 | |
| 943 | return h2s; |
| 944 | |
| 945 | out_free_h2s: |
| 946 | pool_free(pool_head_h2s, h2s); |
| 947 | out: |
| 948 | return NULL; |
| 949 | } |
| 950 | |
| 951 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 952 | * case of memory allocation error. |
| 953 | */ |
| 954 | static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id) |
| 955 | { |
| 956 | struct session *sess = h2c->conn->owner; |
| 957 | struct conn_stream *cs; |
| 958 | struct h2s *h2s; |
| 959 | |
| 960 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 961 | goto out; |
| 962 | |
| 963 | h2s = h2s_new(h2c, id); |
| 964 | if (!h2s) |
| 965 | goto out; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 966 | |
| 967 | cs = cs_new(h2c->conn); |
| 968 | if (!cs) |
| 969 | goto out_close; |
| 970 | |
Olivier Houchard | 746fb77 | 2018-12-15 19:42:00 +0100 | [diff] [blame] | 971 | cs->flags |= CS_FL_NOT_FIRST; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 972 | h2s->cs = cs; |
| 973 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 974 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 975 | |
| 976 | if (stream_create_from_cs(cs) < 0) |
| 977 | goto out_free_cs; |
| 978 | |
Willy Tarreau | 590a051 | 2018-09-05 11:56:48 +0200 | [diff] [blame] | 979 | /* We want the accept date presented to the next stream to be the one |
| 980 | * we have now, the handshake time to be null (since the next stream |
| 981 | * is not delayed by a handshake), and the idle time to count since |
| 982 | * right now. |
| 983 | */ |
| 984 | sess->accept_date = date; |
| 985 | sess->tv_accept = now; |
| 986 | sess->t_handshake = 0; |
| 987 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 988 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 989 | if (h2_frt_has_too_many_cs(h2c)) |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 990 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 991 | return h2s; |
| 992 | |
| 993 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 994 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 995 | cs_free(cs); |
| 996 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 997 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 998 | out: |
Willy Tarreau | 45efc07 | 2018-10-03 18:27:52 +0200 | [diff] [blame] | 999 | sess_log(sess); |
| 1000 | return NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 1001 | } |
| 1002 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1003 | /* allocates a new stream associated to conn_stream <cs> on the h2c connection |
| 1004 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 1005 | * possible stream ID was reached. |
| 1006 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1007 | 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] | 1008 | { |
| 1009 | struct h2s *h2s = NULL; |
| 1010 | |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 1011 | if (h2c->nb_streams >= h2c->streams_limit) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1012 | goto out; |
| 1013 | |
Willy Tarreau | a80dca8 | 2019-01-24 17:08:28 +0100 | [diff] [blame] | 1014 | if (h2_streams_left(h2c) < 1) |
| 1015 | goto out; |
| 1016 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1017 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 1018 | h2s = h2s_new(h2c, 0); |
| 1019 | if (!h2s) |
| 1020 | goto out; |
| 1021 | |
| 1022 | h2s->cs = cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1023 | h2s->sess = sess; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1024 | cs->ctx = h2s; |
| 1025 | h2c->nb_cs++; |
| 1026 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 1027 | out: |
| 1028 | return h2s; |
| 1029 | } |
| 1030 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1031 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 1032 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 1033 | * the various settings codes. |
| 1034 | */ |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1035 | static int h2c_send_settings(struct h2c *h2c) |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1036 | { |
| 1037 | struct buffer *res; |
| 1038 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1039 | struct buffer buf; |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1040 | int mfs; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1041 | int ret; |
| 1042 | |
| 1043 | if (h2c_mux_busy(h2c, NULL)) { |
| 1044 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1048 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1049 | if (!res) { |
| 1050 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1051 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1052 | return 0; |
| 1053 | } |
| 1054 | |
| 1055 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 1056 | chunk_memcpy(&buf, |
| 1057 | "\x00\x00\x00" /* length : 0 for now */ |
| 1058 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 1059 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 1060 | 9); |
| 1061 | |
Willy Tarreau | 0bbad6b | 2019-02-26 16:01:52 +0100 | [diff] [blame] | 1062 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1063 | /* send settings_enable_push=0 */ |
| 1064 | chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6); |
| 1065 | } |
| 1066 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1067 | if (h2_settings_header_table_size != 4096) { |
| 1068 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 1069 | |
| 1070 | write_n32(str + 2, h2_settings_header_table_size); |
| 1071 | chunk_memcat(&buf, str, 6); |
| 1072 | } |
| 1073 | |
| 1074 | if (h2_settings_initial_window_size != 65535) { |
| 1075 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 1076 | |
| 1077 | write_n32(str + 2, h2_settings_initial_window_size); |
| 1078 | chunk_memcat(&buf, str, 6); |
| 1079 | } |
| 1080 | |
| 1081 | if (h2_settings_max_concurrent_streams != 0) { |
| 1082 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 1083 | |
| 1084 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 1085 | * the protocol, so never send this value! |
| 1086 | */ |
| 1087 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 1088 | chunk_memcat(&buf, str, 6); |
| 1089 | } |
| 1090 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1091 | mfs = h2_settings_max_frame_size; |
| 1092 | if (mfs > global.tune.bufsize) |
| 1093 | mfs = global.tune.bufsize; |
| 1094 | |
| 1095 | if (!mfs) |
| 1096 | mfs = global.tune.bufsize; |
| 1097 | |
| 1098 | if (mfs != 16384) { |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1099 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 1100 | |
| 1101 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 1102 | * match bufsize - rewrite size, but at the moment it seems |
| 1103 | * that clients don't take care of it. |
| 1104 | */ |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 1105 | write_n32(str + 2, mfs); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1106 | chunk_memcat(&buf, str, 6); |
| 1107 | } |
| 1108 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1109 | h2_set_frame_size(buf.area, buf.data - 9); |
| 1110 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1111 | if (unlikely(ret <= 0)) { |
| 1112 | if (!ret) { |
| 1113 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1114 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1115 | return 0; |
| 1116 | } |
| 1117 | else { |
| 1118 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1119 | return 0; |
| 1120 | } |
| 1121 | } |
| 1122 | return ret; |
| 1123 | } |
| 1124 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1125 | /* Try to receive a connection preface, then upon success try to send our |
| 1126 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1127 | * missing data. It may return an error in h2c. |
| 1128 | */ |
| 1129 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 1130 | { |
| 1131 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1132 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1133 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1134 | 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] | 1135 | |
| 1136 | if (unlikely(ret1 <= 0)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1137 | if (ret1 < 0) |
| 1138 | sess_log(h2c->conn->owner); |
| 1139 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1140 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 1141 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1145 | ret2 = h2c_send_settings(h2c); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1146 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1147 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1148 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1149 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1150 | } |
| 1151 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1152 | /* Try to send a connection preface, then upon success try to send our |
| 1153 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1154 | * missing data. It may return an error in h2c. |
| 1155 | */ |
| 1156 | static int h2c_bck_send_preface(struct h2c *h2c) |
| 1157 | { |
| 1158 | struct buffer *res; |
| 1159 | |
| 1160 | if (h2c_mux_busy(h2c, NULL)) { |
| 1161 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | res = h2_get_buf(h2c, &h2c->mbuf); |
| 1166 | if (!res) { |
| 1167 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1168 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | if (!b_data(res)) { |
| 1173 | /* preface not yet sent */ |
| 1174 | b_istput(res, ist(H2_CONN_PREFACE)); |
| 1175 | } |
| 1176 | |
| 1177 | return h2c_send_settings(h2c); |
| 1178 | } |
| 1179 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1180 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 1181 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 1182 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 1183 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 1184 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 1185 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 1186 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 1187 | * case so that it is safe to use h2c_error() to report such errors. |
| 1188 | */ |
| 1189 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 1190 | { |
| 1191 | struct buffer *res; |
| 1192 | char str[17]; |
| 1193 | int ret; |
| 1194 | |
| 1195 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 1196 | return 1; // claim that it worked |
| 1197 | |
| 1198 | if (h2c_mux_busy(h2c, h2s)) { |
| 1199 | if (h2s) |
| 1200 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1201 | else |
| 1202 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1203 | return 0; |
| 1204 | } |
| 1205 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1206 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1207 | if (!res) { |
| 1208 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1209 | if (h2s) |
| 1210 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1211 | else |
| 1212 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 1217 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 1218 | |
| 1219 | if (h2c->last_sid < 0) |
| 1220 | h2c->last_sid = h2c->max_id; |
| 1221 | |
| 1222 | write_n32(str + 9, h2c->last_sid); |
| 1223 | write_n32(str + 13, h2c->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1224 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1225 | if (unlikely(ret <= 0)) { |
| 1226 | if (!ret) { |
| 1227 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1228 | if (h2s) |
| 1229 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1230 | else |
| 1231 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1232 | return 0; |
| 1233 | } |
| 1234 | else { |
| 1235 | /* we cannot report this error using GOAWAY, so we mark |
| 1236 | * it and claim a success. |
| 1237 | */ |
| 1238 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1239 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 1240 | return 1; |
| 1241 | } |
| 1242 | } |
| 1243 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 1244 | return ret; |
| 1245 | } |
| 1246 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1247 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 1248 | * during mux operations. This stream must be valid and cannot be closed |
| 1249 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 1250 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 1251 | * not yet. |
| 1252 | * |
| 1253 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1254 | * to write the message, it subscribes the stream to future notifications. |
| 1255 | */ |
| 1256 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1257 | { |
| 1258 | struct buffer *res; |
| 1259 | char str[13]; |
| 1260 | int ret; |
| 1261 | |
| 1262 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 1263 | return 1; |
| 1264 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1265 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1266 | * RST_STREAM in response to a RST_STREAM frame. |
| 1267 | */ |
| 1268 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1269 | ret = 1; |
| 1270 | goto ignore; |
| 1271 | } |
| 1272 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1273 | if (h2c_mux_busy(h2c, h2s)) { |
| 1274 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1278 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1279 | if (!res) { |
| 1280 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1281 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | /* len: 4, type: 3, flags: none */ |
| 1286 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1287 | write_n32(str + 5, h2s->id); |
| 1288 | write_n32(str + 9, h2s->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1289 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1290 | |
| 1291 | if (unlikely(ret <= 0)) { |
| 1292 | if (!ret) { |
| 1293 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1294 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1295 | return 0; |
| 1296 | } |
| 1297 | else { |
| 1298 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1299 | return 0; |
| 1300 | } |
| 1301 | } |
| 1302 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1303 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1304 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1305 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1306 | return ret; |
| 1307 | } |
| 1308 | |
| 1309 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 1310 | * 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] | 1311 | * error code, even if the stream is one of the dummy ones, and will update |
| 1312 | * h2s->st to H2_SS_CLOSED if it was not yet. |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1313 | * |
| 1314 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1315 | * 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] | 1316 | * 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] | 1317 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1318 | */ |
| 1319 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1320 | { |
| 1321 | struct buffer *res; |
| 1322 | char str[13]; |
| 1323 | int ret; |
| 1324 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1325 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1326 | * RST_STREAM in response to a RST_STREAM frame. |
| 1327 | */ |
| 1328 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1329 | ret = 1; |
| 1330 | goto ignore; |
| 1331 | } |
| 1332 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1333 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1334 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1335 | return 0; |
| 1336 | } |
| 1337 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1338 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1339 | if (!res) { |
| 1340 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1341 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1342 | return 0; |
| 1343 | } |
| 1344 | |
| 1345 | /* len: 4, type: 3, flags: none */ |
| 1346 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1347 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1348 | write_n32(str + 5, h2c->dsi); |
Willy Tarreau | e6888ff | 2018-12-23 18:26:26 +0100 | [diff] [blame] | 1349 | write_n32(str + 9, h2s->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1350 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1351 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1352 | if (unlikely(ret <= 0)) { |
| 1353 | if (!ret) { |
| 1354 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1355 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1356 | return 0; |
| 1357 | } |
| 1358 | else { |
| 1359 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1360 | return 0; |
| 1361 | } |
| 1362 | } |
| 1363 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1364 | ignore: |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1365 | if (h2s->id) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1366 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1367 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1368 | } |
| 1369 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1370 | return ret; |
| 1371 | } |
| 1372 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1373 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 1374 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 1375 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 1376 | * on success or zero if nothing was done. In case of lack of room to write the |
| 1377 | * message, it subscribes the requesting stream to future notifications. |
| 1378 | */ |
| 1379 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 1380 | { |
| 1381 | struct h2c *h2c = h2s->h2c; |
| 1382 | struct buffer *res; |
| 1383 | char str[9]; |
| 1384 | int ret; |
| 1385 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1386 | 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] | 1387 | return 1; |
| 1388 | |
| 1389 | if (h2c_mux_busy(h2c, h2s)) { |
| 1390 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1391 | return 0; |
| 1392 | } |
| 1393 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1394 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1395 | if (!res) { |
| 1396 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1397 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
| 1401 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1402 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1403 | write_n32(str + 5, h2s->id); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1404 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1405 | if (likely(ret > 0)) { |
| 1406 | h2s->flags |= H2_SF_ES_SENT; |
| 1407 | } |
| 1408 | else if (!ret) { |
| 1409 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1410 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1411 | return 0; |
| 1412 | } |
| 1413 | else { |
| 1414 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1415 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1416 | } |
| 1417 | return ret; |
| 1418 | } |
| 1419 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1420 | /* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags> |
| 1421 | * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close |
| 1422 | * connection. The stream's state is automatically updated accordingly. If the |
| 1423 | * stream is orphaned, it is destroyed. |
| 1424 | */ |
| 1425 | static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags) |
| 1426 | { |
| 1427 | if (!h2s->cs) { |
| 1428 | /* this stream was already orphaned */ |
| 1429 | h2s_destroy(h2s); |
| 1430 | return; |
| 1431 | } |
| 1432 | |
| 1433 | h2s->cs->flags |= flags; |
| 1434 | if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS)) |
| 1435 | h2s->cs->flags |= CS_FL_ERROR; |
| 1436 | |
| 1437 | h2s_alert(h2s); |
| 1438 | |
| 1439 | if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR) |
| 1440 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 1441 | 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] | 1442 | h2s->st = H2_SS_HREM; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 1443 | 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] | 1444 | h2s_close(h2s); |
| 1445 | } |
| 1446 | |
| 1447 | /* wake the streams attached to the connection, whose id is greater than <last> |
| 1448 | * or unassigned. |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1449 | */ |
| 1450 | static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags) |
| 1451 | { |
| 1452 | struct eb32_node *node; |
| 1453 | struct h2s *h2s; |
| 1454 | |
| 1455 | if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR) |
Willy Tarreau | a851935 | 2018-12-18 16:44:28 +0100 | [diff] [blame] | 1456 | flags |= CS_FL_ERR_PENDING; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1457 | |
| 1458 | if (conn_xprt_read0_pending(h2c->conn)) |
Christopher Faulet | 87a8f35 | 2019-03-22 14:51:36 +0100 | [diff] [blame] | 1459 | flags |= CS_FL_REOS; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1460 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1461 | /* Wake all streams with ID > last */ |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1462 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1463 | while (node) { |
| 1464 | h2s = container_of(node, struct h2s, by_id); |
| 1465 | if (h2s->id <= last) |
| 1466 | break; |
| 1467 | node = eb32_next(node); |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1468 | h2s_wake_one_stream(h2s, flags); |
| 1469 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1470 | |
Christopher Faulet | f02ca00 | 2019-03-07 16:21:34 +0100 | [diff] [blame] | 1471 | /* Wake all streams with unassigned ID (ID == 0) */ |
| 1472 | node = eb32_lookup(&h2c->streams_by_id, 0); |
| 1473 | while (node) { |
| 1474 | h2s = container_of(node, struct h2s, by_id); |
| 1475 | if (h2s->id > 0) |
| 1476 | break; |
| 1477 | node = eb32_next(node); |
| 1478 | h2s_wake_one_stream(h2s, flags); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1479 | } |
| 1480 | } |
| 1481 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1482 | /* Increase all streams' outgoing window size by the difference passed in |
| 1483 | * argument. This is needed upon receipt of the settings frame if the initial |
| 1484 | * window size is different. The difference may be negative and the resulting |
| 1485 | * window size as well, for the time it takes to receive some window updates. |
| 1486 | */ |
| 1487 | static void h2c_update_all_ws(struct h2c *h2c, int diff) |
| 1488 | { |
| 1489 | struct h2s *h2s; |
| 1490 | struct eb32_node *node; |
| 1491 | |
| 1492 | if (!diff) |
| 1493 | return; |
| 1494 | |
| 1495 | node = eb32_first(&h2c->streams_by_id); |
| 1496 | while (node) { |
| 1497 | h2s = container_of(node, struct h2s, by_id); |
| 1498 | h2s->mws += diff; |
Willy Tarreau | b1c9edc | 2019-01-30 16:11:20 +0100 | [diff] [blame] | 1499 | |
| 1500 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1501 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
| 1502 | if (h2s->send_wait) |
| 1503 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 1504 | |
| 1505 | } |
| 1506 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1507 | node = eb32_next(node); |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1512 | * 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] | 1513 | * return an error in h2c. The caller must have already verified frame length |
| 1514 | * and stream ID validity. Described in RFC7540#6.5. |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1515 | */ |
| 1516 | static int h2c_handle_settings(struct h2c *h2c) |
| 1517 | { |
| 1518 | unsigned int offset; |
| 1519 | int error; |
| 1520 | |
| 1521 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1522 | if (h2c->dfl) { |
| 1523 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1524 | goto fail; |
| 1525 | } |
| 1526 | return 1; |
| 1527 | } |
| 1528 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1529 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1530 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1531 | return 0; |
| 1532 | |
| 1533 | /* parse the frame */ |
| 1534 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1535 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1536 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1537 | |
| 1538 | switch (type) { |
| 1539 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1540 | /* we need to update all existing streams with the |
| 1541 | * difference from the previous iws. |
| 1542 | */ |
| 1543 | if (arg < 0) { // RFC7540#6.5.2 |
| 1544 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1545 | goto fail; |
| 1546 | } |
| 1547 | h2c_update_all_ws(h2c, arg - h2c->miw); |
| 1548 | h2c->miw = arg; |
| 1549 | break; |
| 1550 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1551 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1552 | error = H2_ERR_PROTOCOL_ERROR; |
| 1553 | goto fail; |
| 1554 | } |
| 1555 | h2c->mfs = arg; |
| 1556 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1557 | case H2_SETTINGS_ENABLE_PUSH: |
| 1558 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1559 | error = H2_ERR_PROTOCOL_ERROR; |
| 1560 | goto fail; |
| 1561 | } |
| 1562 | break; |
Willy Tarreau | 2e2083a | 2019-01-31 10:34:07 +0100 | [diff] [blame] | 1563 | case H2_SETTINGS_MAX_CONCURRENT_STREAMS: |
| 1564 | if (h2c->flags & H2_CF_IS_BACK) { |
| 1565 | /* the limit is only for the backend; for the frontend it is our limit */ |
| 1566 | if ((unsigned int)arg > h2_settings_max_concurrent_streams) |
| 1567 | arg = h2_settings_max_concurrent_streams; |
| 1568 | h2c->streams_limit = arg; |
| 1569 | } |
| 1570 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | /* need to ACK this frame now */ |
| 1575 | h2c->st0 = H2_CS_FRAME_A; |
| 1576 | return 1; |
| 1577 | fail: |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1578 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1579 | h2c_error(h2c, error); |
| 1580 | return 0; |
| 1581 | } |
| 1582 | |
| 1583 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1584 | * success or one of the h2_status values. |
| 1585 | */ |
| 1586 | static int h2c_ack_settings(struct h2c *h2c) |
| 1587 | { |
| 1588 | struct buffer *res; |
| 1589 | char str[9]; |
| 1590 | int ret = -1; |
| 1591 | |
| 1592 | if (h2c_mux_busy(h2c, NULL)) { |
| 1593 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1594 | return 0; |
| 1595 | } |
| 1596 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1597 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1598 | if (!res) { |
| 1599 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1600 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1601 | return 0; |
| 1602 | } |
| 1603 | |
| 1604 | memcpy(str, |
| 1605 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1606 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1607 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1608 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1609 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1610 | if (unlikely(ret <= 0)) { |
| 1611 | if (!ret) { |
| 1612 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1613 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1614 | return 0; |
| 1615 | } |
| 1616 | else { |
| 1617 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1618 | return 0; |
| 1619 | } |
| 1620 | } |
| 1621 | return ret; |
| 1622 | } |
| 1623 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1624 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1625 | * 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] | 1626 | * missing data. The caller must have already verified frame length |
| 1627 | * and stream ID validity. |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1628 | */ |
| 1629 | static int h2c_handle_ping(struct h2c *h2c) |
| 1630 | { |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1631 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1632 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1633 | h2c->st0 = H2_CS_FRAME_A; |
| 1634 | return 1; |
| 1635 | } |
| 1636 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1637 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1638 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1639 | * error in h2c. |
| 1640 | */ |
| 1641 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1642 | { |
| 1643 | struct buffer *res; |
| 1644 | char str[13]; |
| 1645 | int ret = -1; |
| 1646 | |
| 1647 | if (h2c_mux_busy(h2c, NULL)) { |
| 1648 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1649 | return 0; |
| 1650 | } |
| 1651 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1652 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1653 | if (!res) { |
| 1654 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1655 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1656 | return 0; |
| 1657 | } |
| 1658 | |
| 1659 | /* length: 4, type: 8, flags: none */ |
| 1660 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1661 | write_n32(str + 5, sid); |
| 1662 | write_n32(str + 9, increment); |
| 1663 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1664 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1665 | |
| 1666 | if (unlikely(ret <= 0)) { |
| 1667 | if (!ret) { |
| 1668 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1669 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1670 | return 0; |
| 1671 | } |
| 1672 | else { |
| 1673 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1674 | return 0; |
| 1675 | } |
| 1676 | } |
| 1677 | return ret; |
| 1678 | } |
| 1679 | |
| 1680 | /* try to send pending window update for the connection. It's safe to call it |
| 1681 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1682 | * failure. It may return an error in h2c. |
| 1683 | */ |
| 1684 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1685 | { |
| 1686 | int ret = 1; |
| 1687 | |
| 1688 | if (h2c->rcvd_c <= 0) |
| 1689 | return 1; |
| 1690 | |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 1691 | if (!(h2c->flags & H2_CF_WINDOW_OPENED)) { |
| 1692 | /* increase the advertised connection window to 2G on |
| 1693 | * first update. |
| 1694 | */ |
| 1695 | h2c->flags |= H2_CF_WINDOW_OPENED; |
| 1696 | h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT; |
| 1697 | } |
| 1698 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1699 | /* send WU for the connection */ |
| 1700 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1701 | if (ret > 0) |
| 1702 | h2c->rcvd_c = 0; |
| 1703 | |
| 1704 | return ret; |
| 1705 | } |
| 1706 | |
| 1707 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1708 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1709 | * room or failure. It may return an error in h2c. |
| 1710 | */ |
| 1711 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1712 | { |
| 1713 | int ret = 1; |
| 1714 | |
| 1715 | if (h2c->rcvd_s <= 0) |
| 1716 | return 1; |
| 1717 | |
| 1718 | /* send WU for the stream */ |
| 1719 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1720 | if (ret > 0) |
| 1721 | h2c->rcvd_s = 0; |
| 1722 | |
| 1723 | return ret; |
| 1724 | } |
| 1725 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1726 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1727 | * success, 0 on missing data or one of the h2_status values. |
| 1728 | */ |
| 1729 | static int h2c_ack_ping(struct h2c *h2c) |
| 1730 | { |
| 1731 | struct buffer *res; |
| 1732 | char str[17]; |
| 1733 | int ret = -1; |
| 1734 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1735 | if (b_data(&h2c->dbuf) < 8) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1736 | return 0; |
| 1737 | |
| 1738 | if (h2c_mux_busy(h2c, NULL)) { |
| 1739 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1740 | return 0; |
| 1741 | } |
| 1742 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1743 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1744 | if (!res) { |
| 1745 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1746 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1747 | return 0; |
| 1748 | } |
| 1749 | |
| 1750 | memcpy(str, |
| 1751 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1752 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1753 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1754 | |
| 1755 | /* copy the original payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1756 | h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1757 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1758 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1759 | if (unlikely(ret <= 0)) { |
| 1760 | if (!ret) { |
| 1761 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1762 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1763 | return 0; |
| 1764 | } |
| 1765 | else { |
| 1766 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1767 | return 0; |
| 1768 | } |
| 1769 | } |
| 1770 | return ret; |
| 1771 | } |
| 1772 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1773 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1774 | * 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] | 1775 | * h2c or h2s. The caller must have already verified frame length and stream ID |
| 1776 | * validity. Described in RFC7540#6.9. |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1777 | */ |
| 1778 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1779 | { |
| 1780 | int32_t inc; |
| 1781 | int error; |
| 1782 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1783 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1784 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1785 | return 0; |
| 1786 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1787 | inc = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1788 | |
| 1789 | if (h2c->dsi != 0) { |
| 1790 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1791 | |
| 1792 | /* it's not an error to receive WU on a closed stream */ |
| 1793 | if (h2s->st == H2_SS_CLOSED) |
| 1794 | return 1; |
| 1795 | |
| 1796 | if (!inc) { |
| 1797 | error = H2_ERR_PROTOCOL_ERROR; |
| 1798 | goto strm_err; |
| 1799 | } |
| 1800 | |
| 1801 | if (h2s->mws >= 0 && h2s->mws + inc < 0) { |
| 1802 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1803 | goto strm_err; |
| 1804 | } |
| 1805 | |
| 1806 | h2s->mws += inc; |
| 1807 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1808 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 1809 | if (h2s->send_wait) |
| 1810 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 1811 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1812 | } |
| 1813 | } |
| 1814 | else { |
| 1815 | /* connection window update */ |
| 1816 | if (!inc) { |
| 1817 | error = H2_ERR_PROTOCOL_ERROR; |
| 1818 | goto conn_err; |
| 1819 | } |
| 1820 | |
| 1821 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1822 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1823 | goto conn_err; |
| 1824 | } |
| 1825 | |
| 1826 | h2c->mws += inc; |
| 1827 | } |
| 1828 | |
| 1829 | return 1; |
| 1830 | |
| 1831 | conn_err: |
| 1832 | h2c_error(h2c, error); |
| 1833 | return 0; |
| 1834 | |
| 1835 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 1836 | h2s_error(h2s, error); |
| 1837 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1838 | return 0; |
| 1839 | } |
| 1840 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1841 | /* 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] | 1842 | * the last ID. Returns > 0 on success or zero on missing data. The caller must |
| 1843 | * have already verified frame length and stream ID validity. Described in |
| 1844 | * RFC7540#6.8. |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1845 | */ |
| 1846 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1847 | { |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1848 | int last; |
| 1849 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1850 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1851 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1852 | return 0; |
| 1853 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1854 | last = h2_get_n32(&h2c->dbuf, 0); |
| 1855 | h2c->errcode = h2_get_n32(&h2c->dbuf, 4); |
Olivier Houchard | 9117780 | 2018-12-19 14:49:39 +0100 | [diff] [blame] | 1856 | h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1857 | if (h2c->last_sid < 0) |
| 1858 | h2c->last_sid = last; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1859 | return 1; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1860 | } |
| 1861 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1862 | /* 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] | 1863 | * invalid. Returns > 0 on success or zero on missing data. It may return an |
| 1864 | * error in h2c. The caller must have already verified frame length and stream |
| 1865 | * ID validity. Described in RFC7540#6.3. |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1866 | */ |
| 1867 | static int h2c_handle_priority(struct h2c *h2c) |
| 1868 | { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1869 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1870 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1871 | return 0; |
| 1872 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1873 | if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1874 | /* 7540#5.3 : can't depend on itself */ |
Willy Tarreau | b860c73 | 2019-01-30 15:39:55 +0100 | [diff] [blame] | 1875 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1876 | return 0; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1877 | } |
| 1878 | return 1; |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1879 | } |
| 1880 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1881 | /* 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] | 1882 | * Returns > 0 on success or zero on missing data. The caller must have already |
| 1883 | * verified frame length and stream ID validity. Described in RFC7540#6.4. |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1884 | */ |
| 1885 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1886 | { |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1887 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1888 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1889 | return 0; |
| 1890 | |
| 1891 | /* late RST, already handled */ |
| 1892 | if (h2s->st == H2_SS_CLOSED) |
| 1893 | return 1; |
| 1894 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1895 | h2s->errcode = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1896 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1897 | |
| 1898 | if (h2s->cs) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 1899 | cs_set_error(h2s->cs); |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 1900 | h2s_alert(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | h2s->flags |= H2_SF_RST_RCVD; |
| 1904 | return 1; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1905 | } |
| 1906 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1907 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 1908 | * It may return an error in h2c or h2s. The caller must consider that the |
| 1909 | * return value is the new h2s in case one was allocated (most common case). |
| 1910 | * Described in RFC7540#6.2. Most of the |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1911 | * errors here are reported as connection errors since it's impossible to |
| 1912 | * recover from such errors after the compression context has been altered. |
| 1913 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1914 | 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] | 1915 | { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1916 | struct buffer rxbuf = BUF_NULL; |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1917 | unsigned long long body_len = 0; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1918 | uint32_t flags = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1919 | int error; |
| 1920 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1921 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1922 | return NULL; // empty buffer |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1923 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1924 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1925 | return NULL; // incomplete frame |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1926 | |
| 1927 | /* now either the frame is complete or the buffer is complete */ |
| 1928 | if (h2s->st != H2_SS_IDLE) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1929 | /* The stream exists/existed, this must be a trailers frame */ |
| 1930 | if (h2s->st != H2_SS_CLOSED) { |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1931 | if (h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len) <= 0) |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1932 | goto out; |
| 1933 | goto done; |
| 1934 | } |
Willy Tarreau | 1f03550 | 2019-01-30 11:44:07 +0100 | [diff] [blame] | 1935 | /* the connection was already killed by an RST, let's consume |
| 1936 | * the data and send another RST. |
| 1937 | */ |
| 1938 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
| 1939 | h2s = (struct h2s*)h2_error_stream; |
| 1940 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1941 | } |
| 1942 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 1943 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 1944 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1945 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1946 | goto conn_err; |
| 1947 | } |
Willy Tarreau | 415b1ee | 2019-01-02 13:59:43 +0100 | [diff] [blame] | 1948 | else if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 1949 | goto out; // IDLE but too many cs still present |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1950 | |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 1951 | error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1952 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1953 | /* unrecoverable error ? */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1954 | if (h2c->st0 >= H2_CS_ERROR) |
| 1955 | goto out; |
| 1956 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1957 | if (error <= 0) { |
| 1958 | if (error == 0) |
| 1959 | goto out; // missing data |
| 1960 | |
| 1961 | /* Failed to decode this stream (e.g. too large request) |
| 1962 | * but the HPACK decompressor is still synchronized. |
| 1963 | */ |
| 1964 | h2s = (struct h2s*)h2_error_stream; |
| 1965 | goto send_rst; |
| 1966 | } |
| 1967 | |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1968 | /* 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] | 1969 | * positively from h2c_frt_stream_new(), the stream will report the error, |
| 1970 | * 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] | 1971 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 1972 | h2s = h2c_frt_stream_new(h2c, h2c->dsi); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1973 | if (!h2s) { |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 1974 | h2s = (struct h2s*)h2_refused_stream; |
| 1975 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | h2s->st = H2_SS_OPEN; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1979 | h2s->rxbuf = rxbuf; |
| 1980 | h2s->flags |= flags; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 1981 | h2s->body_len = body_len; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1982 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1983 | done: |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1984 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1985 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1986 | |
| 1987 | if (h2s->flags & H2_SF_ES_RCVD) { |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 1988 | if (h2s->cs) |
| 1989 | h2s->cs->flags |= CS_FL_EOI; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 1990 | if (h2s->st == H2_SS_OPEN) |
| 1991 | h2s->st = H2_SS_HREM; |
| 1992 | else |
| 1993 | h2s_close(h2s); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1994 | } |
| 1995 | |
Willy Tarreau | 3a429f0 | 2019-01-03 11:41:50 +0100 | [diff] [blame] | 1996 | /* update the max stream ID if the request is being processed */ |
| 1997 | if (h2s->id > h2c->max_id) |
| 1998 | h2c->max_id = h2s->id; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1999 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2000 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2001 | |
| 2002 | conn_err: |
| 2003 | h2c_error(h2c, error); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2004 | goto out; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2005 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 2006 | out: |
| 2007 | h2_release_buf(h2c, &rxbuf); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2008 | return NULL; |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 2009 | |
| 2010 | send_rst: |
| 2011 | /* make the demux send an RST for the current stream. We may only |
| 2012 | * do this if we're certain that the HEADERS frame was properly |
| 2013 | * decompressed so that the HPACK decoder is still kept up to date. |
| 2014 | */ |
| 2015 | h2_release_buf(h2c, &rxbuf); |
| 2016 | h2c->st0 = H2_CS_FRAME_E; |
| 2017 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2018 | } |
| 2019 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2020 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 2021 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 2022 | * errors here are reported as connection errors since it's impossible to |
| 2023 | * recover from such errors after the compression context has been altered. |
| 2024 | */ |
| 2025 | static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 2026 | { |
| 2027 | int error; |
| 2028 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2029 | if (!b_size(&h2c->dbuf)) |
| 2030 | return NULL; // empty buffer |
| 2031 | |
| 2032 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
| 2033 | return NULL; // incomplete frame |
| 2034 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2035 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2036 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2037 | /* unrecoverable error ? */ |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2038 | if (h2c->st0 >= H2_CS_ERROR) |
| 2039 | return NULL; |
| 2040 | |
Willy Tarreau | 08bb1d6 | 2019-01-30 16:55:48 +0100 | [diff] [blame] | 2041 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2042 | /* RFC7540#5.1 */ |
| 2043 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2044 | h2c->st0 = H2_CS_FRAME_E; |
| 2045 | return NULL; |
| 2046 | } |
| 2047 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2048 | if (error <= 0) { |
| 2049 | if (error == 0) |
| 2050 | return NULL; // missing data |
| 2051 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2052 | /* stream error : send RST_STREAM */ |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2053 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2054 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 2055 | return NULL; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2056 | } |
| 2057 | |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2058 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 2059 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2060 | if (h2s->cs) |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2061 | h2s->cs->flags |= CS_FL_EOI; |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 2062 | } |
| 2063 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 2064 | 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] | 2065 | h2s->st = H2_SS_ERROR; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2066 | 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] | 2067 | h2s->st = H2_SS_HREM; |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2068 | else if ((!h2s || h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC) |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2069 | h2s_close(h2s); |
| 2070 | |
| 2071 | return h2s; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2072 | } |
| 2073 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2074 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 2075 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 2076 | */ |
| 2077 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 2078 | { |
| 2079 | int error; |
| 2080 | |
| 2081 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 2082 | * to signal an end of stream (with the ES flag). |
| 2083 | */ |
| 2084 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2085 | if (!b_size(&h2c->dbuf) && h2c->dfl) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2086 | return 0; // empty buffer |
| 2087 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2088 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2089 | return 0; // incomplete frame |
| 2090 | |
| 2091 | /* now either the frame is complete or the buffer is complete */ |
| 2092 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2093 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2094 | /* RFC7540#6.1 */ |
| 2095 | error = H2_ERR_STREAM_CLOSED; |
| 2096 | goto strm_err; |
| 2097 | } |
| 2098 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2099 | if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) { |
| 2100 | /* RFC7540#8.1.2 */ |
| 2101 | error = H2_ERR_PROTOCOL_ERROR; |
| 2102 | goto strm_err; |
| 2103 | } |
| 2104 | |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 2105 | if (!h2_frt_transfer_data(h2s)) |
| 2106 | return 0; |
| 2107 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2108 | /* call the upper layers to process the frame, then let the upper layer |
| 2109 | * notify the stream about any change. |
| 2110 | */ |
| 2111 | if (!h2s->cs) { |
| 2112 | error = H2_ERR_STREAM_CLOSED; |
| 2113 | goto strm_err; |
| 2114 | } |
| 2115 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 2116 | if (h2c->st0 >= H2_CS_ERROR) |
| 2117 | return 0; |
| 2118 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2119 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2120 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2121 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | /* check for completion : the callee will change this to FRAME_A or |
| 2125 | * FRAME_H once done. |
| 2126 | */ |
| 2127 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2128 | return 0; |
| 2129 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2130 | /* last frame */ |
| 2131 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2132 | if (h2s->cs) |
| 2133 | h2s->cs->flags |= CS_FL_EOI; |
Willy Tarreau | fc10f59 | 2019-01-30 19:28:32 +0100 | [diff] [blame] | 2134 | if (h2s->st == H2_SS_OPEN) |
| 2135 | h2s->st = H2_SS_HREM; |
| 2136 | else |
| 2137 | h2s_close(h2s); |
| 2138 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2139 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 2140 | |
| 2141 | if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) { |
| 2142 | /* RFC7540#8.1.2 */ |
| 2143 | error = H2_ERR_PROTOCOL_ERROR; |
| 2144 | goto strm_err; |
| 2145 | } |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2146 | } |
| 2147 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2148 | return 1; |
| 2149 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2150 | strm_err: |
Willy Tarreau | 6432dc8 | 2019-01-30 15:42:44 +0100 | [diff] [blame] | 2151 | h2s_error(h2s, error); |
| 2152 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2153 | return 0; |
| 2154 | } |
| 2155 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2156 | /* process Rx frames to be demultiplexed */ |
| 2157 | static void h2_process_demux(struct h2c *h2c) |
| 2158 | { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2159 | struct h2s *h2s = NULL, *tmp_h2s; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2160 | struct h2_fh hdr; |
| 2161 | unsigned int padlen = 0; |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 2162 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2163 | if (h2c->st0 >= H2_CS_ERROR) |
| 2164 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2165 | |
| 2166 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2167 | if (h2c->st0 == H2_CS_PREFACE) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2168 | if (h2c->flags & H2_CF_IS_BACK) |
| 2169 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2170 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 2171 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2172 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2173 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2174 | sess_log(h2c->conn->owner); |
| 2175 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2176 | goto fail; |
| 2177 | } |
| 2178 | |
| 2179 | h2c->max_id = 0; |
| 2180 | h2c->st0 = H2_CS_SETTINGS1; |
| 2181 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2182 | |
| 2183 | if (h2c->st0 == H2_CS_SETTINGS1) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2184 | /* ensure that what is pending is a valid SETTINGS frame |
| 2185 | * without an ACK. |
| 2186 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2187 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2188 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2189 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2190 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2191 | sess_log(h2c->conn->owner); |
| 2192 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2193 | goto fail; |
| 2194 | } |
| 2195 | |
| 2196 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 2197 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2198 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2199 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2200 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2201 | goto fail; |
| 2202 | } |
| 2203 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2204 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2205 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2206 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2207 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2208 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2209 | goto fail; |
| 2210 | } |
| 2211 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2212 | /* that's OK, switch to FRAME_P to process it. This is |
| 2213 | * a SETTINGS frame whose header has already been |
| 2214 | * deleted above. |
| 2215 | */ |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2216 | padlen = 0; |
| 2217 | goto new_frame; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2218 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2219 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2220 | |
| 2221 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2222 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2223 | int ret = 0; |
| 2224 | |
| 2225 | if (h2c->st0 >= H2_CS_ERROR) |
| 2226 | break; |
| 2227 | |
| 2228 | if (h2c->st0 == H2_CS_FRAME_H) { |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 2229 | if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2230 | break; |
| 2231 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2232 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2233 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2234 | if (!h2c->nb_streams) { |
| 2235 | /* only log if no other stream can report the error */ |
| 2236 | sess_log(h2c->conn->owner); |
| 2237 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2238 | break; |
| 2239 | } |
| 2240 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2241 | if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) { |
| 2242 | /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA), |
| 2243 | * we read the pad length and drop it from the remaining |
| 2244 | * payload (one byte + the 9 remaining ones = 10 total |
| 2245 | * removed), so we have a frame payload starting after the |
| 2246 | * pad len. Flow controlled frames (DATA) also count the |
| 2247 | * padlen in the flow control, so it must be adjusted. |
| 2248 | */ |
| 2249 | if (hdr.len < 1) { |
| 2250 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2251 | sess_log(h2c->conn->owner); |
| 2252 | goto fail; |
| 2253 | } |
| 2254 | hdr.len--; |
| 2255 | |
| 2256 | if (b_data(&h2c->dbuf) < 10) |
| 2257 | break; // missing padlen |
| 2258 | |
| 2259 | padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9); |
| 2260 | |
| 2261 | if (padlen > hdr.len) { |
| 2262 | /* RFC7540#6.1 : pad length = length of |
| 2263 | * frame payload or greater => error. |
| 2264 | */ |
| 2265 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2266 | sess_log(h2c->conn->owner); |
| 2267 | goto fail; |
| 2268 | } |
| 2269 | |
| 2270 | if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) { |
| 2271 | h2c->rcvd_c++; |
| 2272 | h2c->rcvd_s++; |
| 2273 | } |
| 2274 | b_del(&h2c->dbuf, 1); |
| 2275 | } |
| 2276 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2277 | |
| 2278 | new_frame: |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2279 | h2c->dfl = hdr.len; |
| 2280 | h2c->dsi = hdr.sid; |
| 2281 | h2c->dft = hdr.ft; |
| 2282 | h2c->dff = hdr.ff; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2283 | h2c->dpl = padlen; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2284 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | 54f46e5 | 2019-01-30 15:11:03 +0100 | [diff] [blame] | 2285 | |
| 2286 | /* check for minimum basic frame format validity */ |
| 2287 | ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize); |
| 2288 | if (ret != H2_ERR_NO_ERROR) { |
| 2289 | h2c_error(h2c, ret); |
| 2290 | sess_log(h2c->conn->owner); |
| 2291 | goto fail; |
| 2292 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2296 | tmp_h2s = h2c_st_by_id(h2c, h2c->dsi); |
| 2297 | |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2298 | if (tmp_h2s != h2s && h2s && h2s->cs && |
| 2299 | (b_data(&h2s->rxbuf) || |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2300 | (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] | 2301 | /* we may have to signal the upper layers */ |
| 2302 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2303 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2304 | } |
| 2305 | h2s = tmp_h2s; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2306 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 2307 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2308 | goto strm_err; |
| 2309 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2310 | if (h2s->st == H2_SS_IDLE && |
| 2311 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 2312 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 2313 | * this state MUST be treated as a connection error |
| 2314 | */ |
| 2315 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2316 | if (!h2c->nb_streams) { |
| 2317 | /* only log if no other stream can report the error */ |
| 2318 | sess_log(h2c->conn->owner); |
| 2319 | } |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2320 | break; |
| 2321 | } |
| 2322 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2323 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 2324 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 2325 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2326 | * this state MUST be treated as a stream error. |
| 2327 | * 6.2, 6.6 and 6.10 further mandate that HEADERS/ |
| 2328 | * PUSH_PROMISE/CONTINUATION cause connection errors. |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2329 | */ |
Willy Tarreau | 5b4eae3 | 2019-01-24 09:43:32 +0100 | [diff] [blame] | 2330 | if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) |
| 2331 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2332 | else |
| 2333 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2334 | goto strm_err; |
| 2335 | } |
| 2336 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2337 | /* Below the management of frames received in closed state is a |
| 2338 | * bit hackish because the spec makes strong differences between |
| 2339 | * streams closed by receiving RST, sending RST, and seeing ES |
| 2340 | * in both directions. In addition to this, the creation of a |
| 2341 | * new stream reusing the identifier of a closed one will be |
| 2342 | * detected here. Given that we cannot keep track of all closed |
| 2343 | * streams forever, we consider that unknown closed streams were |
| 2344 | * closed on RST received, which allows us to respond with an |
| 2345 | * RST without breaking the connection (eg: to abort a transfer). |
| 2346 | * Some frames have to be silently ignored as well. |
| 2347 | */ |
| 2348 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
Willy Tarreau | 3ad5d31 | 2019-01-29 18:33:26 +0100 | [diff] [blame] | 2349 | 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] | 2350 | /* #5.1.1: The identifier of a newly |
| 2351 | * established stream MUST be numerically |
| 2352 | * greater than all streams that the initiating |
| 2353 | * endpoint has opened or reserved. This |
| 2354 | * governs streams that are opened using a |
| 2355 | * HEADERS frame and streams that are reserved |
| 2356 | * using PUSH_PROMISE. An endpoint that |
| 2357 | * receives an unexpected stream identifier |
| 2358 | * MUST respond with a connection error. |
| 2359 | */ |
| 2360 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2361 | goto strm_err; |
| 2362 | } |
| 2363 | |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2364 | 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] | 2365 | /* RFC7540#5.1:closed: an endpoint that |
| 2366 | * receives any frame other than PRIORITY after |
| 2367 | * receiving a RST_STREAM MUST treat that as a |
| 2368 | * stream error of type STREAM_CLOSED. |
| 2369 | * |
| 2370 | * Note that old streams fall into this category |
| 2371 | * and will lead to an RST being sent. |
Willy Tarreau | 8d9ac3e | 2019-01-30 16:58:30 +0100 | [diff] [blame] | 2372 | * |
| 2373 | * However, we cannot generalize this to all frame types. Those |
| 2374 | * carrying compression state must still be processed before |
| 2375 | * being dropped or we'll desynchronize the decoder. This can |
| 2376 | * happen with request trailers received after sending an |
| 2377 | * RST_STREAM, or with header/trailers responses received after |
| 2378 | * sending RST_STREAM (aborted stream). |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2379 | */ |
| 2380 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2381 | h2c->st0 = H2_CS_FRAME_E; |
| 2382 | goto strm_err; |
| 2383 | } |
| 2384 | |
| 2385 | /* RFC7540#5.1:closed: if this state is reached as a |
| 2386 | * result of sending a RST_STREAM frame, the peer that |
| 2387 | * receives the RST_STREAM might have already sent |
| 2388 | * frames on the stream that cannot be withdrawn. An |
| 2389 | * endpoint MUST ignore frames that it receives on |
| 2390 | * closed streams after it has sent a RST_STREAM |
| 2391 | * frame. An endpoint MAY choose to limit the period |
| 2392 | * over which it ignores frames and treat frames that |
| 2393 | * arrive after this time as being in error. |
| 2394 | */ |
Willy Tarreau | 24ff1f8 | 2019-01-30 19:20:09 +0100 | [diff] [blame] | 2395 | if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) { |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2396 | /* RFC7540#5.1:closed: any frame other than |
| 2397 | * PRIO/WU/RST in this state MUST be treated as |
| 2398 | * a connection error |
| 2399 | */ |
| 2400 | if (h2c->dft != H2_FT_RST_STREAM && |
| 2401 | h2c->dft != H2_FT_PRIORITY && |
| 2402 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 2403 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2404 | goto strm_err; |
| 2405 | } |
| 2406 | } |
| 2407 | } |
| 2408 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2409 | #if 0 |
| 2410 | // problem below: it is not possible to completely ignore such |
| 2411 | // streams as we need to maintain the compression state as well |
| 2412 | // and for this we need to completely process these frames (eg: |
| 2413 | // HEADERS frames) as well as counting DATA frames to emit |
| 2414 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 2415 | // This is a typical case of layer violation where the |
| 2416 | // transported contents are critical to the connection's |
| 2417 | // validity and must be ignored at the same time :-( |
| 2418 | |
| 2419 | /* graceful shutdown, ignore streams whose ID is higher than |
| 2420 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 2421 | */ |
| 2422 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2423 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2424 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2425 | h2c->dfl -= ret; |
| 2426 | ret = h2c->dfl == 0; |
| 2427 | goto strm_err; |
| 2428 | } |
| 2429 | #endif |
| 2430 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2431 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 2432 | case H2_FT_SETTINGS: |
| 2433 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2434 | ret = h2c_handle_settings(h2c); |
| 2435 | |
| 2436 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2437 | ret = h2c_ack_settings(h2c); |
| 2438 | break; |
| 2439 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 2440 | case H2_FT_PING: |
| 2441 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2442 | ret = h2c_handle_ping(h2c); |
| 2443 | |
| 2444 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2445 | ret = h2c_ack_ping(h2c); |
| 2446 | break; |
| 2447 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 2448 | case H2_FT_WINDOW_UPDATE: |
| 2449 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2450 | ret = h2c_handle_window_update(h2c, h2s); |
| 2451 | break; |
| 2452 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2453 | case H2_FT_CONTINUATION: |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2454 | /* RFC7540#6.10: CONTINUATION may only be preceeded by |
| 2455 | * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These |
| 2456 | * frames' parsers consume all following CONTINUATION |
| 2457 | * frames so this one is out of sequence. |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2458 | */ |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2459 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2460 | sess_log(h2c->conn->owner); |
| 2461 | goto fail; |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2462 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2463 | case H2_FT_HEADERS: |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2464 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2465 | if (h2c->flags & H2_CF_IS_BACK) |
| 2466 | tmp_h2s = h2c_bck_handle_headers(h2c, h2s); |
| 2467 | else |
| 2468 | tmp_h2s = h2c_frt_handle_headers(h2c, h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2469 | if (tmp_h2s) { |
| 2470 | h2s = tmp_h2s; |
| 2471 | ret = 1; |
| 2472 | } |
| 2473 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2474 | break; |
| 2475 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2476 | case H2_FT_DATA: |
| 2477 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2478 | ret = h2c_frt_handle_data(h2c, h2s); |
| 2479 | |
| 2480 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2481 | ret = h2c_send_strm_wu(h2c); |
| 2482 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2483 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 2484 | case H2_FT_PRIORITY: |
| 2485 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2486 | ret = h2c_handle_priority(h2c); |
| 2487 | break; |
| 2488 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2489 | case H2_FT_RST_STREAM: |
| 2490 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2491 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 2492 | break; |
| 2493 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 2494 | case H2_FT_GOAWAY: |
| 2495 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2496 | ret = h2c_handle_goaway(h2c); |
| 2497 | break; |
| 2498 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2499 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2500 | default: |
| 2501 | /* drop frames that we ignore. They may be larger than |
| 2502 | * the buffer so we drain all of their contents until |
| 2503 | * we reach the end. |
| 2504 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2505 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2506 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2507 | h2c->dfl -= ret; |
| 2508 | ret = h2c->dfl == 0; |
| 2509 | } |
| 2510 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2511 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2512 | /* We may have to send an RST if not done yet */ |
| 2513 | if (h2s->st == H2_SS_ERROR) |
| 2514 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2515 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2516 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2517 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2518 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2519 | /* error or missing data condition met above ? */ |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2520 | if (ret <= 0) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2521 | break; |
| 2522 | |
| 2523 | if (h2c->st0 != H2_CS_FRAME_H) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2524 | b_del(&h2c->dbuf, h2c->dfl); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2525 | h2c->st0 = H2_CS_FRAME_H; |
| 2526 | } |
| 2527 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2528 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2529 | if (h2c->rcvd_c > 0 && |
| 2530 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2531 | h2c_send_conn_wu(h2c); |
| 2532 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2533 | fail: |
| 2534 | /* we can go here on missing data, blocked response or error */ |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2535 | if (h2s && h2s->cs && |
| 2536 | (b_data(&h2s->rxbuf) || |
Christopher Faulet | 63768a6 | 2019-03-22 14:05:52 +0100 | [diff] [blame] | 2537 | (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] | 2538 | /* we may have to signal the upper layers */ |
| 2539 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2540 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2541 | } |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2542 | |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 2543 | h2c_restart_reading(h2c, 0); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2544 | } |
| 2545 | |
| 2546 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2547 | * the end. |
| 2548 | */ |
| 2549 | static int h2_process_mux(struct h2c *h2c) |
| 2550 | { |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2551 | struct h2s *h2s, *h2s_back; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2552 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2553 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2554 | if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) { |
| 2555 | if (unlikely(h2c_bck_send_preface(h2c) <= 0)) { |
| 2556 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2557 | if (h2c->st0 == H2_CS_ERROR) { |
| 2558 | h2c->st0 = H2_CS_ERROR2; |
| 2559 | sess_log(h2c->conn->owner); |
| 2560 | } |
| 2561 | goto fail; |
| 2562 | } |
| 2563 | h2c->st0 = H2_CS_SETTINGS1; |
| 2564 | } |
| 2565 | /* need to wait for the other side */ |
Willy Tarreau | 75a930a | 2018-12-12 08:03:58 +0100 | [diff] [blame] | 2566 | if (h2c->st0 < H2_CS_FRAME_H) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2567 | return 1; |
| 2568 | } |
| 2569 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2570 | /* start by sending possibly pending window updates */ |
| 2571 | if (h2c->rcvd_c > 0 && |
| 2572 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2573 | h2c_send_conn_wu(h2c) < 0) |
| 2574 | goto fail; |
| 2575 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2576 | /* First we always process the flow control list because the streams |
| 2577 | * waiting there were already elected for immediate emission but were |
| 2578 | * blocked just on this. |
| 2579 | */ |
| 2580 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2581 | list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2582 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2583 | h2c->st0 >= H2_CS_ERROR) |
| 2584 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2585 | |
| 2586 | if (!LIST_ISEMPTY(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2587 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2588 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2589 | h2s->flags &= ~H2_SF_BLK_ANY; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2590 | /* For some reason, the upper layer failed to subsribe again, |
| 2591 | * so remove it from the send_list |
| 2592 | */ |
| 2593 | if (!h2s->send_wait) { |
| 2594 | LIST_DEL_INIT(&h2s->list); |
| 2595 | continue; |
| 2596 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2597 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2598 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2599 | tasklet_wakeup(h2s->send_wait->task); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2600 | } |
| 2601 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2602 | list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2603 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2604 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2605 | |
| 2606 | if (!LIST_ISEMPTY(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2607 | continue; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2608 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2609 | /* For some reason, the upper layer failed to subsribe again, |
| 2610 | * so remove it from the send_list |
| 2611 | */ |
| 2612 | if (!h2s->send_wait) { |
| 2613 | LIST_DEL_INIT(&h2s->list); |
| 2614 | continue; |
| 2615 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2616 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2617 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2618 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2619 | tasklet_wakeup(h2s->send_wait->task); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2620 | } |
| 2621 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2622 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2623 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2624 | if (h2c->st0 == H2_CS_ERROR) { |
| 2625 | if (h2c->max_id >= 0) { |
| 2626 | h2c_send_goaway_error(h2c, NULL); |
| 2627 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2628 | return 0; |
| 2629 | } |
| 2630 | |
| 2631 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2632 | } |
| 2633 | return 1; |
| 2634 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2635 | return (1); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2636 | } |
| 2637 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2638 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2639 | /* Attempt to read data, and subscribe if none available. |
| 2640 | * The function returns 1 if data has been received, otherwise zero. |
| 2641 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2642 | static int h2_recv(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2643 | { |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2644 | struct connection *conn = h2c->conn; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2645 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2646 | int max; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2647 | size_t ret; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2648 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2649 | if (h2c->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2650 | return (b_data(&h2c->dbuf)); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2651 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2652 | if (!h2_recv_allowed(h2c)) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2653 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2654 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2655 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2656 | if (!buf) { |
| 2657 | h2c->flags |= H2_CF_DEM_DALLOC; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2658 | return 0; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2659 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2660 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2661 | do { |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 2662 | b_realign_if_empty(buf); |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2663 | if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
| 2664 | /* HTX in use : try to pre-align the buffer like the |
| 2665 | * rxbufs will be to optimize memory copies. We'll make |
| 2666 | * sure that the frame header lands at the end of the |
| 2667 | * HTX block to alias it upon recv. We cannot use the |
| 2668 | * head because rcv_buf() will realign the buffer if |
| 2669 | * it's empty. Thus we cheat and pretend we already |
| 2670 | * have a few bytes there. |
| 2671 | */ |
| 2672 | max = buf_room_for_htx_data(buf) + 9; |
Willy Tarreau | c0960d1 | 2018-12-14 10:59:15 +0100 | [diff] [blame] | 2673 | buf->head = sizeof(struct htx) - 9; |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2674 | } |
| 2675 | else |
| 2676 | max = b_room(buf); |
| 2677 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2678 | if (max) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2679 | ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2680 | else |
| 2681 | ret = 0; |
| 2682 | } while (ret > 0); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2683 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2684 | if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2685 | 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] | 2686 | |
Olivier Houchard | a1411e6 | 2018-08-17 18:42:48 +0200 | [diff] [blame] | 2687 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2688 | h2_release_buf(h2c, &h2c->dbuf); |
Olivier Houchard | 4667773 | 2018-11-29 17:06:17 +0100 | [diff] [blame] | 2689 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2690 | } |
| 2691 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2692 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2693 | h2c->flags |= H2_CF_DEM_DFULL; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2694 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2695 | } |
| 2696 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2697 | /* Try to send data if possible. |
| 2698 | * The function returns 1 if data have been sent, otherwise zero. |
| 2699 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2700 | static int h2_send(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2701 | { |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2702 | struct connection *conn = h2c->conn; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2703 | int done; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2704 | int sent = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2705 | |
| 2706 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 2707 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2708 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2709 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2710 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2711 | /* a handshake was requested */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2712 | goto schedule; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2713 | } |
| 2714 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2715 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2716 | * pending streams into the existing buffer until it's reportedly full |
| 2717 | * or the end of send requests is reached. Then it tries to send this |
| 2718 | * buffer's contents out, marks it not full if at least one byte could |
| 2719 | * be sent, and tries again. |
| 2720 | * |
| 2721 | * The snd_buf() function normally takes a "flags" argument which may |
| 2722 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2723 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2724 | * connection is streaming lots of data (used to increase TLS record |
| 2725 | * size at the expense of latency). The former can be sent any time |
| 2726 | * there's a buffer full flag, as it indicates at least one stream |
| 2727 | * attempted to send and failed so there are pending data. An |
| 2728 | * alternative would be to set it as long as there's an active stream |
| 2729 | * but that would be problematic for ACKs until we have an absolute |
| 2730 | * guarantee that all waiters have at least one byte to send. The |
| 2731 | * latter should possibly not be set for now. |
| 2732 | */ |
| 2733 | |
| 2734 | done = 0; |
| 2735 | while (!done) { |
| 2736 | unsigned int flags = 0; |
| 2737 | |
| 2738 | /* fill as much as we can into the current buffer */ |
| 2739 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2740 | done = h2_process_mux(h2c); |
| 2741 | |
Olivier Houchard | 2b09443 | 2019-01-29 18:28:36 +0100 | [diff] [blame] | 2742 | if (h2c->flags & H2_CF_MUX_MALLOC) |
| 2743 | break; |
| 2744 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2745 | if (conn->flags & CO_FL_ERROR) |
| 2746 | break; |
| 2747 | |
| 2748 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2749 | flags |= CO_SFL_MSG_MORE; |
| 2750 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2751 | if (b_data(&h2c->mbuf)) { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2752 | 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] | 2753 | if (!ret) |
| 2754 | break; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2755 | sent = 1; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2756 | b_del(&h2c->mbuf, ret); |
| 2757 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2758 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2759 | |
| 2760 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2761 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2762 | } |
| 2763 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2764 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2765 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2766 | b_reset(&h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2767 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2768 | /* We're not full anymore, so we can wake any task that are waiting |
| 2769 | * for us. |
| 2770 | */ |
| 2771 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2772 | struct h2s *h2s; |
| 2773 | |
| 2774 | list_for_each_entry(h2s, &h2c->send_list, list) { |
| 2775 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2776 | break; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2777 | |
| 2778 | if (!LIST_ISEMPTY(&h2s->sending_list)) |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2779 | continue; |
| 2780 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 2781 | /* For some reason, the upper layer failed to subsribe again, |
| 2782 | * so remove it from the send_list |
| 2783 | */ |
| 2784 | if (!h2s->send_wait) { |
| 2785 | LIST_DEL_INIT(&h2s->list); |
| 2786 | continue; |
| 2787 | } |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2788 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2789 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2790 | tasklet_wakeup(h2s->send_wait->task); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 2791 | LIST_ADDQ(&h2c->sending_list, &h2s->sending_list); |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 2792 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2793 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2794 | /* We're done, no more to send */ |
| 2795 | if (!b_data(&h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2796 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2797 | schedule: |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2798 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2799 | 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] | 2800 | return sent; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2801 | } |
| 2802 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 2803 | /* this is the tasklet referenced in h2c->wait_event.task */ |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2804 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2805 | { |
| 2806 | struct h2c *h2c = ctx; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2807 | int ret = 0; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2808 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2809 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2810 | ret = h2_send(h2c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2811 | if (!(h2c->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2812 | ret |= h2_recv(h2c); |
Willy Tarreau | cef5c8e | 2018-12-18 10:29:54 +0100 | [diff] [blame] | 2813 | if (ret || b_data(&h2c->dbuf)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2814 | h2_process(h2c); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2815 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2816 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2817 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2818 | /* callback called on any event by the connection handler. |
| 2819 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2820 | * destruction of the connection (which normally doesn not happen in h2). |
| 2821 | */ |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2822 | static int h2_process(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2823 | { |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2824 | struct connection *conn = h2c->conn; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2825 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2826 | if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2827 | h2_process_demux(h2c); |
| 2828 | |
| 2829 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2830 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2831 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2832 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2833 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 2834 | } |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2835 | h2_send(h2c); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2836 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 2837 | if (unlikely(h2c->proxy->state == PR_STSTOPPED)) { |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2838 | /* frontend is stopping, reload likely in progress, let's try |
| 2839 | * to announce a graceful shutdown if not yet done. We don't |
| 2840 | * care if it fails, it will be tried again later. |
| 2841 | */ |
| 2842 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 2843 | if (h2c->last_sid < 0) |
| 2844 | h2c->last_sid = (1U << 31) - 1; |
| 2845 | h2c_send_goaway_error(h2c, NULL); |
| 2846 | } |
| 2847 | } |
| 2848 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2849 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2850 | * If we received early data, and the handshake is done, wake |
| 2851 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2852 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2853 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 2854 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 2855 | struct eb32_node *node; |
| 2856 | struct h2s *h2s; |
| 2857 | |
| 2858 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 2859 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 2860 | |
| 2861 | while (node) { |
| 2862 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | fde287c | 2018-12-19 18:33:16 +0100 | [diff] [blame] | 2863 | if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2864 | h2s_notify_recv(h2s); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2865 | node = eb32_next(node); |
| 2866 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2867 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2868 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 2869 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 2870 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 2871 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 2872 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 2873 | h2_wake_some_streams(h2c, 0, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2874 | |
| 2875 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2876 | /* no more stream, kill the connection now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2877 | h2_release(h2c); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2878 | return -1; |
| 2879 | } |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2880 | } |
| 2881 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2882 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2883 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2884 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2885 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 2886 | h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 2887 | (h2c->st0 != H2_CS_ERROR && |
| 2888 | !b_data(&h2c->mbuf) && |
| 2889 | (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && |
| 2890 | ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list)))) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2891 | h2_release_buf(h2c, &h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2892 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2893 | if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2894 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2895 | 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] | 2896 | task_queue(h2c->task); |
| 2897 | } |
| 2898 | else |
| 2899 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2900 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2901 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2902 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2903 | return 0; |
| 2904 | } |
| 2905 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 2906 | /* wake-up function called by the connection layer (mux_ops.wake) */ |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2907 | static int h2_wake(struct connection *conn) |
| 2908 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2909 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2910 | |
| 2911 | return (h2_process(h2c)); |
| 2912 | } |
| 2913 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2914 | /* Connection timeout management. The principle is that if there's no receipt |
| 2915 | * nor sending for a certain amount of time, the connection is closed. If the |
| 2916 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 2917 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 2918 | * GOAWAY frame. |
| 2919 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2920 | 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] | 2921 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2922 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2923 | int expired = tick_is_expired(t->expire, now_ms); |
| 2924 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2925 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2926 | return t; |
| 2927 | |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 2928 | task_destroy(t); |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2929 | |
| 2930 | if (!h2c) { |
| 2931 | /* resources were already deleted */ |
| 2932 | return NULL; |
| 2933 | } |
| 2934 | |
| 2935 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2936 | h2c_error(h2c, H2_ERR_NO_ERROR); |
| 2937 | h2_wake_some_streams(h2c, 0, 0); |
| 2938 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2939 | if (b_data(&h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2940 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 2941 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2942 | } |
| 2943 | |
| 2944 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2945 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2946 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 2947 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2948 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2949 | 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] | 2950 | 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] | 2951 | if (ret > 0) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2952 | b_del(&h2c->mbuf, ret); |
| 2953 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2954 | } |
| 2955 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2956 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2957 | /* either we can release everything now or it will be done later once |
| 2958 | * the last stream closes. |
| 2959 | */ |
| 2960 | if (eb_is_empty(&h2c->streams_by_id)) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2961 | h2_release(h2c); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2962 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2963 | return NULL; |
| 2964 | } |
| 2965 | |
| 2966 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2967 | /*******************************************/ |
| 2968 | /* functions below are used by the streams */ |
| 2969 | /*******************************************/ |
| 2970 | |
| 2971 | /* |
| 2972 | * Attach a new stream to a connection |
| 2973 | * (Used for outgoing connections) |
| 2974 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2975 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2976 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2977 | struct conn_stream *cs; |
| 2978 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2979 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2980 | |
| 2981 | cs = cs_new(conn); |
| 2982 | if (!cs) |
| 2983 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2984 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2985 | if (!h2s) { |
| 2986 | cs_free(cs); |
| 2987 | return NULL; |
| 2988 | } |
| 2989 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2990 | } |
| 2991 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 2992 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 2993 | * We have to scan because we may have some orphan streams. It might be |
| 2994 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 2995 | * orphans. |
| 2996 | */ |
| 2997 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 2998 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2999 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 3000 | struct h2s *h2s; |
| 3001 | struct eb32_node *node; |
| 3002 | |
| 3003 | node = eb32_first(&h2c->streams_by_id); |
| 3004 | while (node) { |
| 3005 | h2s = container_of(node, struct h2s, by_id); |
| 3006 | if (h2s->cs) |
| 3007 | return h2s->cs; |
| 3008 | node = eb32_next(node); |
| 3009 | } |
| 3010 | return NULL; |
| 3011 | } |
| 3012 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3013 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3014 | * Destroy the mux and the associated connection, if it is no longer used |
| 3015 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3016 | static void h2_destroy(void *ctx) |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3017 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3018 | struct h2c *h2c = ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3019 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 3020 | 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] | 3021 | h2_release(h2c); |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 3022 | } |
| 3023 | |
| 3024 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3025 | * Detach the stream from the connection and possibly release the connection. |
| 3026 | */ |
| 3027 | static void h2_detach(struct conn_stream *cs) |
| 3028 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3029 | struct h2s *h2s = cs->ctx; |
| 3030 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3031 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3032 | |
| 3033 | cs->ctx = NULL; |
| 3034 | if (!h2s) |
| 3035 | return; |
| 3036 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 3037 | /* The stream is about to die, so no need to attempt to run its task */ |
| 3038 | if (!LIST_ISEMPTY(&h2s->sending_list) && |
| 3039 | h2s->send_wait != &h2s->wait_event) { |
| 3040 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
| 3041 | LIST_DEL_INIT(&h2s->sending_list); |
| 3042 | } |
| 3043 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3044 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3045 | h2c = h2s->h2c; |
| 3046 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 3047 | h2c->nb_cs--; |
Willy Tarreau | fa1d357 | 2019-01-31 10:31:51 +0100 | [diff] [blame] | 3048 | if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY && |
| 3049 | !h2_frt_has_too_many_cs(h2c)) { |
| 3050 | /* frontend connection was blocking new streams creation */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3051 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3052 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 3053 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3054 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 3055 | /* this stream may be blocked waiting for some data to leave (possibly |
| 3056 | * an ES or RST frame), so orphan it in this case. |
| 3057 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 3058 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 3059 | (h2c->st0 < H2_CS_ERROR) && |
Olivier Houchard | 16ff261 | 2019-03-21 15:48:46 +0100 | [diff] [blame] | 3060 | (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] | 3061 | return; |
| 3062 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3063 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 3064 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 3065 | /* unblock the connection if it was blocked on this |
| 3066 | * stream. |
| 3067 | */ |
| 3068 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 3069 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 3070 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 3071 | } |
| 3072 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 3073 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3074 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3075 | if (h2c->flags & H2_CF_IS_BACK && |
| 3076 | (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3077 | if (!(h2c->conn->flags & |
| 3078 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 3079 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3080 | h2c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3081 | if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) { |
| 3082 | h2c->conn->owner = NULL; |
| 3083 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3084 | if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn)) |
| 3085 | /* The server doesn't want it, let's kill the connection right away */ |
| 3086 | h2c->conn->mux->destroy(h2c->conn); |
| 3087 | return; |
| 3088 | } |
| 3089 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3090 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3091 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 3092 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 3093 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 3094 | return; |
| 3095 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3096 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3097 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3098 | h2c->conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | 8694978 | 2019-01-31 10:42:05 +0100 | [diff] [blame] | 3099 | if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3100 | struct server *srv = objt_server(h2c->conn->target); |
| 3101 | |
| 3102 | if (srv) { |
| 3103 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 3104 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 3105 | else |
| 3106 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 3107 | } |
| 3108 | |
| 3109 | } |
| 3110 | } |
| 3111 | } |
| 3112 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3113 | /* We don't want to close right now unless we're removing the |
| 3114 | * last stream, and either the connection is in error, or it |
| 3115 | * reached the ID already specified in a GOAWAY frame received |
| 3116 | * or sent (as seen by last_sid >= 0). |
| 3117 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3118 | if (h2c_is_dead(h2c)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3119 | /* no more stream will come, kill it now */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3120 | h2_release(h2c); |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3121 | } |
| 3122 | else if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3123 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3124 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3125 | task_queue(h2c->task); |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 3126 | } |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3127 | else |
| 3128 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3129 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3130 | } |
| 3131 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3132 | /* Performs a synchronous or asynchronous shutr(). |
| 3133 | * FIXME: guess what the return code tries to indicate! |
| 3134 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3135 | static int h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3136 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3137 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3138 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3139 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 3140 | 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] | 3141 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3142 | |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3143 | /* a connstream may require us to immediately kill the whole connection |
| 3144 | * for example because of a "tcp-request content reject" rule that is |
| 3145 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3146 | * close the connection. |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3147 | */ |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3148 | if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) && |
| 3149 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3150 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3151 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3152 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3153 | else if (!(h2s->flags & H2_SF_HEADERS_SENT)) { |
| 3154 | /* Nothing was never sent for this stream, so reset with |
| 3155 | * REFUSED_STREAM error to let the client retry the |
| 3156 | * request. |
| 3157 | */ |
| 3158 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3159 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3160 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3161 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3162 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3163 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3164 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3165 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3166 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3167 | h2s_close(h2s); |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3168 | |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3169 | return 0; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3170 | add_to_list: |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3171 | if (LIST_ISEMPTY(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3172 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3173 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3174 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3175 | h2s->send_wait = sw; |
| 3176 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3177 | h2s->send_wait = sw; |
| 3178 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3179 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3180 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3181 | /* Let the handler know we want shutr */ |
| 3182 | sw->handle = (void *)((long)sw->handle | 1); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3183 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3184 | } |
| 3185 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3186 | /* Performs a synchronous or asynchronous shutw(). |
| 3187 | * FIXME: guess what the return code tries to indicate! |
| 3188 | */ |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3189 | static int h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3190 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3191 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3192 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3193 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 3194 | 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] | 3195 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3196 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3197 | if (h2s->flags & H2_SF_HEADERS_SENT) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3198 | /* we can cleanly close using an empty data frame only after headers */ |
| 3199 | |
| 3200 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 3201 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3202 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3203 | |
| 3204 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3205 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3206 | else |
| 3207 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3208 | } else { |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3209 | /* a connstream may require us to immediately kill the whole connection |
| 3210 | * for example because of a "tcp-request content reject" rule that is |
| 3211 | * normally used to limit abuse. In this case we schedule a goaway to |
| 3212 | * close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3213 | */ |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3214 | if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) && |
| 3215 | !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 3216 | h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM); |
| 3217 | h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM); |
| 3218 | } |
Christopher Faulet | 35757d3 | 2019-03-07 15:51:33 +0100 | [diff] [blame] | 3219 | else { |
| 3220 | /* Nothing was never sent for this stream, so reset with |
| 3221 | * REFUSED_STREAM error to let the client retry the |
| 3222 | * request. |
| 3223 | */ |
| 3224 | h2s_error(h2s, H2_ERR_REFUSED_STREAM); |
| 3225 | } |
Willy Tarreau | 1805904 | 2019-01-31 19:12:48 +0100 | [diff] [blame] | 3226 | |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3227 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3228 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3229 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3230 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3231 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3232 | } |
| 3233 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3234 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3235 | tasklet_wakeup(h2c->wait_event.task); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3236 | return 0; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3237 | |
| 3238 | add_to_list: |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3239 | if (LIST_ISEMPTY(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3240 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3241 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3242 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3243 | h2s->send_wait = sw; |
| 3244 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3245 | h2s->send_wait = sw; |
| 3246 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3247 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3248 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3249 | /* let the handler know we want to shutw */ |
| 3250 | sw->handle = (void *)((long)(sw->handle) | 2); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3251 | return 1; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3252 | } |
| 3253 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3254 | /* This is the tasklet referenced in h2s->wait_event.task, it is used for |
| 3255 | * deferred shutdowns when the h2_detach() was done but the mux buffer was full |
| 3256 | * and prevented the last frame from being emitted. |
| 3257 | */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3258 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3259 | { |
| 3260 | struct h2s *h2s = ctx; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3261 | long reason = (long)h2s->wait_event.handle; |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3262 | int ret = 0; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3263 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3264 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3265 | if (reason & 2) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3266 | ret |= h2_do_shutw(h2s); |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3267 | if (reason & 1) |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3268 | ret |= h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3269 | |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3270 | if (!ret) { |
| 3271 | /* We're done trying to send, remove ourself from the send_list */ |
Olivier Houchard | afc7cb8 | 2019-03-25 14:08:01 +0100 | [diff] [blame] | 3272 | LIST_DEL_INIT(&h2s->list); |
| 3273 | } |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3274 | /* 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] | 3275 | if (!ret && (h2s->cs == NULL)) { |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3276 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3277 | h2s_destroy(h2s); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3278 | |
| 3279 | if (h2c_is_dead(h2c)) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3280 | h2_release(h2c); |
Olivier Houchard | 7a97743 | 2019-03-21 15:47:13 +0100 | [diff] [blame] | 3281 | } |
| 3282 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3283 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3284 | } |
| 3285 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3286 | /* shutr() called by the conn_stream (mux_ops.shutr) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3287 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3288 | { |
| 3289 | struct h2s *h2s = cs->ctx; |
| 3290 | |
| 3291 | if (!mode) |
| 3292 | return; |
| 3293 | |
| 3294 | h2_do_shutr(h2s); |
| 3295 | } |
| 3296 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 3297 | /* shutw() called by the conn_stream (mux_ops.shutw) */ |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3298 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3299 | { |
| 3300 | struct h2s *h2s = cs->ctx; |
| 3301 | |
| 3302 | h2_do_shutw(h2s); |
| 3303 | } |
| 3304 | |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3305 | /* 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] | 3306 | * HTX request or response depending on the connection's side. Returns a |
| 3307 | * positive value on success, a negative value on failure, or 0 if it couldn't |
| 3308 | * proceed. May report connection errors in h2c->errcode if the frame is |
| 3309 | * non-decodable and the connection unrecoverable. In absence of connection |
| 3310 | * 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] | 3311 | * |
| 3312 | * The function may fold CONTINUATION frames into the initial HEADERS frame |
| 3313 | * by removing padding and next frame header, then moving the CONTINUATION |
| 3314 | * frame's payload and adjusting h2c->dfl to match the new aggregated frame, |
| 3315 | * leaving a hole between the main frame and the beginning of the next one. |
| 3316 | * The possibly remaining incomplete or next frame at the end may be moved |
| 3317 | * if the aggregated frame is not deleted, in order to fill the hole. Wrapped |
| 3318 | * HEADERS frames are unwrapped into a temporary buffer before decoding. |
| 3319 | * |
| 3320 | * A buffer at the beginning of processing may look like this : |
| 3321 | * |
| 3322 | * ,---.---------.-----.--------------.--------------.------.---. |
| 3323 | * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///| |
| 3324 | * `---^---------^-----^--------------^--------------^------^---' |
| 3325 | * | | <-----> | | |
| 3326 | * area | dpl | wrap |
| 3327 | * |<--------------> | |
| 3328 | * | dfl | |
| 3329 | * |<-------------------------------------------------->| |
| 3330 | * head data |
| 3331 | * |
| 3332 | * Padding is automatically overwritten when folding, participating to the |
| 3333 | * hole size after dfl : |
| 3334 | * |
| 3335 | * ,---.------------------------.-----.--------------.------.---. |
| 3336 | * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///| |
| 3337 | * `---^------------------------^-----^--------------^------^---' |
| 3338 | * | | <-----> | | |
| 3339 | * area | hole | wrap |
| 3340 | * |<-----------------------> | |
| 3341 | * | dfl | |
| 3342 | * |<-------------------------------------------------->| |
| 3343 | * head data |
| 3344 | * |
| 3345 | * Please note that the HEADERS frame is always deprived from its PADLEN byte |
| 3346 | * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY |
| 3347 | * bit. |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3348 | * |
| 3349 | * The <flags> field must point to either the stream's flags or to a copy of it |
| 3350 | * so that the function can update the following flags : |
| 3351 | * - H2_SF_DATA_CLEN when content-length is seen |
| 3352 | * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion |
| 3353 | * - H2_SF_HEADERS_RCVD once the frame is successfully decoded |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3354 | * |
| 3355 | * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to |
| 3356 | * decoding, in order to detect if we're dealing with a headers or a trailers |
| 3357 | * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3358 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3359 | 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] | 3360 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3361 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3362 | struct buffer *tmp = get_trash_chunk(); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3363 | struct http_hdr list[MAX_HTTP_HDR * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3364 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3365 | unsigned int msgf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3366 | struct htx *htx = NULL; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3367 | int flen; // header frame len |
| 3368 | int hole = 0; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3369 | int ret = 0; |
| 3370 | int outlen; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3371 | int wrap; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3372 | int try = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3373 | |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3374 | next_frame: |
| 3375 | if (b_data(&h2c->dbuf) - hole < h2c->dfl) |
| 3376 | goto leave; // incomplete input frame |
| 3377 | |
| 3378 | /* No END_HEADERS means there's one or more CONTINUATION frames. In |
| 3379 | * this case, we'll try to paste it immediately after the initial |
| 3380 | * HEADERS frame payload and kill any possible padding. The initial |
| 3381 | * frame's length will be increased to represent the concatenation |
| 3382 | * of the two frames. The next frame is read from position <tlen> |
| 3383 | * and written at position <flen> (minus padding if some is present). |
| 3384 | */ |
| 3385 | if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) { |
| 3386 | struct h2_fh hdr; |
| 3387 | int clen; // CONTINUATION frame's payload length |
| 3388 | |
| 3389 | if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) { |
| 3390 | /* no more data, the buffer may be full, either due to |
| 3391 | * too large a frame or because of too large a hole that |
| 3392 | * we're going to compact at the end. |
| 3393 | */ |
| 3394 | goto leave; |
| 3395 | } |
| 3396 | |
| 3397 | if (hdr.ft != H2_FT_CONTINUATION) { |
| 3398 | /* RFC7540#6.10: frame of unexpected type */ |
| 3399 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3400 | goto fail; |
| 3401 | } |
| 3402 | |
| 3403 | if (hdr.sid != h2c->dsi) { |
| 3404 | /* RFC7540#6.10: frame of different stream */ |
| 3405 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3406 | goto fail; |
| 3407 | } |
| 3408 | |
| 3409 | if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { |
| 3410 | /* RFC7540#4.2: invalid frame length */ |
| 3411 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3412 | goto fail; |
| 3413 | } |
| 3414 | |
| 3415 | /* detect when we must stop aggragating frames */ |
| 3416 | h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS; |
| 3417 | |
| 3418 | /* Take as much as we can of the CONTINUATION frame's payload */ |
| 3419 | clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9); |
| 3420 | if (clen > hdr.len) |
| 3421 | clen = hdr.len; |
| 3422 | |
| 3423 | /* Move the frame's payload over the padding, hole and frame |
| 3424 | * header. At least one of hole or dpl is null (see diagrams |
| 3425 | * above). The hole moves after the new aggragated frame. |
| 3426 | */ |
| 3427 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9)); |
| 3428 | h2c->dfl += clen - h2c->dpl; |
| 3429 | hole += h2c->dpl + 9; |
| 3430 | h2c->dpl = 0; |
| 3431 | goto next_frame; |
| 3432 | } |
| 3433 | |
| 3434 | flen = h2c->dfl - h2c->dpl; |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3435 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3436 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3437 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3438 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3439 | copy = alloc_trash_chunk(); |
| 3440 | if (!copy) { |
| 3441 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3442 | goto fail; |
| 3443 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3444 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3445 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3446 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3447 | } |
| 3448 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3449 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3450 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3451 | if (read_n32(hdrs) == h2c->dsi) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3452 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3453 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3454 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3455 | } |
| 3456 | |
Willy Tarreau | a01f45e | 2018-12-31 07:41:24 +0100 | [diff] [blame] | 3457 | if (flen < 5) { |
| 3458 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3459 | goto fail; |
| 3460 | } |
| 3461 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3462 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3463 | flen -= 5; |
| 3464 | } |
| 3465 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3466 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3467 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3468 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3469 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3470 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3471 | /* we can't retry a failed decompression operation so we must be very |
| 3472 | * careful not to take any risks. In practice the output buffer is |
| 3473 | * always empty except maybe for trailers, in which case we simply have |
| 3474 | * to wait for the upper layer to finish consuming what is available. |
| 3475 | */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3476 | |
| 3477 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3478 | htx = htx_from_buf(rxbuf); |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3479 | if (!htx_is_empty(htx)) { |
| 3480 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3481 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3482 | } |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3483 | } else { |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3484 | if (b_data(rxbuf)) { |
| 3485 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3486 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3487 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3488 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3489 | rxbuf->head = 0; |
| 3490 | try = b_size(rxbuf); |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3491 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3492 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3493 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3494 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3495 | sizeof(list)/sizeof(list[0]), tmp); |
| 3496 | if (outlen < 0) { |
| 3497 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3498 | goto fail; |
| 3499 | } |
| 3500 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3501 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3502 | * the parser's state to commit these changes and allow us to later |
| 3503 | * fail solely on the stream if needed. |
| 3504 | */ |
| 3505 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3506 | h2c->dfl = hole = 0; |
| 3507 | h2c->st0 = H2_CS_FRAME_H; |
| 3508 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3509 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3510 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3511 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3512 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3513 | goto trailers; |
| 3514 | |
| 3515 | /* This is the first HEADERS frame so it's a headers block */ |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3516 | if (htx) { |
| 3517 | /* HTX mode */ |
| 3518 | if (h2c->flags & H2_CF_IS_BACK) |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3519 | outlen = h2_make_htx_response(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3520 | else |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3521 | outlen = h2_make_htx_request(list, htx, &msgf, body_len); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3522 | } else { |
| 3523 | /* HTTP/1 mode */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 3524 | 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] | 3525 | if (outlen > 0) |
| 3526 | b_add(rxbuf, outlen); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3527 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3528 | |
| 3529 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3530 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3531 | goto fail; |
| 3532 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3533 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3534 | if (msgf & H2_MSGF_BODY) { |
| 3535 | /* a payload is present */ |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3536 | if (msgf & H2_MSGF_BODY_CL) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3537 | *flags |= H2_SF_DATA_CLEN; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3538 | if (htx) |
| 3539 | htx->extra = *body_len; |
| 3540 | } |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3541 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3542 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3543 | } |
| 3544 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3545 | done: |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3546 | /* indicate that a HEADERS frame was received for this stream, except |
| 3547 | * for 1xx responses. For 1xx responses, another HEADERS frame is |
| 3548 | * expected. |
| 3549 | */ |
| 3550 | if (!(msgf & H2_MSGF_RSP_1XX)) |
| 3551 | *flags |= H2_SF_HEADERS_RCVD; |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3552 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 3553 | 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] | 3554 | /* Mark the end of message, either using EOM in HTX or with the |
| 3555 | * trailing CRLF after the end of trailers. Note that DATA_CHNK |
| 3556 | * is not set during headers with END_STREAM. |
| 3557 | */ |
| 3558 | if (htx) { |
| 3559 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 3560 | goto fail; |
| 3561 | } |
| 3562 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3563 | if (!b_putblk(rxbuf, "\r\n", 2)) |
| 3564 | goto fail; |
| 3565 | } |
| 3566 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3567 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3568 | /* success */ |
| 3569 | ret = 1; |
| 3570 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3571 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3572 | /* 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] | 3573 | * move the remaining data over it. |
| 3574 | */ |
| 3575 | if (hole) { |
| 3576 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3577 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3578 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3579 | b_sub(&h2c->dbuf, hole); |
| 3580 | } |
| 3581 | |
Willy Tarreau | 97215ca | 2019-04-29 10:20:21 +0200 | [diff] [blame] | 3582 | if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) { |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3583 | /* too large frames */ |
| 3584 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3585 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3586 | } |
| 3587 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3588 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3589 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3590 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3591 | return ret; |
| 3592 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3593 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3594 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3595 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3596 | |
| 3597 | trailers: |
| 3598 | /* This is the last HEADERS frame hence a trailer */ |
| 3599 | |
| 3600 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3601 | /* It's a trailer but it's missing ES flag */ |
| 3602 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3603 | goto fail; |
| 3604 | } |
| 3605 | |
| 3606 | /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD |
| 3607 | * block, and when using chunks we must send the 0 CRLF marker. For |
| 3608 | * other modes, the trailers are silently dropped. |
| 3609 | */ |
| 3610 | if (htx) { |
| 3611 | if (!htx_add_endof(htx, HTX_BLK_EOD)) |
| 3612 | goto fail; |
Willy Tarreau | 5255f28 | 2019-01-03 18:41:05 +0100 | [diff] [blame] | 3613 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3614 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3615 | } |
| 3616 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3617 | /* Legacy mode with chunked encoding : we must finalize the |
| 3618 | * data block message emit the trailing CRLF */ |
| 3619 | if (!b_putblk(rxbuf, "0\r\n", 3)) |
| 3620 | goto fail; |
Willy Tarreau | e2b05cc | 2019-01-03 16:18:34 +0100 | [diff] [blame] | 3621 | |
| 3622 | outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try); |
| 3623 | if (outlen > 0) |
| 3624 | b_add(rxbuf, outlen); |
| 3625 | else |
| 3626 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3627 | } |
| 3628 | |
| 3629 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3630 | } |
| 3631 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3632 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 3633 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 3634 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 3635 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 3636 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 3637 | * 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] | 3638 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3639 | * send the current frame, 0 if it couldn't complete, in which case |
| 3640 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3641 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3642 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3643 | * have checked the frame header and ensured that the frame was complete or the |
| 3644 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3645 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3646 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3647 | { |
| 3648 | struct h2c *h2c = h2s->h2c; |
| 3649 | int block1, block2; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3650 | unsigned int flen = 0; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3651 | unsigned int chklen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3652 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3653 | struct buffer *csbuf; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3654 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3655 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3656 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3657 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3658 | if (!csbuf) { |
| 3659 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3660 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3661 | } |
| 3662 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3663 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3664 | flen = h2c->dfl - h2c->dpl; |
Olivier Houchard | 2f30883 | 2018-12-19 15:53:53 +0100 | [diff] [blame] | 3665 | if (h2c->proxy->options2 & PR_O2_USE_HTX) |
| 3666 | htx = htx_from_buf(csbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3667 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3668 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3669 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3670 | if (flen > b_data(&h2c->dbuf)) { |
| 3671 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3672 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3673 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3674 | } |
| 3675 | |
Willy Tarreau | a9b7796 | 2019-01-31 07:23:00 +0100 | [diff] [blame] | 3676 | if (htx) { |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3677 | block1 = htx_free_data_space(htx); |
| 3678 | if (!block1) { |
| 3679 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3680 | goto fail; |
| 3681 | } |
| 3682 | if (flen > block1) |
| 3683 | flen = block1; |
| 3684 | |
| 3685 | /* here, flen is the max we can copy into the output buffer */ |
| 3686 | block1 = b_contig_data(&h2c->dbuf, 0); |
| 3687 | if (flen > block1) |
| 3688 | flen = block1; |
| 3689 | |
| 3690 | if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) { |
| 3691 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3692 | goto fail; |
| 3693 | } |
| 3694 | |
| 3695 | b_del(&h2c->dbuf, flen); |
| 3696 | h2c->dfl -= flen; |
| 3697 | h2c->rcvd_c += flen; |
| 3698 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3699 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3700 | if (h2s->flags & H2_SF_DATA_CLEN) { |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3701 | h2s->body_len -= flen; |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 3702 | htx->extra = h2s->body_len; |
| 3703 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3704 | goto try_again; |
| 3705 | } |
| 3706 | else if (unlikely(b_space_wraps(csbuf))) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3707 | /* it doesn't fit and the buffer is fragmented, |
| 3708 | * so let's defragment it and try again. |
| 3709 | */ |
| 3710 | b_slow_realign(csbuf, trash.area, 0); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3711 | } |
| 3712 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3713 | /* chunked-encoding requires more room */ |
| 3714 | if (h2s->flags & H2_SF_DATA_CHNK) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3715 | chklen = MIN(flen, b_room(csbuf)); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3716 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 3717 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 3718 | (chklen < 1048576) ? 4 : 8; |
| 3719 | chklen += 4; // CRLF, CRLF |
| 3720 | } |
| 3721 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3722 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3723 | if (flen + chklen > b_room(csbuf)) { |
| 3724 | if (chklen >= b_room(csbuf)) { |
| 3725 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3726 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3727 | } |
| 3728 | flen = b_room(csbuf) - chklen; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3732 | /* emit the chunk size */ |
| 3733 | unsigned int chksz = flen; |
| 3734 | char str[10]; |
| 3735 | char *beg; |
| 3736 | |
| 3737 | beg = str + sizeof(str); |
| 3738 | *--beg = '\n'; |
| 3739 | *--beg = '\r'; |
| 3740 | do { |
| 3741 | *--beg = hextab[chksz & 0xF]; |
| 3742 | } while (chksz >>= 4); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3743 | b_putblk(csbuf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3744 | } |
| 3745 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3746 | /* Block1 is the length of the first block before the buffer wraps, |
| 3747 | * block2 is the optional second block to reach the end of the frame. |
| 3748 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3749 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3750 | if (block1 > flen) |
| 3751 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3752 | block2 = flen - block1; |
| 3753 | |
| 3754 | if (block1) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3755 | b_putblk(csbuf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3756 | |
| 3757 | if (block2) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3758 | b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3759 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3760 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3761 | /* emit the CRLF */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3762 | b_putblk(csbuf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3763 | } |
| 3764 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3765 | /* now mark the input data as consumed (will be deleted from the buffer |
| 3766 | * by the caller when seeing FRAME_A after sending the window update). |
| 3767 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3768 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3769 | h2c->dfl -= flen; |
| 3770 | h2c->rcvd_c += flen; |
| 3771 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 3772 | |
Willy Tarreau | 1915ca2 | 2019-01-24 11:49:37 +0100 | [diff] [blame] | 3773 | if (h2s->flags & H2_SF_DATA_CLEN) |
| 3774 | h2s->body_len -= flen; |
| 3775 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3776 | if (h2c->dfl > h2c->dpl) { |
| 3777 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3778 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3779 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3780 | } |
| 3781 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3782 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3783 | /* here we're done with the frame, all the payload (except padding) was |
| 3784 | * transferred. |
| 3785 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3786 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3787 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 3788 | if (htx) { |
| 3789 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3790 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3791 | goto fail; |
| 3792 | } |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3793 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3794 | else if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3795 | /* emit the trailing 0 CRLF CRLF */ |
| 3796 | if (b_room(csbuf) < 5) { |
| 3797 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3798 | goto fail; |
| 3799 | } |
| 3800 | chklen += 5; |
| 3801 | b_putblk(csbuf, "0\r\n\r\n", 5); |
| 3802 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3803 | } |
| 3804 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 3805 | h2c->rcvd_c += h2c->dpl; |
| 3806 | h2c->rcvd_s += h2c->dpl; |
| 3807 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3808 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3809 | if (htx) |
| 3810 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3811 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3812 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3813 | if (htx) |
| 3814 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3815 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3816 | } |
| 3817 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3818 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 3819 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 3820 | * number of bytes sent. The caller must check the stream's status to detect |
| 3821 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3822 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3823 | 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] | 3824 | { |
| 3825 | struct http_hdr list[MAX_HTTP_HDR]; |
| 3826 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3827 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3828 | struct buffer outbuf; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3829 | union h1_sl sl; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3830 | int es_now = 0; |
| 3831 | int ret = 0; |
| 3832 | int hdr; |
| 3833 | |
| 3834 | if (h2c_mux_busy(h2c, h2s)) { |
| 3835 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3836 | return 0; |
| 3837 | } |
| 3838 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3839 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3840 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3841 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3842 | return 0; |
| 3843 | } |
| 3844 | |
| 3845 | /* First, try to parse the H1 response and index it into <list>. |
| 3846 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 3847 | * block does not wrap and we can safely read it this way without |
| 3848 | * having to realign the buffer. |
| 3849 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3850 | 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] | 3851 | list, sizeof(list)/sizeof(list[0]), h1m, &sl); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3852 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 3853 | /* incomplete or invalid response, this is abnormal coming from |
| 3854 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 3855 | * so that won't be fixed, raise an error now. |
| 3856 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3857 | * FIXME: we should instead add the ability to only return a |
| 3858 | * 502 bad gateway. But in theory this is not supposed to |
| 3859 | * happen. |
| 3860 | */ |
| 3861 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3862 | ret = 0; |
| 3863 | goto end; |
| 3864 | } |
| 3865 | |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3866 | h2s->status = sl.st.status; |
Willy Tarreau | db72da0 | 2018-09-13 11:52:20 +0200 | [diff] [blame] | 3867 | |
| 3868 | /* certain statuses have no body or an empty one, regardless of |
| 3869 | * what the headers say. |
| 3870 | */ |
| 3871 | if (sl.st.status >= 100 && sl.st.status < 200) { |
| 3872 | h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK); |
| 3873 | h1m->curr_len = h1m->body_len = 0; |
| 3874 | } |
| 3875 | else if (sl.st.status == 204 || sl.st.status == 304) { |
| 3876 | /* no contents, claim c-len is present and set to zero */ |
| 3877 | h1m->flags &= ~H1_MF_CHNK; |
| 3878 | h1m->flags |= H1_MF_CLEN; |
| 3879 | h1m->curr_len = h1m->body_len = 0; |
| 3880 | } |
| 3881 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3882 | chunk_reset(&outbuf); |
| 3883 | |
| 3884 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3885 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3886 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3887 | outbuf.data = 0; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3888 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3889 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3890 | break; |
| 3891 | realign_again: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3892 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3893 | } |
| 3894 | |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3895 | if (outbuf.size < 9) |
| 3896 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3897 | |
| 3898 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3899 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 3900 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3901 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3902 | |
| 3903 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3904 | if (unlikely(list[0].v.len != 3)) { |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 3905 | /* this is an unparsable response */ |
| 3906 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3907 | ret = 0; |
| 3908 | goto end; |
| 3909 | } |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3910 | |
| 3911 | if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3912 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3913 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3914 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3915 | } |
| 3916 | |
| 3917 | /* encode all headers, stop at empty name */ |
| 3918 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 3919 | /* these ones do not exist in H2 and must be dropped. */ |
| 3920 | if (isteq(list[hdr].n, ist("connection")) || |
| 3921 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 3922 | isteq(list[hdr].n, ist("keep-alive")) || |
| 3923 | isteq(list[hdr].n, ist("upgrade")) || |
| 3924 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3925 | continue; |
| 3926 | |
| 3927 | if (isteq(list[hdr].n, ist(""))) |
| 3928 | break; // end |
| 3929 | |
| 3930 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 3931 | /* output full */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3932 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3933 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3934 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3935 | } |
| 3936 | } |
| 3937 | |
| 3938 | /* we may need to add END_STREAM */ |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 3939 | 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] | 3940 | es_now = 1; |
| 3941 | |
| 3942 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3943 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3944 | |
| 3945 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3946 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3947 | |
| 3948 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3949 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3950 | |
| 3951 | /* commit the H2 response */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3952 | b_add(&h2c->mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3953 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3954 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3955 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3956 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3957 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3958 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3959 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3960 | h2s->flags |= H2_SF_ES_SENT; |
| 3961 | if (h2s->st == H2_SS_OPEN) |
| 3962 | h2s->st = H2_SS_HLOC; |
| 3963 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3964 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3965 | } |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3966 | else if (h2s->status >= 100 && h2s->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3967 | /* 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] | 3968 | h1m_init_res(h1m); |
Willy Tarreau | 9b8cd1f | 2018-09-12 09:24:38 +0200 | [diff] [blame] | 3969 | 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] | 3970 | h2s->h1m.flags |= H1_MF_TOLOWER; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3971 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 3972 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 3973 | |
| 3974 | /* 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] | 3975 | |
| 3976 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 3977 | //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] | 3978 | return ret; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3979 | full: |
| 3980 | h1m_init_res(h1m); |
| 3981 | h1m->err_pos = -1; // don't care about errors on the response path |
| 3982 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3983 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3984 | ret = 0; |
| 3985 | goto end; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3986 | } |
| 3987 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3988 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 3989 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 3990 | * the number of bytes sent. The caller must check the stream's status to |
| 3991 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3992 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3993 | 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] | 3994 | { |
| 3995 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3996 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3997 | struct buffer outbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3998 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 3999 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4000 | int es_now = 0; |
| 4001 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4002 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4003 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4004 | |
| 4005 | if (h2c_mux_busy(h2c, h2s)) { |
| 4006 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4007 | goto end; |
| 4008 | } |
| 4009 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 4010 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4011 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4012 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4013 | goto end; |
| 4014 | } |
| 4015 | |
| 4016 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4017 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4018 | goto end; |
| 4019 | |
| 4020 | chunk_reset(&outbuf); |
| 4021 | |
| 4022 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4023 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4024 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4025 | outbuf.data = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4026 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4027 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4028 | break; |
| 4029 | realign_again: |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 4030 | /* If there are pending data in the output buffer, and we have |
| 4031 | * less than 1/4 of the mbuf's size and everything fits, we'll |
| 4032 | * still perform a copy anyway. Otherwise we'll pretend the mbuf |
| 4033 | * is full and wait, to save some slow realign calls. |
| 4034 | */ |
| 4035 | if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) { |
| 4036 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4037 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4038 | goto end; |
| 4039 | } |
| 4040 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4041 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4042 | } |
| 4043 | |
| 4044 | if (outbuf.size < 9) { |
| 4045 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4046 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4047 | goto end; |
| 4048 | } |
| 4049 | |
| 4050 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4051 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4052 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4053 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4054 | |
| 4055 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 4056 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4057 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 4058 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4059 | break; |
| 4060 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4061 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4062 | if ((long long)size > h1m->curr_len) |
| 4063 | size = h1m->curr_len; |
| 4064 | break; |
| 4065 | default: /* te:chunked : parse chunks */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4066 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 4067 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4068 | if (!ret) |
| 4069 | goto end; |
| 4070 | |
| 4071 | if (ret < 0) { |
| 4072 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4073 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4074 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4075 | goto end; |
| 4076 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4077 | max -= ret; |
| 4078 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4079 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4080 | h1m->state = H1_MSG_CHUNK_SIZE; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4081 | } |
| 4082 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4083 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4084 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 4085 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4086 | if (!ret) |
| 4087 | goto end; |
| 4088 | |
| 4089 | if (ret < 0) { |
| 4090 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 4091 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4092 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4093 | goto end; |
| 4094 | } |
| 4095 | |
| 4096 | size = chunk; |
| 4097 | h1m->curr_len = chunk; |
| 4098 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4099 | max -= ret; |
| 4100 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4101 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4102 | h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4103 | if (!size) |
| 4104 | goto send_empty; |
| 4105 | } |
| 4106 | |
| 4107 | /* in MSG_DATA state, continue below */ |
| 4108 | size = h1m->curr_len; |
| 4109 | break; |
| 4110 | } |
| 4111 | |
| 4112 | /* we have in <size> the exact number of bytes we need to copy from |
| 4113 | * the H1 buffer. We need to check this against the connection's and |
| 4114 | * the stream's send windows, and to ensure that this fits in the max |
| 4115 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4116 | * the frame header). The connection's flow control is applied last so |
| 4117 | * that we can use a separate list of streams which are immediately |
| 4118 | * unblocked on window opening. Note: we don't implement padding. |
| 4119 | */ |
| 4120 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4121 | if (size > max) |
| 4122 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4123 | |
| 4124 | if (size > h2s->mws) |
| 4125 | size = h2s->mws; |
| 4126 | |
| 4127 | if (size <= 0) { |
| 4128 | h2s->flags |= H2_SF_BLK_SFCTL; |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 4129 | if (h2s->send_wait) { |
| 4130 | LIST_DEL(&h2s->list); |
| 4131 | LIST_INIT(&h2s->list); |
| 4132 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4133 | goto end; |
| 4134 | } |
| 4135 | |
| 4136 | if (h2c->mfs && size > h2c->mfs) |
| 4137 | size = h2c->mfs; |
| 4138 | |
| 4139 | if (size + 9 > outbuf.size) { |
| 4140 | /* we have an opportunity for enlarging the too small |
| 4141 | * available space, let's try. |
| 4142 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4143 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4144 | goto realign_again; |
| 4145 | size = outbuf.size - 9; |
| 4146 | } |
| 4147 | |
| 4148 | if (size <= 0) { |
| 4149 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4150 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4151 | goto end; |
| 4152 | } |
| 4153 | |
| 4154 | if (size > h2c->mws) |
| 4155 | size = h2c->mws; |
| 4156 | |
| 4157 | if (size <= 0) { |
| 4158 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4159 | goto end; |
| 4160 | } |
| 4161 | |
| 4162 | /* copy whatever we can */ |
| 4163 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4164 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4165 | if (ret == 1) |
| 4166 | len2 = 0; |
| 4167 | |
| 4168 | if (!ret || len1 + len2 < size) { |
| 4169 | /* FIXME: must normally never happen */ |
| 4170 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4171 | goto end; |
| 4172 | } |
| 4173 | |
| 4174 | /* limit len1/len2 to size */ |
| 4175 | if (len1 + len2 > size) { |
| 4176 | int sub = len1 + len2 - size; |
| 4177 | |
| 4178 | if (len2 > sub) |
| 4179 | len2 -= sub; |
| 4180 | else { |
| 4181 | sub -= len2; |
| 4182 | len2 = 0; |
| 4183 | len1 -= sub; |
| 4184 | } |
| 4185 | } |
| 4186 | |
| 4187 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4188 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4189 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4190 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4191 | |
| 4192 | send_empty: |
| 4193 | /* we may need to add END_STREAM */ |
| 4194 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 4195 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4196 | * |
| 4197 | * FIXME: what we do here is not correct because we send end_stream |
| 4198 | * before knowing if we'll have to send a HEADERS frame for the |
| 4199 | * trailers. More importantly we're not consuming the trailing CRLF |
| 4200 | * after the end of trailers, so it will be left to the caller to |
| 4201 | * eat it. The right way to do it would be to measure trailers here |
| 4202 | * and to send ES only if there are no trailers. |
| 4203 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4204 | */ |
| 4205 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4206 | !h1m->curr_len || h1m->state >= H1_MSG_DONE) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4207 | es_now = 1; |
| 4208 | |
| 4209 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4210 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4211 | |
| 4212 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4213 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4214 | |
| 4215 | /* commit the H2 response */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4216 | b_add(&h2c->mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4217 | |
| 4218 | /* consume incoming H1 response */ |
| 4219 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4220 | max -= size; |
| 4221 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4222 | total += size; |
| 4223 | h1m->curr_len -= size; |
| 4224 | h2s->mws -= size; |
| 4225 | h2c->mws -= size; |
| 4226 | |
| 4227 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4228 | h1m->state = H1_MSG_CHUNK_CRLF; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4229 | goto new_frame; |
| 4230 | } |
| 4231 | } |
| 4232 | |
| 4233 | if (es_now) { |
| 4234 | if (h2s->st == H2_SS_OPEN) |
| 4235 | h2s->st = H2_SS_HLOC; |
| 4236 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4237 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4238 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4239 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 4240 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4241 | total += max; |
| 4242 | ofs += max; |
| 4243 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4244 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4245 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4246 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4247 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4248 | h2s->flags |= H2_SF_ES_SENT; |
| 4249 | } |
| 4250 | |
| 4251 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4252 | 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] | 4253 | return total; |
| 4254 | } |
| 4255 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4256 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 4257 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4258 | * must check the stream's status to detect any error which might have happened |
| 4259 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4260 | * from the message. The htx message is assumed to be valid since produced from |
| 4261 | * the internal code, hence it contains a start line, an optional series of |
| 4262 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4263 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4264 | */ |
| 4265 | static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
| 4266 | { |
| 4267 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4268 | struct h2c *h2c = h2s->h2c; |
| 4269 | struct htx_blk *blk; |
| 4270 | struct htx_blk *blk_end; |
| 4271 | struct buffer outbuf; |
| 4272 | struct htx_sl *sl; |
| 4273 | enum htx_blk_type type; |
| 4274 | int es_now = 0; |
| 4275 | int ret = 0; |
| 4276 | int hdr; |
| 4277 | int idx; |
| 4278 | |
| 4279 | if (h2c_mux_busy(h2c, h2s)) { |
| 4280 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4281 | return 0; |
| 4282 | } |
| 4283 | |
| 4284 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4285 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4286 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4287 | return 0; |
| 4288 | } |
| 4289 | |
| 4290 | /* determine the first block which must not be deleted, blk_end may |
| 4291 | * be NULL if all blocks have to be deleted. |
| 4292 | */ |
| 4293 | idx = htx_get_head(htx); |
| 4294 | blk_end = NULL; |
| 4295 | while (idx != -1) { |
| 4296 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4297 | idx = htx_get_next(htx, idx); |
| 4298 | if (type == HTX_BLK_EOH) { |
| 4299 | if (idx != -1) |
| 4300 | blk_end = htx_get_blk(htx, idx); |
| 4301 | break; |
| 4302 | } |
| 4303 | } |
| 4304 | |
| 4305 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4306 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4307 | ALREADY_CHECKED(sl); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4308 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4309 | if (h2s->status < 100 || h2s->status > 999) |
| 4310 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4311 | |
| 4312 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4313 | hdr = 0; |
| 4314 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4315 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4316 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4317 | blk = htx_get_blk(htx, idx); |
| 4318 | type = htx_get_blk_type(blk); |
| 4319 | |
| 4320 | if (type == HTX_BLK_UNUSED) |
| 4321 | continue; |
| 4322 | |
| 4323 | if (type != HTX_BLK_HDR) |
| 4324 | break; |
| 4325 | |
| 4326 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4327 | goto fail; |
| 4328 | |
| 4329 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4330 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4331 | hdr++; |
| 4332 | } |
| 4333 | |
| 4334 | /* marker for end of headers */ |
| 4335 | list[hdr].n = ist(""); |
| 4336 | |
| 4337 | if (h2s->status == 204 || h2s->status == 304) { |
| 4338 | /* no contents, claim c-len is present and set to zero */ |
| 4339 | es_now = 1; |
| 4340 | } |
| 4341 | |
| 4342 | chunk_reset(&outbuf); |
| 4343 | |
| 4344 | while (1) { |
| 4345 | outbuf.area = b_tail(&h2c->mbuf); |
| 4346 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4347 | outbuf.data = 0; |
| 4348 | |
| 4349 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4350 | break; |
| 4351 | realign_again: |
| 4352 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4353 | } |
| 4354 | |
| 4355 | if (outbuf.size < 9) |
| 4356 | goto full; |
| 4357 | |
| 4358 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4359 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4360 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4361 | outbuf.data = 9; |
| 4362 | |
| 4363 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4364 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4365 | if (b_space_wraps(&h2c->mbuf)) |
| 4366 | goto realign_again; |
| 4367 | goto full; |
| 4368 | } |
| 4369 | |
| 4370 | /* encode all headers, stop at empty name */ |
| 4371 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4372 | /* these ones do not exist in H2 and must be dropped. */ |
| 4373 | if (isteq(list[hdr].n, ist("connection")) || |
| 4374 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4375 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4376 | isteq(list[hdr].n, ist("upgrade")) || |
| 4377 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4378 | continue; |
| 4379 | |
| 4380 | if (isteq(list[hdr].n, ist(""))) |
| 4381 | break; // end |
| 4382 | |
| 4383 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4384 | /* output full */ |
| 4385 | if (b_space_wraps(&h2c->mbuf)) |
| 4386 | goto realign_again; |
| 4387 | goto full; |
| 4388 | } |
| 4389 | } |
| 4390 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4391 | /* we may need to add END_STREAM except for 1xx responses. |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4392 | * FIXME: we should also set it when we know for sure that the |
| 4393 | * content-length is zero as well as on 204/304 |
| 4394 | */ |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4395 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM && |
| 4396 | (h2s->status >= 200 || h2s->status == 101)) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4397 | es_now = 1; |
| 4398 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4399 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4400 | es_now = 1; |
| 4401 | |
| 4402 | /* update the frame's size */ |
| 4403 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4404 | |
| 4405 | if (es_now) |
| 4406 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4407 | |
| 4408 | /* commit the H2 response */ |
| 4409 | b_add(&h2c->mbuf, outbuf.data); |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 4410 | |
| 4411 | /* indicates the HEADERS frame was sent, except for 1xx responses. For |
| 4412 | * 1xx responses, another HEADERS frame is expected. |
| 4413 | */ |
| 4414 | if (h2s->status >= 200 || h2s->status == 101) |
| 4415 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4416 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4417 | if (es_now) { |
| 4418 | h2s->flags |= H2_SF_ES_SENT; |
| 4419 | if (h2s->st == H2_SS_OPEN) |
| 4420 | h2s->st = H2_SS_HLOC; |
| 4421 | else |
| 4422 | h2s_close(h2s); |
| 4423 | } |
| 4424 | |
| 4425 | /* OK we could properly deliver the response */ |
| 4426 | |
| 4427 | /* remove all header blocks including the EOH and compute the |
| 4428 | * corresponding size. |
| 4429 | * |
| 4430 | * FIXME: We should remove everything when es_now is set. |
| 4431 | */ |
| 4432 | ret = 0; |
| 4433 | idx = htx_get_head(htx); |
| 4434 | blk = htx_get_blk(htx, idx); |
| 4435 | while (blk != blk_end) { |
| 4436 | ret += htx_get_blksz(blk); |
| 4437 | blk = htx_remove_blk(htx, blk); |
| 4438 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4439 | |
| 4440 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4441 | htx_remove_blk(htx, blk_end); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4442 | end: |
| 4443 | return ret; |
| 4444 | full: |
| 4445 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4446 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4447 | ret = 0; |
| 4448 | goto end; |
| 4449 | fail: |
| 4450 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4451 | * list etc go here (unrecoverable errors). |
| 4452 | */ |
| 4453 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4454 | ret = 0; |
| 4455 | goto end; |
| 4456 | } |
| 4457 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4458 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4459 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4460 | * must check the stream's status to detect any error which might have happened |
| 4461 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4462 | * from the message. The htx message is assumed to be valid since produced from |
| 4463 | * the internal code, hence it contains a start line, an optional series of |
| 4464 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4465 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4466 | */ |
| 4467 | static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
| 4468 | { |
| 4469 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4470 | struct h2c *h2c = h2s->h2c; |
| 4471 | struct htx_blk *blk; |
| 4472 | struct htx_blk *blk_end; |
| 4473 | struct buffer outbuf; |
| 4474 | struct htx_sl *sl; |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4475 | struct ist meth, path, auth; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4476 | enum htx_blk_type type; |
| 4477 | int es_now = 0; |
| 4478 | int ret = 0; |
| 4479 | int hdr; |
| 4480 | int idx; |
| 4481 | |
| 4482 | if (h2c_mux_busy(h2c, h2s)) { |
| 4483 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4484 | return 0; |
| 4485 | } |
| 4486 | |
| 4487 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4488 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4489 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4490 | return 0; |
| 4491 | } |
| 4492 | |
| 4493 | /* determine the first block which must not be deleted, blk_end may |
| 4494 | * be NULL if all blocks have to be deleted. |
| 4495 | */ |
| 4496 | idx = htx_get_head(htx); |
| 4497 | blk_end = NULL; |
| 4498 | while (idx != -1) { |
| 4499 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4500 | idx = htx_get_next(htx, idx); |
| 4501 | if (type == HTX_BLK_EOH) { |
| 4502 | if (idx != -1) |
| 4503 | blk_end = htx_get_blk(htx, idx); |
| 4504 | break; |
| 4505 | } |
| 4506 | } |
| 4507 | |
| 4508 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4509 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4510 | ALREADY_CHECKED(sl); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4511 | meth = htx_sl_req_meth(sl); |
| 4512 | path = htx_sl_req_uri(sl); |
| 4513 | |
| 4514 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4515 | hdr = 0; |
| 4516 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4517 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4518 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4519 | blk = htx_get_blk(htx, idx); |
| 4520 | type = htx_get_blk_type(blk); |
| 4521 | |
| 4522 | if (type == HTX_BLK_UNUSED) |
| 4523 | continue; |
| 4524 | |
| 4525 | if (type != HTX_BLK_HDR) |
| 4526 | break; |
| 4527 | |
| 4528 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4529 | goto fail; |
| 4530 | |
| 4531 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4532 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4533 | hdr++; |
| 4534 | } |
| 4535 | |
| 4536 | /* marker for end of headers */ |
| 4537 | list[hdr].n = ist(""); |
| 4538 | |
| 4539 | chunk_reset(&outbuf); |
| 4540 | |
| 4541 | while (1) { |
| 4542 | outbuf.area = b_tail(&h2c->mbuf); |
| 4543 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4544 | outbuf.data = 0; |
| 4545 | |
| 4546 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4547 | break; |
| 4548 | realign_again: |
| 4549 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4550 | } |
| 4551 | |
| 4552 | if (outbuf.size < 9) |
| 4553 | goto full; |
| 4554 | |
| 4555 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4556 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4557 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4558 | outbuf.data = 9; |
| 4559 | |
| 4560 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4561 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4562 | if (b_space_wraps(&h2c->mbuf)) |
| 4563 | goto realign_again; |
| 4564 | goto full; |
| 4565 | } |
| 4566 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4567 | /* RFC7540 #8.3: the CONNECT method must have : |
| 4568 | * - :authority set to the URI part (host:port) |
| 4569 | * - :method set to CONNECT |
| 4570 | * - :scheme and :path omitted |
| 4571 | */ |
| 4572 | if (sl->info.req.meth != HTTP_METH_CONNECT) { |
| 4573 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
| 4574 | if (!hpack_encode_scheme(&outbuf, ist("https"))) { |
| 4575 | /* output full */ |
| 4576 | if (b_space_wraps(&h2c->mbuf)) |
| 4577 | goto realign_again; |
| 4578 | goto full; |
| 4579 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4580 | |
Willy Tarreau | 5be92ff | 2019-02-01 15:51:59 +0100 | [diff] [blame] | 4581 | /* encode the path, which necessarily is the second one */ |
| 4582 | if (!hpack_encode_path(&outbuf, path)) { |
| 4583 | /* output full */ |
| 4584 | if (b_space_wraps(&h2c->mbuf)) |
| 4585 | goto realign_again; |
| 4586 | goto full; |
| 4587 | } |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4588 | |
| 4589 | /* look for the Host header and place it in :authority */ |
| 4590 | auth = ist2(NULL, 0); |
| 4591 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4592 | if (isteq(list[hdr].n, ist(""))) |
| 4593 | break; // end |
| 4594 | |
| 4595 | if (isteq(list[hdr].n, ist("host"))) { |
| 4596 | auth = list[hdr].v; |
| 4597 | break; |
| 4598 | } |
| 4599 | } |
| 4600 | } |
| 4601 | else { |
| 4602 | /* for CONNECT, :authority is taken from the path */ |
| 4603 | auth = path; |
| 4604 | } |
| 4605 | |
| 4606 | if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) { |
| 4607 | /* output full */ |
| 4608 | if (b_space_wraps(&h2c->mbuf)) |
| 4609 | goto realign_again; |
| 4610 | goto full; |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4611 | } |
| 4612 | |
| 4613 | /* encode all headers, stop at empty name */ |
| 4614 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4615 | /* these ones do not exist in H2 and must be dropped. */ |
| 4616 | if (isteq(list[hdr].n, ist("connection")) || |
Willy Tarreau | 053c157 | 2019-02-01 16:13:59 +0100 | [diff] [blame] | 4617 | isteq(list[hdr].n, ist("host")) || |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4618 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4619 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4620 | isteq(list[hdr].n, ist("upgrade")) || |
| 4621 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4622 | continue; |
| 4623 | |
| 4624 | if (isteq(list[hdr].n, ist(""))) |
| 4625 | break; // end |
| 4626 | |
| 4627 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4628 | /* output full */ |
| 4629 | if (b_space_wraps(&h2c->mbuf)) |
| 4630 | goto realign_again; |
| 4631 | goto full; |
| 4632 | } |
| 4633 | } |
| 4634 | |
| 4635 | /* we may need to add END_STREAM if we have no body : |
| 4636 | * - request already closed, or : |
| 4637 | * - no transfer-encoding, and : |
| 4638 | * - no content-length or content-length:0 |
| 4639 | * Fixme: this doesn't take into account CONNECT requests. |
| 4640 | */ |
| 4641 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4642 | es_now = 1; |
| 4643 | |
| 4644 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4645 | es_now = 1; |
| 4646 | |
Willy Tarreau | 927b88b | 2019-03-04 08:03:25 +0100 | [diff] [blame] | 4647 | if (!h2s->cs || h2s->cs->flags & CS_FL_SHW) |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4648 | es_now = 1; |
| 4649 | |
| 4650 | /* update the frame's size */ |
| 4651 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4652 | |
| 4653 | if (es_now) |
| 4654 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4655 | |
| 4656 | /* commit the H2 response */ |
| 4657 | b_add(&h2c->mbuf, outbuf.data); |
| 4658 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4659 | h2s->st = H2_SS_OPEN; |
| 4660 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4661 | if (es_now) { |
| 4662 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4663 | h2s->flags |= H2_SF_ES_SENT; |
| 4664 | h2s->st = H2_SS_HLOC; |
| 4665 | } |
| 4666 | |
| 4667 | /* remove all header blocks including the EOH and compute the |
| 4668 | * corresponding size. |
| 4669 | * |
| 4670 | * FIXME: We should remove everything when es_now is set. |
| 4671 | */ |
| 4672 | ret = 0; |
| 4673 | idx = htx_get_head(htx); |
| 4674 | blk = htx_get_blk(htx, idx); |
| 4675 | while (blk != blk_end) { |
| 4676 | ret += htx_get_blksz(blk); |
| 4677 | blk = htx_remove_blk(htx, blk); |
| 4678 | } |
| 4679 | |
| 4680 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4681 | htx_remove_blk(htx, blk_end); |
| 4682 | |
| 4683 | end: |
| 4684 | return ret; |
| 4685 | full: |
| 4686 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4687 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4688 | ret = 0; |
| 4689 | goto end; |
| 4690 | fail: |
| 4691 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4692 | * list etc go here (unrecoverable errors). |
| 4693 | */ |
| 4694 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4695 | ret = 0; |
| 4696 | goto end; |
| 4697 | } |
| 4698 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4699 | /* 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] | 4700 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 4701 | * caller must check the stream's status to detect any error which might have |
| 4702 | * happened subsequently to a successful send. Returns the number of data bytes |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4703 | * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte. |
| 4704 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4705 | 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] | 4706 | { |
| 4707 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4708 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4709 | struct buffer outbuf; |
| 4710 | size_t total = 0; |
| 4711 | int es_now = 0; |
| 4712 | int bsize; /* htx block size */ |
| 4713 | int fsize; /* h2 frame size */ |
| 4714 | struct htx_blk *blk; |
| 4715 | enum htx_blk_type type; |
| 4716 | int idx; |
| 4717 | |
| 4718 | if (h2c_mux_busy(h2c, h2s)) { |
| 4719 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4720 | goto end; |
| 4721 | } |
| 4722 | |
| 4723 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4724 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4725 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4726 | goto end; |
| 4727 | } |
| 4728 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4729 | htx = htx_from_buf(buf); |
| 4730 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4731 | /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However, |
| 4732 | * while looping, we can meet an HTX_BLK_EOM block that we'll leave to |
| 4733 | * the caller to handle. |
| 4734 | */ |
| 4735 | |
| 4736 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4737 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4738 | goto end; |
| 4739 | |
| 4740 | idx = htx_get_head(htx); |
| 4741 | blk = htx_get_blk(htx, idx); |
| 4742 | type = htx_get_blk_type(blk); // DATA or EOD or EOM |
| 4743 | bsize = htx_get_blksz(blk); |
| 4744 | fsize = bsize; |
| 4745 | |
| 4746 | if (type == HTX_BLK_EOD) { |
| 4747 | /* if we have an EOD, we're dealing with chunked data. We may |
| 4748 | * have a set of trailers after us that the caller will want to |
| 4749 | * deal with. Let's simply remove the EOD and return. |
| 4750 | */ |
| 4751 | htx_remove_blk(htx, blk); |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4752 | total++; // EOD counts as one byte |
| 4753 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4754 | goto end; |
| 4755 | } |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 4756 | else if (type == HTX_BLK_EOM) { |
| 4757 | if (h2s->flags & H2_SF_ES_SENT) { |
| 4758 | /* ES already sent */ |
| 4759 | htx_remove_blk(htx, blk); |
| 4760 | total++; // EOM counts as one byte |
| 4761 | count--; |
| 4762 | goto end; |
| 4763 | } |
| 4764 | } |
| 4765 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4766 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4767 | |
| 4768 | /* Perform some optimizations to reduce the number of buffer copies. |
| 4769 | * First, if the mux's buffer is empty and the htx area contains |
| 4770 | * exactly one data block of the same size as the requested count, and |
| 4771 | * this count fits within the frame size, the stream's window size, and |
| 4772 | * the connection's window size, then it's possible to simply swap the |
| 4773 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 4774 | * length to match the entire DATA HTX block in the middle. In this |
| 4775 | * case we perform a true zero-copy operation from end-to-end. This is |
| 4776 | * the situation that happens all the time with large files. Second, if |
| 4777 | * this is not possible, but the mux's output buffer is empty, we still |
| 4778 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 4779 | * making the intermediary buffer's area point to the output buffer's |
| 4780 | * area. In this case we want to skip the HTX header to make sure that |
| 4781 | * copies remain aligned and that this operation remains possible all |
| 4782 | * the time. This goes for headers, data blocks and any data extracted |
| 4783 | * from the HTX blocks. |
| 4784 | */ |
| 4785 | if (unlikely(fsize == count && |
| 4786 | htx->used == 1 && type == HTX_BLK_DATA && |
| 4787 | fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
| 4788 | void *old_area = h2c->mbuf.area; |
| 4789 | |
| 4790 | if (b_data(&h2c->mbuf)) { |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4791 | /* Too bad there are data left there. We're willing to memcpy/memmove |
| 4792 | * up to 1/4 of the buffer, which means that it's OK to copy a large |
| 4793 | * frame into a buffer containing few data if it needs to be realigned, |
| 4794 | * and that it's also OK to copy few data without realigning. Otherwise |
| 4795 | * 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] | 4796 | */ |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4797 | if (fsize + 9 <= b_room(&h2c->mbuf) && |
| 4798 | (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 || |
| 4799 | (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] | 4800 | goto copy; |
Willy Tarreau | 8ab128c | 2019-03-21 17:47:28 +0100 | [diff] [blame] | 4801 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4802 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4803 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4804 | goto end; |
| 4805 | } |
| 4806 | |
| 4807 | /* map an H2 frame to the HTX block so that we can put the |
| 4808 | * frame header there. |
| 4809 | */ |
| 4810 | h2c->mbuf.area = buf->area; |
Olivier Houchard | 84cca66 | 2018-12-14 16:28:08 +0100 | [diff] [blame] | 4811 | h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4812 | h2c->mbuf.data = fsize + 9; |
| 4813 | outbuf.area = b_head(&h2c->mbuf); |
| 4814 | |
| 4815 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 4816 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4817 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4818 | h2_set_frame_size(outbuf.area, fsize); |
| 4819 | |
| 4820 | /* update windows */ |
| 4821 | h2s->mws -= fsize; |
| 4822 | h2c->mws -= fsize; |
| 4823 | |
| 4824 | /* and exchange with our old area */ |
| 4825 | buf->area = old_area; |
| 4826 | buf->data = buf->head = 0; |
| 4827 | total += fsize; |
| 4828 | goto end; |
| 4829 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4830 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4831 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4832 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 4833 | |
| 4834 | while (1) { |
| 4835 | outbuf.area = b_tail(&h2c->mbuf); |
| 4836 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4837 | outbuf.data = 0; |
| 4838 | |
| 4839 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4840 | break; |
| 4841 | realign_again: |
| 4842 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4843 | } |
| 4844 | |
| 4845 | if (outbuf.size < 9) { |
| 4846 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4847 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4848 | goto end; |
| 4849 | } |
| 4850 | |
| 4851 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 4852 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4853 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4854 | outbuf.data = 9; |
| 4855 | |
| 4856 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 4857 | * the HTX buffer. We need to check this against the connection's and |
| 4858 | * the stream's send windows, and to ensure that this fits in the max |
| 4859 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4860 | * the frame header). The connection's flow control is applied last so |
| 4861 | * that we can use a separate list of streams which are immediately |
| 4862 | * unblocked on window opening. Note: we don't implement padding. |
| 4863 | */ |
| 4864 | |
| 4865 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 4866 | * empty frame, thus we force it to zero here. |
| 4867 | */ |
| 4868 | if (type == HTX_BLK_EOM) |
| 4869 | bsize = fsize = 0; |
| 4870 | |
| 4871 | if (!fsize) |
| 4872 | goto send_empty; |
| 4873 | |
| 4874 | if (h2s->mws <= 0) { |
| 4875 | h2s->flags |= H2_SF_BLK_SFCTL; |
| 4876 | if (h2s->send_wait) { |
| 4877 | LIST_DEL(&h2s->list); |
| 4878 | LIST_INIT(&h2s->list); |
| 4879 | } |
| 4880 | goto end; |
| 4881 | } |
| 4882 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4883 | if (fsize > count) |
| 4884 | fsize = count; |
| 4885 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4886 | if (fsize > h2s->mws) |
| 4887 | fsize = h2s->mws; // >0 |
| 4888 | |
| 4889 | if (h2c->mfs && fsize > h2c->mfs) |
| 4890 | fsize = h2c->mfs; // >0 |
| 4891 | |
| 4892 | if (fsize + 9 > outbuf.size) { |
| 4893 | /* we have an opportunity for enlarging the too small |
| 4894 | * available space, let's try. |
| 4895 | * FIXME: is this really interesting to do? Maybe we'll |
| 4896 | * spend lots of time realigning instead of using two |
| 4897 | * frames. |
| 4898 | */ |
| 4899 | if (b_space_wraps(&h2c->mbuf)) |
| 4900 | goto realign_again; |
| 4901 | fsize = outbuf.size - 9; |
| 4902 | |
| 4903 | if (fsize <= 0) { |
| 4904 | /* no need to send an empty frame here */ |
| 4905 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4906 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4907 | goto end; |
| 4908 | } |
| 4909 | } |
| 4910 | |
| 4911 | if (h2c->mws <= 0) { |
| 4912 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4913 | goto end; |
| 4914 | } |
| 4915 | |
| 4916 | if (fsize > h2c->mws) |
| 4917 | fsize = h2c->mws; |
| 4918 | |
| 4919 | /* now let's copy this this into the output buffer */ |
| 4920 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 4921 | h2s->mws -= fsize; |
| 4922 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4923 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4924 | |
| 4925 | send_empty: |
| 4926 | /* update the frame's size */ |
| 4927 | h2_set_frame_size(outbuf.area, fsize); |
| 4928 | |
| 4929 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 4930 | * meeting EOM. We should optimize this later. |
| 4931 | */ |
| 4932 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4933 | total++; // EOM counts as one byte |
| 4934 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4935 | es_now = 1; |
| 4936 | } |
| 4937 | |
| 4938 | if (es_now) |
| 4939 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 4940 | |
| 4941 | /* commit the H2 response */ |
| 4942 | b_add(&h2c->mbuf, fsize + 9); |
| 4943 | |
| 4944 | /* consume incoming HTX block, including EOM */ |
| 4945 | total += fsize; |
| 4946 | if (fsize == bsize) { |
| 4947 | htx_remove_blk(htx, blk); |
| 4948 | if (fsize) |
| 4949 | goto new_frame; |
| 4950 | } else { |
| 4951 | /* we've truncated this block */ |
| 4952 | htx_cut_data_blk(htx, blk, fsize); |
| 4953 | } |
| 4954 | |
| 4955 | if (es_now) { |
| 4956 | if (h2s->st == H2_SS_OPEN) |
| 4957 | h2s->st = H2_SS_HLOC; |
| 4958 | else |
| 4959 | h2s_close(h2s); |
| 4960 | |
| 4961 | h2s->flags |= H2_SF_ES_SENT; |
| 4962 | } |
| 4963 | |
| 4964 | end: |
| 4965 | return total; |
| 4966 | } |
| 4967 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4968 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 4969 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 4970 | * processed. The caller must check the stream's status to detect any error |
| 4971 | * which might have happened subsequently to a successful send. The htx blocks |
| 4972 | * are automatically removed from the message. The htx message is assumed to be |
| 4973 | * valid since produced from the internal code. Processing stops when meeting |
| 4974 | * the EOM, which is also removed. All trailers are processed at once and sent |
| 4975 | * as a single frame. The ES flag is always set. |
| 4976 | */ |
| 4977 | static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx) |
| 4978 | { |
| 4979 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4980 | struct h2c *h2c = h2s->h2c; |
| 4981 | struct htx_blk *blk; |
| 4982 | struct htx_blk *blk_end; |
| 4983 | struct buffer outbuf; |
| 4984 | struct h1m h1m; |
| 4985 | enum htx_blk_type type; |
| 4986 | uint32_t size; |
| 4987 | int ret = 0; |
| 4988 | int hdr; |
| 4989 | int idx; |
| 4990 | void *start; |
| 4991 | |
| 4992 | if (h2c_mux_busy(h2c, h2s)) { |
| 4993 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4994 | goto end; |
| 4995 | } |
| 4996 | |
| 4997 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4998 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4999 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5000 | goto end; |
| 5001 | } |
| 5002 | |
| 5003 | /* The principle is that we parse each and every trailers block using |
| 5004 | * the H1 headers parser, and append it to the list. We don't proceed |
| 5005 | * until EOM is met. blk_end will point to the EOM block. |
| 5006 | */ |
| 5007 | hdr = 0; |
| 5008 | memset(list, 0, sizeof(list)); |
| 5009 | blk_end = NULL; |
| 5010 | |
| 5011 | for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) { |
| 5012 | blk = htx_get_blk(htx, idx); |
| 5013 | type = htx_get_blk_type(blk); |
| 5014 | |
| 5015 | if (type == HTX_BLK_UNUSED) |
| 5016 | continue; |
| 5017 | |
| 5018 | if (type != HTX_BLK_TLR) { |
| 5019 | if (type == HTX_BLK_EOM) |
| 5020 | blk_end = blk; |
| 5021 | break; |
| 5022 | } |
| 5023 | |
| 5024 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 5025 | goto fail; |
| 5026 | |
| 5027 | size = htx_get_blksz(blk); |
| 5028 | start = htx_get_blk_ptr(htx, blk); |
| 5029 | |
| 5030 | h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER; |
| 5031 | h1m.err_pos = 0; |
| 5032 | ret = h1_headers_to_hdr_list(start, start + size, |
| 5033 | list + hdr, sizeof(list)/sizeof(list[0]) - hdr, |
| 5034 | &h1m, NULL); |
| 5035 | if (ret < 0) |
| 5036 | goto fail; |
| 5037 | |
| 5038 | /* ret == 0 if an incomplete trailers block was found (missing |
| 5039 | * empty line), or > 0 if it was found. We have to continue on |
| 5040 | * incomplete messages because the trailers block might be |
| 5041 | * incomplete. |
| 5042 | */ |
| 5043 | |
| 5044 | /* search the new end */ |
| 5045 | while (hdr <= sizeof(list)/sizeof(list[0])) { |
| 5046 | if (!list[hdr].n.len) |
| 5047 | break; |
| 5048 | hdr++; |
| 5049 | } |
| 5050 | } |
| 5051 | |
| 5052 | if (!blk_end) |
| 5053 | goto end; // end not found yet |
| 5054 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5055 | chunk_reset(&outbuf); |
| 5056 | |
| 5057 | while (1) { |
| 5058 | outbuf.area = b_tail(&h2c->mbuf); |
| 5059 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 5060 | outbuf.data = 0; |
| 5061 | |
| 5062 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 5063 | break; |
| 5064 | realign_again: |
| 5065 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 5066 | } |
| 5067 | |
| 5068 | if (outbuf.size < 9) |
| 5069 | goto full; |
| 5070 | |
| 5071 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 5072 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 5073 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 5074 | outbuf.data = 9; |
| 5075 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5076 | /* encode all headers */ |
| 5077 | for (idx = 0; idx < hdr; idx++) { |
| 5078 | /* these ones do not exist in H2 or must not appear in |
| 5079 | * trailers and must be dropped. |
| 5080 | */ |
| 5081 | if (isteq(list[idx].n, ist("host")) || |
| 5082 | isteq(list[idx].n, ist("content-length")) || |
| 5083 | isteq(list[idx].n, ist("connection")) || |
| 5084 | isteq(list[idx].n, ist("proxy-connection")) || |
| 5085 | isteq(list[idx].n, ist("keep-alive")) || |
| 5086 | isteq(list[idx].n, ist("upgrade")) || |
| 5087 | isteq(list[idx].n, ist("te")) || |
| 5088 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 5089 | continue; |
| 5090 | |
| 5091 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 5092 | /* output full */ |
| 5093 | if (b_space_wraps(&h2c->mbuf)) |
| 5094 | goto realign_again; |
| 5095 | goto full; |
| 5096 | } |
| 5097 | } |
| 5098 | |
Willy Tarreau | 67b8cae | 2019-02-21 18:16:35 +0100 | [diff] [blame] | 5099 | if (!hdr) { |
| 5100 | /* here we have a problem, we've received an empty trailers |
| 5101 | * block followed by an EOM. Because of this we can't send a |
| 5102 | * HEADERS frame, so we have to cheat and instead send an empty |
| 5103 | * DATA frame conveying the ES flag. |
| 5104 | */ |
| 5105 | outbuf.area[3] = H2_FT_DATA; |
| 5106 | outbuf.area[4] = H2_F_DATA_END_STREAM; |
| 5107 | } |
| 5108 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5109 | /* update the frame's size */ |
| 5110 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 5111 | |
| 5112 | /* commit the H2 response */ |
| 5113 | b_add(&h2c->mbuf, outbuf.data); |
| 5114 | h2s->flags |= H2_SF_ES_SENT; |
| 5115 | |
| 5116 | if (h2s->st == H2_SS_OPEN) |
| 5117 | h2s->st = H2_SS_HLOC; |
| 5118 | else |
| 5119 | h2s_close(h2s); |
| 5120 | |
| 5121 | /* OK we could properly deliver the response */ |
| 5122 | done: |
| 5123 | /* remove all header blocks including EOM and compute the corresponding size. */ |
| 5124 | ret = 0; |
| 5125 | idx = htx_get_head(htx); |
| 5126 | blk = htx_get_blk(htx, idx); |
| 5127 | while (blk != blk_end) { |
| 5128 | ret += htx_get_blksz(blk); |
| 5129 | blk = htx_remove_blk(htx, blk); |
| 5130 | } |
| 5131 | blk = htx_remove_blk(htx, blk); |
| 5132 | end: |
| 5133 | return ret; |
| 5134 | full: |
| 5135 | h2c->flags |= H2_CF_MUX_MFULL; |
| 5136 | h2s->flags |= H2_SF_BLK_MROOM; |
| 5137 | ret = 0; |
| 5138 | goto end; |
| 5139 | fail: |
| 5140 | /* unparsable HTX messages, too large ones to be produced in the local |
| 5141 | * list etc go here (unrecoverable errors). |
| 5142 | */ |
| 5143 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5144 | ret = 0; |
| 5145 | goto end; |
| 5146 | } |
| 5147 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5148 | /* Called from the upper layer, to subscribe to events, such as being able to send. |
| 5149 | * The <param> argument here is supposed to be a pointer to a wait_event struct |
| 5150 | * which will be passed to h2s->recv_wait or h2s->send_wait depending on the |
| 5151 | * event_type. The event_type must only be a combination of SUB_RETRY_RECV and |
| 5152 | * SUB_RETRY_SEND, other values will lead to -1 being returned. It always |
| 5153 | * returns 0 except for the error above. |
| 5154 | */ |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5155 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 5156 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5157 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5158 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5159 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5160 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5161 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5162 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5163 | if (!(sw->events & SUB_RETRY_RECV)) { |
| 5164 | sw->events |= SUB_RETRY_RECV; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 5165 | sw->handle = h2s; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5166 | h2s->recv_wait = sw; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 5167 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5168 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5169 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5170 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5171 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5172 | if (!(sw->events & SUB_RETRY_SEND)) { |
| 5173 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 5174 | sw->handle = h2s; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5175 | h2s->send_wait = sw; |
Olivier Houchard | 9a0f559 | 2019-04-15 19:22:24 +0200 | [diff] [blame] | 5176 | if (!(h2s->flags & H2_SF_BLK_SFCTL) && |
| 5177 | LIST_ISEMPTY(&h2s->list)) { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5178 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 5179 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 5180 | else |
| 5181 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 5182 | } |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 5183 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5184 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5185 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5186 | if (event_type != 0) |
| 5187 | return -1; |
| 5188 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5189 | } |
| 5190 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5191 | /* Called from the upper layer, to unsubscribe some events (undo h2_subscribe). |
| 5192 | * The <param> argument here is supposed to be a pointer to the same wait_event |
| 5193 | * struct that was passed to h2_subscribe() otherwise nothing will be changed. |
| 5194 | * It always returns zero. |
| 5195 | */ |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5196 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 5197 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5198 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5199 | struct h2s *h2s = cs->ctx; |
| 5200 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5201 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5202 | sw = param; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5203 | if (h2s->recv_wait == sw) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5204 | sw->events &= ~SUB_RETRY_RECV; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5205 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5206 | } |
| 5207 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5208 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5209 | sw = param; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5210 | if (h2s->send_wait == sw) { |
| 5211 | LIST_DEL(&h2s->list); |
| 5212 | LIST_INIT(&h2s->list); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5213 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5214 | /* We were about to send, make sure it does not happen */ |
| 5215 | if (!LIST_ISEMPTY(&h2s->sending_list) && |
| 5216 | h2s->send_wait != &h2s->wait_event) { |
| 5217 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
| 5218 | LIST_DEL_INIT(&h2s->sending_list); |
| 5219 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5220 | h2s->send_wait = NULL; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5221 | |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5222 | } |
| 5223 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5224 | return 0; |
| 5225 | } |
| 5226 | |
| 5227 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5228 | /* Called from the upper layer, to receive data */ |
| 5229 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 5230 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5231 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5232 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5233 | struct htx *h2s_htx = NULL; |
| 5234 | struct htx *buf_htx = NULL; |
| 5235 | struct htx_ret htx_ret; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5236 | size_t ret = 0; |
| 5237 | |
| 5238 | /* transfer possibly pending data to the upper layer */ |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5239 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
| 5240 | /* in HTX mode we ignore the count argument */ |
| 5241 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5242 | if (htx_is_empty(h2s_htx)) { |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5243 | /* Here htx_to_buf() will set buffer data to 0 because |
| 5244 | * the HTX is empty. |
| 5245 | */ |
| 5246 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5247 | goto end; |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5248 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5249 | |
| 5250 | buf_htx = htx_from_buf(buf); |
Christopher Faulet | a413e95 | 2019-01-21 11:49:37 +0100 | [diff] [blame] | 5251 | count = htx_free_data_space(buf_htx); |
| 5252 | if (flags & CO_RFL_KEEP_RSV) { |
| 5253 | if (count <= global.tune.maxrewrite) |
| 5254 | goto end; |
| 5255 | count -= global.tune.maxrewrite; |
| 5256 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5257 | |
Willy Tarreau | 0c22fa7 | 2018-12-04 15:21:35 +0100 | [diff] [blame] | 5258 | 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] | 5259 | |
| 5260 | if (h2s_htx->flags & HTX_FL_PARSING_ERROR) |
| 5261 | buf_htx->flags |= HTX_FL_PARSING_ERROR; |
| 5262 | |
Christopher Faulet | eaf0d2a | 2019-02-18 16:04:35 +0100 | [diff] [blame] | 5263 | 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] | 5264 | htx_to_buf(buf_htx, buf); |
| 5265 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5266 | ret = htx_ret.ret; |
| 5267 | } |
| 5268 | else { |
| 5269 | ret = b_xfer(buf, &h2s->rxbuf, count); |
| 5270 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5271 | |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5272 | end: |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5273 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5274 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5275 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5276 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5277 | if (cs->flags & CS_FL_REOS) |
| 5278 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5279 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5280 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5281 | if (b_size(&h2s->rxbuf)) { |
| 5282 | b_free(&h2s->rxbuf); |
| 5283 | offer_buffers(NULL, tasks_run_queue); |
| 5284 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5285 | } |
| 5286 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5287 | if (ret && h2c->dsi == h2s->id) { |
| 5288 | /* demux is blocking on this stream's buffer */ |
| 5289 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Olivier Houchard | 3ca18bf | 2019-04-05 15:34:34 +0200 | [diff] [blame] | 5290 | h2c_restart_reading(h2c, 1); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5291 | } |
Christopher Faulet | 37070b2 | 2019-02-14 15:12:14 +0100 | [diff] [blame] | 5292 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5293 | return ret; |
| 5294 | } |
| 5295 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5296 | /* stops all senders of this connection for example when the mux buffer is full. |
| 5297 | * They are moved from the sending_list to either fctl_list or send_list. |
| 5298 | */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5299 | static void h2_stop_senders(struct h2c *h2c) |
| 5300 | { |
| 5301 | struct h2s *h2s, *h2s_back; |
| 5302 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5303 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) { |
| 5304 | LIST_DEL_INIT(&h2s->sending_list); |
Willy Tarreau | e73256f | 2019-03-25 18:02:54 +0100 | [diff] [blame] | 5305 | task_remove_from_tasklet_list((struct task *)h2s->send_wait->task); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5306 | h2s->send_wait->events |= SUB_RETRY_SEND; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5307 | } |
| 5308 | } |
| 5309 | |
Willy Tarreau | 749f5ca | 2019-03-21 19:19:36 +0100 | [diff] [blame] | 5310 | /* Called from the upper layer, to send data from buffer <buf> for no more than |
| 5311 | * <count> bytes. Returns the number of bytes effectively sent. Some status |
| 5312 | * flags may be updated on the conn_stream. |
| 5313 | */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 5314 | 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] | 5315 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5316 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5317 | size_t orig_count = count; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 5318 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5319 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5320 | struct htx *htx; |
| 5321 | struct htx_blk *blk; |
| 5322 | enum htx_blk_type btype; |
| 5323 | uint32_t bsize; |
| 5324 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5325 | |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5326 | /* If we were not just woken because we wanted to send but couldn't, |
| 5327 | * and there's somebody else that is waiting to send, do nothing, |
| 5328 | * we will subscribe later and be put at the end of the list |
| 5329 | */ |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5330 | if (LIST_ISEMPTY(&h2s->sending_list) && |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5331 | (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) |
| 5332 | return 0; |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5333 | LIST_DEL_INIT(&h2s->sending_list); |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5334 | |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5335 | /* We couldn't set it to NULL before, because we needed it in case |
| 5336 | * we had to cancel the tasklet |
| 5337 | */ |
| 5338 | h2s->send_wait = NULL; |
| 5339 | |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 5340 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 5341 | return 0; |
| 5342 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5343 | /* htx will be enough to decide if we're using HTX or legacy */ |
| 5344 | htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL; |
| 5345 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5346 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 5347 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 5348 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5349 | if (h2s->id == 0) { |
| 5350 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 5351 | |
| 5352 | if (id < 0) { |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5353 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5354 | return 0; |
| 5355 | } |
| 5356 | |
| 5357 | eb32_delete(&h2s->by_id); |
| 5358 | h2s->by_id.key = h2s->id = id; |
| 5359 | h2s->h2c->max_id = id; |
Willy Tarreau | d64a3eb | 2019-01-23 10:22:21 +0100 | [diff] [blame] | 5360 | h2s->h2c->nb_reserved--; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5361 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 5362 | } |
| 5363 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5364 | if (htx) { |
Willy Tarreau | c14999b | 2018-12-06 14:09:09 +0100 | [diff] [blame] | 5365 | while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) && |
| 5366 | count && !htx_is_empty(htx)) { |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5367 | idx = htx_get_head(htx); |
| 5368 | blk = htx_get_blk(htx, idx); |
| 5369 | btype = htx_get_blk_type(blk); |
| 5370 | bsize = htx_get_blksz(blk); |
| 5371 | |
| 5372 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5373 | case HTX_BLK_REQ_SL: |
| 5374 | /* start-line before headers */ |
| 5375 | ret = h2s_htx_bck_make_req_headers(h2s, htx); |
| 5376 | if (ret > 0) { |
| 5377 | total += ret; |
| 5378 | count -= ret; |
| 5379 | if (ret < bsize) |
| 5380 | goto done; |
| 5381 | } |
| 5382 | break; |
| 5383 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 5384 | case HTX_BLK_RES_SL: |
| 5385 | /* start-line before headers */ |
| 5386 | ret = h2s_htx_frt_make_resp_headers(h2s, htx); |
| 5387 | if (ret > 0) { |
| 5388 | total += ret; |
| 5389 | count -= ret; |
| 5390 | if (ret < bsize) |
| 5391 | goto done; |
| 5392 | } |
| 5393 | break; |
| 5394 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5395 | case HTX_BLK_DATA: |
| 5396 | case HTX_BLK_EOD: |
| 5397 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5398 | /* all these cause the emission of a DATA frame (possibly empty). |
| 5399 | * This EOM necessarily is one before trailers, as the EOM following |
| 5400 | * trailers would have been consumed by the trailers parser. |
| 5401 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5402 | ret = h2s_htx_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5403 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5404 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5405 | total += ret; |
| 5406 | count -= ret; |
| 5407 | if (ret < bsize) |
| 5408 | goto done; |
| 5409 | } |
| 5410 | break; |
| 5411 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5412 | case HTX_BLK_TLR: |
| 5413 | /* This is the first trailers block, all the subsequent ones AND |
| 5414 | * the EOM will be swallowed by the parser. |
| 5415 | */ |
| 5416 | ret = h2s_htx_make_trailers(h2s, htx); |
| 5417 | if (ret > 0) { |
| 5418 | total += ret; |
| 5419 | count -= ret; |
| 5420 | if (ret < bsize) |
| 5421 | goto done; |
| 5422 | } |
| 5423 | break; |
| 5424 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5425 | default: |
| 5426 | htx_remove_blk(htx, blk); |
| 5427 | total += bsize; |
| 5428 | count -= bsize; |
| 5429 | break; |
| 5430 | } |
| 5431 | } |
| 5432 | goto done; |
| 5433 | } |
| 5434 | |
| 5435 | /* legacy transfer mode */ |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5436 | while (h2s->h1m.state < H1_MSG_DONE && count) { |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 5437 | if (h2s->h1m.state <= H1_MSG_LAST_LF) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5438 | if (h2s->h2c->flags & H2_CF_IS_BACK) |
| 5439 | ret = -1; |
| 5440 | else |
| 5441 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5442 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5443 | else if (h2s->h1m.state < H1_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5444 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5445 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5446 | else if (h2s->h1m.state == H1_MSG_TRAILERS) { |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5447 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5448 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5449 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5450 | if (unlikely((int)ret <= 0)) { |
| 5451 | if ((int)ret < 0) |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5452 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5453 | break; |
| 5454 | } |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 5455 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5456 | total += count; |
| 5457 | count = 0; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5458 | h2s->h1m.state = H1_MSG_DONE; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5459 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 5460 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5461 | else { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5462 | cs_set_error(cs); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5463 | break; |
| 5464 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5465 | |
| 5466 | total += ret; |
| 5467 | count -= ret; |
| 5468 | |
| 5469 | if (h2s->st >= H2_SS_ERROR) |
| 5470 | break; |
| 5471 | |
| 5472 | if (h2s->flags & H2_SF_BLK_ANY) |
| 5473 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5474 | } |
| 5475 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5476 | done: |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5477 | if (h2s->st >= H2_SS_ERROR) { |
| 5478 | /* trim any possibly pending data after we close (extra CR-LF, |
| 5479 | * unprocessed trailers, abnormal extra data, ...) |
| 5480 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5481 | total += count; |
| 5482 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5483 | } |
| 5484 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5485 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 5486 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5487 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 5488 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 5489 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5490 | } |
| 5491 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5492 | if (htx) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5493 | htx_to_buf(htx, buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5494 | } else { |
| 5495 | b_del(buf, total); |
| 5496 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5497 | |
| 5498 | /* The mux is full, cancel the pending tasks */ |
| 5499 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 5500 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 5501 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5502 | |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5503 | /* If we're running HTX, and we read the whole buffer, then pretend |
| 5504 | * we read exactly what the caller specified, as with HTX the caller |
| 5505 | * will always give the buffer size, instead of the amount of data |
| 5506 | * available. |
| 5507 | */ |
| 5508 | if (htx && !b_data(buf)) |
| 5509 | total = orig_count; |
| 5510 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5511 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5512 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5513 | tasklet_wakeup(h2s->h2c->wait_event.task); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5514 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5515 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 5516 | /* If we're waiting for flow control, and we got a shutr on the |
| 5517 | * connection, we will never be unlocked, so add an error on |
| 5518 | * the conn_stream. |
| 5519 | */ |
| 5520 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 5521 | !b_data(&h2s->h2c->dbuf) && |
| 5522 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 5523 | if (cs->flags & CS_FL_EOS) |
| 5524 | cs->flags |= CS_FL_ERROR; |
| 5525 | else |
| 5526 | cs->flags |= CS_FL_ERR_PENDING; |
| 5527 | } |
Olivier Houchard | 998410a | 2019-04-15 19:23:37 +0200 | [diff] [blame] | 5528 | if (total > 0) { |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5529 | /* Ok we managed to send something, leave the send_list */ |
Olivier Houchard | d360ac6 | 2019-03-22 17:37:16 +0100 | [diff] [blame] | 5530 | LIST_DEL_INIT(&h2s->list); |
| 5531 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5532 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5533 | } |
| 5534 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5535 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5536 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5537 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5538 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5539 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5540 | struct eb32_node *node; |
| 5541 | int fctl_cnt = 0; |
| 5542 | int send_cnt = 0; |
| 5543 | int tree_cnt = 0; |
| 5544 | int orph_cnt = 0; |
| 5545 | |
| 5546 | if (!h2c) |
| 5547 | return; |
| 5548 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5549 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5550 | fctl_cnt++; |
| 5551 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5552 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5553 | send_cnt++; |
| 5554 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5555 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5556 | node = eb32_first(&h2c->streams_by_id); |
| 5557 | while (node) { |
| 5558 | h2s = container_of(node, struct h2s, by_id); |
| 5559 | tree_cnt++; |
| 5560 | if (!h2s->cs) |
| 5561 | orph_cnt++; |
| 5562 | node = eb32_next(node); |
| 5563 | } |
| 5564 | |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5565 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5566 | " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d" |
| 5567 | " .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] | 5568 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5569 | 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] | 5570 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5571 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5572 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5573 | h2c->msi, |
| 5574 | (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf), |
| 5575 | (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf)); |
| 5576 | |
| 5577 | if (h2s) { |
| 5578 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5579 | h2s, h2s->id, h2s->flags, |
| 5580 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5581 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5582 | h2s->cs); |
| 5583 | if (h2s->cs) |
| 5584 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5585 | h2s->cs->flags, h2s->cs->data); |
| 5586 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5587 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5588 | |
| 5589 | /*******************************************************/ |
| 5590 | /* functions below are dedicated to the config parsers */ |
| 5591 | /*******************************************************/ |
| 5592 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5593 | /* config parser for global "tune.h2.header-table-size" */ |
| 5594 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5595 | struct proxy *defpx, const char *file, int line, |
| 5596 | char **err) |
| 5597 | { |
| 5598 | if (too_many_args(1, args, err, NULL)) |
| 5599 | return -1; |
| 5600 | |
| 5601 | h2_settings_header_table_size = atoi(args[1]); |
| 5602 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5603 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5604 | return -1; |
| 5605 | } |
| 5606 | return 0; |
| 5607 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5608 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5609 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5610 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5611 | struct proxy *defpx, const char *file, int line, |
| 5612 | char **err) |
| 5613 | { |
| 5614 | if (too_many_args(1, args, err, NULL)) |
| 5615 | return -1; |
| 5616 | |
| 5617 | h2_settings_initial_window_size = atoi(args[1]); |
| 5618 | if (h2_settings_initial_window_size < 0) { |
| 5619 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5620 | return -1; |
| 5621 | } |
| 5622 | return 0; |
| 5623 | } |
| 5624 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5625 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5626 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5627 | struct proxy *defpx, const char *file, int line, |
| 5628 | char **err) |
| 5629 | { |
| 5630 | if (too_many_args(1, args, err, NULL)) |
| 5631 | return -1; |
| 5632 | |
| 5633 | h2_settings_max_concurrent_streams = atoi(args[1]); |
Willy Tarreau | 5a490b6 | 2019-01-31 10:39:51 +0100 | [diff] [blame] | 5634 | if ((int)h2_settings_max_concurrent_streams < 0) { |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5635 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5636 | return -1; |
| 5637 | } |
| 5638 | return 0; |
| 5639 | } |
| 5640 | |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5641 | /* config parser for global "tune.h2.max-frame-size" */ |
| 5642 | static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx, |
| 5643 | struct proxy *defpx, const char *file, int line, |
| 5644 | char **err) |
| 5645 | { |
| 5646 | if (too_many_args(1, args, err, NULL)) |
| 5647 | return -1; |
| 5648 | |
| 5649 | h2_settings_max_frame_size = atoi(args[1]); |
| 5650 | if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) { |
| 5651 | memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]); |
| 5652 | return -1; |
| 5653 | } |
| 5654 | return 0; |
| 5655 | } |
| 5656 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5657 | |
| 5658 | /****************************************/ |
| 5659 | /* MUX initialization and instanciation */ |
| 5660 | /***************************************/ |
| 5661 | |
| 5662 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5663 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5664 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5665 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5666 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5667 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5668 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5669 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5670 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5671 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5672 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5673 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5674 | .avail_streams = h2_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 5675 | .used_streams = h2_used_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5676 | .shutr = h2_shutr, |
| 5677 | .shutw = h2_shutw, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5678 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 5679 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5680 | .name = "H2", |
| 5681 | }; |
| 5682 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5683 | /* PROTO selection : this mux registers PROTO token "h2" */ |
| 5684 | static struct mux_proto_list mux_proto_h2 = |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5685 | { .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] | 5686 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5687 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 5688 | |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5689 | |
| 5690 | /* The mux operations */ |
| 5691 | static const struct mux_ops h2_htx_ops = { |
| 5692 | .init = h2_init, |
| 5693 | .wake = h2_wake, |
| 5694 | .snd_buf = h2_snd_buf, |
| 5695 | .rcv_buf = h2_rcv_buf, |
| 5696 | .subscribe = h2_subscribe, |
| 5697 | .unsubscribe = h2_unsubscribe, |
| 5698 | .attach = h2_attach, |
| 5699 | .get_first_cs = h2_get_first_cs, |
| 5700 | .detach = h2_detach, |
| 5701 | .destroy = h2_destroy, |
| 5702 | .avail_streams = h2_avail_streams, |
| 5703 | .used_streams = h2_used_streams, |
| 5704 | .shutr = h2_shutr, |
| 5705 | .shutw = h2_shutw, |
| 5706 | .show_fd = h2_show_fd, |
Christopher Faulet | 9f38f5a | 2019-04-03 09:53:32 +0200 | [diff] [blame] | 5707 | .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX, |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5708 | .name = "H2", |
| 5709 | }; |
| 5710 | |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5711 | static struct mux_proto_list mux_proto_h2_htx = |
Christopher Faulet | 9b57910 | 2019-04-11 22:52:25 +0200 | [diff] [blame] | 5712 | { .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] | 5713 | |
| 5714 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx); |
| 5715 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5716 | /* config keyword parsers */ |
| 5717 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5718 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5719 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5720 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | a24b35c | 2019-02-21 13:24:36 +0100 | [diff] [blame] | 5721 | { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5722 | { 0, NULL, NULL } |
| 5723 | }}; |
| 5724 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5725 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |