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