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