Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP/2 mux-demux for connections |
| 3 | * |
| 4 | * Copyright 2017 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <common/cfgparse.h> |
| 14 | #include <common/config.h> |
Willy Tarreau | afba57a | 2018-12-11 13:44:24 +0100 | [diff] [blame] | 15 | #include <common/h1.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 16 | #include <common/h2.h> |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 17 | #include <common/hpack-dec.h> |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 18 | #include <common/hpack-enc.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 19 | #include <common/hpack-tbl.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 20 | #include <common/htx.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 21 | #include <common/initcall.h> |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 22 | #include <common/net_helper.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 23 | #include <proto/connection.h> |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 24 | #include <proto/http_htx.h> |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 25 | #include <proto/session.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 26 | #include <proto/stream.h> |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 27 | #include <proto/stream_interface.h> |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 28 | #include <types/session.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 29 | #include <eb32tree.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 30 | |
| 31 | |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 32 | /* dummy streams returned for closed, error, refused, idle and states */ |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 33 | static const struct h2s *h2_closed_stream; |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 34 | static const struct h2s *h2_error_stream; |
Willy Tarreau | 8d0d58b | 2018-12-23 18:29:12 +0100 | [diff] [blame] | 35 | static const struct h2s *h2_refused_stream; |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 36 | static const struct h2s *h2_idle_stream; |
| 37 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 38 | /* Connection flags (32 bit), in h2c->flags */ |
| 39 | #define H2_CF_NONE 0x00000000 |
| 40 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 41 | /* Flags indicating why writing to the mux is blocked. */ |
| 42 | #define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer |
| 43 | #define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full |
| 44 | #define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above |
| 45 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 46 | /* Flags indicating why writing to the demux is blocked. |
| 47 | * The first two ones directly affect the ability for the mux to receive data |
| 48 | * from the connection. The other ones affect the mux's ability to demux |
| 49 | * received data. |
| 50 | */ |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 51 | #define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer |
| 52 | #define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 53 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 54 | #define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy |
| 55 | #define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer |
| 56 | #define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer |
| 57 | #define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 58 | #define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave |
| 59 | #define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 60 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 61 | /* other flags */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 62 | #define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent |
| 63 | #define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent |
| 64 | #define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake |
Willy Tarreau | b3fb56d | 2018-10-03 13:56:38 +0200 | [diff] [blame] | 65 | #define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 66 | #define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 67 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 68 | /* H2 connection state, in h2c->st0 */ |
| 69 | enum h2_cs { |
| 70 | H2_CS_PREFACE, // init done, waiting for connection preface |
| 71 | H2_CS_SETTINGS1, // preface OK, waiting for first settings frame |
| 72 | H2_CS_FRAME_H, // first settings frame ok, waiting for frame header |
| 73 | H2_CS_FRAME_P, // frame header OK, waiting for frame payload |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 74 | H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame |
| 75 | H2_CS_FRAME_E, // frame payload OK, trying to send RST frame |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 76 | H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP |
| 77 | H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP |
| 78 | H2_CS_ENTRIES // must be last |
| 79 | } __attribute__((packed)); |
| 80 | |
| 81 | /* H2 connection descriptor */ |
| 82 | struct h2c { |
| 83 | struct connection *conn; |
| 84 | |
| 85 | enum h2_cs st0; /* mux state */ |
| 86 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 87 | |
| 88 | /* 16 bit hole here */ |
| 89 | uint32_t flags; /* connection flags: H2_CF_* */ |
| 90 | int32_t max_id; /* highest ID known on this connection, <0 before preface */ |
| 91 | uint32_t rcvd_c; /* newly received data to ACK for the connection */ |
| 92 | uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */ |
| 93 | |
| 94 | /* states for the demux direction */ |
| 95 | struct hpack_dht *ddht; /* demux dynamic header table */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 96 | struct buffer dbuf; /* demux buffer */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 97 | |
| 98 | int32_t dsi; /* demux stream ID (<0 = idle) */ |
| 99 | int32_t dfl; /* demux frame length (if dsi >= 0) */ |
| 100 | int8_t dft; /* demux frame type (if dsi >= 0) */ |
| 101 | int8_t dff; /* demux frame flags (if dsi >= 0) */ |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 102 | uint8_t dpl; /* demux pad length (part of dfl), init to 0 */ |
| 103 | /* 8 bit hole here */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 104 | int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */ |
| 105 | |
| 106 | /* states for the mux direction */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 107 | struct buffer mbuf; /* mux buffer */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 108 | int32_t msi; /* mux stream ID (<0 = idle) */ |
| 109 | int32_t mfl; /* mux frame length (if dsi >= 0) */ |
| 110 | int8_t mft; /* mux frame type (if dsi >= 0) */ |
| 111 | int8_t mff; /* mux frame flags (if dsi >= 0) */ |
| 112 | /* 16 bit hole here */ |
| 113 | int32_t miw; /* mux initial window size for all new streams */ |
| 114 | int32_t mws; /* mux window size. Can be negative. */ |
| 115 | int32_t mfs; /* mux's max frame size */ |
| 116 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 117 | int timeout; /* idle timeout duration in ticks */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 118 | int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */ |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 119 | unsigned int nb_streams; /* number of streams in the tree */ |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 120 | unsigned int nb_cs; /* number of attached conn_streams */ |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 121 | struct proxy *proxy; /* the proxy this connection was created for */ |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 122 | struct task *task; /* timeout management task */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 123 | struct eb_root streams_by_id; /* all active streams by their ID */ |
| 124 | struct list send_list; /* list of blocked streams requesting to send */ |
| 125 | struct list fctl_list; /* list of streams blocked by connection's fctl */ |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 126 | struct list sending_list; /* list of h2s scheduled to send data */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 127 | struct buffer_wait buf_wait; /* wait list for buffer allocations */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 128 | 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] | 129 | }; |
| 130 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 131 | /* H2 stream state, in h2s->st */ |
| 132 | enum h2_ss { |
| 133 | H2_SS_IDLE = 0, // idle |
| 134 | H2_SS_RLOC, // reserved(local) |
| 135 | H2_SS_RREM, // reserved(remote) |
| 136 | H2_SS_OPEN, // open |
| 137 | H2_SS_HREM, // half-closed(remote) |
| 138 | H2_SS_HLOC, // half-closed(local) |
Willy Tarreau | 96060ba | 2017-10-16 18:34:34 +0200 | [diff] [blame] | 139 | H2_SS_ERROR, // an error needs to be sent using RST_STREAM |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 140 | H2_SS_CLOSED, // closed |
| 141 | H2_SS_ENTRIES // must be last |
| 142 | } __attribute__((packed)); |
| 143 | |
| 144 | /* HTTP/2 stream flags (32 bit), in h2s->flags */ |
| 145 | #define H2_SF_NONE 0x00000000 |
| 146 | #define H2_SF_ES_RCVD 0x00000001 |
| 147 | #define H2_SF_ES_SENT 0x00000002 |
| 148 | |
| 149 | #define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM |
| 150 | #define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM |
| 151 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 152 | /* stream flags indicating the reason the stream is blocked */ |
| 153 | #define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient) |
| 154 | #define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux |
| 155 | #define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl |
| 156 | #define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl |
| 157 | #define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above |
| 158 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 159 | /* stream flags indicating how data is supposed to be sent */ |
| 160 | #define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length |
| 161 | #define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding |
| 162 | |
| 163 | /* step we're currently in when sending chunks. This is needed because we may |
| 164 | * have to transfer chunks as large as a full buffer so there's no room left |
| 165 | * for size nor crlf around. |
| 166 | */ |
| 167 | #define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size |
| 168 | #define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data |
| 169 | #define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data |
| 170 | |
| 171 | #define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size |
| 172 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 173 | #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] | 174 | #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] | 175 | |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 176 | #define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream |
| 177 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 178 | /* H2 stream descriptor, describing the stream as it appears in the H2C, and as |
| 179 | * it is being processed in the internal HTTP representation (H1 for now). |
| 180 | */ |
| 181 | struct h2s { |
| 182 | struct conn_stream *cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 183 | struct session *sess; |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 184 | struct h2c *h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 185 | struct h1m h1m; /* request or response parser state for H1 */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 186 | struct eb32_node by_id; /* place in h2c's streams_by_id */ |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 187 | int32_t id; /* stream ID */ |
| 188 | uint32_t flags; /* H2_SF_* */ |
| 189 | int mws; /* mux window size for this stream */ |
| 190 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 191 | enum h2_ss st; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 192 | uint16_t status; /* HTTP response status */ |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 193 | struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */ |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 194 | struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */ |
| 195 | struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */ |
| 196 | struct wait_event *send_wait; /* The streeam is waiting for flow control */ |
| 197 | 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] | 198 | }; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 199 | |
Willy Tarreau | c640514 | 2017-09-21 20:23:50 +0200 | [diff] [blame] | 200 | /* descriptor for an h2 frame header */ |
| 201 | struct h2_fh { |
| 202 | uint32_t len; /* length, host order, 24 bits */ |
| 203 | uint32_t sid; /* stream id, host order, 31 bits */ |
| 204 | uint8_t ft; /* frame type */ |
| 205 | uint8_t ff; /* frame flags */ |
| 206 | }; |
| 207 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 208 | /* the h2c connection pool */ |
| 209 | DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c)); |
| 210 | |
| 211 | /* the h2s stream pool */ |
| 212 | DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s)); |
| 213 | |
Willy Tarreau | dc57236 | 2018-12-12 08:08:05 +0100 | [diff] [blame] | 214 | /* The default connection window size is 65535, it may only be enlarged using |
| 215 | * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1, |
| 216 | * we'll pretend we already received the difference between the two to send |
| 217 | * an equivalent window update to enlarge it to 2G-1. |
| 218 | */ |
| 219 | #define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535) |
| 220 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 221 | /* a few settings from the global section */ |
| 222 | static int h2_settings_header_table_size = 4096; /* initial value */ |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 223 | static int h2_settings_initial_window_size = 65535; /* initial value */ |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 224 | static int h2_settings_max_concurrent_streams = 100; |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 225 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 226 | /* a dmumy closed stream */ |
| 227 | static const struct h2s *h2_closed_stream = &(const struct h2s){ |
| 228 | .cs = NULL, |
| 229 | .h2c = NULL, |
| 230 | .st = H2_SS_CLOSED, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 231 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 232 | .flags = H2_SF_RST_RCVD, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 233 | .id = 0, |
| 234 | }; |
| 235 | |
Willy Tarreau | ecb9dcd | 2019-01-03 12:00:17 +0100 | [diff] [blame] | 236 | /* a dmumy closed stream returning a PROTOCOL_ERROR error */ |
| 237 | static const struct h2s *h2_error_stream = &(const struct h2s){ |
| 238 | .cs = NULL, |
| 239 | .h2c = NULL, |
| 240 | .st = H2_SS_CLOSED, |
| 241 | .errcode = H2_ERR_PROTOCOL_ERROR, |
| 242 | .flags = 0, |
| 243 | .id = 0, |
| 244 | }; |
| 245 | |
Willy Tarreau | 8d0d58b | 2018-12-23 18:29:12 +0100 | [diff] [blame] | 246 | /* a dmumy closed stream returning a REFUSED_STREAM error */ |
| 247 | static const struct h2s *h2_refused_stream = &(const struct h2s){ |
| 248 | .cs = NULL, |
| 249 | .h2c = NULL, |
| 250 | .st = H2_SS_CLOSED, |
| 251 | .errcode = H2_ERR_REFUSED_STREAM, |
| 252 | .flags = 0, |
| 253 | .id = 0, |
| 254 | }; |
| 255 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 256 | /* and a dummy idle stream for use with any unannounced stream */ |
| 257 | static const struct h2s *h2_idle_stream = &(const struct h2s){ |
| 258 | .cs = NULL, |
| 259 | .h2c = NULL, |
| 260 | .st = H2_SS_IDLE, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 261 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 262 | .id = 0, |
| 263 | }; |
| 264 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 265 | 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] | 266 | static int h2_send(struct h2c *h2c); |
| 267 | static int h2_recv(struct h2c *h2c); |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 268 | static int h2_process(struct h2c *h2c); |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 269 | 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] | 270 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 271 | static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags); |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 272 | static int h2_frt_transfer_data(struct h2s *h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 273 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state); |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 274 | static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess); |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 275 | static void h2s_alert(struct h2s *h2s); |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 276 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 277 | /*****************************************************/ |
| 278 | /* functions below are for dynamic buffer management */ |
| 279 | /*****************************************************/ |
| 280 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 281 | /* indicates whether or not the we may call the h2_recv() function to attempt |
| 282 | * to receive data into the buffer and/or demux pending data. The condition is |
| 283 | * a bit complex due to some API limits for now. The rules are the following : |
| 284 | * - if an error or a shutdown was detected on the connection and the buffer |
| 285 | * is empty, we must not attempt to receive |
| 286 | * - if the demux buf failed to be allocated, we must not try to receive and |
| 287 | * we know there is nothing pending |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 288 | * - if no flag indicates a blocking condition, we may attempt to receive, |
| 289 | * regardless of whether the demux buffer is full or not, so that only |
| 290 | * de demux part decides whether or not to block. This is needed because |
| 291 | * the connection API indeed prevents us from re-enabling receipt that is |
| 292 | * already enabled in a polled state, so we must always immediately stop |
| 293 | * as soon as the demux can't proceed so as never to hit an end of read |
| 294 | * with data pending in the buffers. |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 295 | * - otherwise must may not attempt |
| 296 | */ |
| 297 | static inline int h2_recv_allowed(const struct h2c *h2c) |
| 298 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 299 | if (b_data(&h2c->dbuf) == 0 && |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 300 | (h2c->st0 >= H2_CS_ERROR || |
| 301 | h2c->conn->flags & CO_FL_ERROR || |
| 302 | conn_xprt_read0_pending(h2c->conn))) |
| 303 | return 0; |
| 304 | |
| 305 | if (!(h2c->flags & H2_CF_DEM_DALLOC) && |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 306 | !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 307 | return 1; |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 312 | /* restarts reading on the connection if it was not enabled */ |
| 313 | static inline void h2c_restart_reading(const struct h2c *h2c) |
| 314 | { |
| 315 | if (!h2_recv_allowed(h2c)) |
| 316 | return; |
Willy Tarreau | 872e2fa | 2019-01-03 08:27:41 +0100 | [diff] [blame] | 317 | if (!b_data(&h2c->dbuf) && (h2c->wait_event.events & SUB_RETRY_RECV)) |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 318 | return; |
| 319 | tasklet_wakeup(h2c->wait_event.task); |
| 320 | } |
| 321 | |
| 322 | |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 323 | /* returns true if the connection has too many conn_streams attached */ |
| 324 | static inline int h2_has_too_many_cs(const struct h2c *h2c) |
| 325 | { |
Willy Tarreau | a875466 | 2018-12-23 20:43:58 +0100 | [diff] [blame] | 326 | return h2c->nb_cs > h2_settings_max_concurrent_streams; |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 327 | } |
| 328 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 329 | /* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c |
| 330 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 331 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 332 | * impossible to wake up and we prefer to be woken up later. |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 333 | */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 334 | static int h2_buf_available(void *target) |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 335 | { |
| 336 | struct h2c *h2c = target; |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 337 | struct h2s *h2s; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 338 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 339 | 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] | 340 | h2c->flags &= ~H2_CF_DEM_DALLOC; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 341 | h2c_restart_reading(h2c); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 342 | return 1; |
| 343 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 344 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 345 | if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) { |
| 346 | h2c->flags &= ~H2_CF_MUX_MALLOC; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 347 | |
| 348 | if (h2c->flags & H2_CF_DEM_MROOM) { |
| 349 | h2c->flags &= ~H2_CF_DEM_MROOM; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 350 | h2c_restart_reading(h2c); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 351 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 352 | return 1; |
| 353 | } |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 354 | |
| 355 | if ((h2c->flags & H2_CF_DEM_SALLOC) && |
| 356 | (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs && |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 357 | b_alloc_margin(&h2s->rxbuf, 0)) { |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 358 | h2c->flags &= ~H2_CF_DEM_SALLOC; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 359 | h2c_restart_reading(h2c); |
Willy Tarreau | 0b55907 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 360 | return 1; |
| 361 | } |
| 362 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 363 | return 0; |
| 364 | } |
| 365 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 366 | 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] | 367 | { |
| 368 | struct buffer *buf = NULL; |
| 369 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 370 | if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) && |
| 371 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 372 | h2c->buf_wait.target = h2c; |
| 373 | h2c->buf_wait.wakeup_cb = h2_buf_available; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 374 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 375 | LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 376 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 377 | __conn_xprt_stop_recv(h2c->conn); |
| 378 | } |
| 379 | return buf; |
| 380 | } |
| 381 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 382 | static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 383 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 384 | if (bptr->size) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 385 | b_free(bptr); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 386 | offer_buffers(h2c->buf_wait.target, tasks_run_queue); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 390 | static int h2_avail_streams(struct connection *conn) |
| 391 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 392 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 393 | |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 394 | /* 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] | 395 | return (h2_settings_max_concurrent_streams - h2c->nb_streams); |
| 396 | } |
| 397 | |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 398 | static int h2_max_streams(struct connection *conn) |
| 399 | { |
| 400 | /* XXX Should use the negociated max concurrent stream nb instead of the conf value */ |
| 401 | return h2_settings_max_concurrent_streams; |
| 402 | } |
| 403 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 404 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 405 | /*****************************************************************/ |
| 406 | /* functions below are dedicated to the mux setup and management */ |
| 407 | /*****************************************************************/ |
| 408 | |
Willy Tarreau | 7dc24e4 | 2018-10-03 13:52:41 +0200 | [diff] [blame] | 409 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 410 | * is already initialized before installing the mux, so we detect incoming |
| 411 | * connections from the fact that the context is still NULL. Returns < 0 on |
| 412 | * error. |
| 413 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 414 | static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess) |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 415 | { |
| 416 | struct h2c *h2c; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 417 | struct task *t = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 418 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 419 | h2c = pool_alloc(pool_head_h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 420 | if (!h2c) |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 421 | goto fail_no_h2c; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 422 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 423 | if (conn->ctx) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 424 | h2c->flags = H2_CF_IS_BACK; |
| 425 | h2c->shut_timeout = h2c->timeout = prx->timeout.server; |
| 426 | if (tick_isset(prx->timeout.serverfin)) |
| 427 | h2c->shut_timeout = prx->timeout.serverfin; |
| 428 | } else { |
| 429 | h2c->flags = H2_CF_NONE; |
| 430 | h2c->shut_timeout = h2c->timeout = prx->timeout.client; |
| 431 | if (tick_isset(prx->timeout.clientfin)) |
| 432 | h2c->shut_timeout = prx->timeout.clientfin; |
| 433 | } |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 434 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 435 | h2c->proxy = prx; |
Willy Tarreau | 3340029 | 2017-11-05 11:23:40 +0100 | [diff] [blame] | 436 | h2c->task = NULL; |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 437 | if (tick_isset(h2c->timeout)) { |
| 438 | t = task_new(tid_bit); |
| 439 | if (!t) |
| 440 | goto fail; |
| 441 | |
| 442 | h2c->task = t; |
| 443 | t->process = h2_timeout_task; |
| 444 | t->context = h2c; |
| 445 | t->expire = tick_add(now_ms, h2c->timeout); |
| 446 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 447 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 448 | h2c->wait_event.task = tasklet_new(); |
| 449 | if (!h2c->wait_event.task) |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 450 | goto fail; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 451 | h2c->wait_event.task->process = h2_io_cb; |
| 452 | h2c->wait_event.task->context = h2c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 453 | h2c->wait_event.events = 0; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 454 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 455 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 456 | if (!h2c->ddht) |
| 457 | goto fail; |
| 458 | |
| 459 | /* Initialise the context. */ |
| 460 | h2c->st0 = H2_CS_PREFACE; |
| 461 | h2c->conn = conn; |
| 462 | h2c->max_id = -1; |
| 463 | h2c->errcode = H2_ERR_NO_ERROR; |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 464 | h2c->rcvd_c = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 465 | h2c->rcvd_s = 0; |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 466 | h2c->nb_streams = 0; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 467 | h2c->nb_cs = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 468 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 469 | h2c->dbuf = BUF_NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 470 | h2c->dsi = -1; |
| 471 | h2c->msi = -1; |
| 472 | h2c->last_sid = -1; |
| 473 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 474 | h2c->mbuf = BUF_NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 475 | h2c->miw = 65535; /* mux initial window size */ |
| 476 | h2c->mws = 65535; /* mux window size */ |
| 477 | h2c->mfs = 16384; /* initial max frame size */ |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 478 | h2c->streams_by_id = EB_ROOT; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 479 | LIST_INIT(&h2c->send_list); |
| 480 | LIST_INIT(&h2c->fctl_list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 481 | LIST_INIT(&h2c->sending_list); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 482 | LIST_INIT(&h2c->buf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 483 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 484 | if (t) |
| 485 | task_queue(t); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 486 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 487 | if (h2c->flags & H2_CF_IS_BACK) { |
| 488 | /* FIXME: this is temporary, for outgoing connections we need |
| 489 | * to immediately allocate a stream until the code is modified |
| 490 | * so that the caller calls ->attach(). For now the outgoing cs |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 491 | * is stored as conn->ctx by the caller. |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 492 | */ |
| 493 | struct h2s *h2s; |
| 494 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 495 | h2s = h2c_bck_stream_new(h2c, conn->ctx, sess); |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 496 | if (!h2s) |
| 497 | goto fail_stream; |
| 498 | } |
| 499 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 500 | conn->ctx = h2c; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 501 | |
Willy Tarreau | 0f38358 | 2018-10-03 14:22:21 +0200 | [diff] [blame] | 502 | /* prepare to read something */ |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 503 | h2c_restart_reading(h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 504 | return 0; |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 505 | fail_stream: |
| 506 | hpack_dht_free(h2c->ddht); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 507 | fail: |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 508 | if (t) |
| 509 | task_free(t); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 510 | if (h2c->wait_event.task) |
| 511 | tasklet_free(h2c->wait_event.task); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 512 | pool_free(pool_head_h2c, h2c); |
mildis | cd2d7de | 2018-10-02 16:44:18 +0200 | [diff] [blame] | 513 | fail_no_h2c: |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 514 | return -1; |
| 515 | } |
| 516 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 517 | /* returns the next allocatable outgoing stream ID for the H2 connection, or |
| 518 | * -1 if no more is allocatable. |
| 519 | */ |
| 520 | static inline int32_t h2c_get_next_sid(const struct h2c *h2c) |
| 521 | { |
| 522 | int32_t id = (h2c->max_id + 1) | 1; |
| 523 | if (id & 0x80000000U) |
| 524 | id = -1; |
| 525 | return id; |
| 526 | } |
| 527 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 528 | /* returns the stream associated with id <id> or NULL if not found */ |
| 529 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 530 | { |
| 531 | struct eb32_node *node; |
| 532 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 533 | if (id == 0) |
| 534 | return (struct h2s *)h2_closed_stream; |
| 535 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 536 | if (id > h2c->max_id) |
| 537 | return (struct h2s *)h2_idle_stream; |
| 538 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 539 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 540 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 541 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 542 | |
| 543 | return container_of(node, struct h2s, by_id); |
| 544 | } |
| 545 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 546 | /* release function for a connection. This one should be called to free all |
| 547 | * resources allocated to the mux. |
| 548 | */ |
| 549 | static void h2_release(struct connection *conn) |
| 550 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 551 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 552 | |
| 553 | LIST_DEL(&conn->list); |
| 554 | |
| 555 | if (h2c) { |
| 556 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 557 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 558 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 559 | LIST_DEL(&h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 560 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 561 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 562 | h2_release_buf(h2c, &h2c->dbuf); |
| 563 | h2_release_buf(h2c, &h2c->mbuf); |
| 564 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 565 | if (h2c->task) { |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 566 | h2c->task->context = NULL; |
| 567 | task_wakeup(h2c->task, TASK_WOKEN_OTHER); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 568 | h2c->task = NULL; |
| 569 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 570 | if (h2c->wait_event.task) |
| 571 | tasklet_free(h2c->wait_event.task); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 572 | if (h2c->wait_event.events != 0) |
| 573 | conn->xprt->unsubscribe(conn, h2c->wait_event.events, |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 574 | &h2c->wait_event); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 575 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 576 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | conn->mux = NULL; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 580 | conn->ctx = NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 581 | |
| 582 | conn_stop_tracking(conn); |
| 583 | conn_full_close(conn); |
| 584 | if (conn->destroy_cb) |
| 585 | conn->destroy_cb(conn); |
| 586 | conn_free(conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 590 | /******************************************************/ |
| 591 | /* functions below are for the H2 protocol processing */ |
| 592 | /******************************************************/ |
| 593 | |
| 594 | /* 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] | 595 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 596 | { |
| 597 | return h2s ? h2s->id : 0; |
| 598 | } |
| 599 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 600 | /* 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] | 601 | 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] | 602 | { |
| 603 | if (h2c->msi < 0) |
| 604 | return 0; |
| 605 | |
| 606 | if (h2c->msi == h2s_id(h2s)) |
| 607 | return 0; |
| 608 | |
| 609 | return 1; |
| 610 | } |
| 611 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 612 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 613 | 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] | 614 | { |
| 615 | h2c->errcode = err; |
| 616 | h2c->st0 = H2_CS_ERROR; |
| 617 | } |
| 618 | |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 619 | /* marks an error on the stream */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 620 | 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] | 621 | { |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 622 | if (h2s->id && h2s->st < H2_SS_ERROR) { |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 623 | h2s->errcode = err; |
| 624 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 625 | if (h2s->cs) |
| 626 | cs_set_error(h2s->cs); |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 630 | /* attempt to notify the data layer of recv availability */ |
| 631 | static void __maybe_unused h2s_notify_recv(struct h2s *h2s) |
| 632 | { |
| 633 | struct wait_event *sw; |
| 634 | |
| 635 | if (h2s->recv_wait) { |
| 636 | sw = h2s->recv_wait; |
| 637 | sw->events &= ~SUB_RETRY_RECV; |
| 638 | tasklet_wakeup(sw->task); |
| 639 | h2s->recv_wait = NULL; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | /* attempt to notify the data layer of send availability */ |
| 644 | static void __maybe_unused h2s_notify_send(struct h2s *h2s) |
| 645 | { |
| 646 | struct wait_event *sw; |
| 647 | |
| 648 | if (h2s->send_wait) { |
| 649 | sw = h2s->send_wait; |
| 650 | sw->events &= ~SUB_RETRY_SEND; |
| 651 | tasklet_wakeup(sw->task); |
| 652 | h2s->send_wait = NULL; |
Willy Tarreau | 645b33d | 2018-12-20 15:35:57 +0100 | [diff] [blame] | 653 | LIST_DEL(&h2s->list); |
| 654 | LIST_INIT(&h2s->list); |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
Willy Tarreau | 8b2757c | 2018-12-19 17:36:48 +0100 | [diff] [blame] | 658 | /* alerts the data layer, trying to wake it up by all means, following |
| 659 | * this sequence : |
| 660 | * - if the h2s' data layer is subscribed to recv, then it's woken up for recv |
| 661 | * - if its subscribed to send, then it's woken up for send |
| 662 | * - if it was subscribed to neither, its ->wake() callback is called |
| 663 | * It is safe to call this function with a closed stream which doesn't have a |
| 664 | * conn_stream anymore. |
| 665 | */ |
| 666 | static void __maybe_unused h2s_alert(struct h2s *h2s) |
| 667 | { |
| 668 | if (h2s->recv_wait || h2s->send_wait) { |
| 669 | h2s_notify_recv(h2s); |
| 670 | h2s_notify_send(h2s); |
| 671 | } |
| 672 | else if (h2s->cs && h2s->cs->data_cb->wake != NULL) |
| 673 | h2s->cs->data_cb->wake(h2s->cs); |
| 674 | } |
| 675 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 676 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 677 | 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] | 678 | { |
| 679 | uint8_t *out = frame; |
| 680 | |
| 681 | *out = len >> 16; |
| 682 | write_n16(out + 1, len); |
| 683 | } |
| 684 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 685 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 686 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 687 | * 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] | 688 | * available in the buffer's input prior to calling this function. The buffer |
| 689 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 690 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 691 | 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] | 692 | const struct buffer *b, int o) |
| 693 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 694 | 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] | 695 | } |
| 696 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 697 | 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] | 698 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 699 | 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] | 700 | } |
| 701 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 702 | 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] | 703 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 704 | 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] | 705 | } |
| 706 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 707 | 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] | 708 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 709 | 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] | 710 | } |
| 711 | |
| 712 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 713 | /* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>. |
| 714 | * The algorithm is not obvious. It turns out that H2 headers are neither |
| 715 | * aligned nor do they use regular sizes. And to add to the trouble, the buffer |
| 716 | * may wrap so each byte read must be checked. The header is formed like this : |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 717 | * |
| 718 | * b0 b1 b2 b3 b4 b5..b8 |
| 719 | * +----------+---------+--------+----+----+----------------------+ |
| 720 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 721 | * +----------+---------+--------+----+----+----------------------+ |
| 722 | * |
| 723 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 724 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 725 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 726 | * 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] | 727 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 728 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 729 | */ |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 730 | static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 731 | { |
| 732 | uint64_t w; |
| 733 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 734 | if (b_data(b) < o + 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 735 | return 0; |
| 736 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 737 | w = h2_get_n64(b, o + 1); |
| 738 | h->len = *(uint8_t*)b_peek(b, o) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 739 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 740 | h->ff = w >> 32; |
| 741 | h->ft = w >> 40; |
| 742 | h->len += w >> 48; |
| 743 | return 1; |
| 744 | } |
| 745 | |
| 746 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 747 | * h2_peek_frame_hdr() above. |
| 748 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 749 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 750 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 751 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 755 | 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] | 756 | { |
| 757 | int ret; |
| 758 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 759 | ret = h2_peek_frame_hdr(b, 0, h); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 760 | if (ret > 0) |
| 761 | h2_skip_frame_hdr(b); |
| 762 | return ret; |
| 763 | } |
| 764 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 765 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 766 | * its connection if the stream was not yet closed. Please use this exclusively |
| 767 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 768 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 769 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 770 | { |
| 771 | if (h2s->st != H2_SS_CLOSED) |
| 772 | h2s->h2c->nb_streams--; |
| 773 | h2s->st = H2_SS_CLOSED; |
| 774 | } |
| 775 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 776 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 777 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 778 | { |
| 779 | h2s_close(h2s); |
| 780 | eb32_delete(&h2s->by_id); |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 781 | if (b_size(&h2s->rxbuf)) { |
| 782 | b_free(&h2s->rxbuf); |
| 783 | offer_buffers(NULL, tasks_run_queue); |
| 784 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 785 | if (h2s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 786 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 787 | if (h2s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 788 | h2s->recv_wait->events &= ~SUB_RETRY_RECV; |
Joseph Herlant | d77575d | 2018-11-25 10:54:45 -0800 | [diff] [blame] | 789 | /* There's no need to explicitly call unsubscribe here, the only |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 790 | * reference left would be in the h2c send_list/fctl_list, and if |
| 791 | * we're in it, we're getting out anyway |
| 792 | */ |
| 793 | LIST_DEL(&h2s->list); |
| 794 | LIST_INIT(&h2s->list); |
| 795 | tasklet_free(h2s->wait_event.task); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 796 | pool_free(pool_head_h2s, h2s); |
| 797 | } |
| 798 | |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 799 | /* allocates a new stream <id> for connection <h2c> and adds it into h2c's |
| 800 | * stream tree. In case of error, nothing is added and NULL is returned. The |
| 801 | * causes of errors can be any failed memory allocation. The caller is |
| 802 | * responsible for checking if the connection may support an extra stream |
| 803 | * prior to calling this function. |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 804 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 805 | static struct h2s *h2s_new(struct h2c *h2c, int id) |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 806 | { |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 807 | struct h2s *h2s; |
| 808 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 809 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 810 | if (!h2s) |
| 811 | goto out; |
| 812 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 813 | h2s->wait_event.task = tasklet_new(); |
| 814 | if (!h2s->wait_event.task) { |
| 815 | pool_free(pool_head_h2s, h2s); |
| 816 | goto out; |
| 817 | } |
| 818 | h2s->send_wait = NULL; |
| 819 | h2s->recv_wait = NULL; |
| 820 | h2s->wait_event.task->process = h2_deferred_shut; |
| 821 | h2s->wait_event.task->context = h2s; |
| 822 | h2s->wait_event.handle = NULL; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 823 | h2s->wait_event.events = 0; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 824 | LIST_INIT(&h2s->list); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 825 | h2s->h2c = h2c; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 826 | h2s->cs = NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 827 | h2s->mws = h2c->miw; |
| 828 | h2s->flags = H2_SF_NONE; |
| 829 | h2s->errcode = H2_ERR_NO_ERROR; |
| 830 | h2s->st = H2_SS_IDLE; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 831 | h2s->status = 0; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 832 | h2s->rxbuf = BUF_NULL; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 833 | |
| 834 | if (h2c->flags & H2_CF_IS_BACK) { |
| 835 | h1m_init_req(&h2s->h1m); |
| 836 | h2s->h1m.err_pos = -1; // don't care about errors on the request path |
| 837 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 838 | } else { |
| 839 | h1m_init_res(&h2s->h1m); |
| 840 | h2s->h1m.err_pos = -1; // don't care about errors on the response path |
| 841 | h2s->h1m.flags |= H1_MF_TOLOWER; |
| 842 | } |
| 843 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 844 | h2s->by_id.key = h2s->id = id; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 845 | if (id > 0) |
| 846 | h2c->max_id = id; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 847 | |
| 848 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 849 | h2c->nb_streams++; |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 850 | |
| 851 | return h2s; |
| 852 | |
| 853 | out_free_h2s: |
| 854 | pool_free(pool_head_h2s, h2s); |
| 855 | out: |
| 856 | return NULL; |
| 857 | } |
| 858 | |
| 859 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 860 | * case of memory allocation error. |
| 861 | */ |
| 862 | static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id) |
| 863 | { |
| 864 | struct session *sess = h2c->conn->owner; |
| 865 | struct conn_stream *cs; |
| 866 | struct h2s *h2s; |
| 867 | |
| 868 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 869 | goto out; |
| 870 | |
| 871 | h2s = h2s_new(h2c, id); |
| 872 | if (!h2s) |
| 873 | goto out; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 874 | |
| 875 | cs = cs_new(h2c->conn); |
| 876 | if (!cs) |
| 877 | goto out_close; |
| 878 | |
Olivier Houchard | 746fb77 | 2018-12-15 19:42:00 +0100 | [diff] [blame] | 879 | cs->flags |= CS_FL_NOT_FIRST; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 880 | h2s->cs = cs; |
| 881 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 882 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 883 | |
| 884 | if (stream_create_from_cs(cs) < 0) |
| 885 | goto out_free_cs; |
| 886 | |
Willy Tarreau | 590a051 | 2018-09-05 11:56:48 +0200 | [diff] [blame] | 887 | /* We want the accept date presented to the next stream to be the one |
| 888 | * we have now, the handshake time to be null (since the next stream |
| 889 | * is not delayed by a handshake), and the idle time to count since |
| 890 | * right now. |
| 891 | */ |
| 892 | sess->accept_date = date; |
| 893 | sess->tv_accept = now; |
| 894 | sess->t_handshake = 0; |
| 895 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 896 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 897 | if (h2_has_too_many_cs(h2c)) |
| 898 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 899 | return h2s; |
| 900 | |
| 901 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 902 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 903 | cs_free(cs); |
| 904 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 905 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 906 | out: |
Willy Tarreau | 45efc07 | 2018-10-03 18:27:52 +0200 | [diff] [blame] | 907 | sess_log(sess); |
| 908 | return NULL; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 909 | } |
| 910 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 911 | /* allocates a new stream associated to conn_stream <cs> on the h2c connection |
| 912 | * and returns it, or NULL in case of memory allocation error or if the highest |
| 913 | * possible stream ID was reached. |
| 914 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 915 | static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess) |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 916 | { |
| 917 | struct h2s *h2s = NULL; |
| 918 | |
| 919 | if (h2c->nb_streams >= h2_settings_max_concurrent_streams) |
| 920 | goto out; |
| 921 | |
| 922 | /* Defer choosing the ID until we send the first message to create the stream */ |
| 923 | h2s = h2s_new(h2c, 0); |
| 924 | if (!h2s) |
| 925 | goto out; |
| 926 | |
| 927 | h2s->cs = cs; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 928 | h2s->sess = sess; |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 929 | cs->ctx = h2s; |
| 930 | h2c->nb_cs++; |
| 931 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 932 | out: |
| 933 | return h2s; |
| 934 | } |
| 935 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 936 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 937 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 938 | * the various settings codes. |
| 939 | */ |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 940 | static int h2c_send_settings(struct h2c *h2c) |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 941 | { |
| 942 | struct buffer *res; |
| 943 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 944 | struct buffer buf; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 945 | int ret; |
| 946 | |
| 947 | if (h2c_mux_busy(h2c, NULL)) { |
| 948 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 949 | return 0; |
| 950 | } |
| 951 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 952 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 953 | if (!res) { |
| 954 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 955 | h2c->flags |= H2_CF_DEM_MROOM; |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 960 | chunk_memcpy(&buf, |
| 961 | "\x00\x00\x00" /* length : 0 for now */ |
| 962 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 963 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 964 | 9); |
| 965 | |
| 966 | if (h2_settings_header_table_size != 4096) { |
| 967 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 968 | |
| 969 | write_n32(str + 2, h2_settings_header_table_size); |
| 970 | chunk_memcat(&buf, str, 6); |
| 971 | } |
| 972 | |
| 973 | if (h2_settings_initial_window_size != 65535) { |
| 974 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 975 | |
| 976 | write_n32(str + 2, h2_settings_initial_window_size); |
| 977 | chunk_memcat(&buf, str, 6); |
| 978 | } |
| 979 | |
| 980 | if (h2_settings_max_concurrent_streams != 0) { |
| 981 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 982 | |
| 983 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 984 | * the protocol, so never send this value! |
| 985 | */ |
| 986 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 987 | chunk_memcat(&buf, str, 6); |
| 988 | } |
| 989 | |
| 990 | if (global.tune.bufsize != 16384) { |
| 991 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 992 | |
| 993 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 994 | * match bufsize - rewrite size, but at the moment it seems |
| 995 | * that clients don't take care of it. |
| 996 | */ |
| 997 | write_n32(str + 2, global.tune.bufsize); |
| 998 | chunk_memcat(&buf, str, 6); |
| 999 | } |
| 1000 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1001 | h2_set_frame_size(buf.area, buf.data - 9); |
| 1002 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1003 | if (unlikely(ret <= 0)) { |
| 1004 | if (!ret) { |
| 1005 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1006 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1007 | return 0; |
| 1008 | } |
| 1009 | else { |
| 1010 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1011 | return 0; |
| 1012 | } |
| 1013 | } |
| 1014 | return ret; |
| 1015 | } |
| 1016 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1017 | /* Try to receive a connection preface, then upon success try to send our |
| 1018 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1019 | * missing data. It may return an error in h2c. |
| 1020 | */ |
| 1021 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 1022 | { |
| 1023 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1024 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1025 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1026 | 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] | 1027 | |
| 1028 | if (unlikely(ret1 <= 0)) { |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1029 | if (ret1 < 0) |
| 1030 | sess_log(h2c->conn->owner); |
| 1031 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1032 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 1033 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
Willy Tarreau | 7f0cc49 | 2018-10-08 07:13:08 +0200 | [diff] [blame] | 1037 | ret2 = h2c_send_settings(h2c); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1038 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1039 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1040 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 1041 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1042 | } |
| 1043 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 1044 | /* Try to send a connection preface, then upon success try to send our |
| 1045 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 1046 | * missing data. It may return an error in h2c. |
| 1047 | */ |
| 1048 | static int h2c_bck_send_preface(struct h2c *h2c) |
| 1049 | { |
| 1050 | struct buffer *res; |
| 1051 | |
| 1052 | if (h2c_mux_busy(h2c, NULL)) { |
| 1053 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1054 | return 0; |
| 1055 | } |
| 1056 | |
| 1057 | res = h2_get_buf(h2c, &h2c->mbuf); |
| 1058 | if (!res) { |
| 1059 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1060 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
| 1064 | if (!b_data(res)) { |
| 1065 | /* preface not yet sent */ |
| 1066 | b_istput(res, ist(H2_CONN_PREFACE)); |
| 1067 | } |
| 1068 | |
| 1069 | return h2c_send_settings(h2c); |
| 1070 | } |
| 1071 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1072 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 1073 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 1074 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 1075 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 1076 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 1077 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 1078 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 1079 | * case so that it is safe to use h2c_error() to report such errors. |
| 1080 | */ |
| 1081 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 1082 | { |
| 1083 | struct buffer *res; |
| 1084 | char str[17]; |
| 1085 | int ret; |
| 1086 | |
| 1087 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 1088 | return 1; // claim that it worked |
| 1089 | |
| 1090 | if (h2c_mux_busy(h2c, h2s)) { |
| 1091 | if (h2s) |
| 1092 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1093 | else |
| 1094 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1098 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1099 | if (!res) { |
| 1100 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1101 | if (h2s) |
| 1102 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1103 | else |
| 1104 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
| 1108 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 1109 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 1110 | |
| 1111 | if (h2c->last_sid < 0) |
| 1112 | h2c->last_sid = h2c->max_id; |
| 1113 | |
| 1114 | write_n32(str + 9, h2c->last_sid); |
| 1115 | write_n32(str + 13, h2c->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1116 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1117 | if (unlikely(ret <= 0)) { |
| 1118 | if (!ret) { |
| 1119 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1120 | if (h2s) |
| 1121 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1122 | else |
| 1123 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1124 | return 0; |
| 1125 | } |
| 1126 | else { |
| 1127 | /* we cannot report this error using GOAWAY, so we mark |
| 1128 | * it and claim a success. |
| 1129 | */ |
| 1130 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1131 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 1132 | return 1; |
| 1133 | } |
| 1134 | } |
| 1135 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 1136 | return ret; |
| 1137 | } |
| 1138 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1139 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 1140 | * during mux operations. This stream must be valid and cannot be closed |
| 1141 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 1142 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 1143 | * not yet. |
| 1144 | * |
| 1145 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1146 | * to write the message, it subscribes the stream to future notifications. |
| 1147 | */ |
| 1148 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1149 | { |
| 1150 | struct buffer *res; |
| 1151 | char str[13]; |
| 1152 | int ret; |
| 1153 | |
| 1154 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 1155 | return 1; |
| 1156 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1157 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1158 | * RST_STREAM in response to a RST_STREAM frame. |
| 1159 | */ |
| 1160 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1161 | ret = 1; |
| 1162 | goto ignore; |
| 1163 | } |
| 1164 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1165 | if (h2c_mux_busy(h2c, h2s)) { |
| 1166 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1167 | return 0; |
| 1168 | } |
| 1169 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1170 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1171 | if (!res) { |
| 1172 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1173 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1174 | return 0; |
| 1175 | } |
| 1176 | |
| 1177 | /* len: 4, type: 3, flags: none */ |
| 1178 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 1179 | write_n32(str + 5, h2s->id); |
| 1180 | write_n32(str + 9, h2s->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1181 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1182 | |
| 1183 | if (unlikely(ret <= 0)) { |
| 1184 | if (!ret) { |
| 1185 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1186 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1187 | return 0; |
| 1188 | } |
| 1189 | else { |
| 1190 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1191 | return 0; |
| 1192 | } |
| 1193 | } |
| 1194 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1195 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1196 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1197 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1198 | return ret; |
| 1199 | } |
| 1200 | |
| 1201 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 1202 | * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the |
Willy Tarreau | e6888ff | 2018-12-23 18:26:26 +0100 | [diff] [blame] | 1203 | * error code, even if the stream is one of the dummy ones, and will update |
| 1204 | * h2s->st to H2_SS_CLOSED if it was not yet. |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1205 | * |
| 1206 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 1207 | * 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] | 1208 | * 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] | 1209 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1210 | */ |
| 1211 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1212 | { |
| 1213 | struct buffer *res; |
| 1214 | char str[13]; |
| 1215 | int ret; |
| 1216 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1217 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 1218 | * RST_STREAM in response to a RST_STREAM frame. |
| 1219 | */ |
| 1220 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 1221 | ret = 1; |
| 1222 | goto ignore; |
| 1223 | } |
| 1224 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1225 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1226 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1227 | return 0; |
| 1228 | } |
| 1229 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1230 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1231 | if (!res) { |
| 1232 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1233 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1234 | return 0; |
| 1235 | } |
| 1236 | |
| 1237 | /* len: 4, type: 3, flags: none */ |
| 1238 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1239 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1240 | write_n32(str + 5, h2c->dsi); |
Willy Tarreau | e6888ff | 2018-12-23 18:26:26 +0100 | [diff] [blame] | 1241 | write_n32(str + 9, h2s->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1242 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1243 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1244 | if (unlikely(ret <= 0)) { |
| 1245 | if (!ret) { |
| 1246 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1247 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1248 | return 0; |
| 1249 | } |
| 1250 | else { |
| 1251 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1252 | return 0; |
| 1253 | } |
| 1254 | } |
| 1255 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 1256 | ignore: |
Willy Tarreau | ab0e1da | 2018-10-05 10:16:37 +0200 | [diff] [blame] | 1257 | if (h2s->id) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1258 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1259 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 1260 | } |
| 1261 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1262 | return ret; |
| 1263 | } |
| 1264 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1265 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 1266 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 1267 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 1268 | * on success or zero if nothing was done. In case of lack of room to write the |
| 1269 | * message, it subscribes the requesting stream to future notifications. |
| 1270 | */ |
| 1271 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 1272 | { |
| 1273 | struct h2c *h2c = h2s->h2c; |
| 1274 | struct buffer *res; |
| 1275 | char str[9]; |
| 1276 | int ret; |
| 1277 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1278 | 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] | 1279 | return 1; |
| 1280 | |
| 1281 | if (h2c_mux_busy(h2c, h2s)) { |
| 1282 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 1283 | return 0; |
| 1284 | } |
| 1285 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1286 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1287 | if (!res) { |
| 1288 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1289 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1294 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1295 | write_n32(str + 5, h2s->id); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1296 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1297 | if (likely(ret > 0)) { |
| 1298 | h2s->flags |= H2_SF_ES_SENT; |
| 1299 | } |
| 1300 | else if (!ret) { |
| 1301 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1302 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1303 | return 0; |
| 1304 | } |
| 1305 | else { |
| 1306 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1307 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1308 | } |
| 1309 | return ret; |
| 1310 | } |
| 1311 | |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1312 | /* wake the streams attached to the connection, whose id is greater than <last>, |
| 1313 | * 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] | 1314 | * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection. |
| 1315 | * The stream's state is automatically updated accordingly. |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1316 | */ |
| 1317 | static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags) |
| 1318 | { |
| 1319 | struct eb32_node *node; |
| 1320 | struct h2s *h2s; |
| 1321 | |
| 1322 | if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR) |
Willy Tarreau | a851935 | 2018-12-18 16:44:28 +0100 | [diff] [blame] | 1323 | flags |= CS_FL_ERR_PENDING; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1324 | |
| 1325 | if (conn_xprt_read0_pending(h2c->conn)) |
Willy Tarreau | 2c096c3 | 2018-09-12 09:45:54 +0200 | [diff] [blame] | 1326 | flags |= CS_FL_REOS; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1327 | |
| 1328 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1329 | while (node) { |
| 1330 | h2s = container_of(node, struct h2s, by_id); |
| 1331 | if (h2s->id <= last) |
| 1332 | break; |
| 1333 | node = eb32_next(node); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1334 | |
| 1335 | if (!h2s->cs) { |
| 1336 | /* this stream was already orphaned */ |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 1337 | h2s_destroy(h2s); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1338 | continue; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1339 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1340 | |
| 1341 | h2s->cs->flags |= flags; |
Willy Tarreau | a851935 | 2018-12-18 16:44:28 +0100 | [diff] [blame] | 1342 | if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS)) |
| 1343 | h2s->cs->flags |= CS_FL_ERROR; |
| 1344 | |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 1345 | h2s_alert(h2s); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1346 | |
Willy Tarreau | a851935 | 2018-12-18 16:44:28 +0100 | [diff] [blame] | 1347 | if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR) |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1348 | h2s->st = H2_SS_ERROR; |
Willy Tarreau | 2c096c3 | 2018-09-12 09:45:54 +0200 | [diff] [blame] | 1349 | else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN) |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1350 | h2s->st = H2_SS_HREM; |
Willy Tarreau | 2c096c3 | 2018-09-12 09:45:54 +0200 | [diff] [blame] | 1351 | else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1352 | h2s_close(h2s); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1353 | } |
| 1354 | } |
| 1355 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1356 | /* Increase all streams' outgoing window size by the difference passed in |
| 1357 | * argument. This is needed upon receipt of the settings frame if the initial |
| 1358 | * window size is different. The difference may be negative and the resulting |
| 1359 | * window size as well, for the time it takes to receive some window updates. |
| 1360 | */ |
| 1361 | static void h2c_update_all_ws(struct h2c *h2c, int diff) |
| 1362 | { |
| 1363 | struct h2s *h2s; |
| 1364 | struct eb32_node *node; |
| 1365 | |
| 1366 | if (!diff) |
| 1367 | return; |
| 1368 | |
| 1369 | node = eb32_first(&h2c->streams_by_id); |
| 1370 | while (node) { |
| 1371 | h2s = container_of(node, struct h2s, by_id); |
| 1372 | h2s->mws += diff; |
| 1373 | node = eb32_next(node); |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1378 | * ACKs it if needed. Returns > 0 on success or zero on missing data. It may |
| 1379 | * return an error in h2c. Described in RFC7540#6.5. |
| 1380 | */ |
| 1381 | static int h2c_handle_settings(struct h2c *h2c) |
| 1382 | { |
| 1383 | unsigned int offset; |
| 1384 | int error; |
| 1385 | |
| 1386 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1387 | if (h2c->dfl) { |
| 1388 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1389 | goto fail; |
| 1390 | } |
| 1391 | return 1; |
| 1392 | } |
| 1393 | |
| 1394 | if (h2c->dsi != 0) { |
| 1395 | error = H2_ERR_PROTOCOL_ERROR; |
| 1396 | goto fail; |
| 1397 | } |
| 1398 | |
| 1399 | if (h2c->dfl % 6) { |
| 1400 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1401 | goto fail; |
| 1402 | } |
| 1403 | |
| 1404 | /* that's the limit we can process */ |
| 1405 | if (h2c->dfl > global.tune.bufsize) { |
| 1406 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1407 | goto fail; |
| 1408 | } |
| 1409 | |
| 1410 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1411 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1412 | return 0; |
| 1413 | |
| 1414 | /* parse the frame */ |
| 1415 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1416 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1417 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1418 | |
| 1419 | switch (type) { |
| 1420 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1421 | /* we need to update all existing streams with the |
| 1422 | * difference from the previous iws. |
| 1423 | */ |
| 1424 | if (arg < 0) { // RFC7540#6.5.2 |
| 1425 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1426 | goto fail; |
| 1427 | } |
| 1428 | h2c_update_all_ws(h2c, arg - h2c->miw); |
| 1429 | h2c->miw = arg; |
| 1430 | break; |
| 1431 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1432 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1433 | error = H2_ERR_PROTOCOL_ERROR; |
| 1434 | goto fail; |
| 1435 | } |
| 1436 | h2c->mfs = arg; |
| 1437 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1438 | case H2_SETTINGS_ENABLE_PUSH: |
| 1439 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1440 | error = H2_ERR_PROTOCOL_ERROR; |
| 1441 | goto fail; |
| 1442 | } |
| 1443 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | /* need to ACK this frame now */ |
| 1448 | h2c->st0 = H2_CS_FRAME_A; |
| 1449 | return 1; |
| 1450 | fail: |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1451 | sess_log(h2c->conn->owner); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1452 | h2c_error(h2c, error); |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
| 1456 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1457 | * success or one of the h2_status values. |
| 1458 | */ |
| 1459 | static int h2c_ack_settings(struct h2c *h2c) |
| 1460 | { |
| 1461 | struct buffer *res; |
| 1462 | char str[9]; |
| 1463 | int ret = -1; |
| 1464 | |
| 1465 | if (h2c_mux_busy(h2c, NULL)) { |
| 1466 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1467 | return 0; |
| 1468 | } |
| 1469 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1470 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1471 | if (!res) { |
| 1472 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1473 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1474 | return 0; |
| 1475 | } |
| 1476 | |
| 1477 | memcpy(str, |
| 1478 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1479 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1480 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1481 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1482 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1483 | if (unlikely(ret <= 0)) { |
| 1484 | if (!ret) { |
| 1485 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1486 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1487 | return 0; |
| 1488 | } |
| 1489 | else { |
| 1490 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1491 | return 0; |
| 1492 | } |
| 1493 | } |
| 1494 | return ret; |
| 1495 | } |
| 1496 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1497 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1498 | * the pointer to the payload in <payload>. Returns > 0 on success or zero on |
| 1499 | * missing data. It may return an error in h2c. |
| 1500 | */ |
| 1501 | static int h2c_handle_ping(struct h2c *h2c) |
| 1502 | { |
| 1503 | /* frame length must be exactly 8 */ |
| 1504 | if (h2c->dfl != 8) { |
| 1505 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
| 1509 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1510 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1511 | h2c->st0 = H2_CS_FRAME_A; |
| 1512 | return 1; |
| 1513 | } |
| 1514 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1515 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1516 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1517 | * error in h2c. |
| 1518 | */ |
| 1519 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1520 | { |
| 1521 | struct buffer *res; |
| 1522 | char str[13]; |
| 1523 | int ret = -1; |
| 1524 | |
| 1525 | if (h2c_mux_busy(h2c, NULL)) { |
| 1526 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1527 | return 0; |
| 1528 | } |
| 1529 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1530 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1531 | if (!res) { |
| 1532 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1533 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1534 | return 0; |
| 1535 | } |
| 1536 | |
| 1537 | /* length: 4, type: 8, flags: none */ |
| 1538 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1539 | write_n32(str + 5, sid); |
| 1540 | write_n32(str + 9, increment); |
| 1541 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1542 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1543 | |
| 1544 | if (unlikely(ret <= 0)) { |
| 1545 | if (!ret) { |
| 1546 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1547 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1548 | return 0; |
| 1549 | } |
| 1550 | else { |
| 1551 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1552 | return 0; |
| 1553 | } |
| 1554 | } |
| 1555 | return ret; |
| 1556 | } |
| 1557 | |
| 1558 | /* try to send pending window update for the connection. It's safe to call it |
| 1559 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1560 | * failure. It may return an error in h2c. |
| 1561 | */ |
| 1562 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1563 | { |
| 1564 | int ret = 1; |
| 1565 | |
| 1566 | if (h2c->rcvd_c <= 0) |
| 1567 | return 1; |
| 1568 | |
Willy Tarreau | 97aaa67 | 2018-12-23 09:49:04 +0100 | [diff] [blame] | 1569 | if (!(h2c->flags & H2_CF_WINDOW_OPENED)) { |
| 1570 | /* increase the advertised connection window to 2G on |
| 1571 | * first update. |
| 1572 | */ |
| 1573 | h2c->flags |= H2_CF_WINDOW_OPENED; |
| 1574 | h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT; |
| 1575 | } |
| 1576 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1577 | /* send WU for the connection */ |
| 1578 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1579 | if (ret > 0) |
| 1580 | h2c->rcvd_c = 0; |
| 1581 | |
| 1582 | return ret; |
| 1583 | } |
| 1584 | |
| 1585 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1586 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1587 | * room or failure. It may return an error in h2c. |
| 1588 | */ |
| 1589 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1590 | { |
| 1591 | int ret = 1; |
| 1592 | |
| 1593 | if (h2c->rcvd_s <= 0) |
| 1594 | return 1; |
| 1595 | |
| 1596 | /* send WU for the stream */ |
| 1597 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1598 | if (ret > 0) |
| 1599 | h2c->rcvd_s = 0; |
| 1600 | |
| 1601 | return ret; |
| 1602 | } |
| 1603 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1604 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1605 | * success, 0 on missing data or one of the h2_status values. |
| 1606 | */ |
| 1607 | static int h2c_ack_ping(struct h2c *h2c) |
| 1608 | { |
| 1609 | struct buffer *res; |
| 1610 | char str[17]; |
| 1611 | int ret = -1; |
| 1612 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1613 | if (b_data(&h2c->dbuf) < 8) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1614 | return 0; |
| 1615 | |
| 1616 | if (h2c_mux_busy(h2c, NULL)) { |
| 1617 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1618 | return 0; |
| 1619 | } |
| 1620 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1621 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1622 | if (!res) { |
| 1623 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1624 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
| 1628 | memcpy(str, |
| 1629 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1630 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1631 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1632 | |
| 1633 | /* copy the original payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1634 | h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1635 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1636 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1637 | if (unlikely(ret <= 0)) { |
| 1638 | if (!ret) { |
| 1639 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1640 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1641 | return 0; |
| 1642 | } |
| 1643 | else { |
| 1644 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1645 | return 0; |
| 1646 | } |
| 1647 | } |
| 1648 | return ret; |
| 1649 | } |
| 1650 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1651 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1652 | * Returns > 0 on success or zero on missing data. It may return an error in |
| 1653 | * h2c or h2s. Described in RFC7540#6.9. |
| 1654 | */ |
| 1655 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1656 | { |
| 1657 | int32_t inc; |
| 1658 | int error; |
| 1659 | |
| 1660 | if (h2c->dfl != 4) { |
| 1661 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1662 | goto conn_err; |
| 1663 | } |
| 1664 | |
| 1665 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1666 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1667 | return 0; |
| 1668 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1669 | inc = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1670 | |
| 1671 | if (h2c->dsi != 0) { |
| 1672 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1673 | |
| 1674 | /* it's not an error to receive WU on a closed stream */ |
| 1675 | if (h2s->st == H2_SS_CLOSED) |
| 1676 | return 1; |
| 1677 | |
| 1678 | if (!inc) { |
| 1679 | error = H2_ERR_PROTOCOL_ERROR; |
| 1680 | goto strm_err; |
| 1681 | } |
| 1682 | |
| 1683 | if (h2s->mws >= 0 && h2s->mws + inc < 0) { |
| 1684 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1685 | goto strm_err; |
| 1686 | } |
| 1687 | |
| 1688 | h2s->mws += inc; |
| 1689 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1690 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 1691 | if (h2s->send_wait) |
| 1692 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 1693 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1694 | } |
| 1695 | } |
| 1696 | else { |
| 1697 | /* connection window update */ |
| 1698 | if (!inc) { |
| 1699 | error = H2_ERR_PROTOCOL_ERROR; |
| 1700 | goto conn_err; |
| 1701 | } |
| 1702 | |
| 1703 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1704 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1705 | goto conn_err; |
| 1706 | } |
| 1707 | |
| 1708 | h2c->mws += inc; |
| 1709 | } |
| 1710 | |
| 1711 | return 1; |
| 1712 | |
| 1713 | conn_err: |
| 1714 | h2c_error(h2c, error); |
| 1715 | return 0; |
| 1716 | |
| 1717 | strm_err: |
| 1718 | if (h2s) { |
| 1719 | h2s_error(h2s, error); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1720 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1721 | } |
| 1722 | else |
| 1723 | h2c_error(h2c, error); |
| 1724 | return 0; |
| 1725 | } |
| 1726 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1727 | /* processes a GOAWAY frame, and signals all streams whose ID is greater than |
| 1728 | * the last ID. Returns > 0 on success or zero on missing data. It may return |
| 1729 | * an error in h2c. Described in RFC7540#6.8. |
| 1730 | */ |
| 1731 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1732 | { |
| 1733 | int error; |
| 1734 | int last; |
| 1735 | |
| 1736 | if (h2c->dsi != 0) { |
| 1737 | error = H2_ERR_PROTOCOL_ERROR; |
| 1738 | goto conn_err; |
| 1739 | } |
| 1740 | |
| 1741 | if (h2c->dfl < 8) { |
| 1742 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1743 | goto conn_err; |
| 1744 | } |
| 1745 | |
| 1746 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1747 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1748 | return 0; |
| 1749 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1750 | last = h2_get_n32(&h2c->dbuf, 0); |
| 1751 | h2c->errcode = h2_get_n32(&h2c->dbuf, 4); |
Olivier Houchard | 9117780 | 2018-12-19 14:49:39 +0100 | [diff] [blame] | 1752 | h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1753 | if (h2c->last_sid < 0) |
| 1754 | h2c->last_sid = last; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1755 | return 1; |
| 1756 | |
| 1757 | conn_err: |
| 1758 | h2c_error(h2c, error); |
| 1759 | return 0; |
| 1760 | } |
| 1761 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1762 | /* processes a PRIORITY frame, and either skips it or rejects if it is |
| 1763 | * invalid. Returns > 0 on success or zero on missing data. It may return |
| 1764 | * an error in h2c. Described in RFC7540#6.3. |
| 1765 | */ |
| 1766 | static int h2c_handle_priority(struct h2c *h2c) |
| 1767 | { |
| 1768 | int error; |
| 1769 | |
| 1770 | if (h2c->dsi == 0) { |
| 1771 | error = H2_ERR_PROTOCOL_ERROR; |
| 1772 | goto conn_err; |
| 1773 | } |
| 1774 | |
| 1775 | if (h2c->dfl != 5) { |
| 1776 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1777 | goto conn_err; |
| 1778 | } |
| 1779 | |
| 1780 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1781 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1782 | return 0; |
| 1783 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1784 | if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1785 | /* 7540#5.3 : can't depend on itself */ |
| 1786 | error = H2_ERR_PROTOCOL_ERROR; |
| 1787 | goto conn_err; |
| 1788 | } |
| 1789 | return 1; |
| 1790 | |
| 1791 | conn_err: |
| 1792 | h2c_error(h2c, error); |
| 1793 | return 0; |
| 1794 | } |
| 1795 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1796 | /* processes an RST_STREAM frame, and sets the 32-bit error code on the stream. |
| 1797 | * Returns > 0 on success or zero on missing data. It may return an error in |
| 1798 | * h2c. Described in RFC7540#6.4. |
| 1799 | */ |
| 1800 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1801 | { |
| 1802 | int error; |
| 1803 | |
| 1804 | if (h2c->dsi == 0) { |
| 1805 | error = H2_ERR_PROTOCOL_ERROR; |
| 1806 | goto conn_err; |
| 1807 | } |
| 1808 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1809 | if (h2c->dfl != 4) { |
| 1810 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1811 | goto conn_err; |
| 1812 | } |
| 1813 | |
| 1814 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1815 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1816 | return 0; |
| 1817 | |
| 1818 | /* late RST, already handled */ |
| 1819 | if (h2s->st == H2_SS_CLOSED) |
| 1820 | return 1; |
| 1821 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1822 | h2s->errcode = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1823 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1824 | |
| 1825 | if (h2s->cs) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 1826 | cs_set_error(h2s->cs); |
Willy Tarreau | f830f01 | 2018-12-19 17:44:55 +0100 | [diff] [blame] | 1827 | h2s_alert(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | h2s->flags |= H2_SF_RST_RCVD; |
| 1831 | return 1; |
| 1832 | |
| 1833 | conn_err: |
| 1834 | h2c_error(h2c, error); |
| 1835 | return 0; |
| 1836 | } |
| 1837 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1838 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 1839 | * It may return an error in h2c or h2s. The caller must consider that the |
| 1840 | * return value is the new h2s in case one was allocated (most common case). |
| 1841 | * Described in RFC7540#6.2. Most of the |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 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 | */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1845 | 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] | 1846 | { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1847 | struct buffer rxbuf = BUF_NULL; |
| 1848 | uint32_t flags = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1849 | int error; |
| 1850 | |
| 1851 | if (!h2c->dfl) { |
Willy Tarreau | c4ea04c | 2018-12-23 08:13:59 +0100 | [diff] [blame] | 1852 | /* RFC7540#4.2 */ |
| 1853 | error = H2_ERR_FRAME_SIZE_ERROR; // empty headers frame! |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1854 | sess_log(h2c->conn->owner); |
Willy Tarreau | c4ea04c | 2018-12-23 08:13:59 +0100 | [diff] [blame] | 1855 | goto conn_err; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1856 | } |
| 1857 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1858 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1859 | return NULL; // empty buffer |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1860 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1861 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1862 | return NULL; // incomplete frame |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1863 | |
| 1864 | /* now either the frame is complete or the buffer is complete */ |
| 1865 | if (h2s->st != H2_SS_IDLE) { |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1866 | /* The stream exists/existed, this must be a trailers frame */ |
| 1867 | if (h2s->st != H2_SS_CLOSED) { |
| 1868 | if (!h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags)) |
| 1869 | goto out; |
| 1870 | goto done; |
| 1871 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1872 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1873 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1874 | goto conn_err; |
| 1875 | } |
| 1876 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 1877 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 1878 | error = H2_ERR_PROTOCOL_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1879 | sess_log(h2c->conn->owner); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1880 | goto conn_err; |
| 1881 | } |
Willy Tarreau | 415b1ee | 2019-01-02 13:59:43 +0100 | [diff] [blame] | 1882 | else if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 1883 | goto out; // IDLE but too many cs still present |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1884 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1885 | error = h2c_decode_headers(h2c, &rxbuf, &flags); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1886 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1887 | /* unrecoverable error ? */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1888 | if (h2c->st0 >= H2_CS_ERROR) |
| 1889 | goto out; |
| 1890 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1891 | if (error <= 0) { |
| 1892 | if (error == 0) |
| 1893 | goto out; // missing data |
| 1894 | |
| 1895 | /* Failed to decode this stream (e.g. too large request) |
| 1896 | * but the HPACK decompressor is still synchronized. |
| 1897 | */ |
| 1898 | h2s = (struct h2s*)h2_error_stream; |
| 1899 | goto send_rst; |
| 1900 | } |
| 1901 | |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 1902 | /* 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] | 1903 | * positively from h2c_frt_stream_new(), the stream will report the error, |
| 1904 | * 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] | 1905 | */ |
Willy Tarreau | a8e4954 | 2018-10-03 18:53:55 +0200 | [diff] [blame] | 1906 | h2s = h2c_frt_stream_new(h2c, h2c->dsi); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1907 | if (!h2s) { |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 1908 | h2s = (struct h2s*)h2_refused_stream; |
| 1909 | goto send_rst; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | h2s->st = H2_SS_OPEN; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1913 | h2s->rxbuf = rxbuf; |
| 1914 | h2s->flags |= flags; |
| 1915 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 1916 | done: |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1917 | if (h2c->dff & H2_F_HEADERS_END_STREAM) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1918 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1919 | |
| 1920 | if (h2s->flags & H2_SF_ES_RCVD) { |
| 1921 | h2s->st = H2_SS_HREM; |
Willy Tarreau | 39d6850 | 2018-03-02 12:26:37 +0100 | [diff] [blame] | 1922 | h2s->cs->flags |= CS_FL_REOS; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1923 | } |
| 1924 | |
Willy Tarreau | 3a429f0 | 2019-01-03 11:41:50 +0100 | [diff] [blame] | 1925 | /* update the max stream ID if the request is being processed */ |
| 1926 | if (h2s->id > h2c->max_id) |
| 1927 | h2c->max_id = h2s->id; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1928 | |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1929 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1930 | |
| 1931 | conn_err: |
| 1932 | h2c_error(h2c, error); |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1933 | goto out; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1934 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 1935 | out: |
| 1936 | h2_release_buf(h2c, &rxbuf); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 1937 | return NULL; |
Willy Tarreau | 96a10c2 | 2018-12-23 18:30:44 +0100 | [diff] [blame] | 1938 | |
| 1939 | send_rst: |
| 1940 | /* make the demux send an RST for the current stream. We may only |
| 1941 | * do this if we're certain that the HEADERS frame was properly |
| 1942 | * decompressed so that the HPACK decoder is still kept up to date. |
| 1943 | */ |
| 1944 | h2_release_buf(h2c, &rxbuf); |
| 1945 | h2c->st0 = H2_CS_FRAME_E; |
| 1946 | return h2s; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1947 | } |
| 1948 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 1949 | /* processes a HEADERS frame. Returns h2s on success or NULL on missing data. |
| 1950 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 1951 | * errors here are reported as connection errors since it's impossible to |
| 1952 | * recover from such errors after the compression context has been altered. |
| 1953 | */ |
| 1954 | static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 1955 | { |
| 1956 | int error; |
| 1957 | |
| 1958 | if (!h2c->dfl) { |
Willy Tarreau | c4ea04c | 2018-12-23 08:13:59 +0100 | [diff] [blame] | 1959 | /* RFC7540#4.2 */ |
| 1960 | error = H2_ERR_FRAME_SIZE_ERROR; // empty headers frame! |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 1961 | sess_log(h2c->conn->owner); |
Willy Tarreau | c4ea04c | 2018-12-23 08:13:59 +0100 | [diff] [blame] | 1962 | goto conn_err; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | if (!b_size(&h2c->dbuf)) |
| 1966 | return NULL; // empty buffer |
| 1967 | |
| 1968 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
| 1969 | return NULL; // incomplete frame |
| 1970 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1971 | error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 1972 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1973 | /* unrecoverable error ? */ |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 1974 | if (h2c->st0 >= H2_CS_ERROR) |
| 1975 | return NULL; |
| 1976 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1977 | if (error <= 0) { |
| 1978 | if (error == 0) |
| 1979 | return NULL; // missing data |
| 1980 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 1981 | /* stream error : send RST_STREAM */ |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1982 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 1983 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 1984 | return NULL; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 1985 | } |
| 1986 | |
Willy Tarreau | 45ffc0c | 2019-01-03 09:32:20 +0100 | [diff] [blame] | 1987 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 1988 | h2s->flags |= H2_SF_ES_RCVD; |
| 1989 | h2s->cs->flags |= CS_FL_REOS; |
| 1990 | } |
| 1991 | |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 1992 | if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR) |
| 1993 | h2s->st = H2_SS_ERROR; |
| 1994 | else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN) |
| 1995 | h2s->st = H2_SS_HREM; |
| 1996 | else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC) |
| 1997 | h2s_close(h2s); |
| 1998 | |
| 1999 | return h2s; |
| 2000 | |
| 2001 | conn_err: |
| 2002 | h2c_error(h2c, error); |
| 2003 | return NULL; |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2004 | } |
| 2005 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2006 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 2007 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 2008 | */ |
| 2009 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 2010 | { |
| 2011 | int error; |
| 2012 | |
| 2013 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 2014 | * to signal an end of stream (with the ES flag). |
| 2015 | */ |
| 2016 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2017 | if (!b_size(&h2c->dbuf) && h2c->dfl) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2018 | return 0; // empty buffer |
| 2019 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2020 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2021 | return 0; // incomplete frame |
| 2022 | |
| 2023 | /* now either the frame is complete or the buffer is complete */ |
| 2024 | |
| 2025 | if (!h2c->dsi) { |
| 2026 | /* RFC7540#6.1 */ |
| 2027 | error = H2_ERR_PROTOCOL_ERROR; |
| 2028 | goto conn_err; |
| 2029 | } |
| 2030 | |
| 2031 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 2032 | /* RFC7540#6.1 */ |
| 2033 | error = H2_ERR_STREAM_CLOSED; |
| 2034 | goto strm_err; |
| 2035 | } |
| 2036 | |
Willy Tarreau | a56a6de | 2018-02-26 15:59:07 +0100 | [diff] [blame] | 2037 | if (!h2_frt_transfer_data(h2s)) |
| 2038 | return 0; |
| 2039 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2040 | /* call the upper layers to process the frame, then let the upper layer |
| 2041 | * notify the stream about any change. |
| 2042 | */ |
| 2043 | if (!h2s->cs) { |
| 2044 | error = H2_ERR_STREAM_CLOSED; |
| 2045 | goto strm_err; |
| 2046 | } |
| 2047 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 2048 | if (h2c->st0 >= H2_CS_ERROR) |
| 2049 | return 0; |
| 2050 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2051 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2052 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2053 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | /* check for completion : the callee will change this to FRAME_A or |
| 2057 | * FRAME_H once done. |
| 2058 | */ |
| 2059 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2060 | return 0; |
| 2061 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2062 | |
| 2063 | /* last frame */ |
| 2064 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 2065 | h2s->st = H2_SS_HREM; |
| 2066 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 39d6850 | 2018-03-02 12:26:37 +0100 | [diff] [blame] | 2067 | h2s->cs->flags |= CS_FL_REOS; |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 2068 | } |
| 2069 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2070 | return 1; |
| 2071 | |
| 2072 | conn_err: |
| 2073 | h2c_error(h2c, error); |
| 2074 | return 0; |
| 2075 | |
| 2076 | strm_err: |
| 2077 | if (h2s) { |
| 2078 | h2s_error(h2s, error); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2079 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2080 | } |
| 2081 | else |
| 2082 | h2c_error(h2c, error); |
| 2083 | return 0; |
| 2084 | } |
| 2085 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2086 | /* process Rx frames to be demultiplexed */ |
| 2087 | static void h2_process_demux(struct h2c *h2c) |
| 2088 | { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2089 | struct h2s *h2s = NULL, *tmp_h2s; |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 2090 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2091 | if (h2c->st0 >= H2_CS_ERROR) |
| 2092 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2093 | |
| 2094 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2095 | if (h2c->st0 == H2_CS_PREFACE) { |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2096 | if (h2c->flags & H2_CF_IS_BACK) |
| 2097 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2098 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 2099 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2100 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2101 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2102 | sess_log(h2c->conn->owner); |
| 2103 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2104 | goto fail; |
| 2105 | } |
| 2106 | |
| 2107 | h2c->max_id = 0; |
| 2108 | h2c->st0 = H2_CS_SETTINGS1; |
| 2109 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2110 | |
| 2111 | if (h2c->st0 == H2_CS_SETTINGS1) { |
| 2112 | struct h2_fh hdr; |
| 2113 | |
| 2114 | /* ensure that what is pending is a valid SETTINGS frame |
| 2115 | * without an ACK. |
| 2116 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2117 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2118 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2119 | if (h2c->st0 == H2_CS_ERROR) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2120 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2121 | sess_log(h2c->conn->owner); |
| 2122 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2123 | goto fail; |
| 2124 | } |
| 2125 | |
| 2126 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 2127 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2128 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2129 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2130 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2131 | goto fail; |
| 2132 | } |
| 2133 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2134 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2135 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2136 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2137 | h2c->st0 = H2_CS_ERROR2; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2138 | sess_log(h2c->conn->owner); |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2139 | goto fail; |
| 2140 | } |
| 2141 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2142 | /* that's OK, switch to FRAME_P to process it. This is |
| 2143 | * a SETTINGS frame whose header has already been |
| 2144 | * deleted above. |
| 2145 | */ |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2146 | h2c->dfl = hdr.len; |
| 2147 | h2c->dsi = hdr.sid; |
| 2148 | h2c->dft = hdr.ft; |
| 2149 | h2c->dff = hdr.ff; |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 2150 | h2c->dpl = 0; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 2151 | h2c->st0 = H2_CS_FRAME_P; |
| 2152 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2153 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2154 | |
| 2155 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2156 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2157 | int ret = 0; |
| 2158 | |
| 2159 | if (h2c->st0 >= H2_CS_ERROR) |
| 2160 | break; |
| 2161 | |
| 2162 | if (h2c->st0 == H2_CS_FRAME_H) { |
| 2163 | struct h2_fh hdr; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2164 | unsigned int padlen = 0; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2165 | |
Willy Tarreau | a4428bd | 2018-12-22 18:11:41 +0100 | [diff] [blame] | 2166 | if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2167 | break; |
| 2168 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 2169 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2170 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2171 | h2c->st0 = H2_CS_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2172 | if (!h2c->nb_streams) { |
| 2173 | /* only log if no other stream can report the error */ |
| 2174 | sess_log(h2c->conn->owner); |
| 2175 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2176 | break; |
| 2177 | } |
| 2178 | |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2179 | if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) { |
| 2180 | /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA), |
| 2181 | * we read the pad length and drop it from the remaining |
| 2182 | * payload (one byte + the 9 remaining ones = 10 total |
| 2183 | * removed), so we have a frame payload starting after the |
| 2184 | * pad len. Flow controlled frames (DATA) also count the |
| 2185 | * padlen in the flow control, so it must be adjusted. |
| 2186 | */ |
| 2187 | if (hdr.len < 1) { |
| 2188 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 2189 | sess_log(h2c->conn->owner); |
| 2190 | goto fail; |
| 2191 | } |
| 2192 | hdr.len--; |
| 2193 | |
| 2194 | if (b_data(&h2c->dbuf) < 10) |
| 2195 | break; // missing padlen |
| 2196 | |
| 2197 | padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9); |
| 2198 | |
| 2199 | if (padlen > hdr.len) { |
| 2200 | /* RFC7540#6.1 : pad length = length of |
| 2201 | * frame payload or greater => error. |
| 2202 | */ |
| 2203 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2204 | sess_log(h2c->conn->owner); |
| 2205 | goto fail; |
| 2206 | } |
| 2207 | |
| 2208 | if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) { |
| 2209 | h2c->rcvd_c++; |
| 2210 | h2c->rcvd_s++; |
| 2211 | } |
| 2212 | b_del(&h2c->dbuf, 1); |
| 2213 | } |
| 2214 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2215 | h2c->dfl = hdr.len; |
| 2216 | h2c->dsi = hdr.sid; |
| 2217 | h2c->dft = hdr.ft; |
| 2218 | h2c->dff = hdr.ff; |
Willy Tarreau | 3bf6918 | 2018-12-21 15:34:50 +0100 | [diff] [blame] | 2219 | h2c->dpl = padlen; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2220 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */ |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2224 | tmp_h2s = h2c_st_by_id(h2c, h2c->dsi); |
| 2225 | |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2226 | if (tmp_h2s != h2s && h2s && h2s->cs && |
| 2227 | (b_data(&h2s->rxbuf) || |
| 2228 | (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2229 | /* we may have to signal the upper layers */ |
| 2230 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2231 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2232 | } |
| 2233 | h2s = tmp_h2s; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2234 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 2235 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2236 | goto strm_err; |
| 2237 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2238 | if (h2s->st == H2_SS_IDLE && |
| 2239 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 2240 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 2241 | * this state MUST be treated as a connection error |
| 2242 | */ |
| 2243 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2244 | h2c->st0 = H2_CS_ERROR; |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2245 | if (!h2c->nb_streams) { |
| 2246 | /* only log if no other stream can report the error */ |
| 2247 | sess_log(h2c->conn->owner); |
| 2248 | } |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 2249 | break; |
| 2250 | } |
| 2251 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2252 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 2253 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 2254 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
| 2255 | * this state MUST be treated as a stream error |
| 2256 | */ |
| 2257 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2258 | goto strm_err; |
| 2259 | } |
| 2260 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 2261 | /* Below the management of frames received in closed state is a |
| 2262 | * bit hackish because the spec makes strong differences between |
| 2263 | * streams closed by receiving RST, sending RST, and seeing ES |
| 2264 | * in both directions. In addition to this, the creation of a |
| 2265 | * new stream reusing the identifier of a closed one will be |
| 2266 | * detected here. Given that we cannot keep track of all closed |
| 2267 | * streams forever, we consider that unknown closed streams were |
| 2268 | * closed on RST received, which allows us to respond with an |
| 2269 | * RST without breaking the connection (eg: to abort a transfer). |
| 2270 | * Some frames have to be silently ignored as well. |
| 2271 | */ |
| 2272 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
| 2273 | if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) { |
| 2274 | /* #5.1.1: The identifier of a newly |
| 2275 | * established stream MUST be numerically |
| 2276 | * greater than all streams that the initiating |
| 2277 | * endpoint has opened or reserved. This |
| 2278 | * governs streams that are opened using a |
| 2279 | * HEADERS frame and streams that are reserved |
| 2280 | * using PUSH_PROMISE. An endpoint that |
| 2281 | * receives an unexpected stream identifier |
| 2282 | * MUST respond with a connection error. |
| 2283 | */ |
| 2284 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2285 | goto strm_err; |
| 2286 | } |
| 2287 | |
| 2288 | if (h2s->flags & H2_SF_RST_RCVD) { |
| 2289 | /* RFC7540#5.1:closed: an endpoint that |
| 2290 | * receives any frame other than PRIORITY after |
| 2291 | * receiving a RST_STREAM MUST treat that as a |
| 2292 | * stream error of type STREAM_CLOSED. |
| 2293 | * |
| 2294 | * Note that old streams fall into this category |
| 2295 | * and will lead to an RST being sent. |
| 2296 | */ |
| 2297 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 2298 | h2c->st0 = H2_CS_FRAME_E; |
| 2299 | goto strm_err; |
| 2300 | } |
| 2301 | |
| 2302 | /* RFC7540#5.1:closed: if this state is reached as a |
| 2303 | * result of sending a RST_STREAM frame, the peer that |
| 2304 | * receives the RST_STREAM might have already sent |
| 2305 | * frames on the stream that cannot be withdrawn. An |
| 2306 | * endpoint MUST ignore frames that it receives on |
| 2307 | * closed streams after it has sent a RST_STREAM |
| 2308 | * frame. An endpoint MAY choose to limit the period |
| 2309 | * over which it ignores frames and treat frames that |
| 2310 | * arrive after this time as being in error. |
| 2311 | */ |
| 2312 | if (!(h2s->flags & H2_SF_RST_SENT)) { |
| 2313 | /* RFC7540#5.1:closed: any frame other than |
| 2314 | * PRIO/WU/RST in this state MUST be treated as |
| 2315 | * a connection error |
| 2316 | */ |
| 2317 | if (h2c->dft != H2_FT_RST_STREAM && |
| 2318 | h2c->dft != H2_FT_PRIORITY && |
| 2319 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 2320 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 2321 | goto strm_err; |
| 2322 | } |
| 2323 | } |
| 2324 | } |
| 2325 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2326 | #if 0 |
| 2327 | // problem below: it is not possible to completely ignore such |
| 2328 | // streams as we need to maintain the compression state as well |
| 2329 | // and for this we need to completely process these frames (eg: |
| 2330 | // HEADERS frames) as well as counting DATA frames to emit |
| 2331 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 2332 | // This is a typical case of layer violation where the |
| 2333 | // transported contents are critical to the connection's |
| 2334 | // validity and must be ignored at the same time :-( |
| 2335 | |
| 2336 | /* graceful shutdown, ignore streams whose ID is higher than |
| 2337 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 2338 | */ |
| 2339 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2340 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2341 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 2342 | h2c->dfl -= ret; |
| 2343 | ret = h2c->dfl == 0; |
| 2344 | goto strm_err; |
| 2345 | } |
| 2346 | #endif |
| 2347 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2348 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 2349 | case H2_FT_SETTINGS: |
| 2350 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2351 | ret = h2c_handle_settings(h2c); |
| 2352 | |
| 2353 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2354 | ret = h2c_ack_settings(h2c); |
| 2355 | break; |
| 2356 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 2357 | case H2_FT_PING: |
| 2358 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2359 | ret = h2c_handle_ping(h2c); |
| 2360 | |
| 2361 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2362 | ret = h2c_ack_ping(h2c); |
| 2363 | break; |
| 2364 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 2365 | case H2_FT_WINDOW_UPDATE: |
| 2366 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2367 | ret = h2c_handle_window_update(h2c, h2s); |
| 2368 | break; |
| 2369 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2370 | case H2_FT_CONTINUATION: |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2371 | /* RFC7540#6.10: CONTINUATION may only be preceeded by |
| 2372 | * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These |
| 2373 | * frames' parsers consume all following CONTINUATION |
| 2374 | * frames so this one is out of sequence. |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2375 | */ |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 2376 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2377 | sess_log(h2c->conn->owner); |
| 2378 | goto fail; |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 2379 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2380 | case H2_FT_HEADERS: |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2381 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | c12f38f | 2018-10-08 14:53:27 +0200 | [diff] [blame] | 2382 | if (h2c->flags & H2_CF_IS_BACK) |
| 2383 | tmp_h2s = h2c_bck_handle_headers(h2c, h2s); |
| 2384 | else |
| 2385 | tmp_h2s = h2c_frt_handle_headers(h2c, h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2386 | if (tmp_h2s) { |
| 2387 | h2s = tmp_h2s; |
| 2388 | ret = 1; |
| 2389 | } |
| 2390 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2391 | break; |
| 2392 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2393 | case H2_FT_DATA: |
| 2394 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2395 | ret = h2c_frt_handle_data(h2c, h2s); |
| 2396 | |
| 2397 | if (h2c->st0 == H2_CS_FRAME_A) |
| 2398 | ret = h2c_send_strm_wu(h2c); |
| 2399 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2400 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 2401 | case H2_FT_PRIORITY: |
| 2402 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2403 | ret = h2c_handle_priority(h2c); |
| 2404 | break; |
| 2405 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 2406 | case H2_FT_RST_STREAM: |
| 2407 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2408 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 2409 | break; |
| 2410 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 2411 | case H2_FT_GOAWAY: |
| 2412 | if (h2c->st0 == H2_CS_FRAME_P) |
| 2413 | ret = h2c_handle_goaway(h2c); |
| 2414 | break; |
| 2415 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2416 | case H2_FT_PUSH_PROMISE: |
| 2417 | /* not permitted here, RFC7540#5.1 */ |
| 2418 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 22de8d3 | 2018-09-05 19:55:58 +0200 | [diff] [blame] | 2419 | if (!h2c->nb_streams) { |
| 2420 | /* only log if no other stream can report the error */ |
| 2421 | sess_log(h2c->conn->owner); |
| 2422 | } |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 2423 | break; |
| 2424 | |
| 2425 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2426 | default: |
| 2427 | /* drop frames that we ignore. They may be larger than |
| 2428 | * the buffer so we drain all of their contents until |
| 2429 | * we reach the end. |
| 2430 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2431 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 2432 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2433 | h2c->dfl -= ret; |
| 2434 | ret = h2c->dfl == 0; |
| 2435 | } |
| 2436 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2437 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2438 | /* We may have to send an RST if not done yet */ |
| 2439 | if (h2s->st == H2_SS_ERROR) |
| 2440 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2441 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2442 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2443 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2444 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2445 | /* error or missing data condition met above ? */ |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2446 | if (ret <= 0) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2447 | break; |
| 2448 | |
| 2449 | if (h2c->st0 != H2_CS_FRAME_H) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2450 | b_del(&h2c->dbuf, h2c->dfl); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2451 | h2c->st0 = H2_CS_FRAME_H; |
| 2452 | } |
| 2453 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2454 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2455 | if (h2c->rcvd_c > 0 && |
| 2456 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2457 | h2c_send_conn_wu(h2c); |
| 2458 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2459 | fail: |
| 2460 | /* we can go here on missing data, blocked response or error */ |
Willy Tarreau | 567beb8 | 2018-12-18 16:52:44 +0100 | [diff] [blame] | 2461 | if (h2s && h2s->cs && |
| 2462 | (b_data(&h2s->rxbuf) || |
| 2463 | (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS)))) { |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2464 | /* we may have to signal the upper layers */ |
| 2465 | h2s->cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2466 | h2s_notify_recv(h2s); |
Willy Tarreau | 2a761dc | 2018-02-26 18:50:57 +0100 | [diff] [blame] | 2467 | } |
Willy Tarreau | 1ed87b7 | 2018-11-25 08:45:16 +0100 | [diff] [blame] | 2468 | |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 2469 | h2c_restart_reading(h2c); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2470 | } |
| 2471 | |
| 2472 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2473 | * the end. |
| 2474 | */ |
| 2475 | static int h2_process_mux(struct h2c *h2c) |
| 2476 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2477 | struct h2s *h2s, *h2s_back; |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2478 | |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2479 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 2480 | if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) { |
| 2481 | if (unlikely(h2c_bck_send_preface(h2c) <= 0)) { |
| 2482 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 2483 | if (h2c->st0 == H2_CS_ERROR) { |
| 2484 | h2c->st0 = H2_CS_ERROR2; |
| 2485 | sess_log(h2c->conn->owner); |
| 2486 | } |
| 2487 | goto fail; |
| 2488 | } |
| 2489 | h2c->st0 = H2_CS_SETTINGS1; |
| 2490 | } |
| 2491 | /* need to wait for the other side */ |
Willy Tarreau | 75a930a | 2018-12-12 08:03:58 +0100 | [diff] [blame] | 2492 | if (h2c->st0 < H2_CS_FRAME_H) |
Willy Tarreau | 01b4482 | 2018-10-03 14:26:37 +0200 | [diff] [blame] | 2493 | return 1; |
| 2494 | } |
| 2495 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2496 | /* start by sending possibly pending window updates */ |
| 2497 | if (h2c->rcvd_c > 0 && |
| 2498 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2499 | h2c_send_conn_wu(h2c) < 0) |
| 2500 | goto fail; |
| 2501 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2502 | /* First we always process the flow control list because the streams |
| 2503 | * waiting there were already elected for immediate emission but were |
| 2504 | * blocked just on this. |
| 2505 | */ |
| 2506 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2507 | list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2508 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2509 | h2c->st0 >= H2_CS_ERROR) |
| 2510 | break; |
| 2511 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2512 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2513 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2514 | h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2515 | tasklet_wakeup(h2s->send_wait->task); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2516 | LIST_DEL(&h2s->list); |
| 2517 | LIST_INIT(&h2s->list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 2518 | LIST_ADDQ(&h2c->sending_list, &h2s->list); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2519 | } |
| 2520 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2521 | list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2522 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2523 | break; |
| 2524 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2525 | h2s->flags &= ~H2_SF_BLK_ANY; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2526 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2527 | h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2528 | tasklet_wakeup(h2s->send_wait->task); |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2529 | LIST_DEL(&h2s->list); |
| 2530 | LIST_INIT(&h2s->list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 2531 | LIST_ADDQ(&h2c->sending_list, &h2s->list); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2532 | } |
| 2533 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2534 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2535 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2536 | if (h2c->st0 == H2_CS_ERROR) { |
| 2537 | if (h2c->max_id >= 0) { |
| 2538 | h2c_send_goaway_error(h2c, NULL); |
| 2539 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2540 | return 0; |
| 2541 | } |
| 2542 | |
| 2543 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2544 | } |
| 2545 | return 1; |
| 2546 | } |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2547 | 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] | 2548 | } |
| 2549 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2550 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2551 | /* Attempt to read data, and subscribe if none available. |
| 2552 | * The function returns 1 if data has been received, otherwise zero. |
| 2553 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2554 | static int h2_recv(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2555 | { |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2556 | struct connection *conn = h2c->conn; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2557 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2558 | int max; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2559 | size_t ret; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2560 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2561 | if (h2c->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2562 | return (b_data(&h2c->dbuf)); |
Olivier Houchard | af4021e | 2018-08-09 13:06:55 +0200 | [diff] [blame] | 2563 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2564 | if (!h2_recv_allowed(h2c)) |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2565 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2566 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2567 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2568 | if (!buf) { |
| 2569 | h2c->flags |= H2_CF_DEM_DALLOC; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2570 | return 0; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2571 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2572 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2573 | do { |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 2574 | b_realign_if_empty(buf); |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2575 | if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
| 2576 | /* HTX in use : try to pre-align the buffer like the |
| 2577 | * rxbufs will be to optimize memory copies. We'll make |
| 2578 | * sure that the frame header lands at the end of the |
| 2579 | * HTX block to alias it upon recv. We cannot use the |
| 2580 | * head because rcv_buf() will realign the buffer if |
| 2581 | * it's empty. Thus we cheat and pretend we already |
| 2582 | * have a few bytes there. |
| 2583 | */ |
| 2584 | max = buf_room_for_htx_data(buf) + 9; |
Willy Tarreau | c0960d1 | 2018-12-14 10:59:15 +0100 | [diff] [blame] | 2585 | buf->head = sizeof(struct htx) - 9; |
Willy Tarreau | 2a59e87 | 2018-12-12 08:23:47 +0100 | [diff] [blame] | 2586 | } |
| 2587 | else |
| 2588 | max = b_room(buf); |
| 2589 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2590 | if (max) |
| 2591 | ret = conn->xprt->rcv_buf(conn, buf, max, 0); |
| 2592 | else |
| 2593 | ret = 0; |
| 2594 | } while (ret > 0); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2595 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2596 | if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size)) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2597 | conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h2c->wait_event); |
Olivier Houchard | 81a15af | 2018-10-19 17:26:49 +0200 | [diff] [blame] | 2598 | |
Olivier Houchard | a1411e6 | 2018-08-17 18:42:48 +0200 | [diff] [blame] | 2599 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2600 | h2_release_buf(h2c, &h2c->dbuf); |
Olivier Houchard | 4667773 | 2018-11-29 17:06:17 +0100 | [diff] [blame] | 2601 | return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn)); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2602 | } |
| 2603 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2604 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2605 | h2c->flags |= H2_CF_DEM_DFULL; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2606 | return 1; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2607 | } |
| 2608 | |
Willy Tarreau | 479998a | 2018-11-18 06:30:59 +0100 | [diff] [blame] | 2609 | /* Try to send data if possible. |
| 2610 | * The function returns 1 if data have been sent, otherwise zero. |
| 2611 | */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2612 | static int h2_send(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2613 | { |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2614 | struct connection *conn = h2c->conn; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2615 | int done; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2616 | int sent = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2617 | |
| 2618 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 2619 | return 1; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2620 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2621 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2622 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2623 | /* a handshake was requested */ |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2624 | goto schedule; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2625 | } |
| 2626 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2627 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2628 | * pending streams into the existing buffer until it's reportedly full |
| 2629 | * or the end of send requests is reached. Then it tries to send this |
| 2630 | * buffer's contents out, marks it not full if at least one byte could |
| 2631 | * be sent, and tries again. |
| 2632 | * |
| 2633 | * The snd_buf() function normally takes a "flags" argument which may |
| 2634 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2635 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2636 | * connection is streaming lots of data (used to increase TLS record |
| 2637 | * size at the expense of latency). The former can be sent any time |
| 2638 | * there's a buffer full flag, as it indicates at least one stream |
| 2639 | * attempted to send and failed so there are pending data. An |
| 2640 | * alternative would be to set it as long as there's an active stream |
| 2641 | * but that would be problematic for ACKs until we have an absolute |
| 2642 | * guarantee that all waiters have at least one byte to send. The |
| 2643 | * latter should possibly not be set for now. |
| 2644 | */ |
| 2645 | |
| 2646 | done = 0; |
| 2647 | while (!done) { |
| 2648 | unsigned int flags = 0; |
| 2649 | |
| 2650 | /* fill as much as we can into the current buffer */ |
| 2651 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2652 | done = h2_process_mux(h2c); |
| 2653 | |
| 2654 | if (conn->flags & CO_FL_ERROR) |
| 2655 | break; |
| 2656 | |
| 2657 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2658 | flags |= CO_SFL_MSG_MORE; |
| 2659 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2660 | if (b_data(&h2c->mbuf)) { |
| 2661 | 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] | 2662 | if (!ret) |
| 2663 | break; |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2664 | sent = 1; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2665 | b_del(&h2c->mbuf, ret); |
| 2666 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2667 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2668 | |
| 2669 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2670 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2671 | } |
| 2672 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2673 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2674 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2675 | b_reset(&h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2676 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2677 | /* We're not full anymore, so we can wake any task that are waiting |
| 2678 | * for us. |
| 2679 | */ |
| 2680 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 2681 | while (!LIST_ISEMPTY(&h2c->send_list)) { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2682 | struct h2s *h2s = LIST_ELEM(h2c->send_list.n, |
| 2683 | struct h2s *, list); |
| 2684 | LIST_DEL(&h2s->list); |
| 2685 | LIST_INIT(&h2s->list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 2686 | LIST_ADDQ(&h2c->sending_list, &h2s->list); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2687 | h2s->send_wait->events &= ~SUB_RETRY_SEND; |
| 2688 | h2s->send_wait->events |= SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 2689 | tasklet_wakeup(h2s->send_wait->task); |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 2690 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2691 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2692 | /* We're done, no more to send */ |
| 2693 | if (!b_data(&h2c->mbuf)) |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2694 | return sent; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2695 | schedule: |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2696 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
| 2697 | conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h2c->wait_event); |
Olivier Houchard | d4dd22d | 2018-08-17 18:39:46 +0200 | [diff] [blame] | 2698 | return sent; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2702 | { |
| 2703 | struct h2c *h2c = ctx; |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2704 | int ret = 0; |
Olivier Houchard | 29fb89d | 2018-08-02 18:56:36 +0200 | [diff] [blame] | 2705 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2706 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2707 | ret = h2_send(h2c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2708 | if (!(h2c->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2709 | ret |= h2_recv(h2c); |
Willy Tarreau | cef5c8e | 2018-12-18 10:29:54 +0100 | [diff] [blame] | 2710 | if (ret || b_data(&h2c->dbuf)) |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2711 | h2_process(h2c); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2712 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2713 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2714 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2715 | /* callback called on any event by the connection handler. |
| 2716 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2717 | * destruction of the connection (which normally doesn not happen in h2). |
| 2718 | */ |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2719 | static int h2_process(struct h2c *h2c) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2720 | { |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2721 | struct connection *conn = h2c->conn; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2722 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2723 | if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2724 | h2_process_demux(h2c); |
| 2725 | |
| 2726 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2727 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2728 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2729 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2730 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 2731 | } |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2732 | h2_send(h2c); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2733 | |
Willy Tarreau | 0b37d65 | 2018-10-03 10:33:02 +0200 | [diff] [blame] | 2734 | if (unlikely(h2c->proxy->state == PR_STSTOPPED)) { |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2735 | /* frontend is stopping, reload likely in progress, let's try |
| 2736 | * to announce a graceful shutdown if not yet done. We don't |
| 2737 | * care if it fails, it will be tried again later. |
| 2738 | */ |
| 2739 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 2740 | if (h2c->last_sid < 0) |
| 2741 | h2c->last_sid = (1U << 31) - 1; |
| 2742 | h2c_send_goaway_error(h2c, NULL); |
| 2743 | } |
| 2744 | } |
| 2745 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2746 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2747 | * If we received early data, and the handshake is done, wake |
| 2748 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2749 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2750 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 2751 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 2752 | struct eb32_node *node; |
| 2753 | struct h2s *h2s; |
| 2754 | |
| 2755 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 2756 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 2757 | |
| 2758 | while (node) { |
| 2759 | h2s = container_of(node, struct h2s, by_id); |
Willy Tarreau | fde287c | 2018-12-19 18:33:16 +0100 | [diff] [blame] | 2760 | if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
Willy Tarreau | 7e09445 | 2018-12-19 18:08:52 +0100 | [diff] [blame] | 2761 | h2s_notify_recv(h2s); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2762 | node = eb32_next(node); |
| 2763 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2764 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2765 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 2766 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 2767 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 2768 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 2769 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 2770 | h2_wake_some_streams(h2c, 0, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2771 | |
| 2772 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2773 | /* no more stream, kill the connection now */ |
| 2774 | h2_release(conn); |
| 2775 | return -1; |
| 2776 | } |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2777 | } |
| 2778 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2779 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2780 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2781 | |
Olivier Houchard | 53216e7 | 2018-10-10 15:46:36 +0200 | [diff] [blame] | 2782 | if ((conn->flags & CO_FL_SOCK_WR_SH) || |
| 2783 | h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 2784 | (h2c->st0 != H2_CS_ERROR && |
| 2785 | !b_data(&h2c->mbuf) && |
| 2786 | (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && |
| 2787 | ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list)))) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2788 | h2_release_buf(h2c, &h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2789 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2790 | if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2791 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2792 | 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] | 2793 | task_queue(h2c->task); |
| 2794 | } |
| 2795 | else |
| 2796 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2797 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2798 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 2799 | h2_send(h2c); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2800 | return 0; |
| 2801 | } |
| 2802 | |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2803 | static int h2_wake(struct connection *conn) |
| 2804 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2805 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 2806 | |
| 2807 | return (h2_process(h2c)); |
| 2808 | } |
| 2809 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2810 | /* Connection timeout management. The principle is that if there's no receipt |
| 2811 | * nor sending for a certain amount of time, the connection is closed. If the |
| 2812 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 2813 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 2814 | * GOAWAY frame. |
| 2815 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2816 | 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] | 2817 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2818 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2819 | int expired = tick_is_expired(t->expire, now_ms); |
| 2820 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2821 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2822 | return t; |
| 2823 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2824 | task_delete(t); |
| 2825 | task_free(t); |
| 2826 | |
| 2827 | if (!h2c) { |
| 2828 | /* resources were already deleted */ |
| 2829 | return NULL; |
| 2830 | } |
| 2831 | |
| 2832 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2833 | h2c_error(h2c, H2_ERR_NO_ERROR); |
| 2834 | h2_wake_some_streams(h2c, 0, 0); |
| 2835 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2836 | if (b_data(&h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2837 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 2838 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2839 | } |
| 2840 | |
| 2841 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2842 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2843 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 2844 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2845 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2846 | if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) { |
| 2847 | 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] | 2848 | if (ret > 0) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2849 | b_del(&h2c->mbuf, ret); |
| 2850 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2851 | } |
| 2852 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2853 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2854 | /* either we can release everything now or it will be done later once |
| 2855 | * the last stream closes. |
| 2856 | */ |
| 2857 | if (eb_is_empty(&h2c->streams_by_id)) |
| 2858 | h2_release(h2c->conn); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2859 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2860 | return NULL; |
| 2861 | } |
| 2862 | |
| 2863 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2864 | /*******************************************/ |
| 2865 | /* functions below are used by the streams */ |
| 2866 | /*******************************************/ |
| 2867 | |
| 2868 | /* |
| 2869 | * Attach a new stream to a connection |
| 2870 | * (Used for outgoing connections) |
| 2871 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2872 | static struct conn_stream *h2_attach(struct connection *conn, struct session *sess) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2873 | { |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2874 | struct conn_stream *cs; |
| 2875 | struct h2s *h2s; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2876 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2877 | |
| 2878 | cs = cs_new(conn); |
| 2879 | if (!cs) |
| 2880 | return NULL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2881 | h2s = h2c_bck_stream_new(h2c, cs, sess); |
Olivier Houchard | 7a57e8a | 2018-11-27 17:36:33 +0100 | [diff] [blame] | 2882 | if (!h2s) { |
| 2883 | cs_free(cs); |
| 2884 | return NULL; |
| 2885 | } |
| 2886 | return cs; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2887 | } |
| 2888 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 2889 | /* Retrieves the first valid conn_stream from this connection, or returns NULL. |
| 2890 | * We have to scan because we may have some orphan streams. It might be |
| 2891 | * beneficial to scan backwards from the end to reduce the likeliness to find |
| 2892 | * orphans. |
| 2893 | */ |
| 2894 | static const struct conn_stream *h2_get_first_cs(const struct connection *conn) |
| 2895 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2896 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 2897 | struct h2s *h2s; |
| 2898 | struct eb32_node *node; |
| 2899 | |
| 2900 | node = eb32_first(&h2c->streams_by_id); |
| 2901 | while (node) { |
| 2902 | h2s = container_of(node, struct h2s, by_id); |
| 2903 | if (h2s->cs) |
| 2904 | return h2s->cs; |
| 2905 | node = eb32_next(node); |
| 2906 | } |
| 2907 | return NULL; |
| 2908 | } |
| 2909 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2910 | /* |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 2911 | * Destroy the mux and the associated connection, if it is no longer used |
| 2912 | */ |
| 2913 | static void h2_destroy(struct connection *conn) |
| 2914 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2915 | struct h2c *h2c = conn->ctx; |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 2916 | |
| 2917 | if (eb_is_empty(&h2c->streams_by_id)) |
| 2918 | h2_release(h2c->conn); |
| 2919 | } |
| 2920 | |
| 2921 | /* |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2922 | * Detach the stream from the connection and possibly release the connection. |
| 2923 | */ |
| 2924 | static void h2_detach(struct conn_stream *cs) |
| 2925 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2926 | struct h2s *h2s = cs->ctx; |
| 2927 | struct h2c *h2c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2928 | struct session *sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2929 | |
| 2930 | cs->ctx = NULL; |
| 2931 | if (!h2s) |
| 2932 | return; |
| 2933 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2934 | sess = h2s->sess; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2935 | h2c = h2s->h2c; |
| 2936 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 2937 | h2c->nb_cs--; |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 2938 | if (h2c->flags & H2_CF_DEM_TOOMANY && |
| 2939 | !h2_has_too_many_cs(h2c)) { |
| 2940 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 2941 | h2c_restart_reading(h2c); |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 2942 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2943 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2944 | /* this stream may be blocked waiting for some data to leave (possibly |
| 2945 | * an ES or RST frame), so orphan it in this case. |
| 2946 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 2947 | if (!(cs->conn->flags & CO_FL_ERROR) && |
Willy Tarreau | a2b5181 | 2018-07-27 09:55:14 +0200 | [diff] [blame] | 2948 | (h2c->st0 < H2_CS_ERROR) && |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 2949 | (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] | 2950 | return; |
| 2951 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 2952 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 2953 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 2954 | /* unblock the connection if it was blocked on this |
| 2955 | * stream. |
| 2956 | */ |
| 2957 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 2958 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
Willy Tarreau | 47b515a | 2018-12-21 16:09:41 +0100 | [diff] [blame] | 2959 | h2c_restart_reading(h2c); |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 2960 | } |
| 2961 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 2962 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2963 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2964 | if (h2c->flags & H2_CF_IS_BACK && |
| 2965 | (h2c->proxy->options2 & PR_O2_USE_HTX)) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2966 | if (!(h2c->conn->flags & |
| 2967 | (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 2968 | if (!h2c->conn->owner) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2969 | h2c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 2970 | if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) { |
| 2971 | h2c->conn->owner = NULL; |
| 2972 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2973 | if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn)) |
| 2974 | /* The server doesn't want it, let's kill the connection right away */ |
| 2975 | h2c->conn->mux->destroy(h2c->conn); |
| 2976 | return; |
| 2977 | } |
| 2978 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2979 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 2980 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2981 | if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) |
| 2982 | /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */ |
| 2983 | return; |
| 2984 | } |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2985 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 2986 | if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 2987 | h2c->conn->flags |= CO_FL_PRIVATE; |
Olivier Houchard | 855ac25 | 2018-12-28 14:44:41 +0100 | [diff] [blame] | 2988 | if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2_settings_max_concurrent_streams) { |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2989 | struct server *srv = objt_server(h2c->conn->target); |
| 2990 | |
| 2991 | if (srv) { |
| 2992 | if (h2c->conn->flags & CO_FL_PRIVATE) |
| 2993 | LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list); |
| 2994 | else |
| 2995 | LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list); |
| 2996 | } |
| 2997 | |
| 2998 | } |
| 2999 | } |
| 3000 | } |
| 3001 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3002 | /* We don't want to close right now unless we're removing the |
| 3003 | * last stream, and either the connection is in error, or it |
| 3004 | * reached the ID already specified in a GOAWAY frame received |
| 3005 | * or sent (as seen by last_sid >= 0). |
| 3006 | */ |
| 3007 | if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */ |
| 3008 | ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */ |
Willy Tarreau | 42d55b9 | 2018-06-13 14:24:56 +0200 | [diff] [blame] | 3009 | (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */ |
Olivier Houchard | 52b9466 | 2018-10-21 03:01:20 +0200 | [diff] [blame] | 3010 | (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) || |
Olivier Houchard | 93c8852 | 2018-11-30 15:39:16 +0100 | [diff] [blame] | 3011 | (!(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] | 3012 | (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */ |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3013 | (conn_xprt_read0_pending(h2c->conn) || |
| 3014 | (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) { |
| 3015 | /* no more stream will come, kill it now */ |
| 3016 | h2_release(h2c->conn); |
| 3017 | } |
| 3018 | else if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3019 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3020 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 3021 | task_queue(h2c->task); |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 3022 | } |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 3023 | else |
| 3024 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 3025 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3026 | } |
| 3027 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3028 | static void h2_do_shutr(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3029 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3030 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3031 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3032 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 3033 | 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] | 3034 | return; |
| 3035 | |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3036 | /* if no outgoing data was seen on this stream, it means it was |
| 3037 | * closed with a "tcp-request content" rule that is normally |
| 3038 | * used to kill the connection ASAP (eg: limit abuse). In this |
| 3039 | * case we send a goaway to close the connection. |
| 3040 | */ |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3041 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3042 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3043 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3044 | |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3045 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && |
| 3046 | !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3047 | h2c_send_goaway_error(h2c, h2s) <= 0) |
| 3048 | return; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3049 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3050 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3051 | tasklet_wakeup(h2c->wait_event.task); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3052 | h2s_close(h2s); |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3053 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3054 | return; |
| 3055 | add_to_list: |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3056 | if (LIST_ISEMPTY(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3057 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3058 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3059 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3060 | h2s->send_wait = sw; |
| 3061 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3062 | h2s->send_wait = sw; |
| 3063 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3064 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3065 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3066 | /* Let the handler know we want shutr */ |
| 3067 | sw->handle = (void *)((long)sw->handle | 1); |
| 3068 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3069 | } |
| 3070 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3071 | static void h2_do_shutw(struct h2s *h2s) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3072 | { |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3073 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3074 | struct wait_event *sw = &h2s->wait_event; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3075 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 3076 | 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] | 3077 | return; |
| 3078 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3079 | if (h2s->flags & H2_SF_HEADERS_SENT) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3080 | /* we can cleanly close using an empty data frame only after headers */ |
| 3081 | |
| 3082 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 3083 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3084 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3085 | |
| 3086 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3087 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 3088 | else |
| 3089 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3090 | } else { |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3091 | /* if no outgoing data was seen on this stream, it means it was |
| 3092 | * closed with a "tcp-request content" rule that is normally |
| 3093 | * used to kill the connection ASAP (eg: limit abuse). In this |
| 3094 | * case we send a goaway to close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3095 | */ |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3096 | if (!(h2s->flags & H2_SF_RST_SENT) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3097 | h2s_send_rst_stream(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3098 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 3099 | |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 3100 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && |
| 3101 | !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) && |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3102 | h2c_send_goaway_error(h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3103 | goto add_to_list; |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 3104 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3105 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 3106 | } |
| 3107 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3108 | if (!(h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | 435ce2d | 2018-12-03 18:43:16 +0100 | [diff] [blame] | 3109 | tasklet_wakeup(h2c->wait_event.task); |
| 3110 | return; |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3111 | |
| 3112 | add_to_list: |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3113 | if (LIST_ISEMPTY(&h2s->list)) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3114 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3115 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 3116 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 3117 | h2s->send_wait = sw; |
| 3118 | } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) { |
| 3119 | h2s->send_wait = sw; |
| 3120 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 3121 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3122 | } |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3123 | /* let the handler know we want to shutw */ |
| 3124 | sw->handle = (void *)((long)(sw->handle) | 2); |
| 3125 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state) |
| 3129 | { |
| 3130 | struct h2s *h2s = ctx; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 3131 | long reason = (long)h2s->wait_event.handle; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3132 | |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3133 | if (h2s->send_wait) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3134 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3135 | h2s->send_wait = NULL; |
| 3136 | LIST_DEL(&h2s->list); |
| 3137 | LIST_INIT(&h2s->list); |
| 3138 | } |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3139 | if (reason & 2) |
| 3140 | h2_do_shutw(h2s); |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3141 | if (reason & 1) |
| 3142 | h2_do_shutr(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3143 | |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3144 | if (h2s->st == H2_SS_CLOSED && |
Olivier Houchard | ffda58b | 2018-12-16 01:29:11 +0100 | [diff] [blame] | 3145 | !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs) |
Olivier Houchard | 2c68a46 | 2018-12-15 22:42:20 +0100 | [diff] [blame] | 3146 | h2s_destroy(h2s); |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3147 | return NULL; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3148 | } |
| 3149 | |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 3150 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 3151 | { |
| 3152 | struct h2s *h2s = cs->ctx; |
| 3153 | |
| 3154 | if (!mode) |
| 3155 | return; |
| 3156 | |
| 3157 | h2_do_shutr(h2s); |
| 3158 | } |
| 3159 | |
| 3160 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 3161 | { |
| 3162 | struct h2s *h2s = cs->ctx; |
| 3163 | |
| 3164 | h2_do_shutw(h2s); |
| 3165 | } |
| 3166 | |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3167 | /* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3168 | * HTX request or response depending on the connection's side. Returns a |
| 3169 | * positive value on success, a negative value on failure, or 0 if it couldn't |
| 3170 | * proceed. May report connection errors in h2c->errcode if the frame is |
| 3171 | * non-decodable and the connection unrecoverable. In absence of connection |
| 3172 | * error when a failure is reported, the caller must assume a stream error. |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3173 | * |
| 3174 | * The function may fold CONTINUATION frames into the initial HEADERS frame |
| 3175 | * by removing padding and next frame header, then moving the CONTINUATION |
| 3176 | * frame's payload and adjusting h2c->dfl to match the new aggregated frame, |
| 3177 | * leaving a hole between the main frame and the beginning of the next one. |
| 3178 | * The possibly remaining incomplete or next frame at the end may be moved |
| 3179 | * if the aggregated frame is not deleted, in order to fill the hole. Wrapped |
| 3180 | * HEADERS frames are unwrapped into a temporary buffer before decoding. |
| 3181 | * |
| 3182 | * A buffer at the beginning of processing may look like this : |
| 3183 | * |
| 3184 | * ,---.---------.-----.--------------.--------------.------.---. |
| 3185 | * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///| |
| 3186 | * `---^---------^-----^--------------^--------------^------^---' |
| 3187 | * | | <-----> | | |
| 3188 | * area | dpl | wrap |
| 3189 | * |<--------------> | |
| 3190 | * | dfl | |
| 3191 | * |<-------------------------------------------------->| |
| 3192 | * head data |
| 3193 | * |
| 3194 | * Padding is automatically overwritten when folding, participating to the |
| 3195 | * hole size after dfl : |
| 3196 | * |
| 3197 | * ,---.------------------------.-----.--------------.------.---. |
| 3198 | * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///| |
| 3199 | * `---^------------------------^-----^--------------^------^---' |
| 3200 | * | | <-----> | | |
| 3201 | * area | hole | wrap |
| 3202 | * |<-----------------------> | |
| 3203 | * | dfl | |
| 3204 | * |<-------------------------------------------------->| |
| 3205 | * head data |
| 3206 | * |
| 3207 | * Please note that the HEADERS frame is always deprived from its PADLEN byte |
| 3208 | * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY |
| 3209 | * bit. |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3210 | * |
| 3211 | * The <flags> field must point to either the stream's flags or to a copy of it |
| 3212 | * so that the function can update the following flags : |
| 3213 | * - H2_SF_DATA_CLEN when content-length is seen |
| 3214 | * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion |
| 3215 | * - H2_SF_HEADERS_RCVD once the frame is successfully decoded |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3216 | * |
| 3217 | * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to |
| 3218 | * decoding, in order to detect if we're dealing with a headers or a trailers |
| 3219 | * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen). |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3220 | */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3221 | static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3222 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3223 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3224 | struct buffer *tmp = get_trash_chunk(); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3225 | struct http_hdr list[MAX_HTTP_HDR * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3226 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3227 | unsigned int msgf; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3228 | struct htx *htx = NULL; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3229 | int flen; // header frame len |
| 3230 | int hole = 0; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3231 | int ret = 0; |
| 3232 | int outlen; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3233 | int wrap; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3234 | int try = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3235 | |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3236 | next_frame: |
| 3237 | if (b_data(&h2c->dbuf) - hole < h2c->dfl) |
| 3238 | goto leave; // incomplete input frame |
| 3239 | |
| 3240 | /* No END_HEADERS means there's one or more CONTINUATION frames. In |
| 3241 | * this case, we'll try to paste it immediately after the initial |
| 3242 | * HEADERS frame payload and kill any possible padding. The initial |
| 3243 | * frame's length will be increased to represent the concatenation |
| 3244 | * of the two frames. The next frame is read from position <tlen> |
| 3245 | * and written at position <flen> (minus padding if some is present). |
| 3246 | */ |
| 3247 | if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) { |
| 3248 | struct h2_fh hdr; |
| 3249 | int clen; // CONTINUATION frame's payload length |
| 3250 | |
| 3251 | if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) { |
| 3252 | /* no more data, the buffer may be full, either due to |
| 3253 | * too large a frame or because of too large a hole that |
| 3254 | * we're going to compact at the end. |
| 3255 | */ |
| 3256 | goto leave; |
| 3257 | } |
| 3258 | |
| 3259 | if (hdr.ft != H2_FT_CONTINUATION) { |
| 3260 | /* RFC7540#6.10: frame of unexpected type */ |
| 3261 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3262 | goto fail; |
| 3263 | } |
| 3264 | |
| 3265 | if (hdr.sid != h2c->dsi) { |
| 3266 | /* RFC7540#6.10: frame of different stream */ |
| 3267 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3268 | goto fail; |
| 3269 | } |
| 3270 | |
| 3271 | if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { |
| 3272 | /* RFC7540#4.2: invalid frame length */ |
| 3273 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3274 | goto fail; |
| 3275 | } |
| 3276 | |
| 3277 | /* detect when we must stop aggragating frames */ |
| 3278 | h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS; |
| 3279 | |
| 3280 | /* Take as much as we can of the CONTINUATION frame's payload */ |
| 3281 | clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9); |
| 3282 | if (clen > hdr.len) |
| 3283 | clen = hdr.len; |
| 3284 | |
| 3285 | /* Move the frame's payload over the padding, hole and frame |
| 3286 | * header. At least one of hole or dpl is null (see diagrams |
| 3287 | * above). The hole moves after the new aggragated frame. |
| 3288 | */ |
| 3289 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9)); |
| 3290 | h2c->dfl += clen - h2c->dpl; |
| 3291 | hole += h2c->dpl + 9; |
| 3292 | h2c->dpl = 0; |
| 3293 | goto next_frame; |
| 3294 | } |
| 3295 | |
| 3296 | flen = h2c->dfl - h2c->dpl; |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 3297 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3298 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3299 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3300 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3301 | copy = alloc_trash_chunk(); |
| 3302 | if (!copy) { |
| 3303 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 3304 | goto fail; |
| 3305 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3306 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 3307 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 3308 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3309 | } |
| 3310 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3311 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 3312 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3313 | if (read_n32(hdrs) == h2c->dsi) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3314 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 3315 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | a0d11b6 | 2018-09-05 18:30:05 +0200 | [diff] [blame] | 3316 | goto fail; |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 3317 | } |
| 3318 | |
Willy Tarreau | a01f45e | 2018-12-31 07:41:24 +0100 | [diff] [blame] | 3319 | if (flen < 5) { |
| 3320 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 3321 | goto fail; |
| 3322 | } |
| 3323 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3324 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3325 | flen -= 5; |
| 3326 | } |
| 3327 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3328 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3329 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3330 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3331 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3332 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3333 | /* we can't retry a failed decompression operation so we must be very |
| 3334 | * careful not to take any risks. In practice the output buffer is |
| 3335 | * always empty except maybe for trailers, in which case we simply have |
| 3336 | * to wait for the upper layer to finish consuming what is available. |
| 3337 | */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3338 | |
| 3339 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3340 | htx = htx_from_buf(rxbuf); |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3341 | if (!htx_is_empty(htx)) { |
| 3342 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3343 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3344 | } |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3345 | } else { |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3346 | if (b_data(rxbuf)) { |
| 3347 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3348 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3349 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3350 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3351 | rxbuf->head = 0; |
| 3352 | try = b_size(rxbuf); |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3353 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3354 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3355 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3356 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3357 | sizeof(list)/sizeof(list[0]), tmp); |
| 3358 | if (outlen < 0) { |
| 3359 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3360 | goto fail; |
| 3361 | } |
| 3362 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3363 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3364 | * the parser's state to commit these changes and allow us to later |
| 3365 | * fail solely on the stream if needed. |
| 3366 | */ |
| 3367 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3368 | h2c->dfl = hole = 0; |
| 3369 | h2c->st0 = H2_CS_FRAME_H; |
| 3370 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3371 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3372 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3373 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3374 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3375 | goto trailers; |
| 3376 | |
| 3377 | /* This is the first HEADERS frame so it's a headers block */ |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3378 | if (htx) { |
| 3379 | /* HTX mode */ |
| 3380 | if (h2c->flags & H2_CF_IS_BACK) |
| 3381 | outlen = h2_make_htx_response(list, htx, &msgf); |
| 3382 | else |
| 3383 | outlen = h2_make_htx_request(list, htx, &msgf); |
| 3384 | } else { |
| 3385 | /* HTTP/1 mode */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3386 | outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf); |
Willy Tarreau | 8319593 | 2019-01-03 10:26:23 +0100 | [diff] [blame] | 3387 | if (outlen > 0) |
| 3388 | b_add(rxbuf, outlen); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3389 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3390 | |
| 3391 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3392 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3393 | goto fail; |
| 3394 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3395 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3396 | if (msgf & H2_MSGF_BODY) { |
| 3397 | /* a payload is present */ |
| 3398 | if (msgf & H2_MSGF_BODY_CL) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3399 | *flags |= H2_SF_DATA_CLEN; |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3400 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3401 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3402 | } |
| 3403 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3404 | done: |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3405 | /* indicate that a HEADERS frame was received for this stream */ |
| 3406 | *flags |= H2_SF_HEADERS_RCVD; |
| 3407 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3408 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 3409 | /* Mark the end of message, either using EOM in HTX or with the |
| 3410 | * trailing CRLF after the end of trailers. Note that DATA_CHNK |
| 3411 | * is not set during headers with END_STREAM. |
| 3412 | */ |
| 3413 | if (htx) { |
| 3414 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 3415 | goto fail; |
| 3416 | } |
| 3417 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3418 | if (!b_putblk(rxbuf, "\r\n", 2)) |
| 3419 | goto fail; |
| 3420 | } |
| 3421 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3422 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3423 | /* success */ |
| 3424 | ret = 1; |
| 3425 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3426 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3427 | /* If there is a hole left and it's not at the end, we are forced to |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3428 | * move the remaining data over it. |
| 3429 | */ |
| 3430 | if (hole) { |
| 3431 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3432 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3433 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3434 | b_sub(&h2c->dbuf, hole); |
| 3435 | } |
| 3436 | |
| 3437 | if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) { |
| 3438 | /* too large frames */ |
| 3439 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3440 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3441 | } |
| 3442 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3443 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3444 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3445 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3446 | return ret; |
| 3447 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3448 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3449 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3450 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3451 | |
| 3452 | trailers: |
| 3453 | /* This is the last HEADERS frame hence a trailer */ |
| 3454 | |
| 3455 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3456 | /* It's a trailer but it's missing ES flag */ |
| 3457 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3458 | goto fail; |
| 3459 | } |
| 3460 | |
| 3461 | /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD |
| 3462 | * block, and when using chunks we must send the 0 CRLF marker. For |
| 3463 | * other modes, the trailers are silently dropped. |
| 3464 | */ |
| 3465 | if (htx) { |
| 3466 | if (!htx_add_endof(htx, HTX_BLK_EOD)) |
| 3467 | goto fail; |
Willy Tarreau | 5255f28 | 2019-01-03 18:41:05 +0100 | [diff] [blame] | 3468 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3469 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3470 | } |
| 3471 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3472 | /* Legacy mode with chunked encoding : we must finalize the |
| 3473 | * data block message emit the trailing CRLF */ |
| 3474 | if (!b_putblk(rxbuf, "0\r\n", 3)) |
| 3475 | goto fail; |
Willy Tarreau | e2b05cc | 2019-01-03 16:18:34 +0100 | [diff] [blame] | 3476 | |
| 3477 | outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try); |
| 3478 | if (outlen > 0) |
| 3479 | b_add(rxbuf, outlen); |
| 3480 | else |
| 3481 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3482 | } |
| 3483 | |
| 3484 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3485 | } |
| 3486 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3487 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 3488 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 3489 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 3490 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 3491 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 3492 | * 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] | 3493 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3494 | * send the current frame, 0 if it couldn't complete, in which case |
| 3495 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3496 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3497 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3498 | * have checked the frame header and ensured that the frame was complete or the |
| 3499 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3500 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3501 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3502 | { |
| 3503 | struct h2c *h2c = h2s->h2c; |
| 3504 | int block1, block2; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3505 | unsigned int flen = 0; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3506 | unsigned int chklen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3507 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3508 | struct buffer *csbuf; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3509 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3510 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3511 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3512 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3513 | if (!csbuf) { |
| 3514 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3515 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3516 | } |
| 3517 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3518 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3519 | flen = h2c->dfl - h2c->dpl; |
Olivier Houchard | 2f30883 | 2018-12-19 15:53:53 +0100 | [diff] [blame] | 3520 | if (h2c->proxy->options2 & PR_O2_USE_HTX) |
| 3521 | htx = htx_from_buf(csbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3522 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3523 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3524 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3525 | if (flen > b_data(&h2c->dbuf)) { |
| 3526 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3527 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3528 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3529 | } |
| 3530 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3531 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3532 | block1 = htx_free_data_space(htx); |
| 3533 | if (!block1) { |
| 3534 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3535 | goto fail; |
| 3536 | } |
| 3537 | if (flen > block1) |
| 3538 | flen = block1; |
| 3539 | |
| 3540 | /* here, flen is the max we can copy into the output buffer */ |
| 3541 | block1 = b_contig_data(&h2c->dbuf, 0); |
| 3542 | if (flen > block1) |
| 3543 | flen = block1; |
| 3544 | |
| 3545 | if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) { |
| 3546 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3547 | goto fail; |
| 3548 | } |
| 3549 | |
| 3550 | b_del(&h2c->dbuf, flen); |
| 3551 | h2c->dfl -= flen; |
| 3552 | h2c->rcvd_c += flen; |
| 3553 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 3554 | goto try_again; |
| 3555 | } |
| 3556 | else if (unlikely(b_space_wraps(csbuf))) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3557 | /* it doesn't fit and the buffer is fragmented, |
| 3558 | * so let's defragment it and try again. |
| 3559 | */ |
| 3560 | b_slow_realign(csbuf, trash.area, 0); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3561 | } |
| 3562 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3563 | /* chunked-encoding requires more room */ |
| 3564 | if (h2s->flags & H2_SF_DATA_CHNK) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3565 | chklen = MIN(flen, b_room(csbuf)); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3566 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 3567 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 3568 | (chklen < 1048576) ? 4 : 8; |
| 3569 | chklen += 4; // CRLF, CRLF |
| 3570 | } |
| 3571 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3572 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3573 | if (flen + chklen > b_room(csbuf)) { |
| 3574 | if (chklen >= b_room(csbuf)) { |
| 3575 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3576 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3577 | } |
| 3578 | flen = b_room(csbuf) - chklen; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3579 | } |
| 3580 | |
| 3581 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3582 | /* emit the chunk size */ |
| 3583 | unsigned int chksz = flen; |
| 3584 | char str[10]; |
| 3585 | char *beg; |
| 3586 | |
| 3587 | beg = str + sizeof(str); |
| 3588 | *--beg = '\n'; |
| 3589 | *--beg = '\r'; |
| 3590 | do { |
| 3591 | *--beg = hextab[chksz & 0xF]; |
| 3592 | } while (chksz >>= 4); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3593 | b_putblk(csbuf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3594 | } |
| 3595 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3596 | /* Block1 is the length of the first block before the buffer wraps, |
| 3597 | * block2 is the optional second block to reach the end of the frame. |
| 3598 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3599 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3600 | if (block1 > flen) |
| 3601 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3602 | block2 = flen - block1; |
| 3603 | |
| 3604 | if (block1) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3605 | b_putblk(csbuf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3606 | |
| 3607 | if (block2) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3608 | b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3609 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3610 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3611 | /* emit the CRLF */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3612 | b_putblk(csbuf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3613 | } |
| 3614 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3615 | /* now mark the input data as consumed (will be deleted from the buffer |
| 3616 | * by the caller when seeing FRAME_A after sending the window update). |
| 3617 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3618 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3619 | h2c->dfl -= flen; |
| 3620 | h2c->rcvd_c += flen; |
| 3621 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 3622 | |
| 3623 | if (h2c->dfl > h2c->dpl) { |
| 3624 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3625 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3626 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3627 | } |
| 3628 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3629 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3630 | /* here we're done with the frame, all the payload (except padding) was |
| 3631 | * transferred. |
| 3632 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3633 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3634 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 3635 | if (htx) { |
| 3636 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3637 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3638 | goto fail; |
| 3639 | } |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3640 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3641 | else if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3642 | /* emit the trailing 0 CRLF CRLF */ |
| 3643 | if (b_room(csbuf) < 5) { |
| 3644 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3645 | goto fail; |
| 3646 | } |
| 3647 | chklen += 5; |
| 3648 | b_putblk(csbuf, "0\r\n\r\n", 5); |
| 3649 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3650 | } |
| 3651 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 3652 | h2c->rcvd_c += h2c->dpl; |
| 3653 | h2c->rcvd_s += h2c->dpl; |
| 3654 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3655 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
| 3656 | |
Willy Tarreau | 39d6850 | 2018-03-02 12:26:37 +0100 | [diff] [blame] | 3657 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3658 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 39d6850 | 2018-03-02 12:26:37 +0100 | [diff] [blame] | 3659 | h2s->cs->flags |= CS_FL_REOS; |
| 3660 | } |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3661 | if (htx) |
| 3662 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3663 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3664 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3665 | if (htx) |
| 3666 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3667 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3668 | } |
| 3669 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3670 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 3671 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 3672 | * number of bytes sent. The caller must check the stream's status to detect |
| 3673 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3674 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3675 | 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] | 3676 | { |
| 3677 | struct http_hdr list[MAX_HTTP_HDR]; |
| 3678 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3679 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3680 | struct buffer outbuf; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3681 | union h1_sl sl; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3682 | int es_now = 0; |
| 3683 | int ret = 0; |
| 3684 | int hdr; |
| 3685 | |
| 3686 | if (h2c_mux_busy(h2c, h2s)) { |
| 3687 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3688 | return 0; |
| 3689 | } |
| 3690 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3691 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3692 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3693 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3694 | return 0; |
| 3695 | } |
| 3696 | |
| 3697 | /* First, try to parse the H1 response and index it into <list>. |
| 3698 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 3699 | * block does not wrap and we can safely read it this way without |
| 3700 | * having to realign the buffer. |
| 3701 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3702 | 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] | 3703 | list, sizeof(list)/sizeof(list[0]), h1m, &sl); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3704 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 3705 | /* incomplete or invalid response, this is abnormal coming from |
| 3706 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 3707 | * so that won't be fixed, raise an error now. |
| 3708 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3709 | * FIXME: we should instead add the ability to only return a |
| 3710 | * 502 bad gateway. But in theory this is not supposed to |
| 3711 | * happen. |
| 3712 | */ |
| 3713 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3714 | ret = 0; |
| 3715 | goto end; |
| 3716 | } |
| 3717 | |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3718 | h2s->status = sl.st.status; |
Willy Tarreau | db72da0 | 2018-09-13 11:52:20 +0200 | [diff] [blame] | 3719 | |
| 3720 | /* certain statuses have no body or an empty one, regardless of |
| 3721 | * what the headers say. |
| 3722 | */ |
| 3723 | if (sl.st.status >= 100 && sl.st.status < 200) { |
| 3724 | h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK); |
| 3725 | h1m->curr_len = h1m->body_len = 0; |
| 3726 | } |
| 3727 | else if (sl.st.status == 204 || sl.st.status == 304) { |
| 3728 | /* no contents, claim c-len is present and set to zero */ |
| 3729 | h1m->flags &= ~H1_MF_CHNK; |
| 3730 | h1m->flags |= H1_MF_CLEN; |
| 3731 | h1m->curr_len = h1m->body_len = 0; |
| 3732 | } |
| 3733 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3734 | chunk_reset(&outbuf); |
| 3735 | |
| 3736 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3737 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3738 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3739 | outbuf.data = 0; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3740 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3741 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3742 | break; |
| 3743 | realign_again: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3744 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3745 | } |
| 3746 | |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3747 | if (outbuf.size < 9) |
| 3748 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3749 | |
| 3750 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3751 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 3752 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3753 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3754 | |
| 3755 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3756 | if (unlikely(list[0].v.len != 3)) { |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 3757 | /* this is an unparsable response */ |
| 3758 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3759 | ret = 0; |
| 3760 | goto end; |
| 3761 | } |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3762 | |
| 3763 | if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3764 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3765 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3766 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3767 | } |
| 3768 | |
| 3769 | /* encode all headers, stop at empty name */ |
| 3770 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 3771 | /* these ones do not exist in H2 and must be dropped. */ |
| 3772 | if (isteq(list[hdr].n, ist("connection")) || |
| 3773 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 3774 | isteq(list[hdr].n, ist("keep-alive")) || |
| 3775 | isteq(list[hdr].n, ist("upgrade")) || |
| 3776 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3777 | continue; |
| 3778 | |
| 3779 | if (isteq(list[hdr].n, ist(""))) |
| 3780 | break; // end |
| 3781 | |
| 3782 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 3783 | /* output full */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3784 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3785 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3786 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3787 | } |
| 3788 | } |
| 3789 | |
| 3790 | /* we may need to add END_STREAM */ |
| 3791 | if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW) |
| 3792 | es_now = 1; |
| 3793 | |
| 3794 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3795 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3796 | |
| 3797 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3798 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3799 | |
| 3800 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3801 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3802 | |
| 3803 | /* commit the H2 response */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3804 | b_add(&h2c->mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3805 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3806 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3807 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3808 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3809 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3810 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3811 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3812 | h2s->flags |= H2_SF_ES_SENT; |
| 3813 | if (h2s->st == H2_SS_OPEN) |
| 3814 | h2s->st = H2_SS_HLOC; |
| 3815 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3816 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3817 | } |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3818 | else if (h2s->status >= 100 && h2s->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3819 | /* 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] | 3820 | h1m_init_res(h1m); |
Willy Tarreau | 9b8cd1f | 2018-09-12 09:24:38 +0200 | [diff] [blame] | 3821 | 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] | 3822 | h2s->h1m.flags |= H1_MF_TOLOWER; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3823 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 3824 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 3825 | |
| 3826 | /* 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] | 3827 | |
| 3828 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 3829 | //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] | 3830 | return ret; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3831 | full: |
| 3832 | h1m_init_res(h1m); |
| 3833 | h1m->err_pos = -1; // don't care about errors on the response path |
| 3834 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3835 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3836 | ret = 0; |
| 3837 | goto end; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3838 | } |
| 3839 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3840 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 3841 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 3842 | * the number of bytes sent. The caller must check the stream's status to |
| 3843 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3844 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3845 | 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] | 3846 | { |
| 3847 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3848 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3849 | struct buffer outbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3850 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 3851 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3852 | int es_now = 0; |
| 3853 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3854 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3855 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3856 | |
| 3857 | if (h2c_mux_busy(h2c, h2s)) { |
| 3858 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3859 | goto end; |
| 3860 | } |
| 3861 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3862 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3863 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3864 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3865 | goto end; |
| 3866 | } |
| 3867 | |
| 3868 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3869 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3870 | goto end; |
| 3871 | |
| 3872 | chunk_reset(&outbuf); |
| 3873 | |
| 3874 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3875 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3876 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3877 | outbuf.data = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3878 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3879 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3880 | break; |
| 3881 | realign_again: |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 3882 | /* If there are pending data in the output buffer, and we have |
| 3883 | * less than 1/4 of the mbuf's size and everything fits, we'll |
| 3884 | * still perform a copy anyway. Otherwise we'll pretend the mbuf |
| 3885 | * is full and wait, to save some slow realign calls. |
| 3886 | */ |
| 3887 | if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) { |
| 3888 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3889 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3890 | goto end; |
| 3891 | } |
| 3892 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3893 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3894 | } |
| 3895 | |
| 3896 | if (outbuf.size < 9) { |
| 3897 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3898 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3899 | goto end; |
| 3900 | } |
| 3901 | |
| 3902 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3903 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 3904 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3905 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3906 | |
| 3907 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 3908 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3909 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 3910 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3911 | break; |
| 3912 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3913 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3914 | if ((long long)size > h1m->curr_len) |
| 3915 | size = h1m->curr_len; |
| 3916 | break; |
| 3917 | default: /* te:chunked : parse chunks */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3918 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 3919 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3920 | if (!ret) |
| 3921 | goto end; |
| 3922 | |
| 3923 | if (ret < 0) { |
| 3924 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 3925 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3926 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3927 | goto end; |
| 3928 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3929 | max -= ret; |
| 3930 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3931 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3932 | h1m->state = H1_MSG_CHUNK_SIZE; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3933 | } |
| 3934 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3935 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3936 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 3937 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3938 | if (!ret) |
| 3939 | goto end; |
| 3940 | |
| 3941 | if (ret < 0) { |
| 3942 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 3943 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3944 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3945 | goto end; |
| 3946 | } |
| 3947 | |
| 3948 | size = chunk; |
| 3949 | h1m->curr_len = chunk; |
| 3950 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3951 | max -= ret; |
| 3952 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3953 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3954 | h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3955 | if (!size) |
| 3956 | goto send_empty; |
| 3957 | } |
| 3958 | |
| 3959 | /* in MSG_DATA state, continue below */ |
| 3960 | size = h1m->curr_len; |
| 3961 | break; |
| 3962 | } |
| 3963 | |
| 3964 | /* we have in <size> the exact number of bytes we need to copy from |
| 3965 | * the H1 buffer. We need to check this against the connection's and |
| 3966 | * the stream's send windows, and to ensure that this fits in the max |
| 3967 | * frame size and in the buffer's available space minus 9 bytes (for |
| 3968 | * the frame header). The connection's flow control is applied last so |
| 3969 | * that we can use a separate list of streams which are immediately |
| 3970 | * unblocked on window opening. Note: we don't implement padding. |
| 3971 | */ |
| 3972 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3973 | if (size > max) |
| 3974 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3975 | |
| 3976 | if (size > h2s->mws) |
| 3977 | size = h2s->mws; |
| 3978 | |
| 3979 | if (size <= 0) { |
| 3980 | h2s->flags |= H2_SF_BLK_SFCTL; |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 3981 | if (h2s->send_wait) { |
| 3982 | LIST_DEL(&h2s->list); |
| 3983 | LIST_INIT(&h2s->list); |
| 3984 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3985 | goto end; |
| 3986 | } |
| 3987 | |
| 3988 | if (h2c->mfs && size > h2c->mfs) |
| 3989 | size = h2c->mfs; |
| 3990 | |
| 3991 | if (size + 9 > outbuf.size) { |
| 3992 | /* we have an opportunity for enlarging the too small |
| 3993 | * available space, let's try. |
| 3994 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3995 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3996 | goto realign_again; |
| 3997 | size = outbuf.size - 9; |
| 3998 | } |
| 3999 | |
| 4000 | if (size <= 0) { |
| 4001 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4002 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4003 | goto end; |
| 4004 | } |
| 4005 | |
| 4006 | if (size > h2c->mws) |
| 4007 | size = h2c->mws; |
| 4008 | |
| 4009 | if (size <= 0) { |
| 4010 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4011 | goto end; |
| 4012 | } |
| 4013 | |
| 4014 | /* copy whatever we can */ |
| 4015 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4016 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4017 | if (ret == 1) |
| 4018 | len2 = 0; |
| 4019 | |
| 4020 | if (!ret || len1 + len2 < size) { |
| 4021 | /* FIXME: must normally never happen */ |
| 4022 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4023 | goto end; |
| 4024 | } |
| 4025 | |
| 4026 | /* limit len1/len2 to size */ |
| 4027 | if (len1 + len2 > size) { |
| 4028 | int sub = len1 + len2 - size; |
| 4029 | |
| 4030 | if (len2 > sub) |
| 4031 | len2 -= sub; |
| 4032 | else { |
| 4033 | sub -= len2; |
| 4034 | len2 = 0; |
| 4035 | len1 -= sub; |
| 4036 | } |
| 4037 | } |
| 4038 | |
| 4039 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4040 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4041 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4042 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4043 | |
| 4044 | send_empty: |
| 4045 | /* we may need to add END_STREAM */ |
| 4046 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 4047 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4048 | * |
| 4049 | * FIXME: what we do here is not correct because we send end_stream |
| 4050 | * before knowing if we'll have to send a HEADERS frame for the |
| 4051 | * trailers. More importantly we're not consuming the trailing CRLF |
| 4052 | * after the end of trailers, so it will be left to the caller to |
| 4053 | * eat it. The right way to do it would be to measure trailers here |
| 4054 | * and to send ES only if there are no trailers. |
| 4055 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4056 | */ |
| 4057 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4058 | !h1m->curr_len || h1m->state >= H1_MSG_DONE) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4059 | es_now = 1; |
| 4060 | |
| 4061 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4062 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4063 | |
| 4064 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4065 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4066 | |
| 4067 | /* commit the H2 response */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4068 | b_add(&h2c->mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4069 | |
| 4070 | /* consume incoming H1 response */ |
| 4071 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4072 | max -= size; |
| 4073 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4074 | total += size; |
| 4075 | h1m->curr_len -= size; |
| 4076 | h2s->mws -= size; |
| 4077 | h2c->mws -= size; |
| 4078 | |
| 4079 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4080 | h1m->state = H1_MSG_CHUNK_CRLF; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4081 | goto new_frame; |
| 4082 | } |
| 4083 | } |
| 4084 | |
| 4085 | if (es_now) { |
| 4086 | if (h2s->st == H2_SS_OPEN) |
| 4087 | h2s->st = H2_SS_HLOC; |
| 4088 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4089 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4090 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4091 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 4092 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4093 | total += max; |
| 4094 | ofs += max; |
| 4095 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4096 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4097 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4098 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4099 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4100 | h2s->flags |= H2_SF_ES_SENT; |
| 4101 | } |
| 4102 | |
| 4103 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4104 | 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] | 4105 | return total; |
| 4106 | } |
| 4107 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4108 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 4109 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4110 | * must check the stream's status to detect any error which might have happened |
| 4111 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4112 | * from the message. The htx message is assumed to be valid since produced from |
| 4113 | * the internal code, hence it contains a start line, an optional series of |
| 4114 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4115 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4116 | */ |
| 4117 | static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
| 4118 | { |
| 4119 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4120 | struct h2c *h2c = h2s->h2c; |
| 4121 | struct htx_blk *blk; |
| 4122 | struct htx_blk *blk_end; |
| 4123 | struct buffer outbuf; |
| 4124 | struct htx_sl *sl; |
| 4125 | enum htx_blk_type type; |
| 4126 | int es_now = 0; |
| 4127 | int ret = 0; |
| 4128 | int hdr; |
| 4129 | int idx; |
| 4130 | |
| 4131 | if (h2c_mux_busy(h2c, h2s)) { |
| 4132 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4133 | return 0; |
| 4134 | } |
| 4135 | |
| 4136 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4137 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4138 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4139 | return 0; |
| 4140 | } |
| 4141 | |
| 4142 | /* determine the first block which must not be deleted, blk_end may |
| 4143 | * be NULL if all blocks have to be deleted. |
| 4144 | */ |
| 4145 | idx = htx_get_head(htx); |
| 4146 | blk_end = NULL; |
| 4147 | while (idx != -1) { |
| 4148 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4149 | idx = htx_get_next(htx, idx); |
| 4150 | if (type == HTX_BLK_EOH) { |
| 4151 | if (idx != -1) |
| 4152 | blk_end = htx_get_blk(htx, idx); |
| 4153 | break; |
| 4154 | } |
| 4155 | } |
| 4156 | |
| 4157 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4158 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4159 | ALREADY_CHECKED(sl); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4160 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4161 | if (h2s->status < 100 || h2s->status > 999) |
| 4162 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4163 | |
| 4164 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4165 | hdr = 0; |
| 4166 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4167 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4168 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4169 | blk = htx_get_blk(htx, idx); |
| 4170 | type = htx_get_blk_type(blk); |
| 4171 | |
| 4172 | if (type == HTX_BLK_UNUSED) |
| 4173 | continue; |
| 4174 | |
| 4175 | if (type != HTX_BLK_HDR) |
| 4176 | break; |
| 4177 | |
| 4178 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4179 | goto fail; |
| 4180 | |
| 4181 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4182 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4183 | hdr++; |
| 4184 | } |
| 4185 | |
| 4186 | /* marker for end of headers */ |
| 4187 | list[hdr].n = ist(""); |
| 4188 | |
| 4189 | if (h2s->status == 204 || h2s->status == 304) { |
| 4190 | /* no contents, claim c-len is present and set to zero */ |
| 4191 | es_now = 1; |
| 4192 | } |
| 4193 | |
| 4194 | chunk_reset(&outbuf); |
| 4195 | |
| 4196 | while (1) { |
| 4197 | outbuf.area = b_tail(&h2c->mbuf); |
| 4198 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4199 | outbuf.data = 0; |
| 4200 | |
| 4201 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4202 | break; |
| 4203 | realign_again: |
| 4204 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4205 | } |
| 4206 | |
| 4207 | if (outbuf.size < 9) |
| 4208 | goto full; |
| 4209 | |
| 4210 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4211 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4212 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4213 | outbuf.data = 9; |
| 4214 | |
| 4215 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4216 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4217 | if (b_space_wraps(&h2c->mbuf)) |
| 4218 | goto realign_again; |
| 4219 | goto full; |
| 4220 | } |
| 4221 | |
| 4222 | /* encode all headers, stop at empty name */ |
| 4223 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4224 | /* these ones do not exist in H2 and must be dropped. */ |
| 4225 | if (isteq(list[hdr].n, ist("connection")) || |
| 4226 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4227 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4228 | isteq(list[hdr].n, ist("upgrade")) || |
| 4229 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4230 | continue; |
| 4231 | |
| 4232 | if (isteq(list[hdr].n, ist(""))) |
| 4233 | break; // end |
| 4234 | |
| 4235 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4236 | /* output full */ |
| 4237 | if (b_space_wraps(&h2c->mbuf)) |
| 4238 | goto realign_again; |
| 4239 | goto full; |
| 4240 | } |
| 4241 | } |
| 4242 | |
| 4243 | /* we may need to add END_STREAM. |
| 4244 | * FIXME: we should also set it when we know for sure that the |
| 4245 | * content-length is zero as well as on 204/304 |
| 4246 | */ |
| 4247 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4248 | es_now = 1; |
| 4249 | |
| 4250 | if (h2s->cs->flags & CS_FL_SHW) |
| 4251 | es_now = 1; |
| 4252 | |
| 4253 | /* update the frame's size */ |
| 4254 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4255 | |
| 4256 | if (es_now) |
| 4257 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4258 | |
| 4259 | /* commit the H2 response */ |
| 4260 | b_add(&h2c->mbuf, outbuf.data); |
| 4261 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4262 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4263 | if (es_now) { |
| 4264 | h2s->flags |= H2_SF_ES_SENT; |
| 4265 | if (h2s->st == H2_SS_OPEN) |
| 4266 | h2s->st = H2_SS_HLOC; |
| 4267 | else |
| 4268 | h2s_close(h2s); |
| 4269 | } |
| 4270 | |
| 4271 | /* OK we could properly deliver the response */ |
| 4272 | |
| 4273 | /* remove all header blocks including the EOH and compute the |
| 4274 | * corresponding size. |
| 4275 | * |
| 4276 | * FIXME: We should remove everything when es_now is set. |
| 4277 | */ |
| 4278 | ret = 0; |
| 4279 | idx = htx_get_head(htx); |
| 4280 | blk = htx_get_blk(htx, idx); |
| 4281 | while (blk != blk_end) { |
| 4282 | ret += htx_get_blksz(blk); |
| 4283 | blk = htx_remove_blk(htx, blk); |
| 4284 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4285 | |
| 4286 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4287 | htx_remove_blk(htx, blk_end); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4288 | end: |
| 4289 | return ret; |
| 4290 | full: |
| 4291 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4292 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4293 | ret = 0; |
| 4294 | goto end; |
| 4295 | fail: |
| 4296 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4297 | * list etc go here (unrecoverable errors). |
| 4298 | */ |
| 4299 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4300 | ret = 0; |
| 4301 | goto end; |
| 4302 | } |
| 4303 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4304 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4305 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4306 | * must check the stream's status to detect any error which might have happened |
| 4307 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4308 | * from the message. The htx message is assumed to be valid since produced from |
| 4309 | * the internal code, hence it contains a start line, an optional series of |
| 4310 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4311 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4312 | */ |
| 4313 | static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
| 4314 | { |
| 4315 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4316 | struct h2c *h2c = h2s->h2c; |
| 4317 | struct htx_blk *blk; |
| 4318 | struct htx_blk *blk_end; |
| 4319 | struct buffer outbuf; |
| 4320 | struct htx_sl *sl; |
| 4321 | struct ist meth, path; |
| 4322 | enum htx_blk_type type; |
| 4323 | int es_now = 0; |
| 4324 | int ret = 0; |
| 4325 | int hdr; |
| 4326 | int idx; |
| 4327 | |
| 4328 | if (h2c_mux_busy(h2c, h2s)) { |
| 4329 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4330 | return 0; |
| 4331 | } |
| 4332 | |
| 4333 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4334 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4335 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4336 | return 0; |
| 4337 | } |
| 4338 | |
| 4339 | /* determine the first block which must not be deleted, blk_end may |
| 4340 | * be NULL if all blocks have to be deleted. |
| 4341 | */ |
| 4342 | idx = htx_get_head(htx); |
| 4343 | blk_end = NULL; |
| 4344 | while (idx != -1) { |
| 4345 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4346 | idx = htx_get_next(htx, idx); |
| 4347 | if (type == HTX_BLK_EOH) { |
| 4348 | if (idx != -1) |
| 4349 | blk_end = htx_get_blk(htx, idx); |
| 4350 | break; |
| 4351 | } |
| 4352 | } |
| 4353 | |
| 4354 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4355 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4356 | ALREADY_CHECKED(sl); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4357 | meth = htx_sl_req_meth(sl); |
| 4358 | path = htx_sl_req_uri(sl); |
| 4359 | |
| 4360 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4361 | hdr = 0; |
| 4362 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4363 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4364 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4365 | blk = htx_get_blk(htx, idx); |
| 4366 | type = htx_get_blk_type(blk); |
| 4367 | |
| 4368 | if (type == HTX_BLK_UNUSED) |
| 4369 | continue; |
| 4370 | |
| 4371 | if (type != HTX_BLK_HDR) |
| 4372 | break; |
| 4373 | |
| 4374 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4375 | goto fail; |
| 4376 | |
| 4377 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4378 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4379 | hdr++; |
| 4380 | } |
| 4381 | |
| 4382 | /* marker for end of headers */ |
| 4383 | list[hdr].n = ist(""); |
| 4384 | |
| 4385 | chunk_reset(&outbuf); |
| 4386 | |
| 4387 | while (1) { |
| 4388 | outbuf.area = b_tail(&h2c->mbuf); |
| 4389 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4390 | outbuf.data = 0; |
| 4391 | |
| 4392 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4393 | break; |
| 4394 | realign_again: |
| 4395 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4396 | } |
| 4397 | |
| 4398 | if (outbuf.size < 9) |
| 4399 | goto full; |
| 4400 | |
| 4401 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4402 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4403 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4404 | outbuf.data = 9; |
| 4405 | |
| 4406 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4407 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4408 | if (b_space_wraps(&h2c->mbuf)) |
| 4409 | goto realign_again; |
| 4410 | goto full; |
| 4411 | } |
| 4412 | |
| 4413 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
Willy Tarreau | 7561bcb | 2018-12-10 19:17:06 +0100 | [diff] [blame] | 4414 | if (!hpack_encode_scheme(&outbuf, ist("https"))) { |
| 4415 | /* output full */ |
| 4416 | if (b_space_wraps(&h2c->mbuf)) |
| 4417 | goto realign_again; |
| 4418 | goto full; |
| 4419 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4420 | |
| 4421 | /* encode the path, which necessarily is the second one */ |
Willy Tarreau | 9079981 | 2018-12-10 19:28:38 +0100 | [diff] [blame] | 4422 | if (!hpack_encode_path(&outbuf, path)) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4423 | /* output full */ |
| 4424 | if (b_space_wraps(&h2c->mbuf)) |
| 4425 | goto realign_again; |
| 4426 | goto full; |
| 4427 | } |
| 4428 | |
| 4429 | /* encode all headers, stop at empty name */ |
| 4430 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4431 | /* these ones do not exist in H2 and must be dropped. */ |
| 4432 | if (isteq(list[hdr].n, ist("connection")) || |
| 4433 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4434 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4435 | isteq(list[hdr].n, ist("upgrade")) || |
| 4436 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4437 | continue; |
| 4438 | |
| 4439 | if (isteq(list[hdr].n, ist(""))) |
| 4440 | break; // end |
| 4441 | |
| 4442 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4443 | /* output full */ |
| 4444 | if (b_space_wraps(&h2c->mbuf)) |
| 4445 | goto realign_again; |
| 4446 | goto full; |
| 4447 | } |
| 4448 | } |
| 4449 | |
| 4450 | /* we may need to add END_STREAM if we have no body : |
| 4451 | * - request already closed, or : |
| 4452 | * - no transfer-encoding, and : |
| 4453 | * - no content-length or content-length:0 |
| 4454 | * Fixme: this doesn't take into account CONNECT requests. |
| 4455 | */ |
| 4456 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4457 | es_now = 1; |
| 4458 | |
| 4459 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4460 | es_now = 1; |
| 4461 | |
| 4462 | if (h2s->cs->flags & CS_FL_SHW) |
| 4463 | es_now = 1; |
| 4464 | |
| 4465 | /* update the frame's size */ |
| 4466 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4467 | |
| 4468 | if (es_now) |
| 4469 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4470 | |
| 4471 | /* commit the H2 response */ |
| 4472 | b_add(&h2c->mbuf, outbuf.data); |
| 4473 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4474 | h2s->st = H2_SS_OPEN; |
| 4475 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4476 | if (es_now) { |
| 4477 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4478 | h2s->flags |= H2_SF_ES_SENT; |
| 4479 | h2s->st = H2_SS_HLOC; |
| 4480 | } |
| 4481 | |
| 4482 | /* remove all header blocks including the EOH and compute the |
| 4483 | * corresponding size. |
| 4484 | * |
| 4485 | * FIXME: We should remove everything when es_now is set. |
| 4486 | */ |
| 4487 | ret = 0; |
| 4488 | idx = htx_get_head(htx); |
| 4489 | blk = htx_get_blk(htx, idx); |
| 4490 | while (blk != blk_end) { |
| 4491 | ret += htx_get_blksz(blk); |
| 4492 | blk = htx_remove_blk(htx, blk); |
| 4493 | } |
| 4494 | |
| 4495 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4496 | htx_remove_blk(htx, blk_end); |
| 4497 | |
| 4498 | end: |
| 4499 | return ret; |
| 4500 | full: |
| 4501 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4502 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4503 | ret = 0; |
| 4504 | goto end; |
| 4505 | fail: |
| 4506 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4507 | * list etc go here (unrecoverable errors). |
| 4508 | */ |
| 4509 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4510 | ret = 0; |
| 4511 | goto end; |
| 4512 | } |
| 4513 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4514 | /* Try to send a DATA frame matching HTTP response present in HTX structure |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4515 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 4516 | * caller must check the stream's status to detect any error which might have |
| 4517 | * happened subsequently to a successful send. Returns the number of data bytes |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4518 | * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte. |
| 4519 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4520 | static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4521 | { |
| 4522 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4523 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4524 | struct buffer outbuf; |
| 4525 | size_t total = 0; |
| 4526 | int es_now = 0; |
| 4527 | int bsize; /* htx block size */ |
| 4528 | int fsize; /* h2 frame size */ |
| 4529 | struct htx_blk *blk; |
| 4530 | enum htx_blk_type type; |
| 4531 | int idx; |
| 4532 | |
| 4533 | if (h2c_mux_busy(h2c, h2s)) { |
| 4534 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4535 | goto end; |
| 4536 | } |
| 4537 | |
| 4538 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4539 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4540 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4541 | goto end; |
| 4542 | } |
| 4543 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4544 | htx = htx_from_buf(buf); |
| 4545 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4546 | /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However, |
| 4547 | * while looping, we can meet an HTX_BLK_EOM block that we'll leave to |
| 4548 | * the caller to handle. |
| 4549 | */ |
| 4550 | |
| 4551 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4552 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4553 | goto end; |
| 4554 | |
| 4555 | idx = htx_get_head(htx); |
| 4556 | blk = htx_get_blk(htx, idx); |
| 4557 | type = htx_get_blk_type(blk); // DATA or EOD or EOM |
| 4558 | bsize = htx_get_blksz(blk); |
| 4559 | fsize = bsize; |
| 4560 | |
| 4561 | if (type == HTX_BLK_EOD) { |
| 4562 | /* if we have an EOD, we're dealing with chunked data. We may |
| 4563 | * have a set of trailers after us that the caller will want to |
| 4564 | * deal with. Let's simply remove the EOD and return. |
| 4565 | */ |
| 4566 | htx_remove_blk(htx, blk); |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4567 | total++; // EOD counts as one byte |
| 4568 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4569 | goto end; |
| 4570 | } |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 4571 | else if (type == HTX_BLK_EOM) { |
| 4572 | if (h2s->flags & H2_SF_ES_SENT) { |
| 4573 | /* ES already sent */ |
| 4574 | htx_remove_blk(htx, blk); |
| 4575 | total++; // EOM counts as one byte |
| 4576 | count--; |
| 4577 | goto end; |
| 4578 | } |
| 4579 | } |
| 4580 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4581 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4582 | |
| 4583 | /* Perform some optimizations to reduce the number of buffer copies. |
| 4584 | * First, if the mux's buffer is empty and the htx area contains |
| 4585 | * exactly one data block of the same size as the requested count, and |
| 4586 | * this count fits within the frame size, the stream's window size, and |
| 4587 | * the connection's window size, then it's possible to simply swap the |
| 4588 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 4589 | * length to match the entire DATA HTX block in the middle. In this |
| 4590 | * case we perform a true zero-copy operation from end-to-end. This is |
| 4591 | * the situation that happens all the time with large files. Second, if |
| 4592 | * this is not possible, but the mux's output buffer is empty, we still |
| 4593 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 4594 | * making the intermediary buffer's area point to the output buffer's |
| 4595 | * area. In this case we want to skip the HTX header to make sure that |
| 4596 | * copies remain aligned and that this operation remains possible all |
| 4597 | * the time. This goes for headers, data blocks and any data extracted |
| 4598 | * from the HTX blocks. |
| 4599 | */ |
| 4600 | if (unlikely(fsize == count && |
| 4601 | htx->used == 1 && type == HTX_BLK_DATA && |
| 4602 | fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
| 4603 | void *old_area = h2c->mbuf.area; |
| 4604 | |
| 4605 | if (b_data(&h2c->mbuf)) { |
| 4606 | /* too bad there are data left there. If we have less |
| 4607 | * than 1/4 of the mbuf's size and everything fits, |
| 4608 | * we'll perform a copy anyway. Otherwise we'll pretend |
| 4609 | * the mbuf is full and wait. |
| 4610 | */ |
| 4611 | if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf)) |
| 4612 | goto copy; |
| 4613 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4614 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4615 | goto end; |
| 4616 | } |
| 4617 | |
| 4618 | /* map an H2 frame to the HTX block so that we can put the |
| 4619 | * frame header there. |
| 4620 | */ |
| 4621 | h2c->mbuf.area = buf->area; |
Olivier Houchard | 84cca66 | 2018-12-14 16:28:08 +0100 | [diff] [blame] | 4622 | h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4623 | h2c->mbuf.data = fsize + 9; |
| 4624 | outbuf.area = b_head(&h2c->mbuf); |
| 4625 | |
| 4626 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 4627 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4628 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4629 | h2_set_frame_size(outbuf.area, fsize); |
| 4630 | |
| 4631 | /* update windows */ |
| 4632 | h2s->mws -= fsize; |
| 4633 | h2c->mws -= fsize; |
| 4634 | |
| 4635 | /* and exchange with our old area */ |
| 4636 | buf->area = old_area; |
| 4637 | buf->data = buf->head = 0; |
| 4638 | total += fsize; |
| 4639 | goto end; |
| 4640 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4641 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4642 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4643 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 4644 | |
| 4645 | while (1) { |
| 4646 | outbuf.area = b_tail(&h2c->mbuf); |
| 4647 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4648 | outbuf.data = 0; |
| 4649 | |
| 4650 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4651 | break; |
| 4652 | realign_again: |
| 4653 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4654 | } |
| 4655 | |
| 4656 | if (outbuf.size < 9) { |
| 4657 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4658 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4659 | goto end; |
| 4660 | } |
| 4661 | |
| 4662 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 4663 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4664 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4665 | outbuf.data = 9; |
| 4666 | |
| 4667 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 4668 | * the HTX buffer. We need to check this against the connection's and |
| 4669 | * the stream's send windows, and to ensure that this fits in the max |
| 4670 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4671 | * the frame header). The connection's flow control is applied last so |
| 4672 | * that we can use a separate list of streams which are immediately |
| 4673 | * unblocked on window opening. Note: we don't implement padding. |
| 4674 | */ |
| 4675 | |
| 4676 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 4677 | * empty frame, thus we force it to zero here. |
| 4678 | */ |
| 4679 | if (type == HTX_BLK_EOM) |
| 4680 | bsize = fsize = 0; |
| 4681 | |
| 4682 | if (!fsize) |
| 4683 | goto send_empty; |
| 4684 | |
| 4685 | if (h2s->mws <= 0) { |
| 4686 | h2s->flags |= H2_SF_BLK_SFCTL; |
| 4687 | if (h2s->send_wait) { |
| 4688 | LIST_DEL(&h2s->list); |
| 4689 | LIST_INIT(&h2s->list); |
| 4690 | } |
| 4691 | goto end; |
| 4692 | } |
| 4693 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4694 | if (fsize > count) |
| 4695 | fsize = count; |
| 4696 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4697 | if (fsize > h2s->mws) |
| 4698 | fsize = h2s->mws; // >0 |
| 4699 | |
| 4700 | if (h2c->mfs && fsize > h2c->mfs) |
| 4701 | fsize = h2c->mfs; // >0 |
| 4702 | |
| 4703 | if (fsize + 9 > outbuf.size) { |
| 4704 | /* we have an opportunity for enlarging the too small |
| 4705 | * available space, let's try. |
| 4706 | * FIXME: is this really interesting to do? Maybe we'll |
| 4707 | * spend lots of time realigning instead of using two |
| 4708 | * frames. |
| 4709 | */ |
| 4710 | if (b_space_wraps(&h2c->mbuf)) |
| 4711 | goto realign_again; |
| 4712 | fsize = outbuf.size - 9; |
| 4713 | |
| 4714 | if (fsize <= 0) { |
| 4715 | /* no need to send an empty frame here */ |
| 4716 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4717 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4718 | goto end; |
| 4719 | } |
| 4720 | } |
| 4721 | |
| 4722 | if (h2c->mws <= 0) { |
| 4723 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4724 | goto end; |
| 4725 | } |
| 4726 | |
| 4727 | if (fsize > h2c->mws) |
| 4728 | fsize = h2c->mws; |
| 4729 | |
| 4730 | /* now let's copy this this into the output buffer */ |
| 4731 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 4732 | h2s->mws -= fsize; |
| 4733 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4734 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4735 | |
| 4736 | send_empty: |
| 4737 | /* update the frame's size */ |
| 4738 | h2_set_frame_size(outbuf.area, fsize); |
| 4739 | |
| 4740 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 4741 | * meeting EOM. We should optimize this later. |
| 4742 | */ |
| 4743 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4744 | total++; // EOM counts as one byte |
| 4745 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4746 | es_now = 1; |
| 4747 | } |
| 4748 | |
| 4749 | if (es_now) |
| 4750 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 4751 | |
| 4752 | /* commit the H2 response */ |
| 4753 | b_add(&h2c->mbuf, fsize + 9); |
| 4754 | |
| 4755 | /* consume incoming HTX block, including EOM */ |
| 4756 | total += fsize; |
| 4757 | if (fsize == bsize) { |
| 4758 | htx_remove_blk(htx, blk); |
| 4759 | if (fsize) |
| 4760 | goto new_frame; |
| 4761 | } else { |
| 4762 | /* we've truncated this block */ |
| 4763 | htx_cut_data_blk(htx, blk, fsize); |
| 4764 | } |
| 4765 | |
| 4766 | if (es_now) { |
| 4767 | if (h2s->st == H2_SS_OPEN) |
| 4768 | h2s->st = H2_SS_HLOC; |
| 4769 | else |
| 4770 | h2s_close(h2s); |
| 4771 | |
| 4772 | h2s->flags |= H2_SF_ES_SENT; |
| 4773 | } |
| 4774 | |
| 4775 | end: |
| 4776 | return total; |
| 4777 | } |
| 4778 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4779 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 4780 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 4781 | * processed. The caller must check the stream's status to detect any error |
| 4782 | * which might have happened subsequently to a successful send. The htx blocks |
| 4783 | * are automatically removed from the message. The htx message is assumed to be |
| 4784 | * valid since produced from the internal code. Processing stops when meeting |
| 4785 | * the EOM, which is also removed. All trailers are processed at once and sent |
| 4786 | * as a single frame. The ES flag is always set. |
| 4787 | */ |
| 4788 | static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx) |
| 4789 | { |
| 4790 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4791 | struct h2c *h2c = h2s->h2c; |
| 4792 | struct htx_blk *blk; |
| 4793 | struct htx_blk *blk_end; |
| 4794 | struct buffer outbuf; |
| 4795 | struct h1m h1m; |
| 4796 | enum htx_blk_type type; |
| 4797 | uint32_t size; |
| 4798 | int ret = 0; |
| 4799 | int hdr; |
| 4800 | int idx; |
| 4801 | void *start; |
| 4802 | |
| 4803 | if (h2c_mux_busy(h2c, h2s)) { |
| 4804 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4805 | goto end; |
| 4806 | } |
| 4807 | |
| 4808 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4809 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4810 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4811 | goto end; |
| 4812 | } |
| 4813 | |
| 4814 | /* The principle is that we parse each and every trailers block using |
| 4815 | * the H1 headers parser, and append it to the list. We don't proceed |
| 4816 | * until EOM is met. blk_end will point to the EOM block. |
| 4817 | */ |
| 4818 | hdr = 0; |
| 4819 | memset(list, 0, sizeof(list)); |
| 4820 | blk_end = NULL; |
| 4821 | |
| 4822 | for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) { |
| 4823 | blk = htx_get_blk(htx, idx); |
| 4824 | type = htx_get_blk_type(blk); |
| 4825 | |
| 4826 | if (type == HTX_BLK_UNUSED) |
| 4827 | continue; |
| 4828 | |
| 4829 | if (type != HTX_BLK_TLR) { |
| 4830 | if (type == HTX_BLK_EOM) |
| 4831 | blk_end = blk; |
| 4832 | break; |
| 4833 | } |
| 4834 | |
| 4835 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4836 | goto fail; |
| 4837 | |
| 4838 | size = htx_get_blksz(blk); |
| 4839 | start = htx_get_blk_ptr(htx, blk); |
| 4840 | |
| 4841 | h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER; |
| 4842 | h1m.err_pos = 0; |
| 4843 | ret = h1_headers_to_hdr_list(start, start + size, |
| 4844 | list + hdr, sizeof(list)/sizeof(list[0]) - hdr, |
| 4845 | &h1m, NULL); |
| 4846 | if (ret < 0) |
| 4847 | goto fail; |
| 4848 | |
| 4849 | /* ret == 0 if an incomplete trailers block was found (missing |
| 4850 | * empty line), or > 0 if it was found. We have to continue on |
| 4851 | * incomplete messages because the trailers block might be |
| 4852 | * incomplete. |
| 4853 | */ |
| 4854 | |
| 4855 | /* search the new end */ |
| 4856 | while (hdr <= sizeof(list)/sizeof(list[0])) { |
| 4857 | if (!list[hdr].n.len) |
| 4858 | break; |
| 4859 | hdr++; |
| 4860 | } |
| 4861 | } |
| 4862 | |
| 4863 | if (!blk_end) |
| 4864 | goto end; // end not found yet |
| 4865 | |
| 4866 | if (!hdr) |
| 4867 | goto done; |
| 4868 | |
| 4869 | chunk_reset(&outbuf); |
| 4870 | |
| 4871 | while (1) { |
| 4872 | outbuf.area = b_tail(&h2c->mbuf); |
| 4873 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4874 | outbuf.data = 0; |
| 4875 | |
| 4876 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4877 | break; |
| 4878 | realign_again: |
| 4879 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4880 | } |
| 4881 | |
| 4882 | if (outbuf.size < 9) |
| 4883 | goto full; |
| 4884 | |
| 4885 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 4886 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 4887 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4888 | outbuf.data = 9; |
| 4889 | |
| 4890 | /* encode status, which necessarily is the first one */ |
| 4891 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
| 4892 | if (b_space_wraps(&h2c->mbuf)) |
| 4893 | goto realign_again; |
| 4894 | goto full; |
| 4895 | } |
| 4896 | |
| 4897 | /* encode all headers */ |
| 4898 | for (idx = 0; idx < hdr; idx++) { |
| 4899 | /* these ones do not exist in H2 or must not appear in |
| 4900 | * trailers and must be dropped. |
| 4901 | */ |
| 4902 | if (isteq(list[idx].n, ist("host")) || |
| 4903 | isteq(list[idx].n, ist("content-length")) || |
| 4904 | isteq(list[idx].n, ist("connection")) || |
| 4905 | isteq(list[idx].n, ist("proxy-connection")) || |
| 4906 | isteq(list[idx].n, ist("keep-alive")) || |
| 4907 | isteq(list[idx].n, ist("upgrade")) || |
| 4908 | isteq(list[idx].n, ist("te")) || |
| 4909 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 4910 | continue; |
| 4911 | |
| 4912 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 4913 | /* output full */ |
| 4914 | if (b_space_wraps(&h2c->mbuf)) |
| 4915 | goto realign_again; |
| 4916 | goto full; |
| 4917 | } |
| 4918 | } |
| 4919 | |
| 4920 | /* update the frame's size */ |
| 4921 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4922 | |
| 4923 | /* commit the H2 response */ |
| 4924 | b_add(&h2c->mbuf, outbuf.data); |
| 4925 | h2s->flags |= H2_SF_ES_SENT; |
| 4926 | |
| 4927 | if (h2s->st == H2_SS_OPEN) |
| 4928 | h2s->st = H2_SS_HLOC; |
| 4929 | else |
| 4930 | h2s_close(h2s); |
| 4931 | |
| 4932 | /* OK we could properly deliver the response */ |
| 4933 | done: |
| 4934 | /* remove all header blocks including EOM and compute the corresponding size. */ |
| 4935 | ret = 0; |
| 4936 | idx = htx_get_head(htx); |
| 4937 | blk = htx_get_blk(htx, idx); |
| 4938 | while (blk != blk_end) { |
| 4939 | ret += htx_get_blksz(blk); |
| 4940 | blk = htx_remove_blk(htx, blk); |
| 4941 | } |
| 4942 | blk = htx_remove_blk(htx, blk); |
| 4943 | end: |
| 4944 | return ret; |
| 4945 | full: |
| 4946 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4947 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4948 | ret = 0; |
| 4949 | goto end; |
| 4950 | fail: |
| 4951 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4952 | * list etc go here (unrecoverable errors). |
| 4953 | */ |
| 4954 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4955 | ret = 0; |
| 4956 | goto end; |
| 4957 | } |
| 4958 | |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4959 | /* Called from the upper layer, to subscribe to events, such as being able to send */ |
| 4960 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 4961 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4962 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4963 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 4964 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4965 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4966 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 4967 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4968 | if (!(sw->events & SUB_RETRY_RECV)) { |
| 4969 | sw->events |= SUB_RETRY_RECV; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 4970 | sw->handle = h2s; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4971 | h2s->recv_wait = sw; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 4972 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4973 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4974 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4975 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4976 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4977 | if (!(sw->events & SUB_RETRY_SEND)) { |
| 4978 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 4979 | sw->handle = h2s; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4980 | h2s->send_wait = sw; |
| 4981 | if (!(h2s->flags & H2_SF_BLK_SFCTL)) { |
| 4982 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 4983 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 4984 | else |
| 4985 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 4986 | } |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 4987 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4988 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4989 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4990 | if (event_type != 0) |
| 4991 | return -1; |
| 4992 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4993 | |
| 4994 | |
| 4995 | } |
| 4996 | |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4997 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 4998 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4999 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5000 | struct h2s *h2s = cs->ctx; |
| 5001 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5002 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5003 | sw = param; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5004 | if (h2s->recv_wait == sw) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5005 | sw->events &= ~SUB_RETRY_RECV; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5006 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5007 | } |
| 5008 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5009 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5010 | sw = param; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5011 | if (h2s->send_wait == sw) { |
| 5012 | LIST_DEL(&h2s->list); |
| 5013 | LIST_INIT(&h2s->list); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5014 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5015 | h2s->send_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5016 | } |
| 5017 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5018 | if (event_type & SUB_CALL_UNSUBSCRIBE) { |
| 5019 | sw = param; |
| 5020 | if (h2s->send_wait == sw) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5021 | sw->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5022 | h2s->send_wait = NULL; |
Olivier Houchard | f29cd5c | 2018-12-20 11:56:28 +0100 | [diff] [blame] | 5023 | LIST_DEL(&h2s->list); |
| 5024 | LIST_INIT(&h2s->list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5025 | } |
| 5026 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5027 | return 0; |
| 5028 | } |
| 5029 | |
| 5030 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5031 | /* Called from the upper layer, to receive data */ |
| 5032 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 5033 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5034 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5035 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5036 | struct htx *h2s_htx = NULL; |
| 5037 | struct htx *buf_htx = NULL; |
| 5038 | struct htx_ret htx_ret; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5039 | size_t ret = 0; |
| 5040 | |
| 5041 | /* transfer possibly pending data to the upper layer */ |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5042 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
| 5043 | /* in HTX mode we ignore the count argument */ |
| 5044 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5045 | if (htx_is_empty(h2s_htx)) { |
| 5046 | if (cs->flags & CS_FL_REOS) |
| 5047 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5048 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5049 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5050 | goto end; |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5051 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5052 | |
| 5053 | buf_htx = htx_from_buf(buf); |
| 5054 | count = htx_free_space(buf_htx); |
| 5055 | |
Willy Tarreau | 0c22fa7 | 2018-12-04 15:21:35 +0100 | [diff] [blame] | 5056 | 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] | 5057 | |
| 5058 | buf_htx->extra = h2s_htx->extra; |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5059 | htx_to_buf(buf_htx, buf); |
| 5060 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5061 | ret = htx_ret.ret; |
| 5062 | } |
| 5063 | else { |
| 5064 | ret = b_xfer(buf, &h2s->rxbuf, count); |
| 5065 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5066 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5067 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5068 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5069 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5070 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5071 | if (cs->flags & CS_FL_REOS) |
| 5072 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5073 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5074 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5075 | if (b_size(&h2s->rxbuf)) { |
| 5076 | b_free(&h2s->rxbuf); |
| 5077 | offer_buffers(NULL, tasks_run_queue); |
| 5078 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5079 | } |
| 5080 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5081 | if (ret && h2c->dsi == h2s->id) { |
| 5082 | /* demux is blocking on this stream's buffer */ |
| 5083 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 872e2fa | 2019-01-03 08:27:41 +0100 | [diff] [blame] | 5084 | h2c_restart_reading(h2c); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5085 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5086 | end: |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5087 | return ret; |
| 5088 | } |
| 5089 | |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5090 | static void h2_stop_senders(struct h2c *h2c) |
| 5091 | { |
| 5092 | struct h2s *h2s, *h2s_back; |
| 5093 | |
| 5094 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) { |
| 5095 | /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */ |
| 5096 | if (h2c->msi == h2s_id(h2s)) |
| 5097 | continue; |
| 5098 | LIST_DEL(&h2s->list); |
| 5099 | LIST_INIT(&h2s->list); |
| 5100 | task_remove_from_task_list((struct task *)h2s->send_wait->task); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5101 | h2s->send_wait->events |= SUB_RETRY_SEND; |
| 5102 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5103 | LIST_ADD(&h2c->send_list, &h2s->list); |
| 5104 | } |
| 5105 | } |
| 5106 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5107 | /* Called from the upper layer, to send data */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 5108 | 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] | 5109 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5110 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5111 | size_t orig_count = count; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 5112 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5113 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5114 | struct htx *htx; |
| 5115 | struct htx_blk *blk; |
| 5116 | enum htx_blk_type btype; |
| 5117 | uint32_t bsize; |
| 5118 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5119 | |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5120 | if (h2s->send_wait) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5121 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5122 | h2s->send_wait = NULL; |
| 5123 | LIST_DEL(&h2s->list); |
| 5124 | LIST_INIT(&h2s->list); |
| 5125 | } |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 5126 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 5127 | return 0; |
| 5128 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5129 | /* htx will be enough to decide if we're using HTX or legacy */ |
| 5130 | htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL; |
| 5131 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5132 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 5133 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 5134 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5135 | if (h2s->id == 0) { |
| 5136 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 5137 | |
| 5138 | if (id < 0) { |
| 5139 | cs->ctx = NULL; |
| 5140 | cs->flags |= CS_FL_ERROR; |
| 5141 | h2s_destroy(h2s); |
| 5142 | return 0; |
| 5143 | } |
| 5144 | |
| 5145 | eb32_delete(&h2s->by_id); |
| 5146 | h2s->by_id.key = h2s->id = id; |
| 5147 | h2s->h2c->max_id = id; |
| 5148 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 5149 | } |
| 5150 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5151 | if (htx) { |
Willy Tarreau | c14999b | 2018-12-06 14:09:09 +0100 | [diff] [blame] | 5152 | while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) && |
| 5153 | count && !htx_is_empty(htx)) { |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5154 | idx = htx_get_head(htx); |
| 5155 | blk = htx_get_blk(htx, idx); |
| 5156 | btype = htx_get_blk_type(blk); |
| 5157 | bsize = htx_get_blksz(blk); |
| 5158 | |
| 5159 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5160 | case HTX_BLK_REQ_SL: |
| 5161 | /* start-line before headers */ |
| 5162 | ret = h2s_htx_bck_make_req_headers(h2s, htx); |
| 5163 | if (ret > 0) { |
| 5164 | total += ret; |
| 5165 | count -= ret; |
| 5166 | if (ret < bsize) |
| 5167 | goto done; |
| 5168 | } |
| 5169 | break; |
| 5170 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 5171 | case HTX_BLK_RES_SL: |
| 5172 | /* start-line before headers */ |
| 5173 | ret = h2s_htx_frt_make_resp_headers(h2s, htx); |
| 5174 | if (ret > 0) { |
| 5175 | total += ret; |
| 5176 | count -= ret; |
| 5177 | if (ret < bsize) |
| 5178 | goto done; |
| 5179 | } |
| 5180 | break; |
| 5181 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5182 | case HTX_BLK_DATA: |
| 5183 | case HTX_BLK_EOD: |
| 5184 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5185 | /* all these cause the emission of a DATA frame (possibly empty). |
| 5186 | * This EOM necessarily is one before trailers, as the EOM following |
| 5187 | * trailers would have been consumed by the trailers parser. |
| 5188 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5189 | ret = h2s_htx_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5190 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5191 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5192 | total += ret; |
| 5193 | count -= ret; |
| 5194 | if (ret < bsize) |
| 5195 | goto done; |
| 5196 | } |
| 5197 | break; |
| 5198 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5199 | case HTX_BLK_TLR: |
| 5200 | /* This is the first trailers block, all the subsequent ones AND |
| 5201 | * the EOM will be swallowed by the parser. |
| 5202 | */ |
| 5203 | ret = h2s_htx_make_trailers(h2s, htx); |
| 5204 | if (ret > 0) { |
| 5205 | total += ret; |
| 5206 | count -= ret; |
| 5207 | if (ret < bsize) |
| 5208 | goto done; |
| 5209 | } |
| 5210 | break; |
| 5211 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5212 | default: |
| 5213 | htx_remove_blk(htx, blk); |
| 5214 | total += bsize; |
| 5215 | count -= bsize; |
| 5216 | break; |
| 5217 | } |
| 5218 | } |
| 5219 | goto done; |
| 5220 | } |
| 5221 | |
| 5222 | /* legacy transfer mode */ |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5223 | while (h2s->h1m.state < H1_MSG_DONE && count) { |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 5224 | if (h2s->h1m.state <= H1_MSG_LAST_LF) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5225 | if (h2s->h2c->flags & H2_CF_IS_BACK) |
| 5226 | ret = -1; |
| 5227 | else |
| 5228 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5229 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5230 | else if (h2s->h1m.state < H1_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5231 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5232 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5233 | else if (h2s->h1m.state == H1_MSG_TRAILERS) { |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5234 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5235 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5236 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5237 | if (unlikely((int)ret <= 0)) { |
| 5238 | if ((int)ret < 0) |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5239 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5240 | break; |
| 5241 | } |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 5242 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5243 | total += count; |
| 5244 | count = 0; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5245 | h2s->h1m.state = H1_MSG_DONE; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5246 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 5247 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5248 | else { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5249 | cs_set_error(cs); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5250 | break; |
| 5251 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5252 | |
| 5253 | total += ret; |
| 5254 | count -= ret; |
| 5255 | |
| 5256 | if (h2s->st >= H2_SS_ERROR) |
| 5257 | break; |
| 5258 | |
| 5259 | if (h2s->flags & H2_SF_BLK_ANY) |
| 5260 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5261 | } |
| 5262 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5263 | done: |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5264 | if (h2s->st >= H2_SS_ERROR) { |
| 5265 | /* trim any possibly pending data after we close (extra CR-LF, |
| 5266 | * unprocessed trailers, abnormal extra data, ...) |
| 5267 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5268 | total += count; |
| 5269 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5270 | } |
| 5271 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5272 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 5273 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5274 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 5275 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 5276 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5277 | } |
| 5278 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5279 | if (htx) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5280 | htx_to_buf(htx, buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5281 | } else { |
| 5282 | b_del(buf, total); |
| 5283 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5284 | |
| 5285 | /* The mux is full, cancel the pending tasks */ |
| 5286 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 5287 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 5288 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5289 | |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5290 | /* If we're running HTX, and we read the whole buffer, then pretend |
| 5291 | * we read exactly what the caller specified, as with HTX the caller |
| 5292 | * will always give the buffer size, instead of the amount of data |
| 5293 | * available. |
| 5294 | */ |
| 5295 | if (htx && !b_data(buf)) |
| 5296 | total = orig_count; |
| 5297 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5298 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5299 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5300 | tasklet_wakeup(h2s->h2c->wait_event.task); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5301 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5302 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 5303 | /* If we're waiting for flow control, and we got a shutr on the |
| 5304 | * connection, we will never be unlocked, so add an error on |
| 5305 | * the conn_stream. |
| 5306 | */ |
| 5307 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 5308 | !b_data(&h2s->h2c->dbuf) && |
| 5309 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 5310 | if (cs->flags & CS_FL_EOS) |
| 5311 | cs->flags |= CS_FL_ERROR; |
| 5312 | else |
| 5313 | cs->flags |= CS_FL_ERR_PENDING; |
| 5314 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5315 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5316 | } |
| 5317 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5318 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5319 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5320 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5321 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5322 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5323 | struct eb32_node *node; |
| 5324 | int fctl_cnt = 0; |
| 5325 | int send_cnt = 0; |
| 5326 | int tree_cnt = 0; |
| 5327 | int orph_cnt = 0; |
| 5328 | |
| 5329 | if (!h2c) |
| 5330 | return; |
| 5331 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5332 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5333 | fctl_cnt++; |
| 5334 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5335 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5336 | send_cnt++; |
| 5337 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5338 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5339 | node = eb32_first(&h2c->streams_by_id); |
| 5340 | while (node) { |
| 5341 | h2s = container_of(node, struct h2s, by_id); |
| 5342 | tree_cnt++; |
| 5343 | if (!h2s->cs) |
| 5344 | orph_cnt++; |
| 5345 | node = eb32_next(node); |
| 5346 | } |
| 5347 | |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5348 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5349 | " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d" |
| 5350 | " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d .mbuf=%u@%p+%u/%u", |
Willy Tarreau | 616ac81 | 2018-07-24 14:12:42 +0200 | [diff] [blame] | 5351 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5352 | h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt, |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5353 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5354 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5355 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5356 | h2c->msi, |
| 5357 | (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf), |
| 5358 | (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf)); |
| 5359 | |
| 5360 | if (h2s) { |
| 5361 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5362 | h2s, h2s->id, h2s->flags, |
| 5363 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5364 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5365 | h2s->cs); |
| 5366 | if (h2s->cs) |
| 5367 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5368 | h2s->cs->flags, h2s->cs->data); |
| 5369 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5370 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5371 | |
| 5372 | /*******************************************************/ |
| 5373 | /* functions below are dedicated to the config parsers */ |
| 5374 | /*******************************************************/ |
| 5375 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5376 | /* config parser for global "tune.h2.header-table-size" */ |
| 5377 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5378 | struct proxy *defpx, const char *file, int line, |
| 5379 | char **err) |
| 5380 | { |
| 5381 | if (too_many_args(1, args, err, NULL)) |
| 5382 | return -1; |
| 5383 | |
| 5384 | h2_settings_header_table_size = atoi(args[1]); |
| 5385 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5386 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5387 | return -1; |
| 5388 | } |
| 5389 | return 0; |
| 5390 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5391 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5392 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5393 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5394 | struct proxy *defpx, const char *file, int line, |
| 5395 | char **err) |
| 5396 | { |
| 5397 | if (too_many_args(1, args, err, NULL)) |
| 5398 | return -1; |
| 5399 | |
| 5400 | h2_settings_initial_window_size = atoi(args[1]); |
| 5401 | if (h2_settings_initial_window_size < 0) { |
| 5402 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5403 | return -1; |
| 5404 | } |
| 5405 | return 0; |
| 5406 | } |
| 5407 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5408 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5409 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5410 | struct proxy *defpx, const char *file, int line, |
| 5411 | char **err) |
| 5412 | { |
| 5413 | if (too_many_args(1, args, err, NULL)) |
| 5414 | return -1; |
| 5415 | |
| 5416 | h2_settings_max_concurrent_streams = atoi(args[1]); |
| 5417 | if (h2_settings_max_concurrent_streams < 0) { |
| 5418 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5419 | return -1; |
| 5420 | } |
| 5421 | return 0; |
| 5422 | } |
| 5423 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5424 | |
| 5425 | /****************************************/ |
| 5426 | /* MUX initialization and instanciation */ |
| 5427 | /***************************************/ |
| 5428 | |
| 5429 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5430 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5431 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5432 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5433 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5434 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5435 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5436 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5437 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5438 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5439 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5440 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5441 | .avail_streams = h2_avail_streams, |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 5442 | .max_streams = h2_max_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5443 | .shutr = h2_shutr, |
| 5444 | .shutw = h2_shutw, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5445 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 5446 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5447 | .name = "H2", |
| 5448 | }; |
| 5449 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5450 | /* PROTO selection : this mux registers PROTO token "h2" */ |
| 5451 | static struct mux_proto_list mux_proto_h2 = |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5452 | { .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] | 5453 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5454 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 5455 | |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5456 | static struct mux_proto_list mux_proto_h2_htx = |
| 5457 | { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops }; |
| 5458 | |
| 5459 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx); |
| 5460 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5461 | /* config keyword parsers */ |
| 5462 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5463 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5464 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5465 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5466 | { 0, NULL, NULL } |
| 5467 | }}; |
| 5468 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5469 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |