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 | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 32 | /* dummy streams returned for idle and closed states */ |
| 33 | static const struct h2s *h2_closed_stream; |
| 34 | static const struct h2s *h2_idle_stream; |
| 35 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 36 | /* Connection flags (32 bit), in h2c->flags */ |
| 37 | #define H2_CF_NONE 0x00000000 |
| 38 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 39 | /* Flags indicating why writing to the mux is blocked. */ |
| 40 | #define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer |
| 41 | #define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full |
| 42 | #define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above |
| 43 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 44 | /* Flags indicating why writing to the demux is blocked. |
| 45 | * The first two ones directly affect the ability for the mux to receive data |
| 46 | * from the connection. The other ones affect the mux's ability to demux |
| 47 | * received data. |
| 48 | */ |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 49 | #define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer |
| 50 | #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] | 51 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 52 | #define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy |
| 53 | #define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer |
| 54 | #define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer |
| 55 | #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] | 56 | #define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave |
| 57 | #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] | 58 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 59 | /* other flags */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 60 | #define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent |
| 61 | #define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent |
| 62 | #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] | 63 | #define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 64 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 65 | /* H2 connection state, in h2c->st0 */ |
| 66 | enum h2_cs { |
| 67 | H2_CS_PREFACE, // init done, waiting for connection preface |
| 68 | H2_CS_SETTINGS1, // preface OK, waiting for first settings frame |
| 69 | H2_CS_FRAME_H, // first settings frame ok, waiting for frame header |
| 70 | H2_CS_FRAME_P, // frame header OK, waiting for frame payload |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 71 | H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame |
| 72 | H2_CS_FRAME_E, // frame payload OK, trying to send RST frame |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 73 | H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP |
| 74 | H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP |
| 75 | H2_CS_ENTRIES // must be last |
| 76 | } __attribute__((packed)); |
| 77 | |
| 78 | /* H2 connection descriptor */ |
| 79 | struct h2c { |
| 80 | struct connection *conn; |
| 81 | |
| 82 | enum h2_cs st0; /* mux state */ |
| 83 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 84 | |
| 85 | /* 16 bit hole here */ |
| 86 | uint32_t flags; /* connection flags: H2_CF_* */ |
| 87 | int32_t max_id; /* highest ID known on this connection, <0 before preface */ |
| 88 | uint32_t rcvd_c; /* newly received data to ACK for the connection */ |
| 89 | uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */ |
| 90 | |
| 91 | /* states for the demux direction */ |
| 92 | struct hpack_dht *ddht; /* demux dynamic header table */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 93 | struct buffer dbuf; /* demux buffer */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 94 | |
| 95 | int32_t dsi; /* demux stream ID (<0 = idle) */ |
| 96 | int32_t dfl; /* demux frame length (if dsi >= 0) */ |
| 97 | int8_t dft; /* demux frame type (if dsi >= 0) */ |
| 98 | int8_t dff; /* demux frame flags (if dsi >= 0) */ |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 99 | uint8_t dpl; /* demux pad length (part of dfl), init to 0 */ |
| 100 | /* 8 bit hole here */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 101 | int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */ |
| 102 | |
| 103 | /* states for the mux direction */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 104 | struct buffer mbuf; /* mux buffer */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 105 | int32_t msi; /* mux stream ID (<0 = idle) */ |
| 106 | int32_t mfl; /* mux frame length (if dsi >= 0) */ |
| 107 | int8_t mft; /* mux frame type (if dsi >= 0) */ |
| 108 | int8_t mff; /* mux frame flags (if dsi >= 0) */ |
| 109 | /* 16 bit hole here */ |
| 110 | int32_t miw; /* mux initial window size for all new streams */ |
| 111 | int32_t mws; /* mux window size. Can be negative. */ |
| 112 | int32_t mfs; /* mux's max frame size */ |
| 113 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 114 | int timeout; /* idle timeout duration in ticks */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 115 | int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */ |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 116 | unsigned int nb_streams; /* number of streams in the tree */ |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 117 | unsigned int nb_cs; /* number of attached conn_streams */ |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 118 | struct proxy *proxy; /* the proxy this connection was created for */ |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 119 | struct task *task; /* timeout management task */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 120 | struct eb_root streams_by_id; /* all active streams by their ID */ |
| 121 | struct list send_list; /* list of blocked streams requesting to send */ |
| 122 | struct list fctl_list; /* list of streams blocked by connection's fctl */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 123 | struct list sending_list; /* list of h2s scheduled to send data */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 124 | struct buffer_wait buf_wait; /* wait list for buffer allocations */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 125 | 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] | 126 | }; |
| 127 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 128 | /* H2 stream state, in h2s->st */ |
| 129 | enum h2_ss { |
| 130 | H2_SS_IDLE = 0, // idle |
| 131 | H2_SS_RLOC, // reserved(local) |
| 132 | H2_SS_RREM, // reserved(remote) |
| 133 | H2_SS_OPEN, // open |
| 134 | H2_SS_HREM, // half-closed(remote) |
| 135 | H2_SS_HLOC, // half-closed(local) |
Willy Tarreau | 96060ba | 2017-10-16 18:34:34 +0200 | [diff] [blame] | 136 | H2_SS_ERROR, // an error needs to be sent using RST_STREAM |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 137 | H2_SS_CLOSED, // closed |
| 138 | H2_SS_ENTRIES // must be last |
| 139 | } __attribute__((packed)); |
| 140 | |
| 141 | /* HTTP/2 stream flags (32 bit), in h2s->flags */ |
| 142 | #define H2_SF_NONE 0x00000000 |
| 143 | #define H2_SF_ES_RCVD 0x00000001 |
| 144 | #define H2_SF_ES_SENT 0x00000002 |
| 145 | |
| 146 | #define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM |
| 147 | #define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM |
| 148 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 149 | /* stream flags indicating the reason the stream is blocked */ |
| 150 | #define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient) |
| 151 | #define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux |
| 152 | #define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl |
| 153 | #define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl |
| 154 | #define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above |
| 155 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 156 | /* stream flags indicating how data is supposed to be sent */ |
| 157 | #define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length |
| 158 | #define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding |
| 159 | |
| 160 | /* step we're currently in when sending chunks. This is needed because we may |
| 161 | * have to transfer chunks as large as a full buffer so there's no room left |
| 162 | * for size nor crlf around. |
| 163 | */ |
| 164 | #define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size |
| 165 | #define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data |
| 166 | #define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data |
| 167 | |
| 168 | #define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size |
| 169 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 170 | #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] | 171 | #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] | 172 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 173 | /* H2 stream descriptor, describing the stream as it appears in the H2C, and as |
| 174 | * it is being processed in the internal HTTP representation (H1 for now). |
| 175 | */ |
| 176 | struct h2s { |
| 177 | struct conn_stream *cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 178 | struct session *sess; |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 179 | struct h2c *h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 180 | struct h1m h1m; /* request or response parser state for H1 */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 181 | struct eb32_node by_id; /* place in h2c's streams_by_id */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 182 | int32_t id; /* stream ID */ |
| 183 | uint32_t flags; /* H2_SF_* */ |
| 184 | int mws; /* mux window size for this stream */ |
| 185 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 186 | enum h2_ss st; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 187 | uint16_t status; /* HTTP response status */ |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 188 | struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 189 | struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */ |
| 190 | struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */ |
| 191 | struct wait_event *send_wait; /* The streeam is waiting for flow control */ |
| 192 | struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 193 | }; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 194 | |
Willy Tarreau | c640514 | 2017-09-21 20:23:50 +0200 | [diff] [blame] | 195 | /* descriptor for an h2 frame header */ |
| 196 | struct h2_fh { |
| 197 | uint32_t len; /* length, host order, 24 bits */ |
| 198 | uint32_t sid; /* stream id, host order, 31 bits */ |
| 199 | uint8_t ft; /* frame type */ |
| 200 | uint8_t ff; /* frame flags */ |
| 201 | }; |
| 202 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 203 | /* the h2c connection pool */ |
| 204 | DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c)); |
| 205 | |
| 206 | /* the h2s stream pool */ |
| 207 | DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s)); |
| 208 | |
Willy Tarreau | dc57236 | 2018-12-12 08:08:05 +0100 | [diff] [blame] | 209 | /* The default connection window size is 65535, it may only be enlarged using |
| 210 | * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1, |
| 211 | * we'll pretend we already received the difference between the two to send |
| 212 | * an equivalent window update to enlarge it to 2G-1. |
| 213 | */ |
| 214 | #define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535) |
| 215 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 216 | /* a few settings from the global section */ |
| 217 | static int h2_settings_header_table_size = 4096; /* initial value */ |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 218 | static int h2_settings_initial_window_size = 65535; /* initial value */ |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 219 | static int h2_settings_max_concurrent_streams = 100; |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 220 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 221 | /* a dmumy closed stream */ |
| 222 | static const struct h2s *h2_closed_stream = &(const struct h2s){ |
| 223 | .cs = NULL, |
| 224 | .h2c = NULL, |
| 225 | .st = H2_SS_CLOSED, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 226 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 227 | .flags = H2_SF_RST_RCVD, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 228 | .id = 0, |
| 229 | }; |
| 230 | |
| 231 | /* and a dummy idle stream for use with any unannounced stream */ |
| 232 | static const struct h2s *h2_idle_stream = &(const struct h2s){ |
| 233 | .cs = NULL, |
| 234 | .h2c = NULL, |
| 235 | .st = H2_SS_IDLE, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 236 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 237 | .id = 0, |
| 238 | }; |
| 239 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 240 | 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] | 241 | static int h2_send(struct h2c *h2c); |
| 242 | static int h2_recv(struct h2c *h2c); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 243 | static int h2_process(struct h2c *h2c); |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 244 | 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] | 245 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 246 | static int h2s_decode_headers(struct h2s *h2s); |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 247 | static int h2_frt_transfer_data(struct h2s *h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 248 | 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] | 249 | 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] | 250 | static void h2s_alert(struct h2s *h2s); |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 251 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 252 | /*****************************************************/ |
| 253 | /* functions below are for dynamic buffer management */ |
| 254 | /*****************************************************/ |
| 255 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 256 | /* indicates whether or not the we may call the h2_recv() function to attempt |
| 257 | * to receive data into the buffer and/or demux pending data. The condition is |
| 258 | * a bit complex due to some API limits for now. The rules are the following : |
| 259 | * - if an error or a shutdown was detected on the connection and the buffer |
| 260 | * is empty, we must not attempt to receive |
| 261 | * - if the demux buf failed to be allocated, we must not try to receive and |
| 262 | * we know there is nothing pending |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 263 | * - if no flag indicates a blocking condition, we may attempt to receive, |
| 264 | * regardless of whether the demux buffer is full or not, so that only |
| 265 | * de demux part decides whether or not to block. This is needed because |
| 266 | * the connection API indeed prevents us from re-enabling receipt that is |
| 267 | * already enabled in a polled state, so we must always immediately stop |
| 268 | * as soon as the demux can't proceed so as never to hit an end of read |
| 269 | * with data pending in the buffers. |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 270 | * - otherwise must may not attempt |
| 271 | */ |
| 272 | static inline int h2_recv_allowed(const struct h2c *h2c) |
| 273 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 274 | if (b_data(&h2c->dbuf) == 0 && |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 275 | (h2c->st0 >= H2_CS_ERROR || |
| 276 | h2c->conn->flags & CO_FL_ERROR || |
| 277 | conn_xprt_read0_pending(h2c->conn))) |
| 278 | return 0; |
| 279 | |
| 280 | if (!(h2c->flags & H2_CF_DEM_DALLOC) && |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 281 | !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 282 | return 1; |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 287 | /* returns true if the connection has too many conn_streams attached */ |
| 288 | static inline int h2_has_too_many_cs(const struct h2c *h2c) |
| 289 | { |
| 290 | return h2c->nb_cs >= h2_settings_max_concurrent_streams; |
| 291 | } |
| 292 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 293 | /* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c |
| 294 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 295 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 296 | * impossible to wake up and we prefer to be woken up later. |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 297 | */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 298 | static int h2_buf_available(void *target) |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 299 | { |
| 300 | struct h2c *h2c = target; |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 301 | struct h2s *h2s; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 302 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 303 | 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] | 304 | h2c->flags &= ~H2_CF_DEM_DALLOC; |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 305 | if (h2_recv_allowed(h2c)) |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 306 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 307 | return 1; |
| 308 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 309 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 310 | if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) { |
| 311 | h2c->flags &= ~H2_CF_MUX_MALLOC; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 312 | |
| 313 | if (h2c->flags & H2_CF_DEM_MROOM) { |
| 314 | h2c->flags &= ~H2_CF_DEM_MROOM; |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 315 | if (h2_recv_allowed(h2c)) |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 316 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 317 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 318 | return 1; |
| 319 | } |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 320 | |
| 321 | if ((h2c->flags & H2_CF_DEM_SALLOC) && |
| 322 | (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs && |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 323 | b_alloc_margin(&h2s->rxbuf, 0)) { |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 324 | h2c->flags &= ~H2_CF_DEM_SALLOC; |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 325 | if (h2_recv_allowed(h2c)) |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 326 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 327 | return 1; |
| 328 | } |
| 329 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 330 | return 0; |
| 331 | } |
| 332 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 333 | 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] | 334 | { |
| 335 | struct buffer *buf = NULL; |
| 336 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 337 | if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) && |
| 338 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 339 | h2c->buf_wait.target = h2c; |
| 340 | h2c->buf_wait.wakeup_cb = h2_buf_available; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 341 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 342 | LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 343 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 344 | __conn_xprt_stop_recv(h2c->conn); |
| 345 | } |
| 346 | return buf; |
| 347 | } |
| 348 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 349 | static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 350 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 351 | if (bptr->size) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 352 | b_free(bptr); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 353 | offer_buffers(h2c->buf_wait.target, tasks_run_queue); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 357 | static int h2_avail_streams(struct connection *conn) |
| 358 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 359 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 360 | |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 361 | /* XXX Should use the negociated max concurrent stream nb instead of the conf value */ |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 362 | return (h2_settings_max_concurrent_streams - h2c->nb_streams); |
| 363 | } |
| 364 | |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 365 | static int h2_max_streams(struct connection *conn) |
| 366 | { |
| 367 | /* XXX Should use the negociated max concurrent stream nb instead of the conf value */ |
| 368 | return h2_settings_max_concurrent_streams; |
| 369 | } |
| 370 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 371 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 372 | /*****************************************************************/ |
| 373 | /* functions below are dedicated to the mux setup and management */ |
| 374 | /*****************************************************************/ |
| 375 | |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 376 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 377 | * is already initialized before installing the mux, so we detect incoming |
| 378 | * connections from the fact that the context is still NULL. Returns < 0 on |
| 379 | * error. |
| 380 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 381 | static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess) |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 382 | { |
| 383 | struct h2c *h2c; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 384 | struct task *t = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 385 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 386 | h2c = pool_alloc(pool_head_h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 387 | if (!h2c) |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 388 | goto fail_no_h2c; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 389 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 390 | if (conn->ctx) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 391 | h2c->flags = H2_CF_IS_BACK; |
| 392 | h2c->shut_timeout = h2c->timeout = prx->timeout.server; |
| 393 | if (tick_isset(prx->timeout.serverfin)) |
| 394 | h2c->shut_timeout = prx->timeout.serverfin; |
| 395 | } else { |
| 396 | h2c->flags = H2_CF_NONE; |
| 397 | h2c->shut_timeout = h2c->timeout = prx->timeout.client; |
| 398 | if (tick_isset(prx->timeout.clientfin)) |
| 399 | h2c->shut_timeout = prx->timeout.clientfin; |
| 400 | } |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 401 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 402 | h2c->proxy = prx; |
Willy Tarreau | 3340029 | 2017-11-05 11:23:40 +0100 | [diff] [blame] | 403 | h2c->task = NULL; |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 404 | if (tick_isset(h2c->timeout)) { |
| 405 | t = task_new(tid_bit); |
| 406 | if (!t) |
| 407 | goto fail; |
| 408 | |
| 409 | h2c->task = t; |
| 410 | t->process = h2_timeout_task; |
| 411 | t->context = h2c; |
| 412 | t->expire = tick_add(now_ms, h2c->timeout); |
| 413 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 414 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 415 | h2c->wait_event.task = tasklet_new(); |
| 416 | if (!h2c->wait_event.task) |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 417 | goto fail; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 418 | h2c->wait_event.task->process = h2_io_cb; |
| 419 | h2c->wait_event.task->context = h2c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 420 | h2c->wait_event.events = 0; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 421 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 422 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 423 | if (!h2c->ddht) |
| 424 | goto fail; |
| 425 | |
| 426 | /* Initialise the context. */ |
| 427 | h2c->st0 = H2_CS_PREFACE; |
| 428 | h2c->conn = conn; |
| 429 | h2c->max_id = -1; |
| 430 | h2c->errcode = H2_ERR_NO_ERROR; |
Willy Tarreau | dc57236 | 2018-12-12 08:08:05 +0100 | [diff] [blame] | 431 | h2c->rcvd_c = H2_INITIAL_WINDOW_INCREMENT; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 432 | h2c->rcvd_s = 0; |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 433 | h2c->nb_streams = 0; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 434 | h2c->nb_cs = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 435 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 436 | h2c->dbuf = BUF_NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 437 | h2c->dsi = -1; |
| 438 | h2c->msi = -1; |
| 439 | h2c->last_sid = -1; |
| 440 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 441 | h2c->mbuf = BUF_NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 442 | h2c->miw = 65535; /* mux initial window size */ |
| 443 | h2c->mws = 65535; /* mux window size */ |
| 444 | h2c->mfs = 16384; /* initial max frame size */ |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 445 | h2c->streams_by_id = EB_ROOT; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 446 | LIST_INIT(&h2c->send_list); |
| 447 | LIST_INIT(&h2c->fctl_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 448 | LIST_INIT(&h2c->sending_list); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 449 | LIST_INIT(&h2c->buf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 450 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 451 | if (t) |
| 452 | task_queue(t); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 453 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 454 | if (h2c->flags & H2_CF_IS_BACK) { |
| 455 | /* FIXME: this is temporary, for outgoing connections we need |
| 456 | * to immediately allocate a stream until the code is modified |
| 457 | * so that the caller calls ->attach(). For now the outgoing cs |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 458 | * is stored as conn->ctx by the caller. |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 459 | */ |
| 460 | struct h2s *h2s; |
| 461 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 462 | h2s = h2c_bck_stream_new(h2c, conn->ctx, sess); |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 463 | if (!h2s) |
| 464 | goto fail_stream; |
| 465 | } |
| 466 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 467 | conn->ctx = h2c; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 468 | |
Willy Tarreau | 0f38358 | 2018-10-03 14:22:21 +0200 | [diff] [blame] | 469 | /* prepare to read something */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 470 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 471 | return 0; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 472 | fail_stream: |
| 473 | hpack_dht_free(h2c->ddht); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 474 | fail: |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 475 | if (t) |
| 476 | task_free(t); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 477 | if (h2c->wait_event.task) |
| 478 | tasklet_free(h2c->wait_event.task); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 479 | pool_free(pool_head_h2c, h2c); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 480 | fail_no_h2c: |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 481 | return -1; |
| 482 | } |
| 483 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 484 | /* returns the next allocatable outgoing stream ID for the H2 connection, or |
| 485 | * -1 if no more is allocatable. |
| 486 | */ |
| 487 | static inline int32_t h2c_get_next_sid(const struct h2c *h2c) |
| 488 | { |
| 489 | int32_t id = (h2c->max_id + 1) | 1; |
| 490 | if (id & 0x80000000U) |
| 491 | id = -1; |
| 492 | return id; |
| 493 | } |
| 494 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 495 | /* returns the stream associated with id <id> or NULL if not found */ |
| 496 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 497 | { |
| 498 | struct eb32_node *node; |
| 499 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 500 | if (id == 0) |
| 501 | return (struct h2s *)h2_closed_stream; |
| 502 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 503 | if (id > h2c->max_id) |
| 504 | return (struct h2s *)h2_idle_stream; |
| 505 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 506 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 507 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 508 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 509 | |
| 510 | return container_of(node, struct h2s, by_id); |
| 511 | } |
| 512 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 513 | /* release function for a connection. This one should be called to free all |
| 514 | * resources allocated to the mux. |
| 515 | */ |
| 516 | static void h2_release(struct connection *conn) |
| 517 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 518 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 519 | |
| 520 | LIST_DEL(&conn->list); |
| 521 | |
| 522 | if (h2c) { |
| 523 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 524 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 525 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 526 | LIST_DEL(&h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 527 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 528 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 529 | h2_release_buf(h2c, &h2c->dbuf); |
| 530 | h2_release_buf(h2c, &h2c->mbuf); |
| 531 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 532 | if (h2c->task) { |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 533 | h2c->task->context = NULL; |
| 534 | task_wakeup(h2c->task, TASK_WOKEN_OTHER); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 535 | h2c->task = NULL; |
| 536 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 537 | if (h2c->wait_event.task) |
| 538 | tasklet_free(h2c->wait_event.task); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 539 | if (h2c->wait_event.events != 0) |
| 540 | conn->xprt->unsubscribe(conn, h2c->wait_event.events, |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 541 | &h2c->wait_event); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 542 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 543 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | conn->mux = NULL; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 547 | conn->ctx = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 548 | |
| 549 | conn_stop_tracking(conn); |
| 550 | conn_full_close(conn); |
| 551 | if (conn->destroy_cb) |
| 552 | conn->destroy_cb(conn); |
| 553 | conn_free(conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 557 | /******************************************************/ |
| 558 | /* functions below are for the H2 protocol processing */ |
| 559 | /******************************************************/ |
| 560 | |
| 561 | /* 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] | 562 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 563 | { |
| 564 | return h2s ? h2s->id : 0; |
| 565 | } |
| 566 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 567 | /* 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] | 568 | 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] | 569 | { |
| 570 | if (h2c->msi < 0) |
| 571 | return 0; |
| 572 | |
| 573 | if (h2c->msi == h2s_id(h2s)) |
| 574 | return 0; |
| 575 | |
| 576 | return 1; |
| 577 | } |
| 578 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 579 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 580 | 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] | 581 | { |
| 582 | h2c->errcode = err; |
| 583 | h2c->st0 = H2_CS_ERROR; |
| 584 | } |
| 585 | |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 586 | /* marks an error on the stream */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 587 | 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] | 588 | { |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 589 | if (h2s->id && h2s->st < H2_SS_ERROR) { |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 590 | h2s->errcode = err; |
| 591 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 592 | if (h2s->cs) |
| 593 | cs_set_error(h2s->cs); |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 597 | /* attempt to notify the data layer of recv availability */ |
| 598 | static void __maybe_unused h2s_notify_recv(struct h2s *h2s) |
| 599 | { |
| 600 | struct wait_event *sw; |
| 601 | |
| 602 | if (h2s->recv_wait) { |
| 603 | sw = h2s->recv_wait; |
| 604 | sw->events &= ~SUB_RETRY_RECV; |
| 605 | tasklet_wakeup(sw->task); |
| 606 | h2s->recv_wait = NULL; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | /* attempt to notify the data layer of send availability */ |
| 611 | static void __maybe_unused h2s_notify_send(struct h2s *h2s) |
| 612 | { |
| 613 | struct wait_event *sw; |
| 614 | |
| 615 | if (h2s->send_wait) { |
| 616 | sw = h2s->send_wait; |
| 617 | sw->events &= ~SUB_RETRY_SEND; |
| 618 | tasklet_wakeup(sw->task); |
| 619 | h2s->send_wait = NULL; |
| 620 | } |
| 621 | } |
| 622 | |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 623 | /* alerts the data layer, trying to wake it up by all means, following |
| 624 | * this sequence : |
| 625 | * - if the h2s' data layer is subscribed to recv, then it's woken up for recv |
| 626 | * - if its subscribed to send, then it's woken up for send |
| 627 | * - if it was subscribed to neither, its ->wake() callback is called |
| 628 | * It is safe to call this function with a closed stream which doesn't have a |
| 629 | * conn_stream anymore. |
| 630 | */ |
| 631 | static void __maybe_unused h2s_alert(struct h2s *h2s) |
| 632 | { |
| 633 | if (h2s->recv_wait || h2s->send_wait) { |
| 634 | h2s_notify_recv(h2s); |
| 635 | h2s_notify_send(h2s); |
| 636 | } |
| 637 | else if (h2s->cs && h2s->cs->data_cb->wake != NULL) |
| 638 | h2s->cs->data_cb->wake(h2s->cs); |
| 639 | } |
| 640 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 641 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 642 | 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] | 643 | { |
| 644 | uint8_t *out = frame; |
| 645 | |
| 646 | *out = len >> 16; |
| 647 | write_n16(out + 1, len); |
| 648 | } |
| 649 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 650 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 651 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 652 | * 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] | 653 | * available in the buffer's input prior to calling this function. The buffer |
| 654 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 655 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 656 | 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] | 657 | const struct buffer *b, int o) |
| 658 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 659 | 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] | 660 | } |
| 661 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 662 | 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] | 663 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 664 | 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] | 665 | } |
| 666 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 667 | 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] | 668 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 669 | 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] | 670 | } |
| 671 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 672 | 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] | 673 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 674 | 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] | 675 | } |
| 676 | |
| 677 | |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 678 | /* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm |
| 679 | * is not obvious. It turns out that H2 headers are neither aligned nor do they |
| 680 | * use regular sizes. And to add to the trouble, the buffer may wrap so each |
| 681 | * byte read must be checked. The header is formed like this : |
| 682 | * |
| 683 | * b0 b1 b2 b3 b4 b5..b8 |
| 684 | * +----------+---------+--------+----+----+----------------------+ |
| 685 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 686 | * +----------+---------+--------+----+----+----------------------+ |
| 687 | * |
| 688 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 689 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 690 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 691 | * 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] | 692 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 693 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 694 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 695 | static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 696 | { |
| 697 | uint64_t w; |
| 698 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 699 | if (b_data(b) < 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 700 | return 0; |
| 701 | |
Willy Tarreau | 9c7f2d1 | 2018-06-15 11:51:32 +0200 | [diff] [blame] | 702 | w = h2_get_n64(b, 1); |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 703 | h->len = *(uint8_t*)b_head(b) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 704 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 705 | h->ff = w >> 32; |
| 706 | h->ft = w >> 40; |
| 707 | h->len += w >> 48; |
| 708 | return 1; |
| 709 | } |
| 710 | |
| 711 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 712 | * h2_peek_frame_hdr() above. |
| 713 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 714 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 715 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 716 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 720 | 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] | 721 | { |
| 722 | int ret; |
| 723 | |
| 724 | ret = h2_peek_frame_hdr(b, h); |
| 725 | if (ret > 0) |
| 726 | h2_skip_frame_hdr(b); |
| 727 | return ret; |
| 728 | } |
| 729 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 730 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 731 | * its connection if the stream was not yet closed. Please use this exclusively |
| 732 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 733 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 734 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 735 | { |
| 736 | if (h2s->st != H2_SS_CLOSED) |
| 737 | h2s->h2c->nb_streams--; |
| 738 | h2s->st = H2_SS_CLOSED; |
| 739 | } |
| 740 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 741 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 742 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 743 | { |
| 744 | h2s_close(h2s); |
| 745 | eb32_delete(&h2s->by_id); |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 746 | if (b_size(&h2s->rxbuf)) { |
| 747 | b_free(&h2s->rxbuf); |
| 748 | offer_buffers(NULL, tasks_run_queue); |
| 749 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 750 | if (h2s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 751 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 752 | if (h2s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 753 | h2s->recv_wait->events &= ~SUB_RETRY_RECV; |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 754 | /* There's no need to explicitly call unsubscribe here, the only |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 755 | * reference left would be in the h2c send_list/fctl_list, and if |
| 756 | * we're in it, we're getting out anyway |
| 757 | */ |
| 758 | LIST_DEL(&h2s->list); |
| 759 | LIST_INIT(&h2s->list); |
| 760 | tasklet_free(h2s->wait_event.task); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 761 | pool_free(pool_head_h2s, h2s); |
| 762 | } |
| 763 | |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 764 | /* allocates a new stream <id> for connection <h2c> and adds it into h2c's |
| 765 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 766 | * causes of errors can be any failed memory allocation. The caller is |
| 767 | * responsible for checking if the connection may support an extra stream |
| 768 | * prior to calling this function. |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 769 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 770 | static struct h2s *h2s_new(struct h2c *h2c, int id) |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 771 | { |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 772 | struct h2s *h2s; |
| 773 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 774 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 775 | if (!h2s) |
| 776 | goto out; |
| 777 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 778 | h2s->wait_event.task = tasklet_new(); |
| 779 | if (!h2s->wait_event.task) { |
| 780 | pool_free(pool_head_h2s, h2s); |
| 781 | goto out; |
| 782 | } |
| 783 | h2s->send_wait = NULL; |
| 784 | h2s->recv_wait = NULL; |
| 785 | h2s->wait_event.task->process = h2_deferred_shut; |
| 786 | h2s->wait_event.task->context = h2s; |
| 787 | h2s->wait_event.handle = NULL; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 788 | h2s->wait_event.events = 0; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 789 | LIST_INIT(&h2s->list); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 790 | h2s->h2c = h2c; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 791 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 792 | h2s->mws = h2c->miw; |
| 793 | h2s->flags = H2_SF_NONE; |
| 794 | h2s->errcode = H2_ERR_NO_ERROR; |
| 795 | h2s->st = H2_SS_IDLE; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 796 | h2s->status = 0; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 797 | h2s->rxbuf = BUF_NULL; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 798 | |
| 799 | if (h2c->flags & H2_CF_IS_BACK) { |
| 800 | h1m_init_req(&h2s->h1m); |
| 801 | h2s->h1m.err_pos = -1; // don't care about errors on the request path |
| 802 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 803 | } else { |
| 804 | h1m_init_res(&h2s->h1m); |
| 805 | h2s->h1m.err_pos = -1; // don't care about errors on the response path |
| 806 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 807 | } |
| 808 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 809 | h2s->by_id.key = h2s->id = id; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 810 | if (id > 0) |
| 811 | h2c->max_id = id; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 812 | |
| 813 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 814 | h2c->nb_streams++; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 815 | |
| 816 | return h2s; |
| 817 | |
| 818 | out_free_h2s: |
| 819 | pool_free(pool_head_h2s, h2s); |
| 820 | out: |
| 821 | return NULL; |
| 822 | } |
| 823 | |
| 824 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 825 | * case of memory allocation error. |
| 826 | */ |
| 827 | static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id) |
| 828 | { |
| 829 | struct session *sess = h2c->conn->owner; |
| 830 | struct conn_stream *cs; |
| 831 | struct h2s *h2s; |
| 832 | |
| 833 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 834 | goto out; |
| 835 | |
| 836 | h2s = h2s_new(h2c, id); |
| 837 | if (!h2s) |
| 838 | goto out; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 839 | |
| 840 | cs = cs_new(h2c->conn); |
| 841 | if (!cs) |
| 842 | goto out_close; |
| 843 | |
Olivier Houchard | 746fb77 | 2018-12-15 19:42:00 +0100 | [diff] [blame] | 844 | cs->flags |= CS_FL_NOT_FIRST; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 845 | h2s->cs = cs; |
| 846 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 847 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 848 | |
| 849 | if (stream_create_from_cs(cs) < 0) |
| 850 | goto out_free_cs; |
| 851 | |
Willy Tarreau | 590a051 | 2018-09-05 11:56:48 +0200 | [diff] [blame] | 852 | /* We want the accept date presented to the next stream to be the one |
| 853 | * we have now, the handshake time to be null (since the next stream |
| 854 | * is not delayed by a handshake), and the idle time to count since |
| 855 | * right now. |
| 856 | */ |
| 857 | sess->accept_date = date; |
| 858 | sess->tv_accept = now; |
| 859 | sess->t_handshake = 0; |
| 860 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 861 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 862 | if (h2_has_too_many_cs(h2c)) |
| 863 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 864 | return h2s; |
| 865 | |
| 866 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 867 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 868 | cs_free(cs); |
| 869 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 870 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 871 | out: |
Willy Tarreau | 45efc07 | 2018-10-03 18:27:52 +0200 | [diff] [blame] | 872 | sess_log(sess); |
| 873 | return NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 874 | } |
| 875 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 876 | /* allocates a new stream associated to conn_stream <cs> on the h2c connection |
| 877 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 878 | * possible stream ID was reached. |
| 879 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 880 | 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] | 881 | { |
| 882 | struct h2s *h2s = NULL; |
| 883 | |
| 884 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 885 | goto out; |
| 886 | |
| 887 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 888 | h2s = h2s_new(h2c, 0); |
| 889 | if (!h2s) |
| 890 | goto out; |
| 891 | |
| 892 | h2s->cs = cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 893 | h2s->sess = sess; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 894 | cs->ctx = h2s; |
| 895 | h2c->nb_cs++; |
| 896 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 897 | out: |
| 898 | return h2s; |
| 899 | } |
| 900 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 901 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 902 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 903 | * the various settings codes. |
| 904 | */ |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 905 | static int h2c_send_settings(struct h2c *h2c) |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 906 | { |
| 907 | struct buffer *res; |
| 908 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 909 | struct buffer buf; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 910 | int ret; |
| 911 | |
| 912 | if (h2c_mux_busy(h2c, NULL)) { |
| 913 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 914 | return 0; |
| 915 | } |
| 916 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 917 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 918 | if (!res) { |
| 919 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 920 | h2c->flags |= H2_CF_DEM_MROOM; |
| 921 | return 0; |
| 922 | } |
| 923 | |
| 924 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 925 | chunk_memcpy(&buf, |
| 926 | "\x00\x00\x00" /* length : 0 for now */ |
| 927 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 928 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 929 | 9); |
| 930 | |
| 931 | if (h2_settings_header_table_size != 4096) { |
| 932 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 933 | |
| 934 | write_n32(str + 2, h2_settings_header_table_size); |
| 935 | chunk_memcat(&buf, str, 6); |
| 936 | } |
| 937 | |
| 938 | if (h2_settings_initial_window_size != 65535) { |
| 939 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 940 | |
| 941 | write_n32(str + 2, h2_settings_initial_window_size); |
| 942 | chunk_memcat(&buf, str, 6); |
| 943 | } |
| 944 | |
| 945 | if (h2_settings_max_concurrent_streams != 0) { |
| 946 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 947 | |
| 948 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 949 | * the protocol, so never send this value! |
| 950 | */ |
| 951 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 952 | chunk_memcat(&buf, str, 6); |
| 953 | } |
| 954 | |
| 955 | if (global.tune.bufsize != 16384) { |
| 956 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 957 | |
| 958 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 959 | * match bufsize - rewrite size, but at the moment it seems |
| 960 | * that clients don't take care of it. |
| 961 | */ |
| 962 | write_n32(str + 2, global.tune.bufsize); |
| 963 | chunk_memcat(&buf, str, 6); |
| 964 | } |
| 965 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 966 | h2_set_frame_size(buf.area, buf.data - 9); |
| 967 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 968 | if (unlikely(ret <= 0)) { |
| 969 | if (!ret) { |
| 970 | h2c->flags |= H2_CF_MUX_MFULL; |
| 971 | h2c->flags |= H2_CF_DEM_MROOM; |
| 972 | return 0; |
| 973 | } |
| 974 | else { |
| 975 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 976 | return 0; |
| 977 | } |
| 978 | } |
| 979 | return ret; |
| 980 | } |
| 981 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 982 | /* Try to receive a connection preface, then upon success try to send our |
| 983 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 984 | * missing data. It may return an error in h2c. |
| 985 | */ |
| 986 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 987 | { |
| 988 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 989 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 990 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 991 | 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] | 992 | |
| 993 | if (unlikely(ret1 <= 0)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 994 | if (ret1 < 0) |
| 995 | sess_log(h2c->conn->owner); |
| 996 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 997 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 998 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1002 | ret2 = h2c_send_settings(h2c); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1003 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1004 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1005 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1006 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1007 | } |
| 1008 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1009 | /* Try to send a connection preface, then upon success try to send our |
| 1010 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1011 | * missing data. It may return an error in h2c. |
| 1012 | */ |
| 1013 | static int h2c_bck_send_preface(struct h2c *h2c) |
| 1014 | { |
| 1015 | struct buffer *res; |
| 1016 | |
| 1017 | if (h2c_mux_busy(h2c, NULL)) { |
| 1018 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
| 1022 | res = h2_get_buf(h2c, &h2c->mbuf); |
| 1023 | if (!res) { |
| 1024 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1025 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
| 1029 | if (!b_data(res)) { |
| 1030 | /* preface not yet sent */ |
| 1031 | b_istput(res, ist(H2_CONN_PREFACE)); |
| 1032 | } |
| 1033 | |
| 1034 | return h2c_send_settings(h2c); |
| 1035 | } |
| 1036 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1037 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 1038 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 1039 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 1040 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 1041 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 1042 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 1043 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 1044 | * case so that it is safe to use h2c_error() to report such errors. |
| 1045 | */ |
| 1046 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 1047 | { |
| 1048 | struct buffer *res; |
| 1049 | char str[17]; |
| 1050 | int ret; |
| 1051 | |
| 1052 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 1053 | return 1; // claim that it worked |
| 1054 | |
| 1055 | if (h2c_mux_busy(h2c, h2s)) { |
| 1056 | if (h2s) |
| 1057 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1058 | else |
| 1059 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1060 | return 0; |
| 1061 | } |
| 1062 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1063 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1064 | if (!res) { |
| 1065 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1066 | if (h2s) |
| 1067 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1068 | else |
| 1069 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
| 1073 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 1074 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 1075 | |
| 1076 | if (h2c->last_sid < 0) |
| 1077 | h2c->last_sid = h2c->max_id; |
| 1078 | |
| 1079 | write_n32(str + 9, h2c->last_sid); |
| 1080 | write_n32(str + 13, h2c->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1081 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1082 | if (unlikely(ret <= 0)) { |
| 1083 | if (!ret) { |
| 1084 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1085 | if (h2s) |
| 1086 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1087 | else |
| 1088 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1089 | return 0; |
| 1090 | } |
| 1091 | else { |
| 1092 | /* we cannot report this error using GOAWAY, so we mark |
| 1093 | * it and claim a success. |
| 1094 | */ |
| 1095 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1096 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 1097 | return 1; |
| 1098 | } |
| 1099 | } |
| 1100 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 1101 | return ret; |
| 1102 | } |
| 1103 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1104 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 1105 | * during mux operations. This stream must be valid and cannot be closed |
| 1106 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 1107 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 1108 | * not yet. |
| 1109 | * |
| 1110 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1111 | * to write the message, it subscribes the stream to future notifications. |
| 1112 | */ |
| 1113 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1114 | { |
| 1115 | struct buffer *res; |
| 1116 | char str[13]; |
| 1117 | int ret; |
| 1118 | |
| 1119 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 1120 | return 1; |
| 1121 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1122 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1123 | * RST_STREAM in response to a RST_STREAM frame. |
| 1124 | */ |
| 1125 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1126 | ret = 1; |
| 1127 | goto ignore; |
| 1128 | } |
| 1129 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1130 | if (h2c_mux_busy(h2c, h2s)) { |
| 1131 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1135 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1136 | if (!res) { |
| 1137 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1138 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
| 1142 | /* len: 4, type: 3, flags: none */ |
| 1143 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1144 | write_n32(str + 5, h2s->id); |
| 1145 | write_n32(str + 9, h2s->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1146 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1147 | |
| 1148 | if (unlikely(ret <= 0)) { |
| 1149 | if (!ret) { |
| 1150 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1151 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1152 | return 0; |
| 1153 | } |
| 1154 | else { |
| 1155 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1156 | return 0; |
| 1157 | } |
| 1158 | } |
| 1159 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1160 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1161 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1162 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1163 | return ret; |
| 1164 | } |
| 1165 | |
| 1166 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 1167 | * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the |
| 1168 | * error code unless the stream's state already is IDLE or CLOSED in which |
| 1169 | * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if |
| 1170 | * it was not yet. |
| 1171 | * |
| 1172 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1173 | * 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] | 1174 | * 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] | 1175 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1176 | */ |
| 1177 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1178 | { |
| 1179 | struct buffer *res; |
| 1180 | char str[13]; |
| 1181 | int ret; |
| 1182 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1183 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1184 | * RST_STREAM in response to a RST_STREAM frame. |
| 1185 | */ |
| 1186 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1187 | ret = 1; |
| 1188 | goto ignore; |
| 1189 | } |
| 1190 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1191 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1192 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1193 | return 0; |
| 1194 | } |
| 1195 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1196 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1197 | if (!res) { |
| 1198 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1199 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1200 | return 0; |
| 1201 | } |
| 1202 | |
| 1203 | /* len: 4, type: 3, flags: none */ |
| 1204 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1205 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1206 | write_n32(str + 5, h2c->dsi); |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1207 | write_n32(str + 9, h2s->id ? h2s->errcode : H2_ERR_STREAM_CLOSED); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1208 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1209 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1210 | if (unlikely(ret <= 0)) { |
| 1211 | if (!ret) { |
| 1212 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1213 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1214 | return 0; |
| 1215 | } |
| 1216 | else { |
| 1217 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1218 | return 0; |
| 1219 | } |
| 1220 | } |
| 1221 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1222 | ignore: |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1223 | if (h2s->id) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1224 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1225 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1226 | } |
| 1227 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1228 | return ret; |
| 1229 | } |
| 1230 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1231 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 1232 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 1233 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 1234 | * on success or zero if nothing was done. In case of lack of room to write the |
| 1235 | * message, it subscribes the requesting stream to future notifications. |
| 1236 | */ |
| 1237 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 1238 | { |
| 1239 | struct h2c *h2c = h2s->h2c; |
| 1240 | struct buffer *res; |
| 1241 | char str[9]; |
| 1242 | int ret; |
| 1243 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1244 | 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] | 1245 | return 1; |
| 1246 | |
| 1247 | if (h2c_mux_busy(h2c, h2s)) { |
| 1248 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1252 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1253 | if (!res) { |
| 1254 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1255 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
| 1259 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1260 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1261 | write_n32(str + 5, h2s->id); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1262 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1263 | if (likely(ret > 0)) { |
| 1264 | h2s->flags |= H2_SF_ES_SENT; |
| 1265 | } |
| 1266 | else if (!ret) { |
| 1267 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1268 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1269 | return 0; |
| 1270 | } |
| 1271 | else { |
| 1272 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1273 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1274 | } |
| 1275 | return ret; |
| 1276 | } |
| 1277 | |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1278 | /* wake the streams attached to the connection, whose id is greater than <last>, |
| 1279 | * and assign their conn_stream the CS_FL_* flags <flags> in addition to |
Willy Tarreau | 2c096c3 | 2018-09-12 09:45:54 +0200 | [diff] [blame] | 1280 | * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection. |
| 1281 | * The stream's state is automatically updated accordingly. |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1282 | */ |
| 1283 | static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags) |
| 1284 | { |
| 1285 | struct eb32_node *node; |
| 1286 | struct h2s *h2s; |
| 1287 | |
| 1288 | if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR) |
Willy Tarreau | a851935 | 2018-12-18 16:44:28 +0100 | [diff] [blame] | 1289 | flags |= CS_FL_ERR_PENDING; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1290 | |
| 1291 | if (conn_xprt_read0_pending(h2c->conn)) |
Willy Tarreau | 2c096c3 | 2018-09-12 09:45:54 +0200 | [diff] [blame] | 1292 | flags |= CS_FL_REOS; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1293 | |
| 1294 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1295 | while (node) { |
| 1296 | h2s = container_of(node, struct h2s, by_id); |
| 1297 | if (h2s->id <= last) |
| 1298 | break; |
| 1299 | node = eb32_next(node); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1300 | |
| 1301 | if (!h2s->cs) { |
| 1302 | /* this stream was already orphaned */ |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 1303 | h2s_destroy(h2s); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1304 | continue; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1305 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1306 | |
| 1307 | h2s->cs->flags |= flags; |
Willy Tarreau | a851935 | 2018-12-18 16:44:28 +0100 | [diff] [blame] | 1308 | if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS)) |
| 1309 | h2s->cs->flags |= CS_FL_ERROR; |
| 1310 | |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 1311 | h2s_alert(h2s); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1312 | |
Willy Tarreau | a851935 | 2018-12-18 16:44:28 +0100 | [diff] [blame] | 1313 | if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR) |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1314 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | 2c096c3 | 2018-09-12 09:45:54 +0200 | [diff] [blame] | 1315 | else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN) |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1316 | h2s->st = H2_SS_HREM; |
Willy Tarreau | 2c096c3 | 2018-09-12 09:45:54 +0200 | [diff] [blame] | 1317 | else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1318 | h2s_close(h2s); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1319 | } |
| 1320 | } |
| 1321 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1322 | /* Increase all streams' outgoing window size by the difference passed in |
| 1323 | * argument. This is needed upon receipt of the settings frame if the initial |
| 1324 | * window size is different. The difference may be negative and the resulting |
| 1325 | * window size as well, for the time it takes to receive some window updates. |
| 1326 | */ |
| 1327 | static void h2c_update_all_ws(struct h2c *h2c, int diff) |
| 1328 | { |
| 1329 | struct h2s *h2s; |
| 1330 | struct eb32_node *node; |
| 1331 | |
| 1332 | if (!diff) |
| 1333 | return; |
| 1334 | |
| 1335 | node = eb32_first(&h2c->streams_by_id); |
| 1336 | while (node) { |
| 1337 | h2s = container_of(node, struct h2s, by_id); |
| 1338 | h2s->mws += diff; |
| 1339 | node = eb32_next(node); |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1344 | * ACKs it if needed. Returns > 0 on success or zero on missing data. It may |
| 1345 | * return an error in h2c. Described in RFC7540#6.5. |
| 1346 | */ |
| 1347 | static int h2c_handle_settings(struct h2c *h2c) |
| 1348 | { |
| 1349 | unsigned int offset; |
| 1350 | int error; |
| 1351 | |
| 1352 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1353 | if (h2c->dfl) { |
| 1354 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1355 | goto fail; |
| 1356 | } |
| 1357 | return 1; |
| 1358 | } |
| 1359 | |
| 1360 | if (h2c->dsi != 0) { |
| 1361 | error = H2_ERR_PROTOCOL_ERROR; |
| 1362 | goto fail; |
| 1363 | } |
| 1364 | |
| 1365 | if (h2c->dfl % 6) { |
| 1366 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1367 | goto fail; |
| 1368 | } |
| 1369 | |
| 1370 | /* that's the limit we can process */ |
| 1371 | if (h2c->dfl > global.tune.bufsize) { |
| 1372 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1373 | goto fail; |
| 1374 | } |
| 1375 | |
| 1376 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1377 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1378 | return 0; |
| 1379 | |
| 1380 | /* parse the frame */ |
| 1381 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1382 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1383 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1384 | |
| 1385 | switch (type) { |
| 1386 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1387 | /* we need to update all existing streams with the |
| 1388 | * difference from the previous iws. |
| 1389 | */ |
| 1390 | if (arg < 0) { // RFC7540#6.5.2 |
| 1391 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1392 | goto fail; |
| 1393 | } |
| 1394 | h2c_update_all_ws(h2c, arg - h2c->miw); |
| 1395 | h2c->miw = arg; |
| 1396 | break; |
| 1397 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1398 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1399 | error = H2_ERR_PROTOCOL_ERROR; |
| 1400 | goto fail; |
| 1401 | } |
| 1402 | h2c->mfs = arg; |
| 1403 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1404 | case H2_SETTINGS_ENABLE_PUSH: |
| 1405 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1406 | error = H2_ERR_PROTOCOL_ERROR; |
| 1407 | goto fail; |
| 1408 | } |
| 1409 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | /* need to ACK this frame now */ |
| 1414 | h2c->st0 = H2_CS_FRAME_A; |
| 1415 | return 1; |
| 1416 | fail: |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1417 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1418 | h2c_error(h2c, error); |
| 1419 | return 0; |
| 1420 | } |
| 1421 | |
| 1422 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1423 | * success or one of the h2_status values. |
| 1424 | */ |
| 1425 | static int h2c_ack_settings(struct h2c *h2c) |
| 1426 | { |
| 1427 | struct buffer *res; |
| 1428 | char str[9]; |
| 1429 | int ret = -1; |
| 1430 | |
| 1431 | if (h2c_mux_busy(h2c, NULL)) { |
| 1432 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1433 | return 0; |
| 1434 | } |
| 1435 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1436 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1437 | if (!res) { |
| 1438 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1439 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1440 | return 0; |
| 1441 | } |
| 1442 | |
| 1443 | memcpy(str, |
| 1444 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1445 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1446 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1447 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1448 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1449 | if (unlikely(ret <= 0)) { |
| 1450 | if (!ret) { |
| 1451 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1452 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1453 | return 0; |
| 1454 | } |
| 1455 | else { |
| 1456 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1457 | return 0; |
| 1458 | } |
| 1459 | } |
| 1460 | return ret; |
| 1461 | } |
| 1462 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1463 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1464 | * the pointer to the payload in <payload>. Returns > 0 on success or zero on |
| 1465 | * missing data. It may return an error in h2c. |
| 1466 | */ |
| 1467 | static int h2c_handle_ping(struct h2c *h2c) |
| 1468 | { |
| 1469 | /* frame length must be exactly 8 */ |
| 1470 | if (h2c->dfl != 8) { |
| 1471 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 1472 | return 0; |
| 1473 | } |
| 1474 | |
| 1475 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1476 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1477 | h2c->st0 = H2_CS_FRAME_A; |
| 1478 | return 1; |
| 1479 | } |
| 1480 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1481 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1482 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1483 | * error in h2c. |
| 1484 | */ |
| 1485 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1486 | { |
| 1487 | struct buffer *res; |
| 1488 | char str[13]; |
| 1489 | int ret = -1; |
| 1490 | |
| 1491 | if (h2c_mux_busy(h2c, NULL)) { |
| 1492 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1493 | return 0; |
| 1494 | } |
| 1495 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1496 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1497 | if (!res) { |
| 1498 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1499 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1500 | return 0; |
| 1501 | } |
| 1502 | |
| 1503 | /* length: 4, type: 8, flags: none */ |
| 1504 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1505 | write_n32(str + 5, sid); |
| 1506 | write_n32(str + 9, increment); |
| 1507 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1508 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1509 | |
| 1510 | if (unlikely(ret <= 0)) { |
| 1511 | if (!ret) { |
| 1512 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1513 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1514 | return 0; |
| 1515 | } |
| 1516 | else { |
| 1517 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1518 | return 0; |
| 1519 | } |
| 1520 | } |
| 1521 | return ret; |
| 1522 | } |
| 1523 | |
| 1524 | /* try to send pending window update for the connection. It's safe to call it |
| 1525 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1526 | * failure. It may return an error in h2c. |
| 1527 | */ |
| 1528 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1529 | { |
| 1530 | int ret = 1; |
| 1531 | |
| 1532 | if (h2c->rcvd_c <= 0) |
| 1533 | return 1; |
| 1534 | |
| 1535 | /* send WU for the connection */ |
| 1536 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1537 | if (ret > 0) |
| 1538 | h2c->rcvd_c = 0; |
| 1539 | |
| 1540 | return ret; |
| 1541 | } |
| 1542 | |
| 1543 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1544 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1545 | * room or failure. It may return an error in h2c. |
| 1546 | */ |
| 1547 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1548 | { |
| 1549 | int ret = 1; |
| 1550 | |
| 1551 | if (h2c->rcvd_s <= 0) |
| 1552 | return 1; |
| 1553 | |
| 1554 | /* send WU for the stream */ |
| 1555 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1556 | if (ret > 0) |
| 1557 | h2c->rcvd_s = 0; |
| 1558 | |
| 1559 | return ret; |
| 1560 | } |
| 1561 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1562 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1563 | * success, 0 on missing data or one of the h2_status values. |
| 1564 | */ |
| 1565 | static int h2c_ack_ping(struct h2c *h2c) |
| 1566 | { |
| 1567 | struct buffer *res; |
| 1568 | char str[17]; |
| 1569 | int ret = -1; |
| 1570 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1571 | if (b_data(&h2c->dbuf) < 8) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1572 | return 0; |
| 1573 | |
| 1574 | if (h2c_mux_busy(h2c, NULL)) { |
| 1575 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1576 | return 0; |
| 1577 | } |
| 1578 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1579 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1580 | if (!res) { |
| 1581 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1582 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1583 | return 0; |
| 1584 | } |
| 1585 | |
| 1586 | memcpy(str, |
| 1587 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1588 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1589 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1590 | |
| 1591 | /* copy the original payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1592 | h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1593 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1594 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1595 | if (unlikely(ret <= 0)) { |
| 1596 | if (!ret) { |
| 1597 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1598 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1599 | return 0; |
| 1600 | } |
| 1601 | else { |
| 1602 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1603 | return 0; |
| 1604 | } |
| 1605 | } |
| 1606 | return ret; |
| 1607 | } |
| 1608 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1609 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1610 | * Returns > 0 on success or zero on missing data. It may return an error in |
| 1611 | * h2c or h2s. Described in RFC7540#6.9. |
| 1612 | */ |
| 1613 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1614 | { |
| 1615 | int32_t inc; |
| 1616 | int error; |
| 1617 | |
| 1618 | if (h2c->dfl != 4) { |
| 1619 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1620 | goto conn_err; |
| 1621 | } |
| 1622 | |
| 1623 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1624 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1625 | return 0; |
| 1626 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1627 | inc = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1628 | |
| 1629 | if (h2c->dsi != 0) { |
| 1630 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1631 | |
| 1632 | /* it's not an error to receive WU on a closed stream */ |
| 1633 | if (h2s->st == H2_SS_CLOSED) |
| 1634 | return 1; |
| 1635 | |
| 1636 | if (!inc) { |
| 1637 | error = H2_ERR_PROTOCOL_ERROR; |
| 1638 | goto strm_err; |
| 1639 | } |
| 1640 | |
| 1641 | if (h2s->mws >= 0 && h2s->mws + inc < 0) { |
| 1642 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1643 | goto strm_err; |
| 1644 | } |
| 1645 | |
| 1646 | h2s->mws += inc; |
| 1647 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1648 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 1649 | if (h2s->send_wait) |
| 1650 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 1651 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1652 | } |
| 1653 | } |
| 1654 | else { |
| 1655 | /* connection window update */ |
| 1656 | if (!inc) { |
| 1657 | error = H2_ERR_PROTOCOL_ERROR; |
| 1658 | goto conn_err; |
| 1659 | } |
| 1660 | |
| 1661 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1662 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1663 | goto conn_err; |
| 1664 | } |
| 1665 | |
| 1666 | h2c->mws += inc; |
| 1667 | } |
| 1668 | |
| 1669 | return 1; |
| 1670 | |
| 1671 | conn_err: |
| 1672 | h2c_error(h2c, error); |
| 1673 | return 0; |
| 1674 | |
| 1675 | strm_err: |
| 1676 | if (h2s) { |
| 1677 | h2s_error(h2s, error); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1678 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1679 | } |
| 1680 | else |
| 1681 | h2c_error(h2c, error); |
| 1682 | return 0; |
| 1683 | } |
| 1684 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1685 | /* processes a GOAWAY frame, and signals all streams whose ID is greater than |
| 1686 | * the last ID. Returns > 0 on success or zero on missing data. It may return |
| 1687 | * an error in h2c. Described in RFC7540#6.8. |
| 1688 | */ |
| 1689 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1690 | { |
| 1691 | int error; |
| 1692 | int last; |
| 1693 | |
| 1694 | if (h2c->dsi != 0) { |
| 1695 | error = H2_ERR_PROTOCOL_ERROR; |
| 1696 | goto conn_err; |
| 1697 | } |
| 1698 | |
| 1699 | if (h2c->dfl < 8) { |
| 1700 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1701 | goto conn_err; |
| 1702 | } |
| 1703 | |
| 1704 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1705 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1706 | return 0; |
| 1707 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1708 | last = h2_get_n32(&h2c->dbuf, 0); |
| 1709 | h2c->errcode = h2_get_n32(&h2c->dbuf, 4); |
Olivier Houchard | 9117780 | 2018-12-19 14:49:39 +0100 | [diff] [blame] | 1710 | h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1711 | if (h2c->last_sid < 0) |
| 1712 | h2c->last_sid = last; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1713 | return 1; |
| 1714 | |
| 1715 | conn_err: |
| 1716 | h2c_error(h2c, error); |
| 1717 | return 0; |
| 1718 | } |
| 1719 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1720 | /* processes a PRIORITY frame, and either skips it or rejects if it is |
| 1721 | * invalid. Returns > 0 on success or zero on missing data. It may return |
| 1722 | * an error in h2c. Described in RFC7540#6.3. |
| 1723 | */ |
| 1724 | static int h2c_handle_priority(struct h2c *h2c) |
| 1725 | { |
| 1726 | int error; |
| 1727 | |
| 1728 | if (h2c->dsi == 0) { |
| 1729 | error = H2_ERR_PROTOCOL_ERROR; |
| 1730 | goto conn_err; |
| 1731 | } |
| 1732 | |
| 1733 | if (h2c->dfl != 5) { |
| 1734 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1735 | goto conn_err; |
| 1736 | } |
| 1737 | |
| 1738 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1739 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1740 | return 0; |
| 1741 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1742 | if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1743 | /* 7540#5.3 : can't depend on itself */ |
| 1744 | error = H2_ERR_PROTOCOL_ERROR; |
| 1745 | goto conn_err; |
| 1746 | } |
| 1747 | return 1; |
| 1748 | |
| 1749 | conn_err: |
| 1750 | h2c_error(h2c, error); |
| 1751 | return 0; |
| 1752 | } |
| 1753 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1754 | /* processes an RST_STREAM frame, and sets the 32-bit error code on the stream. |
| 1755 | * Returns > 0 on success or zero on missing data. It may return an error in |
| 1756 | * h2c. Described in RFC7540#6.4. |
| 1757 | */ |
| 1758 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1759 | { |
| 1760 | int error; |
| 1761 | |
| 1762 | if (h2c->dsi == 0) { |
| 1763 | error = H2_ERR_PROTOCOL_ERROR; |
| 1764 | goto conn_err; |
| 1765 | } |
| 1766 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1767 | if (h2c->dfl != 4) { |
| 1768 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1769 | goto conn_err; |
| 1770 | } |
| 1771 | |
| 1772 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1773 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1774 | return 0; |
| 1775 | |
| 1776 | /* late RST, already handled */ |
| 1777 | if (h2s->st == H2_SS_CLOSED) |
| 1778 | return 1; |
| 1779 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1780 | h2s->errcode = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1781 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1782 | |
| 1783 | if (h2s->cs) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 1784 | cs_set_error(h2s->cs); |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 1785 | h2s_alert(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | h2s->flags |= H2_SF_RST_RCVD; |
| 1789 | return 1; |
| 1790 | |
| 1791 | conn_err: |
| 1792 | h2c_error(h2c, error); |
| 1793 | return 0; |
| 1794 | } |
| 1795 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1796 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 1797 | * It may return an error in h2c or h2s. The caller must consider that the |
| 1798 | * return value is the new h2s in case one was allocated (most common case). |
| 1799 | * Described in RFC7540#6.2. Most of the |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1800 | * errors here are reported as connection errors since it's impossible to |
| 1801 | * recover from such errors after the compression context has been altered. |
| 1802 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1803 | 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] | 1804 | { |
| 1805 | int error; |
| 1806 | |
| 1807 | if (!h2c->dfl) { |
| 1808 | error = H2_ERR_PROTOCOL_ERROR; // empty headers frame! |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1809 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1810 | goto strm_err; |
| 1811 | } |
| 1812 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1813 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1814 | return NULL; // empty buffer |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1815 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1816 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1817 | return NULL; // incomplete frame |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1818 | |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 1819 | if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 1820 | return 0; // too many cs still present |
| 1821 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1822 | /* now either the frame is complete or the buffer is complete */ |
| 1823 | if (h2s->st != H2_SS_IDLE) { |
| 1824 | /* FIXME: stream already exists, this is only allowed for |
| 1825 | * trailers (not supported for now). |
| 1826 | */ |
| 1827 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1828 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1829 | goto conn_err; |
| 1830 | } |
| 1831 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 1832 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 1833 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1834 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1835 | goto conn_err; |
| 1836 | } |
| 1837 | |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1838 | /* 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] | 1839 | * positively from h2c_frt_stream_new(), the stream will report the error, |
| 1840 | * 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] | 1841 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 1842 | h2s = h2c_frt_stream_new(h2c, h2c->dsi); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1843 | if (!h2s) { |
| 1844 | error = H2_ERR_INTERNAL_ERROR; |
| 1845 | goto conn_err; |
| 1846 | } |
| 1847 | |
| 1848 | h2s->st = H2_SS_OPEN; |
| 1849 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 1850 | h2s->st = H2_SS_HREM; |
| 1851 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 39d6850 | 2018-03-02 12:26:37 +0100 | [diff] [blame] | 1852 | /* note: cs cannot be null for now (just created above) */ |
| 1853 | h2s->cs->flags |= CS_FL_REOS; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1854 | } |
| 1855 | |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 1856 | if (!h2s_decode_headers(h2s)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1857 | return NULL; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1858 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 1859 | if (h2c->st0 >= H2_CS_ERROR) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1860 | return NULL; |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 1861 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1862 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1863 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1864 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1865 | } |
| 1866 | else { |
| 1867 | /* update the max stream ID if the request is being processed */ |
| 1868 | if (h2s->id > h2c->max_id) |
| 1869 | h2c->max_id = h2s->id; |
| 1870 | } |
| 1871 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1872 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1873 | |
| 1874 | conn_err: |
| 1875 | h2c_error(h2c, error); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1876 | return NULL; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1877 | |
| 1878 | strm_err: |
| 1879 | if (h2s) { |
| 1880 | h2s_error(h2s, error); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1881 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1882 | } |
| 1883 | else |
| 1884 | h2c_error(h2c, error); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1885 | return NULL; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1886 | } |
| 1887 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 1888 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 1889 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 1890 | * errors here are reported as connection errors since it's impossible to |
| 1891 | * recover from such errors after the compression context has been altered. |
| 1892 | */ |
| 1893 | static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 1894 | { |
| 1895 | int error; |
| 1896 | |
| 1897 | if (!h2c->dfl) { |
| 1898 | error = H2_ERR_PROTOCOL_ERROR; // empty headers frame! |
| 1899 | sess_log(h2c->conn->owner); |
| 1900 | goto strm_err; |
| 1901 | } |
| 1902 | |
| 1903 | if (!b_size(&h2c->dbuf)) |
| 1904 | return NULL; // empty buffer |
| 1905 | |
| 1906 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
| 1907 | return NULL; // incomplete frame |
| 1908 | |
| 1909 | if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 1910 | return 0; // too many cs still present |
| 1911 | |
| 1912 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 1913 | h2s->flags |= H2_SF_ES_RCVD; |
| 1914 | h2s->cs->flags |= CS_FL_REOS; |
| 1915 | } |
| 1916 | |
| 1917 | if (!h2s_decode_headers(h2s)) |
| 1918 | return NULL; |
| 1919 | |
| 1920 | if (h2c->st0 >= H2_CS_ERROR) |
| 1921 | return NULL; |
| 1922 | |
| 1923 | if (h2s->st >= H2_SS_ERROR) { |
| 1924 | /* stream error : send RST_STREAM */ |
| 1925 | h2c->st0 = H2_CS_FRAME_E; |
| 1926 | } |
| 1927 | |
| 1928 | if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR) |
| 1929 | h2s->st = H2_SS_ERROR; |
| 1930 | else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN) |
| 1931 | h2s->st = H2_SS_HREM; |
| 1932 | else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC) |
| 1933 | h2s_close(h2s); |
| 1934 | |
| 1935 | return h2s; |
| 1936 | |
| 1937 | conn_err: |
| 1938 | h2c_error(h2c, error); |
| 1939 | return NULL; |
| 1940 | |
| 1941 | strm_err: |
| 1942 | if (h2s) { |
| 1943 | h2s_error(h2s, error); |
| 1944 | h2c->st0 = H2_CS_FRAME_E; |
| 1945 | } |
| 1946 | else |
| 1947 | h2c_error(h2c, error); |
| 1948 | return NULL; |
| 1949 | } |
| 1950 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1951 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 1952 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 1953 | */ |
| 1954 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 1955 | { |
| 1956 | int error; |
| 1957 | |
| 1958 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 1959 | * to signal an end of stream (with the ES flag). |
| 1960 | */ |
| 1961 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1962 | if (!b_size(&h2c->dbuf) && h2c->dfl) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1963 | return 0; // empty buffer |
| 1964 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1965 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1966 | return 0; // incomplete frame |
| 1967 | |
| 1968 | /* now either the frame is complete or the buffer is complete */ |
| 1969 | |
| 1970 | if (!h2c->dsi) { |
| 1971 | /* RFC7540#6.1 */ |
| 1972 | error = H2_ERR_PROTOCOL_ERROR; |
| 1973 | goto conn_err; |
| 1974 | } |
| 1975 | |
| 1976 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 1977 | /* RFC7540#6.1 */ |
| 1978 | error = H2_ERR_STREAM_CLOSED; |
| 1979 | goto strm_err; |
| 1980 | } |
| 1981 | |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 1982 | if (!h2_frt_transfer_data(h2s)) |
| 1983 | return 0; |
| 1984 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1985 | /* call the upper layers to process the frame, then let the upper layer |
| 1986 | * notify the stream about any change. |
| 1987 | */ |
| 1988 | if (!h2s->cs) { |
| 1989 | error = H2_ERR_STREAM_CLOSED; |
| 1990 | goto strm_err; |
| 1991 | } |
| 1992 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 1993 | if (h2c->st0 >= H2_CS_ERROR) |
| 1994 | return 0; |
| 1995 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1996 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1997 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1998 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | /* check for completion : the callee will change this to FRAME_A or |
| 2002 | * FRAME_H once done. |
| 2003 | */ |
| 2004 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2005 | return 0; |
| 2006 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2007 | |
| 2008 | /* last frame */ |
| 2009 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 2010 | h2s->st = H2_SS_HREM; |
| 2011 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 39d6850 | 2018-03-02 12:26:37 +0100 | [diff] [blame] | 2012 | h2s->cs->flags |= CS_FL_REOS; |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2013 | } |
| 2014 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2015 | return 1; |
| 2016 | |
| 2017 | conn_err: |
| 2018 | h2c_error(h2c, error); |
| 2019 | return 0; |
| 2020 | |
| 2021 | strm_err: |
| 2022 | if (h2s) { |
| 2023 | h2s_error(h2s, error); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2024 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2025 | } |
| 2026 | else |
| 2027 | h2c_error(h2c, error); |
| 2028 | return 0; |
| 2029 | } |
| 2030 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2031 | /* process Rx frames to be demultiplexed */ |
| 2032 | static void h2_process_demux(struct h2c *h2c) |
| 2033 | { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2034 | struct h2s *h2s = NULL, *tmp_h2s; |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 2035 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2036 | if (h2c->st0 >= H2_CS_ERROR) |
| 2037 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2038 | |
| 2039 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2040 | if (h2c->st0 == H2_CS_PREFACE) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2041 | if (h2c->flags & H2_CF_IS_BACK) |
| 2042 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2043 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 2044 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2045 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2046 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2047 | sess_log(h2c->conn->owner); |
| 2048 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2049 | goto fail; |
| 2050 | } |
| 2051 | |
| 2052 | h2c->max_id = 0; |
| 2053 | h2c->st0 = H2_CS_SETTINGS1; |
| 2054 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2055 | |
| 2056 | if (h2c->st0 == H2_CS_SETTINGS1) { |
| 2057 | struct h2_fh hdr; |
| 2058 | |
| 2059 | /* ensure that what is pending is a valid SETTINGS frame |
| 2060 | * without an ACK. |
| 2061 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2062 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2063 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2064 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2065 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2066 | sess_log(h2c->conn->owner); |
| 2067 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2068 | goto fail; |
| 2069 | } |
| 2070 | |
| 2071 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 2072 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2073 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2074 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2075 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2076 | goto fail; |
| 2077 | } |
| 2078 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2079 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2080 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2081 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2082 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2083 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2084 | goto fail; |
| 2085 | } |
| 2086 | |
| 2087 | /* that's OK, switch to FRAME_P to process it */ |
| 2088 | h2c->dfl = hdr.len; |
| 2089 | h2c->dsi = hdr.sid; |
| 2090 | h2c->dft = hdr.ft; |
| 2091 | h2c->dff = hdr.ff; |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 2092 | h2c->dpl = 0; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2093 | h2c->st0 = H2_CS_FRAME_P; |
| 2094 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2095 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2096 | |
| 2097 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2098 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2099 | int ret = 0; |
| 2100 | |
| 2101 | if (h2c->st0 >= H2_CS_ERROR) |
| 2102 | break; |
| 2103 | |
| 2104 | if (h2c->st0 == H2_CS_FRAME_H) { |
| 2105 | struct h2_fh hdr; |
| 2106 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2107 | if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2108 | break; |
| 2109 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2110 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2111 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2112 | h2c->st0 = H2_CS_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2113 | if (!h2c->nb_streams) { |
| 2114 | /* only log if no other stream can report the error */ |
| 2115 | sess_log(h2c->conn->owner); |
| 2116 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2117 | break; |
| 2118 | } |
| 2119 | |
| 2120 | h2c->dfl = hdr.len; |
| 2121 | h2c->dsi = hdr.sid; |
| 2122 | h2c->dft = hdr.ft; |
| 2123 | h2c->dff = hdr.ff; |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 2124 | h2c->dpl = 0; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2125 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2126 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2130 | tmp_h2s = h2c_st_by_id(h2c, h2c->dsi); |
| 2131 | |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2132 | if (tmp_h2s != h2s && h2s && h2s->cs && |
| 2133 | (b_data(&h2s->rxbuf) || |
| 2134 | (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2135 | /* we may have to signal the upper layers */ |
| 2136 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2137 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2138 | } |
| 2139 | h2s = tmp_h2s; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2140 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 2141 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2142 | goto strm_err; |
| 2143 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2144 | if (h2s->st == H2_SS_IDLE && |
| 2145 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 2146 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 2147 | * this state MUST be treated as a connection error |
| 2148 | */ |
| 2149 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2150 | h2c->st0 = H2_CS_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2151 | if (!h2c->nb_streams) { |
| 2152 | /* only log if no other stream can report the error */ |
| 2153 | sess_log(h2c->conn->owner); |
| 2154 | } |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2155 | break; |
| 2156 | } |
| 2157 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2158 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 2159 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 2160 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
| 2161 | * this state MUST be treated as a stream error |
| 2162 | */ |
| 2163 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2164 | goto strm_err; |
| 2165 | } |
| 2166 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2167 | /* Below the management of frames received in closed state is a |
| 2168 | * bit hackish because the spec makes strong differences between |
| 2169 | * streams closed by receiving RST, sending RST, and seeing ES |
| 2170 | * in both directions. In addition to this, the creation of a |
| 2171 | * new stream reusing the identifier of a closed one will be |
| 2172 | * detected here. Given that we cannot keep track of all closed |
| 2173 | * streams forever, we consider that unknown closed streams were |
| 2174 | * closed on RST received, which allows us to respond with an |
| 2175 | * RST without breaking the connection (eg: to abort a transfer). |
| 2176 | * Some frames have to be silently ignored as well. |
| 2177 | */ |
| 2178 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
| 2179 | if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) { |
| 2180 | /* #5.1.1: The identifier of a newly |
| 2181 | * established stream MUST be numerically |
| 2182 | * greater than all streams that the initiating |
| 2183 | * endpoint has opened or reserved. This |
| 2184 | * governs streams that are opened using a |
| 2185 | * HEADERS frame and streams that are reserved |
| 2186 | * using PUSH_PROMISE. An endpoint that |
| 2187 | * receives an unexpected stream identifier |
| 2188 | * MUST respond with a connection error. |
| 2189 | */ |
| 2190 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2191 | goto strm_err; |
| 2192 | } |
| 2193 | |
| 2194 | if (h2s->flags & H2_SF_RST_RCVD) { |
| 2195 | /* RFC7540#5.1:closed: an endpoint that |
| 2196 | * receives any frame other than PRIORITY after |
| 2197 | * receiving a RST_STREAM MUST treat that as a |
| 2198 | * stream error of type STREAM_CLOSED. |
| 2199 | * |
| 2200 | * Note that old streams fall into this category |
| 2201 | * and will lead to an RST being sent. |
| 2202 | */ |
| 2203 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2204 | h2c->st0 = H2_CS_FRAME_E; |
| 2205 | goto strm_err; |
| 2206 | } |
| 2207 | |
| 2208 | /* RFC7540#5.1:closed: if this state is reached as a |
| 2209 | * result of sending a RST_STREAM frame, the peer that |
| 2210 | * receives the RST_STREAM might have already sent |
| 2211 | * frames on the stream that cannot be withdrawn. An |
| 2212 | * endpoint MUST ignore frames that it receives on |
| 2213 | * closed streams after it has sent a RST_STREAM |
| 2214 | * frame. An endpoint MAY choose to limit the period |
| 2215 | * over which it ignores frames and treat frames that |
| 2216 | * arrive after this time as being in error. |
| 2217 | */ |
| 2218 | if (!(h2s->flags & H2_SF_RST_SENT)) { |
| 2219 | /* RFC7540#5.1:closed: any frame other than |
| 2220 | * PRIO/WU/RST in this state MUST be treated as |
| 2221 | * a connection error |
| 2222 | */ |
| 2223 | if (h2c->dft != H2_FT_RST_STREAM && |
| 2224 | h2c->dft != H2_FT_PRIORITY && |
| 2225 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 2226 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2227 | goto strm_err; |
| 2228 | } |
| 2229 | } |
| 2230 | } |
| 2231 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2232 | #if 0 |
| 2233 | // problem below: it is not possible to completely ignore such |
| 2234 | // streams as we need to maintain the compression state as well |
| 2235 | // and for this we need to completely process these frames (eg: |
| 2236 | // HEADERS frames) as well as counting DATA frames to emit |
| 2237 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 2238 | // This is a typical case of layer violation where the |
| 2239 | // transported contents are critical to the connection's |
| 2240 | // validity and must be ignored at the same time :-( |
| 2241 | |
| 2242 | /* graceful shutdown, ignore streams whose ID is higher than |
| 2243 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 2244 | */ |
| 2245 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2246 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2247 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2248 | h2c->dfl -= ret; |
| 2249 | ret = h2c->dfl == 0; |
| 2250 | goto strm_err; |
| 2251 | } |
| 2252 | #endif |
| 2253 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2254 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 2255 | case H2_FT_SETTINGS: |
| 2256 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2257 | ret = h2c_handle_settings(h2c); |
| 2258 | |
| 2259 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2260 | ret = h2c_ack_settings(h2c); |
| 2261 | break; |
| 2262 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 2263 | case H2_FT_PING: |
| 2264 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2265 | ret = h2c_handle_ping(h2c); |
| 2266 | |
| 2267 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2268 | ret = h2c_ack_ping(h2c); |
| 2269 | break; |
| 2270 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 2271 | case H2_FT_WINDOW_UPDATE: |
| 2272 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2273 | ret = h2c_handle_window_update(h2c, h2s); |
| 2274 | break; |
| 2275 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2276 | case H2_FT_CONTINUATION: |
| 2277 | /* we currently don't support CONTINUATION frames since |
| 2278 | * we have nowhere to store the partial HEADERS frame. |
| 2279 | * Let's abort the stream on an INTERNAL_ERROR here. |
| 2280 | */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2281 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2282 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2283 | h2c->st0 = H2_CS_FRAME_E; |
| 2284 | } |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2285 | break; |
| 2286 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2287 | case H2_FT_HEADERS: |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2288 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2289 | if (h2c->flags & H2_CF_IS_BACK) |
| 2290 | tmp_h2s = h2c_bck_handle_headers(h2c, h2s); |
| 2291 | else |
| 2292 | tmp_h2s = h2c_frt_handle_headers(h2c, h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2293 | if (tmp_h2s) { |
| 2294 | h2s = tmp_h2s; |
| 2295 | ret = 1; |
| 2296 | } |
| 2297 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2298 | break; |
| 2299 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2300 | case H2_FT_DATA: |
| 2301 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2302 | ret = h2c_frt_handle_data(h2c, h2s); |
| 2303 | |
| 2304 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2305 | ret = h2c_send_strm_wu(h2c); |
| 2306 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2307 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 2308 | case H2_FT_PRIORITY: |
| 2309 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2310 | ret = h2c_handle_priority(h2c); |
| 2311 | break; |
| 2312 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2313 | case H2_FT_RST_STREAM: |
| 2314 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2315 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 2316 | break; |
| 2317 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 2318 | case H2_FT_GOAWAY: |
| 2319 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2320 | ret = h2c_handle_goaway(h2c); |
| 2321 | break; |
| 2322 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2323 | case H2_FT_PUSH_PROMISE: |
| 2324 | /* not permitted here, RFC7540#5.1 */ |
| 2325 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2326 | if (!h2c->nb_streams) { |
| 2327 | /* only log if no other stream can report the error */ |
| 2328 | sess_log(h2c->conn->owner); |
| 2329 | } |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2330 | break; |
| 2331 | |
| 2332 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2333 | default: |
| 2334 | /* drop frames that we ignore. They may be larger than |
| 2335 | * the buffer so we drain all of their contents until |
| 2336 | * we reach the end. |
| 2337 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2338 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2339 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2340 | h2c->dfl -= ret; |
| 2341 | ret = h2c->dfl == 0; |
| 2342 | } |
| 2343 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2344 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2345 | /* We may have to send an RST if not done yet */ |
| 2346 | if (h2s->st == H2_SS_ERROR) |
| 2347 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2348 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2349 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2350 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2351 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2352 | /* error or missing data condition met above ? */ |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2353 | if (ret <= 0) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2354 | break; |
| 2355 | |
| 2356 | if (h2c->st0 != H2_CS_FRAME_H) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2357 | b_del(&h2c->dbuf, h2c->dfl); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2358 | h2c->st0 = H2_CS_FRAME_H; |
| 2359 | } |
| 2360 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2361 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2362 | if (h2c->rcvd_c > 0 && |
| 2363 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2364 | h2c_send_conn_wu(h2c); |
| 2365 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2366 | fail: |
| 2367 | /* we can go here on missing data, blocked response or error */ |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2368 | if (h2s && h2s->cs && |
| 2369 | (b_data(&h2s->rxbuf) || |
| 2370 | (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2371 | /* we may have to signal the upper layers */ |
| 2372 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2373 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2374 | } |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2375 | |
| 2376 | if (h2_recv_allowed(h2c)) |
| 2377 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2381 | * the end. |
| 2382 | */ |
| 2383 | static int h2_process_mux(struct h2c *h2c) |
| 2384 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2385 | struct h2s *h2s, *h2s_back; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2386 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2387 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2388 | if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) { |
| 2389 | if (unlikely(h2c_bck_send_preface(h2c) <= 0)) { |
| 2390 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2391 | if (h2c->st0 == H2_CS_ERROR) { |
| 2392 | h2c->st0 = H2_CS_ERROR2; |
| 2393 | sess_log(h2c->conn->owner); |
| 2394 | } |
| 2395 | goto fail; |
| 2396 | } |
| 2397 | h2c->st0 = H2_CS_SETTINGS1; |
| 2398 | } |
| 2399 | /* need to wait for the other side */ |
Willy Tarreau | 75a930a | 2018-12-12 08:03:58 +0100 | [diff] [blame] | 2400 | if (h2c->st0 < H2_CS_FRAME_H) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2401 | return 1; |
| 2402 | } |
| 2403 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2404 | /* start by sending possibly pending window updates */ |
| 2405 | if (h2c->rcvd_c > 0 && |
| 2406 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2407 | h2c_send_conn_wu(h2c) < 0) |
| 2408 | goto fail; |
| 2409 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2410 | /* First we always process the flow control list because the streams |
| 2411 | * waiting there were already elected for immediate emission but were |
| 2412 | * blocked just on this. |
| 2413 | */ |
| 2414 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2415 | list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2416 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2417 | h2c->st0 >= H2_CS_ERROR) |
| 2418 | break; |
| 2419 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2420 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2421 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2422 | h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2423 | tasklet_wakeup(h2s->send_wait->task); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2424 | LIST_DEL(&h2s->list); |
| 2425 | LIST_INIT(&h2s->list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 2426 | LIST_ADDQ(&h2c->sending_list, &h2s->list); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2427 | } |
| 2428 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2429 | list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2430 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2431 | break; |
| 2432 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2433 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2434 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2435 | h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2436 | tasklet_wakeup(h2s->send_wait->task); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2437 | LIST_DEL(&h2s->list); |
| 2438 | LIST_INIT(&h2s->list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 2439 | LIST_ADDQ(&h2c->sending_list, &h2s->list); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2440 | } |
| 2441 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2442 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2443 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2444 | if (h2c->st0 == H2_CS_ERROR) { |
| 2445 | if (h2c->max_id >= 0) { |
| 2446 | h2c_send_goaway_error(h2c, NULL); |
| 2447 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2448 | return 0; |
| 2449 | } |
| 2450 | |
| 2451 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2452 | } |
| 2453 | return 1; |
| 2454 | } |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2455 | return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2456 | } |
| 2457 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2458 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2459 | /* Attempt to read data, and subscribe if none available. |
| 2460 | * The function returns 1 if data has been received, otherwise zero. |
| 2461 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2462 | static int h2_recv(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2463 | { |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2464 | struct connection *conn = h2c->conn; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2465 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2466 | int max; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2467 | size_t ret; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2468 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2469 | if (h2c->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2470 | return (b_data(&h2c->dbuf)); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2471 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2472 | if (!h2_recv_allowed(h2c)) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2473 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2474 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2475 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2476 | if (!buf) { |
| 2477 | h2c->flags |= H2_CF_DEM_DALLOC; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2478 | return 0; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2479 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2480 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2481 | do { |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 2482 | b_realign_if_empty(buf); |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2483 | if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
| 2484 | /* HTX in use : try to pre-align the buffer like the |
| 2485 | * rxbufs will be to optimize memory copies. We'll make |
| 2486 | * sure that the frame header lands at the end of the |
| 2487 | * HTX block to alias it upon recv. We cannot use the |
| 2488 | * head because rcv_buf() will realign the buffer if |
| 2489 | * it's empty. Thus we cheat and pretend we already |
| 2490 | * have a few bytes there. |
| 2491 | */ |
| 2492 | max = buf_room_for_htx_data(buf) + 9; |
Willy Tarreau | c0960d1 | 2018-12-14 10:59:15 +0100 | [diff] [blame] | 2493 | buf->head = sizeof(struct htx) - 9; |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2494 | } |
| 2495 | else |
| 2496 | max = b_room(buf); |
| 2497 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2498 | if (max) |
| 2499 | ret = conn->xprt->rcv_buf(conn, buf, max, 0); |
| 2500 | else |
| 2501 | ret = 0; |
| 2502 | } while (ret > 0); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2503 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2504 | if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size)) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2505 | conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event); |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2506 | |
Olivier Houchard | a1411e6 | 2018-08-17 18:42:48 +0200 | [diff] [blame] | 2507 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2508 | h2_release_buf(h2c, &h2c->dbuf); |
Olivier Houchard | 4667773 | 2018-11-29 17:06:17 +0100 | [diff] [blame] | 2509 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2510 | } |
| 2511 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2512 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2513 | h2c->flags |= H2_CF_DEM_DFULL; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2514 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2515 | } |
| 2516 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2517 | /* Try to send data if possible. |
| 2518 | * The function returns 1 if data have been sent, otherwise zero. |
| 2519 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2520 | static int h2_send(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2521 | { |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2522 | struct connection *conn = h2c->conn; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2523 | int done; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2524 | int sent = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2525 | |
| 2526 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 2527 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2528 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2529 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2530 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2531 | /* a handshake was requested */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2532 | goto schedule; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2533 | } |
| 2534 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2535 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2536 | * pending streams into the existing buffer until it's reportedly full |
| 2537 | * or the end of send requests is reached. Then it tries to send this |
| 2538 | * buffer's contents out, marks it not full if at least one byte could |
| 2539 | * be sent, and tries again. |
| 2540 | * |
| 2541 | * The snd_buf() function normally takes a "flags" argument which may |
| 2542 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2543 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2544 | * connection is streaming lots of data (used to increase TLS record |
| 2545 | * size at the expense of latency). The former can be sent any time |
| 2546 | * there's a buffer full flag, as it indicates at least one stream |
| 2547 | * attempted to send and failed so there are pending data. An |
| 2548 | * alternative would be to set it as long as there's an active stream |
| 2549 | * but that would be problematic for ACKs until we have an absolute |
| 2550 | * guarantee that all waiters have at least one byte to send. The |
| 2551 | * latter should possibly not be set for now. |
| 2552 | */ |
| 2553 | |
| 2554 | done = 0; |
| 2555 | while (!done) { |
| 2556 | unsigned int flags = 0; |
| 2557 | |
| 2558 | /* fill as much as we can into the current buffer */ |
| 2559 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2560 | done = h2_process_mux(h2c); |
| 2561 | |
| 2562 | if (conn->flags & CO_FL_ERROR) |
| 2563 | break; |
| 2564 | |
| 2565 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2566 | flags |= CO_SFL_MSG_MORE; |
| 2567 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2568 | if (b_data(&h2c->mbuf)) { |
| 2569 | int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2570 | if (!ret) |
| 2571 | break; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2572 | sent = 1; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2573 | b_del(&h2c->mbuf, ret); |
| 2574 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2575 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2576 | |
| 2577 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2578 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2579 | } |
| 2580 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2581 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2582 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2583 | b_reset(&h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2584 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2585 | /* We're not full anymore, so we can wake any task that are waiting |
| 2586 | * for us. |
| 2587 | */ |
| 2588 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 2589 | while (!LIST_ISEMPTY(&h2c->send_list)) { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2590 | struct h2s *h2s = LIST_ELEM(h2c->send_list.n, |
| 2591 | struct h2s *, list); |
| 2592 | LIST_DEL(&h2s->list); |
| 2593 | LIST_INIT(&h2s->list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 2594 | LIST_ADDQ(&h2c->sending_list, &h2s->list); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2595 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2596 | h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2597 | tasklet_wakeup(h2s->send_wait->task); |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 2598 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2599 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2600 | /* We're done, no more to send */ |
| 2601 | if (!b_data(&h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2602 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2603 | schedule: |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2604 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
| 2605 | conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event); |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2606 | return sent; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2610 | { |
| 2611 | struct h2c *h2c = ctx; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2612 | int ret = 0; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2613 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2614 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2615 | ret = h2_send(h2c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2616 | if (!(h2c->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2617 | ret |= h2_recv(h2c); |
Willy Tarreau | cef5c8e | 2018-12-18 10:29:54 +0100 | [diff] [blame] | 2618 | if (ret || b_data(&h2c->dbuf)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2619 | h2_process(h2c); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2620 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2621 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2622 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2623 | /* callback called on any event by the connection handler. |
| 2624 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2625 | * destruction of the connection (which normally doesn not happen in h2). |
| 2626 | */ |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2627 | static int h2_process(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2628 | { |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2629 | struct connection *conn = h2c->conn; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2630 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2631 | if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2632 | h2_process_demux(h2c); |
| 2633 | |
| 2634 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2635 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2636 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2637 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2638 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 2639 | } |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2640 | h2_send(h2c); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2641 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 2642 | if (unlikely(h2c->proxy->state == PR_STSTOPPED)) { |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2643 | /* frontend is stopping, reload likely in progress, let's try |
| 2644 | * to announce a graceful shutdown if not yet done. We don't |
| 2645 | * care if it fails, it will be tried again later. |
| 2646 | */ |
| 2647 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 2648 | if (h2c->last_sid < 0) |
| 2649 | h2c->last_sid = (1U << 31) - 1; |
| 2650 | h2c_send_goaway_error(h2c, NULL); |
| 2651 | } |
| 2652 | } |
| 2653 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2654 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2655 | * If we received early data, and the handshake is done, wake |
| 2656 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2657 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2658 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 2659 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 2660 | struct eb32_node *node; |
| 2661 | struct h2s *h2s; |
| 2662 | |
| 2663 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 2664 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 2665 | |
| 2666 | while (node) { |
| 2667 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | fde287c | 2018-12-19 18:33:16 +0100 | [diff] [blame] | 2668 | if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2669 | h2s_notify_recv(h2s); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2670 | node = eb32_next(node); |
| 2671 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2672 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2673 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 2674 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 2675 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 2676 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 2677 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 2678 | h2_wake_some_streams(h2c, 0, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2679 | |
| 2680 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2681 | /* no more stream, kill the connection now */ |
| 2682 | h2_release(conn); |
| 2683 | return -1; |
| 2684 | } |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2685 | } |
| 2686 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2687 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2688 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2689 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2690 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 2691 | h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 2692 | (h2c->st0 != H2_CS_ERROR && |
| 2693 | !b_data(&h2c->mbuf) && |
| 2694 | (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && |
| 2695 | ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list)))) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2696 | h2_release_buf(h2c, &h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2697 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2698 | if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2699 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2700 | 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] | 2701 | task_queue(h2c->task); |
| 2702 | } |
| 2703 | else |
| 2704 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2705 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2706 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2707 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2708 | return 0; |
| 2709 | } |
| 2710 | |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2711 | static int h2_wake(struct connection *conn) |
| 2712 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2713 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2714 | |
| 2715 | return (h2_process(h2c)); |
| 2716 | } |
| 2717 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2718 | /* Connection timeout management. The principle is that if there's no receipt |
| 2719 | * nor sending for a certain amount of time, the connection is closed. If the |
| 2720 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 2721 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 2722 | * GOAWAY frame. |
| 2723 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2724 | 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] | 2725 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2726 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2727 | int expired = tick_is_expired(t->expire, now_ms); |
| 2728 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2729 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2730 | return t; |
| 2731 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2732 | task_delete(t); |
| 2733 | task_free(t); |
| 2734 | |
| 2735 | if (!h2c) { |
| 2736 | /* resources were already deleted */ |
| 2737 | return NULL; |
| 2738 | } |
| 2739 | |
| 2740 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2741 | h2c_error(h2c, H2_ERR_NO_ERROR); |
| 2742 | h2_wake_some_streams(h2c, 0, 0); |
| 2743 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2744 | if (b_data(&h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2745 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 2746 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2747 | } |
| 2748 | |
| 2749 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2750 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2751 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 2752 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2753 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2754 | if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) { |
| 2755 | int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2756 | if (ret > 0) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2757 | b_del(&h2c->mbuf, ret); |
| 2758 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2759 | } |
| 2760 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2761 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2762 | /* either we can release everything now or it will be done later once |
| 2763 | * the last stream closes. |
| 2764 | */ |
| 2765 | if (eb_is_empty(&h2c->streams_by_id)) |
| 2766 | h2_release(h2c->conn); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2767 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2768 | return NULL; |
| 2769 | } |
| 2770 | |
| 2771 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2772 | /*******************************************/ |
| 2773 | /* functions below are used by the streams */ |
| 2774 | /*******************************************/ |
| 2775 | |
| 2776 | /* |
| 2777 | * Attach a new stream to a connection |
| 2778 | * (Used for outgoing connections) |
| 2779 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2780 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2781 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2782 | struct conn_stream *cs; |
| 2783 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2784 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2785 | |
| 2786 | cs = cs_new(conn); |
| 2787 | if (!cs) |
| 2788 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2789 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2790 | if (!h2s) { |
| 2791 | cs_free(cs); |
| 2792 | return NULL; |
| 2793 | } |
| 2794 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2795 | } |
| 2796 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 2797 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 2798 | * We have to scan because we may have some orphan streams. It might be |
| 2799 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 2800 | * orphans. |
| 2801 | */ |
| 2802 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 2803 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2804 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 2805 | struct h2s *h2s; |
| 2806 | struct eb32_node *node; |
| 2807 | |
| 2808 | node = eb32_first(&h2c->streams_by_id); |
| 2809 | while (node) { |
| 2810 | h2s = container_of(node, struct h2s, by_id); |
| 2811 | if (h2s->cs) |
| 2812 | return h2s->cs; |
| 2813 | node = eb32_next(node); |
| 2814 | } |
| 2815 | return NULL; |
| 2816 | } |
| 2817 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2818 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 2819 | * Destroy the mux and the associated connection, if it is no longer used |
| 2820 | */ |
| 2821 | static void h2_destroy(struct connection *conn) |
| 2822 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2823 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 2824 | |
| 2825 | if (eb_is_empty(&h2c->streams_by_id)) |
| 2826 | h2_release(h2c->conn); |
| 2827 | } |
| 2828 | |
| 2829 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2830 | * Detach the stream from the connection and possibly release the connection. |
| 2831 | */ |
| 2832 | static void h2_detach(struct conn_stream *cs) |
| 2833 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2834 | struct h2s *h2s = cs->ctx; |
| 2835 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2836 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2837 | |
| 2838 | cs->ctx = NULL; |
| 2839 | if (!h2s) |
| 2840 | return; |
| 2841 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2842 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2843 | h2c = h2s->h2c; |
| 2844 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 2845 | h2c->nb_cs--; |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 2846 | if (h2c->flags & H2_CF_DEM_TOOMANY && |
| 2847 | !h2_has_too_many_cs(h2c)) { |
| 2848 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2849 | if (h2_recv_allowed(h2c)) |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2850 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 2851 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2852 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2853 | /* this stream may be blocked waiting for some data to leave (possibly |
| 2854 | * an ES or RST frame), so orphan it in this case. |
| 2855 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 2856 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 2857 | (h2c->st0 < H2_CS_ERROR) && |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 2858 | (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2859 | return; |
| 2860 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 2861 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 2862 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 2863 | /* unblock the connection if it was blocked on this |
| 2864 | * stream. |
| 2865 | */ |
| 2866 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 2867 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2868 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 2869 | } |
| 2870 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 2871 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2872 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2873 | if (h2c->flags & H2_CF_IS_BACK && |
| 2874 | (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2875 | if (!(h2c->conn->flags & |
| 2876 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 2877 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2878 | h2c->conn->owner = sess; |
| 2879 | session_add_conn(sess, h2c->conn, h2c->conn->target); |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2880 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 2881 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2882 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 2883 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 2884 | return; |
| 2885 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2886 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 2887 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 2888 | h2c->conn->flags |= CO_FL_PRIVATE; |
| 2889 | if (LIST_ISEMPTY(&h2c->conn->list)) { |
| 2890 | struct server *srv = objt_server(h2c->conn->target); |
| 2891 | |
| 2892 | if (srv) { |
| 2893 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 2894 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 2895 | else |
| 2896 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 2897 | } |
| 2898 | |
| 2899 | } |
| 2900 | } |
| 2901 | } |
| 2902 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 2903 | /* We don't want to close right now unless we're removing the |
| 2904 | * last stream, and either the connection is in error, or it |
| 2905 | * reached the ID already specified in a GOAWAY frame received |
| 2906 | * or sent (as seen by last_sid >= 0). |
| 2907 | */ |
| 2908 | if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */ |
| 2909 | ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */ |
Willy Tarreau | 42d55b9 | 2018-06-13 14:24:56 +0200 | [diff] [blame] | 2910 | (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */ |
Olivier Houchard | 52b9466 | 2018-10-21 03:01:20 +0200 | [diff] [blame] | 2911 | (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) || |
Olivier Houchard | 93c8852 | 2018-11-30 15:39:16 +0100 | [diff] [blame] | 2912 | (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2913 | (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */ |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 2914 | (conn_xprt_read0_pending(h2c->conn) || |
| 2915 | (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) { |
| 2916 | /* no more stream will come, kill it now */ |
| 2917 | h2_release(h2c->conn); |
| 2918 | } |
| 2919 | else if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2920 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 2921 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 2922 | task_queue(h2c->task); |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 2923 | } |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 2924 | else |
| 2925 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2926 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2927 | } |
| 2928 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 2929 | static void h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2930 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 2931 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2932 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2933 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2934 | 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] | 2935 | return; |
| 2936 | |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2937 | /* if no outgoing data was seen on this stream, it means it was |
| 2938 | * closed with a "tcp-request content" rule that is normally |
| 2939 | * used to kill the connection ASAP (eg: limit abuse). In this |
| 2940 | * case we send a goaway to close the connection. |
| 2941 | */ |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 2942 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 2943 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2944 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 2945 | |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2946 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && |
| 2947 | !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 2948 | h2c_send_goaway_error(h2c, h2s) <= 0) |
| 2949 | return; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2950 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2951 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 2952 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 2953 | h2s_close(h2s); |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2954 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 2955 | return; |
| 2956 | add_to_list: |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2957 | if (LIST_ISEMPTY(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2958 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2959 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 2960 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 2961 | h2s->send_wait = sw; |
| 2962 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 2963 | h2s->send_wait = sw; |
| 2964 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 2965 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2966 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 2967 | /* Let the handler know we want shutr */ |
| 2968 | sw->handle = (void *)((long)sw->handle | 1); |
| 2969 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2970 | } |
| 2971 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 2972 | static void h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2973 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 2974 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2975 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2976 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2977 | 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] | 2978 | return; |
| 2979 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 2980 | if (h2s->flags & H2_SF_HEADERS_SENT) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 2981 | /* we can cleanly close using an empty data frame only after headers */ |
| 2982 | |
| 2983 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 2984 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2985 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 2986 | |
| 2987 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 2988 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 2989 | else |
| 2990 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2991 | } else { |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2992 | /* if no outgoing data was seen on this stream, it means it was |
| 2993 | * closed with a "tcp-request content" rule that is normally |
| 2994 | * used to kill the connection ASAP (eg: limit abuse). In this |
| 2995 | * case we send a goaway to close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 2996 | */ |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 2997 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 2998 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2999 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3000 | |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3001 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && |
| 3002 | !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3003 | h2c_send_goaway_error(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3004 | goto add_to_list; |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3005 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3006 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3007 | } |
| 3008 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3009 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3010 | tasklet_wakeup(h2c->wait_event.task); |
| 3011 | return; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3012 | |
| 3013 | add_to_list: |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3014 | if (LIST_ISEMPTY(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3015 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3016 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3017 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3018 | h2s->send_wait = sw; |
| 3019 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3020 | h2s->send_wait = sw; |
| 3021 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3022 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3023 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3024 | /* let the handler know we want to shutw */ |
| 3025 | sw->handle = (void *)((long)(sw->handle) | 2); |
| 3026 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3027 | } |
| 3028 | |
| 3029 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3030 | { |
| 3031 | struct h2s *h2s = ctx; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3032 | long reason = (long)h2s->wait_event.handle; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3033 | |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3034 | if (h2s->send_wait) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3035 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3036 | h2s->send_wait = NULL; |
| 3037 | LIST_DEL(&h2s->list); |
| 3038 | LIST_INIT(&h2s->list); |
| 3039 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3040 | if (reason & 2) |
| 3041 | h2_do_shutw(h2s); |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3042 | if (reason & 1) |
| 3043 | h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3044 | |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3045 | if (h2s->st == H2_SS_CLOSED && |
Olivier Houchard | ffda58b | 2018-12-16 01:29:11 +0100 | [diff] [blame] | 3046 | !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs) |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3047 | h2s_destroy(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3048 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3049 | } |
| 3050 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3051 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3052 | { |
| 3053 | struct h2s *h2s = cs->ctx; |
| 3054 | |
| 3055 | if (!mode) |
| 3056 | return; |
| 3057 | |
| 3058 | h2_do_shutr(h2s); |
| 3059 | } |
| 3060 | |
| 3061 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3062 | { |
| 3063 | struct h2s *h2s = cs->ctx; |
| 3064 | |
| 3065 | h2_do_shutw(h2s); |
| 3066 | } |
| 3067 | |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3068 | /* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3069 | * HTX request or response depending on the connection's side. Returns the |
| 3070 | * number of bytes emitted if > 0, or 0 if it couldn't proceed. Stream errors |
| 3071 | * are reported in h2s->errcode and connection errors in h2c->errcode. |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3072 | */ |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3073 | static int h2s_decode_headers(struct h2s *h2s) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3074 | { |
| 3075 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3076 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3077 | struct buffer *tmp = get_trash_chunk(); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3078 | struct http_hdr list[MAX_HTTP_HDR * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3079 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3080 | unsigned int msgf; |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3081 | struct buffer *csbuf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3082 | struct htx *htx = NULL; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3083 | int flen = h2c->dfl; |
| 3084 | int outlen = 0; |
| 3085 | int wrap; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3086 | int try = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3087 | |
| 3088 | if (!h2c->dfl) { |
| 3089 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame! |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 3090 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3091 | return 0; |
| 3092 | } |
| 3093 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3094 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3095 | return 0; // incomplete input frame |
| 3096 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3097 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3098 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3099 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3100 | copy = alloc_trash_chunk(); |
| 3101 | if (!copy) { |
| 3102 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3103 | goto fail; |
| 3104 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3105 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3106 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3107 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3108 | } |
| 3109 | |
| 3110 | /* The padlen is the first byte before data, and the padding appears |
| 3111 | * after data. padlen+data+padding are included in flen. |
| 3112 | */ |
| 3113 | if (h2c->dff & H2_F_HEADERS_PADDED) { |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 3114 | h2c->dpl = *hdrs; |
| 3115 | if (h2c->dpl >= flen) { |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3116 | /* RFC7540#6.2 : pad length = length of frame payload or greater */ |
| 3117 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3118 | goto fail; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3119 | } |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 3120 | flen -= h2c->dpl + 1; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3121 | hdrs += 1; // skip Pad Length |
| 3122 | } |
| 3123 | |
| 3124 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3125 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3126 | if (read_n32(hdrs) == h2s->id) { |
| 3127 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3128 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3129 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3130 | } |
| 3131 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3132 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3133 | flen -= 5; |
| 3134 | } |
| 3135 | |
| 3136 | /* FIXME: lack of END_HEADERS means there's a continuation frame, we |
| 3137 | * don't support this for now and can't even decompress so we have to |
| 3138 | * break the connection. |
| 3139 | */ |
| 3140 | if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) { |
| 3141 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3142 | goto fail; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3143 | } |
| 3144 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3145 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3146 | if (!csbuf) { |
| 3147 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3148 | goto fail; |
| 3149 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3150 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3151 | /* we can't retry a failed decompression operation so we must be very |
| 3152 | * careful not to take any risks. In practice the output buffer is |
| 3153 | * always empty except maybe for trailers, in which case we simply have |
| 3154 | * to wait for the upper layer to finish consuming what is available. |
| 3155 | */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3156 | |
| 3157 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
| 3158 | htx = htx_from_buf(&h2s->rxbuf); |
| 3159 | if (!htx_is_empty(htx)) |
| 3160 | goto fail; |
| 3161 | } else { |
| 3162 | if (b_data(csbuf)) |
| 3163 | goto fail; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3164 | |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3165 | csbuf->head = 0; |
| 3166 | try = b_size(csbuf); |
| 3167 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3168 | |
| 3169 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3170 | sizeof(list)/sizeof(list[0]), tmp); |
| 3171 | if (outlen < 0) { |
| 3172 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3173 | goto fail; |
| 3174 | } |
| 3175 | |
| 3176 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3177 | msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3178 | |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3179 | if (htx) { |
| 3180 | /* HTX mode */ |
| 3181 | if (h2c->flags & H2_CF_IS_BACK) |
| 3182 | outlen = h2_make_htx_response(list, htx, &msgf); |
| 3183 | else |
| 3184 | outlen = h2_make_htx_request(list, htx, &msgf); |
| 3185 | } else { |
| 3186 | /* HTTP/1 mode */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3187 | outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3188 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3189 | |
| 3190 | if (outlen < 0) { |
| 3191 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3192 | goto fail; |
| 3193 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3194 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3195 | if (msgf & H2_MSGF_BODY) { |
| 3196 | /* a payload is present */ |
| 3197 | if (msgf & H2_MSGF_BODY_CL) |
| 3198 | h2s->flags |= H2_SF_DATA_CLEN; |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3199 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3200 | h2s->flags |= H2_SF_DATA_CHNK; |
| 3201 | } |
| 3202 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3203 | /* now consume the input data */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3204 | b_del(&h2c->dbuf, h2c->dfl); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3205 | h2c->st0 = H2_CS_FRAME_H; |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3206 | b_add(csbuf, outlen); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3207 | |
Willy Tarreau | 39d6850 | 2018-03-02 12:26:37 +0100 | [diff] [blame] | 3208 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3209 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 39d6850 | 2018-03-02 12:26:37 +0100 | [diff] [blame] | 3210 | h2s->cs->flags |= CS_FL_REOS; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3211 | if (htx) |
| 3212 | htx_add_endof(htx, HTX_BLK_EOM); |
Willy Tarreau | 39d6850 | 2018-03-02 12:26:37 +0100 | [diff] [blame] | 3213 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3214 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3215 | leave: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3216 | if (htx) |
| 3217 | htx_to_buf(htx, &h2s->rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3218 | free_trash_chunk(copy); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3219 | return outlen; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3220 | fail: |
| 3221 | outlen = 0; |
| 3222 | goto leave; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3223 | } |
| 3224 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3225 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 3226 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 3227 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 3228 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 3229 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 3230 | * 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] | 3231 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3232 | * send the current frame, 0 if it couldn't complete, in which case |
| 3233 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3234 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3235 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3236 | * have checked the frame header and ensured that the frame was complete or the |
| 3237 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3238 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3239 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3240 | { |
| 3241 | struct h2c *h2c = h2s->h2c; |
| 3242 | int block1, block2; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3243 | unsigned int flen = 0; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3244 | unsigned int chklen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3245 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3246 | struct buffer *csbuf; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3247 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3248 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3249 | |
| 3250 | /* The padlen is the first byte before data, and the padding appears |
| 3251 | * after data. padlen+data+padding are included in flen. |
| 3252 | */ |
Willy Tarreau | 7912781 | 2017-12-03 21:06:59 +0100 | [diff] [blame] | 3253 | if (h2c->dff & H2_F_DATA_PADDED) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3254 | if (b_data(&h2c->dbuf) < 1) |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3255 | return 0; |
| 3256 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3257 | h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 3258 | if (h2c->dpl >= h2c->dfl) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3259 | /* RFC7540#6.1 : pad length = length of frame payload or greater */ |
| 3260 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3261 | return 0; |
| 3262 | } |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3263 | |
| 3264 | /* skip the padlen byte */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3265 | b_del(&h2c->dbuf, 1); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3266 | h2c->dfl--; |
| 3267 | h2c->rcvd_c++; h2c->rcvd_s++; |
| 3268 | h2c->dff &= ~H2_F_DATA_PADDED; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3269 | } |
| 3270 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3271 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3272 | if (!csbuf) { |
| 3273 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3274 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3275 | } |
| 3276 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3277 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3278 | flen = h2c->dfl - h2c->dpl; |
Olivier Houchard | 2f30883 | 2018-12-19 15:53:53 +0100 | [diff] [blame] | 3279 | if (h2c->proxy->options2 & PR_O2_USE_HTX) |
| 3280 | htx = htx_from_buf(csbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3281 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3282 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3283 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3284 | if (flen > b_data(&h2c->dbuf)) { |
| 3285 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3286 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3287 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3288 | } |
| 3289 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3290 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3291 | block1 = htx_free_data_space(htx); |
| 3292 | if (!block1) { |
| 3293 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3294 | goto fail; |
| 3295 | } |
| 3296 | if (flen > block1) |
| 3297 | flen = block1; |
| 3298 | |
| 3299 | /* here, flen is the max we can copy into the output buffer */ |
| 3300 | block1 = b_contig_data(&h2c->dbuf, 0); |
| 3301 | if (flen > block1) |
| 3302 | flen = block1; |
| 3303 | |
| 3304 | if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) { |
| 3305 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3306 | goto fail; |
| 3307 | } |
| 3308 | |
| 3309 | b_del(&h2c->dbuf, flen); |
| 3310 | h2c->dfl -= flen; |
| 3311 | h2c->rcvd_c += flen; |
| 3312 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 3313 | goto try_again; |
| 3314 | } |
| 3315 | else if (unlikely(b_space_wraps(csbuf))) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3316 | /* it doesn't fit and the buffer is fragmented, |
| 3317 | * so let's defragment it and try again. |
| 3318 | */ |
| 3319 | b_slow_realign(csbuf, trash.area, 0); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3320 | } |
| 3321 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3322 | /* chunked-encoding requires more room */ |
| 3323 | if (h2s->flags & H2_SF_DATA_CHNK) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3324 | chklen = MIN(flen, b_room(csbuf)); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3325 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 3326 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 3327 | (chklen < 1048576) ? 4 : 8; |
| 3328 | chklen += 4; // CRLF, CRLF |
| 3329 | } |
| 3330 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3331 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3332 | if (flen + chklen > b_room(csbuf)) { |
| 3333 | if (chklen >= b_room(csbuf)) { |
| 3334 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3335 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3336 | } |
| 3337 | flen = b_room(csbuf) - chklen; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3338 | } |
| 3339 | |
| 3340 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3341 | /* emit the chunk size */ |
| 3342 | unsigned int chksz = flen; |
| 3343 | char str[10]; |
| 3344 | char *beg; |
| 3345 | |
| 3346 | beg = str + sizeof(str); |
| 3347 | *--beg = '\n'; |
| 3348 | *--beg = '\r'; |
| 3349 | do { |
| 3350 | *--beg = hextab[chksz & 0xF]; |
| 3351 | } while (chksz >>= 4); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3352 | b_putblk(csbuf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3353 | } |
| 3354 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3355 | /* Block1 is the length of the first block before the buffer wraps, |
| 3356 | * block2 is the optional second block to reach the end of the frame. |
| 3357 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3358 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3359 | if (block1 > flen) |
| 3360 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3361 | block2 = flen - block1; |
| 3362 | |
| 3363 | if (block1) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3364 | b_putblk(csbuf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3365 | |
| 3366 | if (block2) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3367 | b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3368 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3369 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3370 | /* emit the CRLF */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3371 | b_putblk(csbuf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3372 | } |
| 3373 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3374 | /* now mark the input data as consumed (will be deleted from the buffer |
| 3375 | * by the caller when seeing FRAME_A after sending the window update). |
| 3376 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3377 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3378 | h2c->dfl -= flen; |
| 3379 | h2c->rcvd_c += flen; |
| 3380 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 3381 | |
| 3382 | if (h2c->dfl > h2c->dpl) { |
| 3383 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3384 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3385 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3386 | } |
| 3387 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3388 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3389 | /* here we're done with the frame, all the payload (except padding) was |
| 3390 | * transferred. |
| 3391 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3392 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3393 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 3394 | if (htx) { |
| 3395 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3396 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3397 | goto fail; |
| 3398 | } |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3399 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3400 | else if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3401 | /* emit the trailing 0 CRLF CRLF */ |
| 3402 | if (b_room(csbuf) < 5) { |
| 3403 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3404 | goto fail; |
| 3405 | } |
| 3406 | chklen += 5; |
| 3407 | b_putblk(csbuf, "0\r\n\r\n", 5); |
| 3408 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3409 | } |
| 3410 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 3411 | h2c->rcvd_c += h2c->dpl; |
| 3412 | h2c->rcvd_s += h2c->dpl; |
| 3413 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3414 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
| 3415 | |
Willy Tarreau | 39d6850 | 2018-03-02 12:26:37 +0100 | [diff] [blame] | 3416 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3417 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 39d6850 | 2018-03-02 12:26:37 +0100 | [diff] [blame] | 3418 | h2s->cs->flags |= CS_FL_REOS; |
| 3419 | } |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3420 | if (htx) |
| 3421 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3422 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3423 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3424 | if (htx) |
| 3425 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3426 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3427 | } |
| 3428 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3429 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 3430 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 3431 | * number of bytes sent. The caller must check the stream's status to detect |
| 3432 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3433 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3434 | 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] | 3435 | { |
| 3436 | struct http_hdr list[MAX_HTTP_HDR]; |
| 3437 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3438 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3439 | struct buffer outbuf; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3440 | union h1_sl sl; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3441 | int es_now = 0; |
| 3442 | int ret = 0; |
| 3443 | int hdr; |
| 3444 | |
| 3445 | if (h2c_mux_busy(h2c, h2s)) { |
| 3446 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3447 | return 0; |
| 3448 | } |
| 3449 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3450 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3451 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3452 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3453 | return 0; |
| 3454 | } |
| 3455 | |
| 3456 | /* First, try to parse the H1 response and index it into <list>. |
| 3457 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 3458 | * block does not wrap and we can safely read it this way without |
| 3459 | * having to realign the buffer. |
| 3460 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3461 | 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] | 3462 | list, sizeof(list)/sizeof(list[0]), h1m, &sl); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3463 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 3464 | /* incomplete or invalid response, this is abnormal coming from |
| 3465 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 3466 | * so that won't be fixed, raise an error now. |
| 3467 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3468 | * FIXME: we should instead add the ability to only return a |
| 3469 | * 502 bad gateway. But in theory this is not supposed to |
| 3470 | * happen. |
| 3471 | */ |
| 3472 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3473 | ret = 0; |
| 3474 | goto end; |
| 3475 | } |
| 3476 | |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3477 | h2s->status = sl.st.status; |
Willy Tarreau | db72da0 | 2018-09-13 11:52:20 +0200 | [diff] [blame] | 3478 | |
| 3479 | /* certain statuses have no body or an empty one, regardless of |
| 3480 | * what the headers say. |
| 3481 | */ |
| 3482 | if (sl.st.status >= 100 && sl.st.status < 200) { |
| 3483 | h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK); |
| 3484 | h1m->curr_len = h1m->body_len = 0; |
| 3485 | } |
| 3486 | else if (sl.st.status == 204 || sl.st.status == 304) { |
| 3487 | /* no contents, claim c-len is present and set to zero */ |
| 3488 | h1m->flags &= ~H1_MF_CHNK; |
| 3489 | h1m->flags |= H1_MF_CLEN; |
| 3490 | h1m->curr_len = h1m->body_len = 0; |
| 3491 | } |
| 3492 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3493 | chunk_reset(&outbuf); |
| 3494 | |
| 3495 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3496 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3497 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3498 | outbuf.data = 0; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3499 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3500 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3501 | break; |
| 3502 | realign_again: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3503 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3504 | } |
| 3505 | |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3506 | if (outbuf.size < 9) |
| 3507 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3508 | |
| 3509 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3510 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 3511 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3512 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3513 | |
| 3514 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3515 | if (unlikely(list[0].v.len != 3)) { |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 3516 | /* this is an unparsable response */ |
| 3517 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3518 | ret = 0; |
| 3519 | goto end; |
| 3520 | } |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3521 | |
| 3522 | if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3523 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3524 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3525 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | /* encode all headers, stop at empty name */ |
| 3529 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 3530 | /* these ones do not exist in H2 and must be dropped. */ |
| 3531 | if (isteq(list[hdr].n, ist("connection")) || |
| 3532 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 3533 | isteq(list[hdr].n, ist("keep-alive")) || |
| 3534 | isteq(list[hdr].n, ist("upgrade")) || |
| 3535 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3536 | continue; |
| 3537 | |
| 3538 | if (isteq(list[hdr].n, ist(""))) |
| 3539 | break; // end |
| 3540 | |
| 3541 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 3542 | /* output full */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3543 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3544 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3545 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3546 | } |
| 3547 | } |
| 3548 | |
| 3549 | /* we may need to add END_STREAM */ |
| 3550 | if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW) |
| 3551 | es_now = 1; |
| 3552 | |
| 3553 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3554 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3555 | |
| 3556 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3557 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3558 | |
| 3559 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3560 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3561 | |
| 3562 | /* commit the H2 response */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3563 | b_add(&h2c->mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3564 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3565 | |
| 3566 | /* for now we don't implemented CONTINUATION, so we wait for a |
| 3567 | * body or directly end in TRL2. |
| 3568 | */ |
| 3569 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3570 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3571 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3572 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3573 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3574 | h2s->flags |= H2_SF_ES_SENT; |
| 3575 | if (h2s->st == H2_SS_OPEN) |
| 3576 | h2s->st = H2_SS_HLOC; |
| 3577 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3578 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3579 | } |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3580 | else if (h2s->status >= 100 && h2s->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3581 | /* 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] | 3582 | h1m_init_res(h1m); |
Willy Tarreau | 9b8cd1f | 2018-09-12 09:24:38 +0200 | [diff] [blame] | 3583 | 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] | 3584 | h2s->h1m.flags |= H1_MF_TOLOWER; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3585 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 3586 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 3587 | |
| 3588 | /* 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] | 3589 | |
| 3590 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 3591 | //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] | 3592 | return ret; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3593 | full: |
| 3594 | h1m_init_res(h1m); |
| 3595 | h1m->err_pos = -1; // don't care about errors on the response path |
| 3596 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3597 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3598 | ret = 0; |
| 3599 | goto end; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3600 | } |
| 3601 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3602 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 3603 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 3604 | * the number of bytes sent. The caller must check the stream's status to |
| 3605 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3606 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3607 | 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] | 3608 | { |
| 3609 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3610 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3611 | struct buffer outbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3612 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 3613 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3614 | int es_now = 0; |
| 3615 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3616 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3617 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3618 | |
| 3619 | if (h2c_mux_busy(h2c, h2s)) { |
| 3620 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3621 | goto end; |
| 3622 | } |
| 3623 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3624 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3625 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3626 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3627 | goto end; |
| 3628 | } |
| 3629 | |
| 3630 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3631 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3632 | goto end; |
| 3633 | |
| 3634 | chunk_reset(&outbuf); |
| 3635 | |
| 3636 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3637 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3638 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3639 | outbuf.data = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3640 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3641 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3642 | break; |
| 3643 | realign_again: |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 3644 | /* If there are pending data in the output buffer, and we have |
| 3645 | * less than 1/4 of the mbuf's size and everything fits, we'll |
| 3646 | * still perform a copy anyway. Otherwise we'll pretend the mbuf |
| 3647 | * is full and wait, to save some slow realign calls. |
| 3648 | */ |
| 3649 | if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) { |
| 3650 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3651 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3652 | goto end; |
| 3653 | } |
| 3654 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3655 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3656 | } |
| 3657 | |
| 3658 | if (outbuf.size < 9) { |
| 3659 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3660 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3661 | goto end; |
| 3662 | } |
| 3663 | |
| 3664 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3665 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 3666 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3667 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3668 | |
| 3669 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 3670 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3671 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 3672 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3673 | break; |
| 3674 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3675 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3676 | if ((long long)size > h1m->curr_len) |
| 3677 | size = h1m->curr_len; |
| 3678 | break; |
| 3679 | default: /* te:chunked : parse chunks */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3680 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 3681 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3682 | if (!ret) |
| 3683 | goto end; |
| 3684 | |
| 3685 | if (ret < 0) { |
| 3686 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 3687 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3688 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3689 | goto end; |
| 3690 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3691 | max -= ret; |
| 3692 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3693 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3694 | h1m->state = H1_MSG_CHUNK_SIZE; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3695 | } |
| 3696 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3697 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3698 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 3699 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3700 | if (!ret) |
| 3701 | goto end; |
| 3702 | |
| 3703 | if (ret < 0) { |
| 3704 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 3705 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3706 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3707 | goto end; |
| 3708 | } |
| 3709 | |
| 3710 | size = chunk; |
| 3711 | h1m->curr_len = chunk; |
| 3712 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3713 | max -= ret; |
| 3714 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3715 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3716 | h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3717 | if (!size) |
| 3718 | goto send_empty; |
| 3719 | } |
| 3720 | |
| 3721 | /* in MSG_DATA state, continue below */ |
| 3722 | size = h1m->curr_len; |
| 3723 | break; |
| 3724 | } |
| 3725 | |
| 3726 | /* we have in <size> the exact number of bytes we need to copy from |
| 3727 | * the H1 buffer. We need to check this against the connection's and |
| 3728 | * the stream's send windows, and to ensure that this fits in the max |
| 3729 | * frame size and in the buffer's available space minus 9 bytes (for |
| 3730 | * the frame header). The connection's flow control is applied last so |
| 3731 | * that we can use a separate list of streams which are immediately |
| 3732 | * unblocked on window opening. Note: we don't implement padding. |
| 3733 | */ |
| 3734 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3735 | if (size > max) |
| 3736 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3737 | |
| 3738 | if (size > h2s->mws) |
| 3739 | size = h2s->mws; |
| 3740 | |
| 3741 | if (size <= 0) { |
| 3742 | h2s->flags |= H2_SF_BLK_SFCTL; |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 3743 | if (h2s->send_wait) { |
| 3744 | LIST_DEL(&h2s->list); |
| 3745 | LIST_INIT(&h2s->list); |
| 3746 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3747 | goto end; |
| 3748 | } |
| 3749 | |
| 3750 | if (h2c->mfs && size > h2c->mfs) |
| 3751 | size = h2c->mfs; |
| 3752 | |
| 3753 | if (size + 9 > outbuf.size) { |
| 3754 | /* we have an opportunity for enlarging the too small |
| 3755 | * available space, let's try. |
| 3756 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3757 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3758 | goto realign_again; |
| 3759 | size = outbuf.size - 9; |
| 3760 | } |
| 3761 | |
| 3762 | if (size <= 0) { |
| 3763 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3764 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3765 | goto end; |
| 3766 | } |
| 3767 | |
| 3768 | if (size > h2c->mws) |
| 3769 | size = h2c->mws; |
| 3770 | |
| 3771 | if (size <= 0) { |
| 3772 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 3773 | goto end; |
| 3774 | } |
| 3775 | |
| 3776 | /* copy whatever we can */ |
| 3777 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3778 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3779 | if (ret == 1) |
| 3780 | len2 = 0; |
| 3781 | |
| 3782 | if (!ret || len1 + len2 < size) { |
| 3783 | /* FIXME: must normally never happen */ |
| 3784 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3785 | goto end; |
| 3786 | } |
| 3787 | |
| 3788 | /* limit len1/len2 to size */ |
| 3789 | if (len1 + len2 > size) { |
| 3790 | int sub = len1 + len2 - size; |
| 3791 | |
| 3792 | if (len2 > sub) |
| 3793 | len2 -= sub; |
| 3794 | else { |
| 3795 | sub -= len2; |
| 3796 | len2 = 0; |
| 3797 | len1 -= sub; |
| 3798 | } |
| 3799 | } |
| 3800 | |
| 3801 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3802 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3803 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3804 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3805 | |
| 3806 | send_empty: |
| 3807 | /* we may need to add END_STREAM */ |
| 3808 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 3809 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 3810 | * |
| 3811 | * FIXME: what we do here is not correct because we send end_stream |
| 3812 | * before knowing if we'll have to send a HEADERS frame for the |
| 3813 | * trailers. More importantly we're not consuming the trailing CRLF |
| 3814 | * after the end of trailers, so it will be left to the caller to |
| 3815 | * eat it. The right way to do it would be to measure trailers here |
| 3816 | * and to send ES only if there are no trailers. |
| 3817 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3818 | */ |
| 3819 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3820 | !h1m->curr_len || h1m->state >= H1_MSG_DONE) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3821 | es_now = 1; |
| 3822 | |
| 3823 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3824 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3825 | |
| 3826 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3827 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3828 | |
| 3829 | /* commit the H2 response */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3830 | b_add(&h2c->mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3831 | |
| 3832 | /* consume incoming H1 response */ |
| 3833 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3834 | max -= size; |
| 3835 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3836 | total += size; |
| 3837 | h1m->curr_len -= size; |
| 3838 | h2s->mws -= size; |
| 3839 | h2c->mws -= size; |
| 3840 | |
| 3841 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3842 | h1m->state = H1_MSG_CHUNK_CRLF; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3843 | goto new_frame; |
| 3844 | } |
| 3845 | } |
| 3846 | |
| 3847 | if (es_now) { |
| 3848 | if (h2s->st == H2_SS_OPEN) |
| 3849 | h2s->st = H2_SS_HLOC; |
| 3850 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3851 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3852 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3853 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 3854 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3855 | total += max; |
| 3856 | ofs += max; |
| 3857 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3858 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3859 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3860 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3861 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3862 | h2s->flags |= H2_SF_ES_SENT; |
| 3863 | } |
| 3864 | |
| 3865 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 3866 | 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] | 3867 | return total; |
| 3868 | } |
| 3869 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3870 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 3871 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 3872 | * must check the stream's status to detect any error which might have happened |
| 3873 | * subsequently to a successful send. The htx blocks are automatically removed |
| 3874 | * from the message. The htx message is assumed to be valid since produced from |
| 3875 | * the internal code, hence it contains a start line, an optional series of |
| 3876 | * header blocks and an end of header, otherwise an invalid frame could be |
| 3877 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 3878 | */ |
| 3879 | static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
| 3880 | { |
| 3881 | struct http_hdr list[MAX_HTTP_HDR]; |
| 3882 | struct h2c *h2c = h2s->h2c; |
| 3883 | struct htx_blk *blk; |
| 3884 | struct htx_blk *blk_end; |
| 3885 | struct buffer outbuf; |
| 3886 | struct htx_sl *sl; |
| 3887 | enum htx_blk_type type; |
| 3888 | int es_now = 0; |
| 3889 | int ret = 0; |
| 3890 | int hdr; |
| 3891 | int idx; |
| 3892 | |
| 3893 | if (h2c_mux_busy(h2c, h2s)) { |
| 3894 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3895 | return 0; |
| 3896 | } |
| 3897 | |
| 3898 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 3899 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3900 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3901 | return 0; |
| 3902 | } |
| 3903 | |
| 3904 | /* determine the first block which must not be deleted, blk_end may |
| 3905 | * be NULL if all blocks have to be deleted. |
| 3906 | */ |
| 3907 | idx = htx_get_head(htx); |
| 3908 | blk_end = NULL; |
| 3909 | while (idx != -1) { |
| 3910 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 3911 | idx = htx_get_next(htx, idx); |
| 3912 | if (type == HTX_BLK_EOH) { |
| 3913 | if (idx != -1) |
| 3914 | blk_end = htx_get_blk(htx, idx); |
| 3915 | break; |
| 3916 | } |
| 3917 | } |
| 3918 | |
| 3919 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 3920 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 3921 | ALREADY_CHECKED(sl); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3922 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3923 | if (h2s->status < 100 || h2s->status > 999) |
| 3924 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3925 | |
| 3926 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 3927 | hdr = 0; |
| 3928 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 3929 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3930 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 3931 | blk = htx_get_blk(htx, idx); |
| 3932 | type = htx_get_blk_type(blk); |
| 3933 | |
| 3934 | if (type == HTX_BLK_UNUSED) |
| 3935 | continue; |
| 3936 | |
| 3937 | if (type != HTX_BLK_HDR) |
| 3938 | break; |
| 3939 | |
| 3940 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 3941 | goto fail; |
| 3942 | |
| 3943 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 3944 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3945 | hdr++; |
| 3946 | } |
| 3947 | |
| 3948 | /* marker for end of headers */ |
| 3949 | list[hdr].n = ist(""); |
| 3950 | |
| 3951 | if (h2s->status == 204 || h2s->status == 304) { |
| 3952 | /* no contents, claim c-len is present and set to zero */ |
| 3953 | es_now = 1; |
| 3954 | } |
| 3955 | |
| 3956 | chunk_reset(&outbuf); |
| 3957 | |
| 3958 | while (1) { |
| 3959 | outbuf.area = b_tail(&h2c->mbuf); |
| 3960 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 3961 | outbuf.data = 0; |
| 3962 | |
| 3963 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 3964 | break; |
| 3965 | realign_again: |
| 3966 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 3967 | } |
| 3968 | |
| 3969 | if (outbuf.size < 9) |
| 3970 | goto full; |
| 3971 | |
| 3972 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 3973 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 3974 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3975 | outbuf.data = 9; |
| 3976 | |
| 3977 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3978 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 3979 | if (b_space_wraps(&h2c->mbuf)) |
| 3980 | goto realign_again; |
| 3981 | goto full; |
| 3982 | } |
| 3983 | |
| 3984 | /* encode all headers, stop at empty name */ |
| 3985 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 3986 | /* these ones do not exist in H2 and must be dropped. */ |
| 3987 | if (isteq(list[hdr].n, ist("connection")) || |
| 3988 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 3989 | isteq(list[hdr].n, ist("keep-alive")) || |
| 3990 | isteq(list[hdr].n, ist("upgrade")) || |
| 3991 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 3992 | continue; |
| 3993 | |
| 3994 | if (isteq(list[hdr].n, ist(""))) |
| 3995 | break; // end |
| 3996 | |
| 3997 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 3998 | /* output full */ |
| 3999 | if (b_space_wraps(&h2c->mbuf)) |
| 4000 | goto realign_again; |
| 4001 | goto full; |
| 4002 | } |
| 4003 | } |
| 4004 | |
| 4005 | /* we may need to add END_STREAM. |
| 4006 | * FIXME: we should also set it when we know for sure that the |
| 4007 | * content-length is zero as well as on 204/304 |
| 4008 | */ |
| 4009 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4010 | es_now = 1; |
| 4011 | |
| 4012 | if (h2s->cs->flags & CS_FL_SHW) |
| 4013 | es_now = 1; |
| 4014 | |
| 4015 | /* update the frame's size */ |
| 4016 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4017 | |
| 4018 | if (es_now) |
| 4019 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4020 | |
| 4021 | /* commit the H2 response */ |
| 4022 | b_add(&h2c->mbuf, outbuf.data); |
| 4023 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4024 | |
| 4025 | /* for now we don't implemented CONTINUATION, so we wait for a |
| 4026 | * body or directly end in TRL2. |
| 4027 | */ |
| 4028 | if (es_now) { |
| 4029 | h2s->flags |= H2_SF_ES_SENT; |
| 4030 | if (h2s->st == H2_SS_OPEN) |
| 4031 | h2s->st = H2_SS_HLOC; |
| 4032 | else |
| 4033 | h2s_close(h2s); |
| 4034 | } |
| 4035 | |
| 4036 | /* OK we could properly deliver the response */ |
| 4037 | |
| 4038 | /* remove all header blocks including the EOH and compute the |
| 4039 | * corresponding size. |
| 4040 | * |
| 4041 | * FIXME: We should remove everything when es_now is set. |
| 4042 | */ |
| 4043 | ret = 0; |
| 4044 | idx = htx_get_head(htx); |
| 4045 | blk = htx_get_blk(htx, idx); |
| 4046 | while (blk != blk_end) { |
| 4047 | ret += htx_get_blksz(blk); |
| 4048 | blk = htx_remove_blk(htx, blk); |
| 4049 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4050 | |
| 4051 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4052 | htx_remove_blk(htx, blk_end); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4053 | end: |
| 4054 | return ret; |
| 4055 | full: |
| 4056 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4057 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4058 | ret = 0; |
| 4059 | goto end; |
| 4060 | fail: |
| 4061 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4062 | * list etc go here (unrecoverable errors). |
| 4063 | */ |
| 4064 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4065 | ret = 0; |
| 4066 | goto end; |
| 4067 | } |
| 4068 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4069 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4070 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4071 | * must check the stream's status to detect any error which might have happened |
| 4072 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4073 | * from the message. The htx message is assumed to be valid since produced from |
| 4074 | * the internal code, hence it contains a start line, an optional series of |
| 4075 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4076 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4077 | */ |
| 4078 | static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
| 4079 | { |
| 4080 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4081 | struct h2c *h2c = h2s->h2c; |
| 4082 | struct htx_blk *blk; |
| 4083 | struct htx_blk *blk_end; |
| 4084 | struct buffer outbuf; |
| 4085 | struct htx_sl *sl; |
| 4086 | struct ist meth, path; |
| 4087 | enum htx_blk_type type; |
| 4088 | int es_now = 0; |
| 4089 | int ret = 0; |
| 4090 | int hdr; |
| 4091 | int idx; |
| 4092 | |
| 4093 | if (h2c_mux_busy(h2c, h2s)) { |
| 4094 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4095 | return 0; |
| 4096 | } |
| 4097 | |
| 4098 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4099 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4100 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4101 | return 0; |
| 4102 | } |
| 4103 | |
| 4104 | /* determine the first block which must not be deleted, blk_end may |
| 4105 | * be NULL if all blocks have to be deleted. |
| 4106 | */ |
| 4107 | idx = htx_get_head(htx); |
| 4108 | blk_end = NULL; |
| 4109 | while (idx != -1) { |
| 4110 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4111 | idx = htx_get_next(htx, idx); |
| 4112 | if (type == HTX_BLK_EOH) { |
| 4113 | if (idx != -1) |
| 4114 | blk_end = htx_get_blk(htx, idx); |
| 4115 | break; |
| 4116 | } |
| 4117 | } |
| 4118 | |
| 4119 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4120 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4121 | ALREADY_CHECKED(sl); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4122 | meth = htx_sl_req_meth(sl); |
| 4123 | path = htx_sl_req_uri(sl); |
| 4124 | |
| 4125 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4126 | hdr = 0; |
| 4127 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4128 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4129 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4130 | blk = htx_get_blk(htx, idx); |
| 4131 | type = htx_get_blk_type(blk); |
| 4132 | |
| 4133 | if (type == HTX_BLK_UNUSED) |
| 4134 | continue; |
| 4135 | |
| 4136 | if (type != HTX_BLK_HDR) |
| 4137 | break; |
| 4138 | |
| 4139 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4140 | goto fail; |
| 4141 | |
| 4142 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4143 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4144 | hdr++; |
| 4145 | } |
| 4146 | |
| 4147 | /* marker for end of headers */ |
| 4148 | list[hdr].n = ist(""); |
| 4149 | |
| 4150 | chunk_reset(&outbuf); |
| 4151 | |
| 4152 | while (1) { |
| 4153 | outbuf.area = b_tail(&h2c->mbuf); |
| 4154 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4155 | outbuf.data = 0; |
| 4156 | |
| 4157 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4158 | break; |
| 4159 | realign_again: |
| 4160 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4161 | } |
| 4162 | |
| 4163 | if (outbuf.size < 9) |
| 4164 | goto full; |
| 4165 | |
| 4166 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4167 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4168 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4169 | outbuf.data = 9; |
| 4170 | |
| 4171 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4172 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4173 | if (b_space_wraps(&h2c->mbuf)) |
| 4174 | goto realign_again; |
| 4175 | goto full; |
| 4176 | } |
| 4177 | |
| 4178 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
Willy Tarreau | 7561bcb | 2018-12-10 19:17:06 +0100 | [diff] [blame] | 4179 | if (!hpack_encode_scheme(&outbuf, ist("https"))) { |
| 4180 | /* output full */ |
| 4181 | if (b_space_wraps(&h2c->mbuf)) |
| 4182 | goto realign_again; |
| 4183 | goto full; |
| 4184 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4185 | |
| 4186 | /* encode the path, which necessarily is the second one */ |
Willy Tarreau | 9079981 | 2018-12-10 19:28:38 +0100 | [diff] [blame] | 4187 | if (!hpack_encode_path(&outbuf, path)) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4188 | /* output full */ |
| 4189 | if (b_space_wraps(&h2c->mbuf)) |
| 4190 | goto realign_again; |
| 4191 | goto full; |
| 4192 | } |
| 4193 | |
| 4194 | /* encode all headers, stop at empty name */ |
| 4195 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4196 | /* these ones do not exist in H2 and must be dropped. */ |
| 4197 | if (isteq(list[hdr].n, ist("connection")) || |
| 4198 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4199 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4200 | isteq(list[hdr].n, ist("upgrade")) || |
| 4201 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4202 | continue; |
| 4203 | |
| 4204 | if (isteq(list[hdr].n, ist(""))) |
| 4205 | break; // end |
| 4206 | |
| 4207 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4208 | /* output full */ |
| 4209 | if (b_space_wraps(&h2c->mbuf)) |
| 4210 | goto realign_again; |
| 4211 | goto full; |
| 4212 | } |
| 4213 | } |
| 4214 | |
| 4215 | /* we may need to add END_STREAM if we have no body : |
| 4216 | * - request already closed, or : |
| 4217 | * - no transfer-encoding, and : |
| 4218 | * - no content-length or content-length:0 |
| 4219 | * Fixme: this doesn't take into account CONNECT requests. |
| 4220 | */ |
| 4221 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4222 | es_now = 1; |
| 4223 | |
| 4224 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4225 | es_now = 1; |
| 4226 | |
| 4227 | if (h2s->cs->flags & CS_FL_SHW) |
| 4228 | es_now = 1; |
| 4229 | |
| 4230 | /* update the frame's size */ |
| 4231 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4232 | |
| 4233 | if (es_now) |
| 4234 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4235 | |
| 4236 | /* commit the H2 response */ |
| 4237 | b_add(&h2c->mbuf, outbuf.data); |
| 4238 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4239 | h2s->st = H2_SS_OPEN; |
| 4240 | |
| 4241 | /* for now we don't implemented CONTINUATION, so we wait for a |
| 4242 | * body or directly end in TRL2. |
| 4243 | */ |
| 4244 | if (es_now) { |
| 4245 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4246 | h2s->flags |= H2_SF_ES_SENT; |
| 4247 | h2s->st = H2_SS_HLOC; |
| 4248 | } |
| 4249 | |
| 4250 | /* remove all header blocks including the EOH and compute the |
| 4251 | * corresponding size. |
| 4252 | * |
| 4253 | * FIXME: We should remove everything when es_now is set. |
| 4254 | */ |
| 4255 | ret = 0; |
| 4256 | idx = htx_get_head(htx); |
| 4257 | blk = htx_get_blk(htx, idx); |
| 4258 | while (blk != blk_end) { |
| 4259 | ret += htx_get_blksz(blk); |
| 4260 | blk = htx_remove_blk(htx, blk); |
| 4261 | } |
| 4262 | |
| 4263 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4264 | htx_remove_blk(htx, blk_end); |
| 4265 | |
| 4266 | end: |
| 4267 | return ret; |
| 4268 | full: |
| 4269 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4270 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4271 | ret = 0; |
| 4272 | goto end; |
| 4273 | fail: |
| 4274 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4275 | * list etc go here (unrecoverable errors). |
| 4276 | */ |
| 4277 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4278 | ret = 0; |
| 4279 | goto end; |
| 4280 | } |
| 4281 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4282 | /* 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] | 4283 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 4284 | * caller must check the stream's status to detect any error which might have |
| 4285 | * happened subsequently to a successful send. Returns the number of data bytes |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4286 | * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte. |
| 4287 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4288 | 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] | 4289 | { |
| 4290 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4291 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4292 | struct buffer outbuf; |
| 4293 | size_t total = 0; |
| 4294 | int es_now = 0; |
| 4295 | int bsize; /* htx block size */ |
| 4296 | int fsize; /* h2 frame size */ |
| 4297 | struct htx_blk *blk; |
| 4298 | enum htx_blk_type type; |
| 4299 | int idx; |
| 4300 | |
| 4301 | if (h2c_mux_busy(h2c, h2s)) { |
| 4302 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4303 | goto end; |
| 4304 | } |
| 4305 | |
| 4306 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4307 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4308 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4309 | goto end; |
| 4310 | } |
| 4311 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4312 | htx = htx_from_buf(buf); |
| 4313 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4314 | /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However, |
| 4315 | * while looping, we can meet an HTX_BLK_EOM block that we'll leave to |
| 4316 | * the caller to handle. |
| 4317 | */ |
| 4318 | |
| 4319 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4320 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4321 | goto end; |
| 4322 | |
| 4323 | idx = htx_get_head(htx); |
| 4324 | blk = htx_get_blk(htx, idx); |
| 4325 | type = htx_get_blk_type(blk); // DATA or EOD or EOM |
| 4326 | bsize = htx_get_blksz(blk); |
| 4327 | fsize = bsize; |
| 4328 | |
| 4329 | if (type == HTX_BLK_EOD) { |
| 4330 | /* if we have an EOD, we're dealing with chunked data. We may |
| 4331 | * have a set of trailers after us that the caller will want to |
| 4332 | * deal with. Let's simply remove the EOD and return. |
| 4333 | */ |
| 4334 | htx_remove_blk(htx, blk); |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4335 | total++; // EOD counts as one byte |
| 4336 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4337 | goto end; |
| 4338 | } |
| 4339 | |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4340 | if (type != HTX_BLK_DATA && type != HTX_BLK_EOM) |
| 4341 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4342 | |
| 4343 | /* Perform some optimizations to reduce the number of buffer copies. |
| 4344 | * First, if the mux's buffer is empty and the htx area contains |
| 4345 | * exactly one data block of the same size as the requested count, and |
| 4346 | * this count fits within the frame size, the stream's window size, and |
| 4347 | * the connection's window size, then it's possible to simply swap the |
| 4348 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 4349 | * length to match the entire DATA HTX block in the middle. In this |
| 4350 | * case we perform a true zero-copy operation from end-to-end. This is |
| 4351 | * the situation that happens all the time with large files. Second, if |
| 4352 | * this is not possible, but the mux's output buffer is empty, we still |
| 4353 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 4354 | * making the intermediary buffer's area point to the output buffer's |
| 4355 | * area. In this case we want to skip the HTX header to make sure that |
| 4356 | * copies remain aligned and that this operation remains possible all |
| 4357 | * the time. This goes for headers, data blocks and any data extracted |
| 4358 | * from the HTX blocks. |
| 4359 | */ |
| 4360 | if (unlikely(fsize == count && |
| 4361 | htx->used == 1 && type == HTX_BLK_DATA && |
| 4362 | fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
| 4363 | void *old_area = h2c->mbuf.area; |
| 4364 | |
| 4365 | if (b_data(&h2c->mbuf)) { |
| 4366 | /* too bad there are data left there. If we have less |
| 4367 | * than 1/4 of the mbuf's size and everything fits, |
| 4368 | * we'll perform a copy anyway. Otherwise we'll pretend |
| 4369 | * the mbuf is full and wait. |
| 4370 | */ |
| 4371 | if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf)) |
| 4372 | goto copy; |
| 4373 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4374 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4375 | goto end; |
| 4376 | } |
| 4377 | |
| 4378 | /* map an H2 frame to the HTX block so that we can put the |
| 4379 | * frame header there. |
| 4380 | */ |
| 4381 | h2c->mbuf.area = buf->area; |
Olivier Houchard | 84cca66 | 2018-12-14 16:28:08 +0100 | [diff] [blame] | 4382 | h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4383 | h2c->mbuf.data = fsize + 9; |
| 4384 | outbuf.area = b_head(&h2c->mbuf); |
| 4385 | |
| 4386 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 4387 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4388 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4389 | h2_set_frame_size(outbuf.area, fsize); |
| 4390 | |
| 4391 | /* update windows */ |
| 4392 | h2s->mws -= fsize; |
| 4393 | h2c->mws -= fsize; |
| 4394 | |
| 4395 | /* and exchange with our old area */ |
| 4396 | buf->area = old_area; |
| 4397 | buf->data = buf->head = 0; |
| 4398 | total += fsize; |
| 4399 | goto end; |
| 4400 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4401 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4402 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4403 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 4404 | |
| 4405 | while (1) { |
| 4406 | outbuf.area = b_tail(&h2c->mbuf); |
| 4407 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4408 | outbuf.data = 0; |
| 4409 | |
| 4410 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4411 | break; |
| 4412 | realign_again: |
| 4413 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4414 | } |
| 4415 | |
| 4416 | if (outbuf.size < 9) { |
| 4417 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4418 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4419 | goto end; |
| 4420 | } |
| 4421 | |
| 4422 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 4423 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4424 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4425 | outbuf.data = 9; |
| 4426 | |
| 4427 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 4428 | * the HTX buffer. We need to check this against the connection's and |
| 4429 | * the stream's send windows, and to ensure that this fits in the max |
| 4430 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4431 | * the frame header). The connection's flow control is applied last so |
| 4432 | * that we can use a separate list of streams which are immediately |
| 4433 | * unblocked on window opening. Note: we don't implement padding. |
| 4434 | */ |
| 4435 | |
| 4436 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 4437 | * empty frame, thus we force it to zero here. |
| 4438 | */ |
| 4439 | if (type == HTX_BLK_EOM) |
| 4440 | bsize = fsize = 0; |
| 4441 | |
| 4442 | if (!fsize) |
| 4443 | goto send_empty; |
| 4444 | |
| 4445 | if (h2s->mws <= 0) { |
| 4446 | h2s->flags |= H2_SF_BLK_SFCTL; |
| 4447 | if (h2s->send_wait) { |
| 4448 | LIST_DEL(&h2s->list); |
| 4449 | LIST_INIT(&h2s->list); |
| 4450 | } |
| 4451 | goto end; |
| 4452 | } |
| 4453 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4454 | if (fsize > count) |
| 4455 | fsize = count; |
| 4456 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4457 | if (fsize > h2s->mws) |
| 4458 | fsize = h2s->mws; // >0 |
| 4459 | |
| 4460 | if (h2c->mfs && fsize > h2c->mfs) |
| 4461 | fsize = h2c->mfs; // >0 |
| 4462 | |
| 4463 | if (fsize + 9 > outbuf.size) { |
| 4464 | /* we have an opportunity for enlarging the too small |
| 4465 | * available space, let's try. |
| 4466 | * FIXME: is this really interesting to do? Maybe we'll |
| 4467 | * spend lots of time realigning instead of using two |
| 4468 | * frames. |
| 4469 | */ |
| 4470 | if (b_space_wraps(&h2c->mbuf)) |
| 4471 | goto realign_again; |
| 4472 | fsize = outbuf.size - 9; |
| 4473 | |
| 4474 | if (fsize <= 0) { |
| 4475 | /* no need to send an empty frame here */ |
| 4476 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4477 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4478 | goto end; |
| 4479 | } |
| 4480 | } |
| 4481 | |
| 4482 | if (h2c->mws <= 0) { |
| 4483 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4484 | goto end; |
| 4485 | } |
| 4486 | |
| 4487 | if (fsize > h2c->mws) |
| 4488 | fsize = h2c->mws; |
| 4489 | |
| 4490 | /* now let's copy this this into the output buffer */ |
| 4491 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 4492 | h2s->mws -= fsize; |
| 4493 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4494 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4495 | |
| 4496 | send_empty: |
| 4497 | /* update the frame's size */ |
| 4498 | h2_set_frame_size(outbuf.area, fsize); |
| 4499 | |
| 4500 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 4501 | * meeting EOM. We should optimize this later. |
| 4502 | */ |
| 4503 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4504 | total++; // EOM counts as one byte |
| 4505 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4506 | es_now = 1; |
| 4507 | } |
| 4508 | |
| 4509 | if (es_now) |
| 4510 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 4511 | |
| 4512 | /* commit the H2 response */ |
| 4513 | b_add(&h2c->mbuf, fsize + 9); |
| 4514 | |
| 4515 | /* consume incoming HTX block, including EOM */ |
| 4516 | total += fsize; |
| 4517 | if (fsize == bsize) { |
| 4518 | htx_remove_blk(htx, blk); |
| 4519 | if (fsize) |
| 4520 | goto new_frame; |
| 4521 | } else { |
| 4522 | /* we've truncated this block */ |
| 4523 | htx_cut_data_blk(htx, blk, fsize); |
| 4524 | } |
| 4525 | |
| 4526 | if (es_now) { |
| 4527 | if (h2s->st == H2_SS_OPEN) |
| 4528 | h2s->st = H2_SS_HLOC; |
| 4529 | else |
| 4530 | h2s_close(h2s); |
| 4531 | |
| 4532 | h2s->flags |= H2_SF_ES_SENT; |
| 4533 | } |
| 4534 | |
| 4535 | end: |
| 4536 | return total; |
| 4537 | } |
| 4538 | |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4539 | /* Called from the upper layer, to subscribe to events, such as being able to send */ |
| 4540 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 4541 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4542 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4543 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 4544 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4545 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4546 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 4547 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4548 | if (!(sw->events & SUB_RETRY_RECV)) { |
| 4549 | sw->events |= SUB_RETRY_RECV; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 4550 | sw->handle = h2s; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4551 | h2s->recv_wait = sw; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 4552 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4553 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4554 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4555 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4556 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4557 | if (!(sw->events & SUB_RETRY_SEND)) { |
| 4558 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 4559 | sw->handle = h2s; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4560 | h2s->send_wait = sw; |
| 4561 | if (!(h2s->flags & H2_SF_BLK_SFCTL)) { |
| 4562 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 4563 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 4564 | else |
| 4565 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 4566 | } |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 4567 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4568 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4569 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4570 | if (event_type != 0) |
| 4571 | return -1; |
| 4572 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4573 | |
| 4574 | |
| 4575 | } |
| 4576 | |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4577 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 4578 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4579 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4580 | struct h2s *h2s = cs->ctx; |
| 4581 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4582 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4583 | sw = param; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4584 | if (h2s->recv_wait == sw) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4585 | sw->events &= ~SUB_RETRY_RECV; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4586 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4587 | } |
| 4588 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4589 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4590 | sw = param; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4591 | if (h2s->send_wait == sw) { |
| 4592 | LIST_DEL(&h2s->list); |
| 4593 | LIST_INIT(&h2s->list); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4594 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4595 | h2s->send_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4596 | } |
| 4597 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4598 | if (event_type & SUB_CALL_UNSUBSCRIBE) { |
| 4599 | sw = param; |
| 4600 | if (h2s->send_wait == sw) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4601 | sw->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4602 | h2s->send_wait = NULL; |
| 4603 | } |
| 4604 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4605 | return 0; |
| 4606 | } |
| 4607 | |
| 4608 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4609 | /* Called from the upper layer, to receive data */ |
| 4610 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 4611 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 4612 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 4613 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 4614 | struct htx *h2s_htx = NULL; |
| 4615 | struct htx *buf_htx = NULL; |
| 4616 | struct htx_ret htx_ret; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4617 | size_t ret = 0; |
| 4618 | |
| 4619 | /* transfer possibly pending data to the upper layer */ |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 4620 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
| 4621 | /* in HTX mode we ignore the count argument */ |
| 4622 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 4623 | if (htx_is_empty(h2s_htx)) { |
| 4624 | if (cs->flags & CS_FL_REOS) |
| 4625 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 4626 | if (cs->flags & CS_FL_ERR_PENDING) |
| 4627 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 4628 | goto end; |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 4629 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 4630 | |
| 4631 | buf_htx = htx_from_buf(buf); |
| 4632 | count = htx_free_space(buf_htx); |
| 4633 | |
Willy Tarreau | 0c22fa7 | 2018-12-04 15:21:35 +0100 | [diff] [blame] | 4634 | htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 4635 | |
| 4636 | buf_htx->extra = h2s_htx->extra; |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4637 | htx_to_buf(buf_htx, buf); |
| 4638 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 4639 | ret = htx_ret.ret; |
| 4640 | } |
| 4641 | else { |
| 4642 | ret = b_xfer(buf, &h2s->rxbuf, count); |
| 4643 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4644 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 4645 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 4646 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4647 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 4648 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4649 | if (cs->flags & CS_FL_REOS) |
| 4650 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 4651 | if (cs->flags & CS_FL_ERR_PENDING) |
| 4652 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 4653 | if (b_size(&h2s->rxbuf)) { |
| 4654 | b_free(&h2s->rxbuf); |
| 4655 | offer_buffers(NULL, tasks_run_queue); |
| 4656 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4657 | } |
| 4658 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 4659 | if (ret && h2c->dsi == h2s->id) { |
| 4660 | /* demux is blocking on this stream's buffer */ |
| 4661 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4662 | if (b_data(&h2c->dbuf) || !(h2c->wait_event.events & SUB_RETRY_RECV)) { |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 4663 | if (h2_recv_allowed(h2c)) |
| 4664 | tasklet_wakeup(h2c->wait_event.task); |
| 4665 | } |
| 4666 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 4667 | end: |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4668 | return ret; |
| 4669 | } |
| 4670 | |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4671 | static void h2_stop_senders(struct h2c *h2c) |
| 4672 | { |
| 4673 | struct h2s *h2s, *h2s_back; |
| 4674 | |
| 4675 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) { |
| 4676 | /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */ |
| 4677 | if (h2c->msi == h2s_id(h2s)) |
| 4678 | continue; |
| 4679 | LIST_DEL(&h2s->list); |
| 4680 | LIST_INIT(&h2s->list); |
| 4681 | task_remove_from_task_list((struct task *)h2s->send_wait->task); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4682 | h2s->send_wait->events |= SUB_RETRY_SEND; |
| 4683 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4684 | LIST_ADD(&h2c->send_list, &h2s->list); |
| 4685 | } |
| 4686 | } |
| 4687 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 4688 | /* Called from the upper layer, to send data */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 4689 | 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] | 4690 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4691 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 4692 | size_t orig_count = count; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 4693 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4694 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4695 | struct htx *htx; |
| 4696 | struct htx_blk *blk; |
| 4697 | enum htx_blk_type btype; |
| 4698 | uint32_t bsize; |
| 4699 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4700 | |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4701 | if (h2s->send_wait) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4702 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4703 | h2s->send_wait = NULL; |
| 4704 | LIST_DEL(&h2s->list); |
| 4705 | LIST_INIT(&h2s->list); |
| 4706 | } |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 4707 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 4708 | return 0; |
| 4709 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4710 | /* htx will be enough to decide if we're using HTX or legacy */ |
| 4711 | htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL; |
| 4712 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 4713 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 4714 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 4715 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 4716 | if (h2s->id == 0) { |
| 4717 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 4718 | |
| 4719 | if (id < 0) { |
| 4720 | cs->ctx = NULL; |
| 4721 | cs->flags |= CS_FL_ERROR; |
| 4722 | h2s_destroy(h2s); |
| 4723 | return 0; |
| 4724 | } |
| 4725 | |
| 4726 | eb32_delete(&h2s->by_id); |
| 4727 | h2s->by_id.key = h2s->id = id; |
| 4728 | h2s->h2c->max_id = id; |
| 4729 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 4730 | } |
| 4731 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4732 | if (htx) { |
Willy Tarreau | c14999b | 2018-12-06 14:09:09 +0100 | [diff] [blame] | 4733 | while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) && |
| 4734 | count && !htx_is_empty(htx)) { |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4735 | idx = htx_get_head(htx); |
| 4736 | blk = htx_get_blk(htx, idx); |
| 4737 | btype = htx_get_blk_type(blk); |
| 4738 | bsize = htx_get_blksz(blk); |
| 4739 | |
| 4740 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4741 | case HTX_BLK_REQ_SL: |
| 4742 | /* start-line before headers */ |
| 4743 | ret = h2s_htx_bck_make_req_headers(h2s, htx); |
| 4744 | if (ret > 0) { |
| 4745 | total += ret; |
| 4746 | count -= ret; |
| 4747 | if (ret < bsize) |
| 4748 | goto done; |
| 4749 | } |
| 4750 | break; |
| 4751 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4752 | case HTX_BLK_RES_SL: |
| 4753 | /* start-line before headers */ |
| 4754 | ret = h2s_htx_frt_make_resp_headers(h2s, htx); |
| 4755 | if (ret > 0) { |
| 4756 | total += ret; |
| 4757 | count -= ret; |
| 4758 | if (ret < bsize) |
| 4759 | goto done; |
| 4760 | } |
| 4761 | break; |
| 4762 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4763 | case HTX_BLK_DATA: |
| 4764 | case HTX_BLK_EOD: |
| 4765 | case HTX_BLK_EOM: |
| 4766 | /* all these cause the emission of a DATA frame (possibly empty) */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4767 | ret = h2s_htx_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4768 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4769 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4770 | total += ret; |
| 4771 | count -= ret; |
| 4772 | if (ret < bsize) |
| 4773 | goto done; |
| 4774 | } |
| 4775 | break; |
| 4776 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4777 | default: |
| 4778 | htx_remove_blk(htx, blk); |
| 4779 | total += bsize; |
| 4780 | count -= bsize; |
| 4781 | break; |
| 4782 | } |
| 4783 | } |
| 4784 | goto done; |
| 4785 | } |
| 4786 | |
| 4787 | /* legacy transfer mode */ |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4788 | while (h2s->h1m.state < H1_MSG_DONE && count) { |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 4789 | if (h2s->h1m.state <= H1_MSG_LAST_LF) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4790 | if (h2s->h2c->flags & H2_CF_IS_BACK) |
| 4791 | ret = -1; |
| 4792 | else |
| 4793 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4794 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4795 | else if (h2s->h1m.state < H1_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 4796 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4797 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4798 | else if (h2s->h1m.state == H1_MSG_TRAILERS) { |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4799 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 4800 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4801 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4802 | if (unlikely((int)ret <= 0)) { |
| 4803 | if ((int)ret < 0) |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4804 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4805 | break; |
| 4806 | } |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4807 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 4808 | total += count; |
| 4809 | count = 0; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 4810 | h2s->h1m.state = H1_MSG_DONE; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4811 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4812 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4813 | else { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 4814 | cs_set_error(cs); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4815 | break; |
| 4816 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 4817 | |
| 4818 | total += ret; |
| 4819 | count -= ret; |
| 4820 | |
| 4821 | if (h2s->st >= H2_SS_ERROR) |
| 4822 | break; |
| 4823 | |
| 4824 | if (h2s->flags & H2_SF_BLK_ANY) |
| 4825 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4826 | } |
| 4827 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4828 | done: |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4829 | if (h2s->st >= H2_SS_ERROR) { |
| 4830 | /* trim any possibly pending data after we close (extra CR-LF, |
| 4831 | * unprocessed trailers, abnormal extra data, ...) |
| 4832 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 4833 | total += count; |
| 4834 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4835 | } |
| 4836 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 4837 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 4838 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 4839 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 4840 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4841 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 4842 | } |
| 4843 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4844 | if (htx) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4845 | htx_to_buf(htx, buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4846 | } else { |
| 4847 | b_del(buf, total); |
| 4848 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4849 | |
| 4850 | /* The mux is full, cancel the pending tasks */ |
| 4851 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 4852 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 4853 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 4854 | |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 4855 | /* If we're running HTX, and we read the whole buffer, then pretend |
| 4856 | * we read exactly what the caller specified, as with HTX the caller |
| 4857 | * will always give the buffer size, instead of the amount of data |
| 4858 | * available. |
| 4859 | */ |
| 4860 | if (htx && !b_data(buf)) |
| 4861 | total = orig_count; |
| 4862 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 4863 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4864 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4865 | tasklet_wakeup(h2s->h2c->wait_event.task); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 4866 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 4867 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 4868 | /* If we're waiting for flow control, and we got a shutr on the |
| 4869 | * connection, we will never be unlocked, so add an error on |
| 4870 | * the conn_stream. |
| 4871 | */ |
| 4872 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 4873 | !b_data(&h2s->h2c->dbuf) && |
| 4874 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 4875 | if (cs->flags & CS_FL_EOS) |
| 4876 | cs->flags |= CS_FL_ERROR; |
| 4877 | else |
| 4878 | cs->flags |= CS_FL_ERR_PENDING; |
| 4879 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 4880 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 4881 | } |
| 4882 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 4883 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4884 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 4885 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 4886 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 4887 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 4888 | struct eb32_node *node; |
| 4889 | int fctl_cnt = 0; |
| 4890 | int send_cnt = 0; |
| 4891 | int tree_cnt = 0; |
| 4892 | int orph_cnt = 0; |
| 4893 | |
| 4894 | if (!h2c) |
| 4895 | return; |
| 4896 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4897 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 4898 | fctl_cnt++; |
| 4899 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4900 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 4901 | send_cnt++; |
| 4902 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 4903 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 4904 | node = eb32_first(&h2c->streams_by_id); |
| 4905 | while (node) { |
| 4906 | h2s = container_of(node, struct h2s, by_id); |
| 4907 | tree_cnt++; |
| 4908 | if (!h2s->cs) |
| 4909 | orph_cnt++; |
| 4910 | node = eb32_next(node); |
| 4911 | } |
| 4912 | |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 4913 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 4914 | " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d" |
| 4915 | " .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] | 4916 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 4917 | 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] | 4918 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 4919 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 4920 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 4921 | h2c->msi, |
| 4922 | (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf), |
| 4923 | (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf)); |
| 4924 | |
| 4925 | if (h2s) { |
| 4926 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 4927 | h2s, h2s->id, h2s->flags, |
| 4928 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 4929 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 4930 | h2s->cs); |
| 4931 | if (h2s->cs) |
| 4932 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 4933 | h2s->cs->flags, h2s->cs->data); |
| 4934 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 4935 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 4936 | |
| 4937 | /*******************************************************/ |
| 4938 | /* functions below are dedicated to the config parsers */ |
| 4939 | /*******************************************************/ |
| 4940 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 4941 | /* config parser for global "tune.h2.header-table-size" */ |
| 4942 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 4943 | struct proxy *defpx, const char *file, int line, |
| 4944 | char **err) |
| 4945 | { |
| 4946 | if (too_many_args(1, args, err, NULL)) |
| 4947 | return -1; |
| 4948 | |
| 4949 | h2_settings_header_table_size = atoi(args[1]); |
| 4950 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 4951 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 4952 | return -1; |
| 4953 | } |
| 4954 | return 0; |
| 4955 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 4956 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 4957 | /* config parser for global "tune.h2.initial-window-size" */ |
| 4958 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 4959 | struct proxy *defpx, const char *file, int line, |
| 4960 | char **err) |
| 4961 | { |
| 4962 | if (too_many_args(1, args, err, NULL)) |
| 4963 | return -1; |
| 4964 | |
| 4965 | h2_settings_initial_window_size = atoi(args[1]); |
| 4966 | if (h2_settings_initial_window_size < 0) { |
| 4967 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 4968 | return -1; |
| 4969 | } |
| 4970 | return 0; |
| 4971 | } |
| 4972 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 4973 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 4974 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 4975 | struct proxy *defpx, const char *file, int line, |
| 4976 | char **err) |
| 4977 | { |
| 4978 | if (too_many_args(1, args, err, NULL)) |
| 4979 | return -1; |
| 4980 | |
| 4981 | h2_settings_max_concurrent_streams = atoi(args[1]); |
| 4982 | if (h2_settings_max_concurrent_streams < 0) { |
| 4983 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 4984 | return -1; |
| 4985 | } |
| 4986 | return 0; |
| 4987 | } |
| 4988 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 4989 | |
| 4990 | /****************************************/ |
| 4991 | /* MUX initialization and instanciation */ |
| 4992 | /***************************************/ |
| 4993 | |
| 4994 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 4995 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 4996 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 4997 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 4998 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 4999 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5000 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5001 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5002 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5003 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5004 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5005 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5006 | .avail_streams = h2_avail_streams, |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 5007 | .max_streams = h2_max_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5008 | .shutr = h2_shutr, |
| 5009 | .shutw = h2_shutw, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5010 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 5011 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5012 | .name = "H2", |
| 5013 | }; |
| 5014 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5015 | /* PROTO selection : this mux registers PROTO token "h2" */ |
| 5016 | static struct mux_proto_list mux_proto_h2 = |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5017 | { .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] | 5018 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5019 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 5020 | |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5021 | static struct mux_proto_list mux_proto_h2_htx = |
| 5022 | { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops }; |
| 5023 | |
| 5024 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx); |
| 5025 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5026 | /* config keyword parsers */ |
| 5027 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5028 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5029 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5030 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5031 | { 0, NULL, NULL } |
| 5032 | }}; |
| 5033 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5034 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |