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 | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3319 | hdrs += 5; // stream dep = 4, weight = 1 |
| 3320 | flen -= 5; |
| 3321 | } |
| 3322 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3323 | if (!h2_get_buf(h2c, rxbuf)) { |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3324 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3325 | goto leave; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3326 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3327 | |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3328 | /* we can't retry a failed decompression operation so we must be very |
| 3329 | * careful not to take any risks. In practice the output buffer is |
| 3330 | * always empty except maybe for trailers, in which case we simply have |
| 3331 | * to wait for the upper layer to finish consuming what is available. |
| 3332 | */ |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3333 | |
| 3334 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3335 | htx = htx_from_buf(rxbuf); |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3336 | if (!htx_is_empty(htx)) { |
| 3337 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3338 | goto leave; |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3339 | } |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3340 | } else { |
Willy Tarreau | 8dbb170 | 2019-01-03 08:52:09 +0100 | [diff] [blame] | 3341 | if (b_data(rxbuf)) { |
| 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 | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3345 | |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3346 | rxbuf->head = 0; |
| 3347 | try = b_size(rxbuf); |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3348 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3349 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3350 | /* past this point we cannot roll back in case of error */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3351 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 3352 | sizeof(list)/sizeof(list[0]), tmp); |
| 3353 | if (outlen < 0) { |
| 3354 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 3355 | goto fail; |
| 3356 | } |
| 3357 | |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3358 | /* The PACK decompressor was updated, let's update the input buffer and |
| 3359 | * the parser's state to commit these changes and allow us to later |
| 3360 | * fail solely on the stream if needed. |
| 3361 | */ |
| 3362 | b_del(&h2c->dbuf, h2c->dfl + hole); |
| 3363 | h2c->dfl = hole = 0; |
| 3364 | h2c->st0 = H2_CS_FRAME_H; |
| 3365 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3366 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 880f580 | 2019-01-03 08:10:14 +0100 | [diff] [blame] | 3367 | msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | bd4a6b6 | 2018-11-27 09:29:36 +0100 | [diff] [blame] | 3368 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3369 | if (*flags & H2_SF_HEADERS_RCVD) |
| 3370 | goto trailers; |
| 3371 | |
| 3372 | /* This is the first HEADERS frame so it's a headers block */ |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3373 | if (htx) { |
| 3374 | /* HTX mode */ |
| 3375 | if (h2c->flags & H2_CF_IS_BACK) |
| 3376 | outlen = h2_make_htx_response(list, htx, &msgf); |
| 3377 | else |
| 3378 | outlen = h2_make_htx_request(list, htx, &msgf); |
| 3379 | } else { |
| 3380 | /* HTTP/1 mode */ |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3381 | outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf); |
Willy Tarreau | 8319593 | 2019-01-03 10:26:23 +0100 | [diff] [blame] | 3382 | if (outlen > 0) |
| 3383 | b_add(rxbuf, outlen); |
Willy Tarreau | c3e18f3 | 2018-10-08 14:51:56 +0200 | [diff] [blame] | 3384 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3385 | |
| 3386 | if (outlen < 0) { |
Willy Tarreau | 2591923 | 2019-01-03 14:48:18 +0100 | [diff] [blame] | 3387 | /* too large headers? this is a stream error only */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 3388 | goto fail; |
| 3389 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3390 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3391 | if (msgf & H2_MSGF_BODY) { |
| 3392 | /* a payload is present */ |
| 3393 | if (msgf & H2_MSGF_BODY_CL) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3394 | *flags |= H2_SF_DATA_CLEN; |
Olivier Houchard | 50d660c | 2018-12-08 00:18:31 +0100 | [diff] [blame] | 3395 | else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3396 | *flags |= H2_SF_DATA_CHNK; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 3397 | } |
| 3398 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3399 | done: |
Willy Tarreau | 6cc85a5 | 2019-01-02 15:49:20 +0100 | [diff] [blame] | 3400 | /* indicate that a HEADERS frame was received for this stream */ |
| 3401 | *flags |= H2_SF_HEADERS_RCVD; |
| 3402 | |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3403 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 3404 | /* Mark the end of message, either using EOM in HTX or with the |
| 3405 | * trailing CRLF after the end of trailers. Note that DATA_CHNK |
| 3406 | * is not set during headers with END_STREAM. |
| 3407 | */ |
| 3408 | if (htx) { |
| 3409 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 3410 | goto fail; |
| 3411 | } |
| 3412 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3413 | if (!b_putblk(rxbuf, "\r\n", 2)) |
| 3414 | goto fail; |
| 3415 | } |
| 3416 | } |
Willy Tarreau | 937f760 | 2018-02-26 15:22:17 +0100 | [diff] [blame] | 3417 | |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3418 | /* success */ |
| 3419 | ret = 1; |
| 3420 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3421 | leave: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3422 | /* 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] | 3423 | * move the remaining data over it. |
| 3424 | */ |
| 3425 | if (hole) { |
| 3426 | if (b_data(&h2c->dbuf) > h2c->dfl + hole) |
| 3427 | b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole), |
| 3428 | b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole); |
| 3429 | b_sub(&h2c->dbuf, hole); |
| 3430 | } |
| 3431 | |
| 3432 | if (b_full(&h2c->dbuf) && h2c->dfl > b_data(&h2c->dbuf)) { |
| 3433 | /* too large frames */ |
| 3434 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3435 | ret = -1; |
Willy Tarreau | ea18f86 | 2018-12-22 20:19:26 +0100 | [diff] [blame] | 3436 | } |
| 3437 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3438 | if (htx) |
Willy Tarreau | 5c8cafa | 2018-12-23 11:30:42 +0100 | [diff] [blame] | 3439 | htx_to_buf(htx, rxbuf); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3440 | free_trash_chunk(copy); |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3441 | return ret; |
| 3442 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3443 | fail: |
Willy Tarreau | 86277d4 | 2019-01-02 15:36:11 +0100 | [diff] [blame] | 3444 | ret = -1; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 3445 | goto leave; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3446 | |
| 3447 | trailers: |
| 3448 | /* This is the last HEADERS frame hence a trailer */ |
| 3449 | |
| 3450 | if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) { |
| 3451 | /* It's a trailer but it's missing ES flag */ |
| 3452 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 3453 | goto fail; |
| 3454 | } |
| 3455 | |
| 3456 | /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD |
| 3457 | * block, and when using chunks we must send the 0 CRLF marker. For |
| 3458 | * other modes, the trailers are silently dropped. |
| 3459 | */ |
| 3460 | if (htx) { |
| 3461 | if (!htx_add_endof(htx, HTX_BLK_EOD)) |
| 3462 | goto fail; |
Willy Tarreau | 5255f28 | 2019-01-03 18:41:05 +0100 | [diff] [blame] | 3463 | if (h2_make_htx_trailers(list, htx) <= 0) |
| 3464 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3465 | } |
| 3466 | else if (*flags & H2_SF_DATA_CHNK) { |
| 3467 | /* Legacy mode with chunked encoding : we must finalize the |
| 3468 | * data block message emit the trailing CRLF */ |
| 3469 | if (!b_putblk(rxbuf, "0\r\n", 3)) |
| 3470 | goto fail; |
Willy Tarreau | e2b05cc | 2019-01-03 16:18:34 +0100 | [diff] [blame] | 3471 | |
| 3472 | outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try); |
| 3473 | if (outlen > 0) |
| 3474 | b_add(rxbuf, outlen); |
| 3475 | else |
| 3476 | goto fail; |
Willy Tarreau | 88d138e | 2019-01-02 19:38:14 +0100 | [diff] [blame] | 3477 | } |
| 3478 | |
| 3479 | goto done; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 3480 | } |
| 3481 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3482 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 3483 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 3484 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 3485 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 3486 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 3487 | * 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] | 3488 | * parser state is automatically updated. Returns > 0 if it could completely |
| 3489 | * send the current frame, 0 if it couldn't complete, in which case |
| 3490 | * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty |
| 3491 | * DATA frame can return 0 as a valid result). Stream errors are reported in |
| 3492 | * h2s->errcode and connection errors in h2c->errcode. The caller must already |
| 3493 | * have checked the frame header and ensured that the frame was complete or the |
| 3494 | * buffer full. It changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3495 | */ |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3496 | static int h2_frt_transfer_data(struct h2s *h2s) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3497 | { |
| 3498 | struct h2c *h2c = h2s->h2c; |
| 3499 | int block1, block2; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3500 | unsigned int flen = 0; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3501 | unsigned int chklen = 0; |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3502 | struct htx *htx = NULL; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3503 | struct buffer *csbuf; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3504 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3505 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3506 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 3507 | csbuf = h2_get_buf(h2c, &h2s->rxbuf); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3508 | if (!csbuf) { |
| 3509 | h2c->flags |= H2_CF_DEM_SALLOC; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3510 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3511 | } |
| 3512 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3513 | try_again: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3514 | flen = h2c->dfl - h2c->dpl; |
Olivier Houchard | 2f30883 | 2018-12-19 15:53:53 +0100 | [diff] [blame] | 3515 | if (h2c->proxy->options2 & PR_O2_USE_HTX) |
| 3516 | htx = htx_from_buf(csbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3517 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3518 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3519 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3520 | if (flen > b_data(&h2c->dbuf)) { |
| 3521 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3522 | if (!flen) |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3523 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3524 | } |
| 3525 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3526 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3527 | block1 = htx_free_data_space(htx); |
| 3528 | if (!block1) { |
| 3529 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3530 | goto fail; |
| 3531 | } |
| 3532 | if (flen > block1) |
| 3533 | flen = block1; |
| 3534 | |
| 3535 | /* here, flen is the max we can copy into the output buffer */ |
| 3536 | block1 = b_contig_data(&h2c->dbuf, 0); |
| 3537 | if (flen > block1) |
| 3538 | flen = block1; |
| 3539 | |
| 3540 | if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) { |
| 3541 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3542 | goto fail; |
| 3543 | } |
| 3544 | |
| 3545 | b_del(&h2c->dbuf, flen); |
| 3546 | h2c->dfl -= flen; |
| 3547 | h2c->rcvd_c += flen; |
| 3548 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 3549 | goto try_again; |
| 3550 | } |
| 3551 | else if (unlikely(b_space_wraps(csbuf))) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3552 | /* it doesn't fit and the buffer is fragmented, |
| 3553 | * so let's defragment it and try again. |
| 3554 | */ |
| 3555 | b_slow_realign(csbuf, trash.area, 0); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3556 | } |
| 3557 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3558 | /* chunked-encoding requires more room */ |
| 3559 | if (h2s->flags & H2_SF_DATA_CHNK) { |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3560 | chklen = MIN(flen, b_room(csbuf)); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3561 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 3562 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 3563 | (chklen < 1048576) ? 4 : 8; |
| 3564 | chklen += 4; // CRLF, CRLF |
| 3565 | } |
| 3566 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3567 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3568 | if (flen + chklen > b_room(csbuf)) { |
| 3569 | if (chklen >= b_room(csbuf)) { |
| 3570 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3571 | goto fail; |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3572 | } |
| 3573 | flen = b_room(csbuf) - chklen; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3574 | } |
| 3575 | |
| 3576 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3577 | /* emit the chunk size */ |
| 3578 | unsigned int chksz = flen; |
| 3579 | char str[10]; |
| 3580 | char *beg; |
| 3581 | |
| 3582 | beg = str + sizeof(str); |
| 3583 | *--beg = '\n'; |
| 3584 | *--beg = '\r'; |
| 3585 | do { |
| 3586 | *--beg = hextab[chksz & 0xF]; |
| 3587 | } while (chksz >>= 4); |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3588 | b_putblk(csbuf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3589 | } |
| 3590 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3591 | /* Block1 is the length of the first block before the buffer wraps, |
| 3592 | * block2 is the optional second block to reach the end of the frame. |
| 3593 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3594 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3595 | if (block1 > flen) |
| 3596 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3597 | block2 = flen - block1; |
| 3598 | |
| 3599 | if (block1) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3600 | b_putblk(csbuf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3601 | |
| 3602 | if (block2) |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3603 | b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3604 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3605 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3606 | /* emit the CRLF */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3607 | b_putblk(csbuf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3608 | } |
| 3609 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3610 | /* now mark the input data as consumed (will be deleted from the buffer |
| 3611 | * by the caller when seeing FRAME_A after sending the window update). |
| 3612 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3613 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3614 | h2c->dfl -= flen; |
| 3615 | h2c->rcvd_c += flen; |
| 3616 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 3617 | |
| 3618 | if (h2c->dfl > h2c->dpl) { |
| 3619 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3620 | h2c->flags |= H2_CF_DEM_SFULL; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3621 | goto fail; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3622 | } |
| 3623 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 3624 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 3625 | /* here we're done with the frame, all the payload (except padding) was |
| 3626 | * transferred. |
| 3627 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3628 | |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3629 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 3630 | if (htx) { |
| 3631 | if (!htx_add_endof(htx, HTX_BLK_EOM)) { |
| 3632 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3633 | goto fail; |
| 3634 | } |
Willy Tarreau | d755ea6 | 2018-02-26 15:44:54 +0100 | [diff] [blame] | 3635 | } |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3636 | else if (h2s->flags & H2_SF_DATA_CHNK) { |
| 3637 | /* emit the trailing 0 CRLF CRLF */ |
| 3638 | if (b_room(csbuf) < 5) { |
| 3639 | h2c->flags |= H2_CF_DEM_SFULL; |
| 3640 | goto fail; |
| 3641 | } |
| 3642 | chklen += 5; |
| 3643 | b_putblk(csbuf, "0\r\n\r\n", 5); |
| 3644 | } |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 3645 | } |
| 3646 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 3647 | h2c->rcvd_c += h2c->dpl; |
| 3648 | h2c->rcvd_s += h2c->dpl; |
| 3649 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3650 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
| 3651 | |
Willy Tarreau | 39d6850 | 2018-03-02 12:26:37 +0100 | [diff] [blame] | 3652 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3653 | h2s->flags |= H2_SF_ES_RCVD; |
Willy Tarreau | 39d6850 | 2018-03-02 12:26:37 +0100 | [diff] [blame] | 3654 | h2s->cs->flags |= CS_FL_REOS; |
| 3655 | } |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3656 | if (htx) |
| 3657 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 61ea7dc | 2018-12-01 23:23:04 +0100 | [diff] [blame] | 3658 | return 1; |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3659 | fail: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3660 | if (htx) |
| 3661 | htx_to_buf(htx, csbuf); |
Willy Tarreau | 454b57b | 2018-02-26 15:50:05 +0100 | [diff] [blame] | 3662 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 3663 | } |
| 3664 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3665 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 3666 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 3667 | * number of bytes sent. The caller must check the stream's status to detect |
| 3668 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3669 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3670 | 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] | 3671 | { |
| 3672 | struct http_hdr list[MAX_HTTP_HDR]; |
| 3673 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3674 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3675 | struct buffer outbuf; |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3676 | union h1_sl sl; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3677 | int es_now = 0; |
| 3678 | int ret = 0; |
| 3679 | int hdr; |
| 3680 | |
| 3681 | if (h2c_mux_busy(h2c, h2s)) { |
| 3682 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3683 | return 0; |
| 3684 | } |
| 3685 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3686 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3687 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3688 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3689 | return 0; |
| 3690 | } |
| 3691 | |
| 3692 | /* First, try to parse the H1 response and index it into <list>. |
| 3693 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 3694 | * block does not wrap and we can safely read it this way without |
| 3695 | * having to realign the buffer. |
| 3696 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3697 | 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] | 3698 | list, sizeof(list)/sizeof(list[0]), h1m, &sl); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3699 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 3700 | /* incomplete or invalid response, this is abnormal coming from |
| 3701 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 3702 | * so that won't be fixed, raise an error now. |
| 3703 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3704 | * FIXME: we should instead add the ability to only return a |
| 3705 | * 502 bad gateway. But in theory this is not supposed to |
| 3706 | * happen. |
| 3707 | */ |
| 3708 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3709 | ret = 0; |
| 3710 | goto end; |
| 3711 | } |
| 3712 | |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3713 | h2s->status = sl.st.status; |
Willy Tarreau | db72da0 | 2018-09-13 11:52:20 +0200 | [diff] [blame] | 3714 | |
| 3715 | /* certain statuses have no body or an empty one, regardless of |
| 3716 | * what the headers say. |
| 3717 | */ |
| 3718 | if (sl.st.status >= 100 && sl.st.status < 200) { |
| 3719 | h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK); |
| 3720 | h1m->curr_len = h1m->body_len = 0; |
| 3721 | } |
| 3722 | else if (sl.st.status == 204 || sl.st.status == 304) { |
| 3723 | /* no contents, claim c-len is present and set to zero */ |
| 3724 | h1m->flags &= ~H1_MF_CHNK; |
| 3725 | h1m->flags |= H1_MF_CLEN; |
| 3726 | h1m->curr_len = h1m->body_len = 0; |
| 3727 | } |
| 3728 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3729 | chunk_reset(&outbuf); |
| 3730 | |
| 3731 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3732 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3733 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3734 | outbuf.data = 0; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3735 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3736 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3737 | break; |
| 3738 | realign_again: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3739 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3740 | } |
| 3741 | |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3742 | if (outbuf.size < 9) |
| 3743 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3744 | |
| 3745 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3746 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 3747 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3748 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3749 | |
| 3750 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3751 | if (unlikely(list[0].v.len != 3)) { |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 3752 | /* this is an unparsable response */ |
| 3753 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3754 | ret = 0; |
| 3755 | goto end; |
| 3756 | } |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 3757 | |
| 3758 | if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3759 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3760 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3761 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3762 | } |
| 3763 | |
| 3764 | /* encode all headers, stop at empty name */ |
| 3765 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 3766 | /* these ones do not exist in H2 and must be dropped. */ |
| 3767 | if (isteq(list[hdr].n, ist("connection")) || |
| 3768 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 3769 | isteq(list[hdr].n, ist("keep-alive")) || |
| 3770 | isteq(list[hdr].n, ist("upgrade")) || |
| 3771 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3772 | continue; |
| 3773 | |
| 3774 | if (isteq(list[hdr].n, ist(""))) |
| 3775 | break; // end |
| 3776 | |
| 3777 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 3778 | /* output full */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3779 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3780 | goto realign_again; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3781 | goto full; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3782 | } |
| 3783 | } |
| 3784 | |
| 3785 | /* we may need to add END_STREAM */ |
| 3786 | if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW) |
| 3787 | es_now = 1; |
| 3788 | |
| 3789 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3790 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3791 | |
| 3792 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3793 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3794 | |
| 3795 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3796 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3797 | |
| 3798 | /* commit the H2 response */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3799 | b_add(&h2c->mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3800 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3801 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3802 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3803 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3804 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3805 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3806 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3807 | h2s->flags |= H2_SF_ES_SENT; |
| 3808 | if (h2s->st == H2_SS_OPEN) |
| 3809 | h2s->st = H2_SS_HLOC; |
| 3810 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3811 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3812 | } |
Willy Tarreau | 9c5e22e | 2018-09-11 19:22:14 +0200 | [diff] [blame] | 3813 | else if (h2s->status >= 100 && h2s->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3814 | /* 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] | 3815 | h1m_init_res(h1m); |
Willy Tarreau | 9b8cd1f | 2018-09-12 09:24:38 +0200 | [diff] [blame] | 3816 | 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] | 3817 | h2s->h1m.flags |= H1_MF_TOLOWER; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3818 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 3819 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 3820 | |
| 3821 | /* 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] | 3822 | |
| 3823 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 3824 | //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] | 3825 | return ret; |
Willy Tarreau | b5b7d4a | 2018-09-12 18:51:18 +0200 | [diff] [blame] | 3826 | full: |
| 3827 | h1m_init_res(h1m); |
| 3828 | h1m->err_pos = -1; // don't care about errors on the response path |
| 3829 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3830 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3831 | ret = 0; |
| 3832 | goto end; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3833 | } |
| 3834 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3835 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 3836 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 3837 | * the number of bytes sent. The caller must check the stream's status to |
| 3838 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3839 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3840 | 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] | 3841 | { |
| 3842 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 3843 | struct h1m *h1m = &h2s->h1m; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3844 | struct buffer outbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3845 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 3846 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3847 | int es_now = 0; |
| 3848 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3849 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3850 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3851 | |
| 3852 | if (h2c_mux_busy(h2c, h2s)) { |
| 3853 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3854 | goto end; |
| 3855 | } |
| 3856 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3857 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3858 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3859 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3860 | goto end; |
| 3861 | } |
| 3862 | |
| 3863 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3864 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3865 | goto end; |
| 3866 | |
| 3867 | chunk_reset(&outbuf); |
| 3868 | |
| 3869 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3870 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3871 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3872 | outbuf.data = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3873 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3874 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3875 | break; |
| 3876 | realign_again: |
Willy Tarreau | 06ae84a | 2018-12-12 09:17:21 +0100 | [diff] [blame] | 3877 | /* If there are pending data in the output buffer, and we have |
| 3878 | * less than 1/4 of the mbuf's size and everything fits, we'll |
| 3879 | * still perform a copy anyway. Otherwise we'll pretend the mbuf |
| 3880 | * is full and wait, to save some slow realign calls. |
| 3881 | */ |
| 3882 | if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) { |
| 3883 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3884 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3885 | goto end; |
| 3886 | } |
| 3887 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3888 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3889 | } |
| 3890 | |
| 3891 | if (outbuf.size < 9) { |
| 3892 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3893 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3894 | goto end; |
| 3895 | } |
| 3896 | |
| 3897 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3898 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 3899 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3900 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3901 | |
| 3902 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 3903 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3904 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 3905 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3906 | break; |
| 3907 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3908 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3909 | if ((long long)size > h1m->curr_len) |
| 3910 | size = h1m->curr_len; |
| 3911 | break; |
| 3912 | default: /* te:chunked : parse chunks */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3913 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 3914 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3915 | if (!ret) |
| 3916 | goto end; |
| 3917 | |
| 3918 | if (ret < 0) { |
| 3919 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 3920 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3921 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3922 | goto end; |
| 3923 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3924 | max -= ret; |
| 3925 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3926 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3927 | h1m->state = H1_MSG_CHUNK_SIZE; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3928 | } |
| 3929 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3930 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3931 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 3932 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3933 | if (!ret) |
| 3934 | goto end; |
| 3935 | |
| 3936 | if (ret < 0) { |
| 3937 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
Willy Tarreau | 25173a7 | 2018-09-12 09:05:16 +0200 | [diff] [blame] | 3938 | h1m->err_pos = ofs + max + ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3939 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3940 | goto end; |
| 3941 | } |
| 3942 | |
| 3943 | size = chunk; |
| 3944 | h1m->curr_len = chunk; |
| 3945 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3946 | max -= ret; |
| 3947 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3948 | total += ret; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 3949 | h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3950 | if (!size) |
| 3951 | goto send_empty; |
| 3952 | } |
| 3953 | |
| 3954 | /* in MSG_DATA state, continue below */ |
| 3955 | size = h1m->curr_len; |
| 3956 | break; |
| 3957 | } |
| 3958 | |
| 3959 | /* we have in <size> the exact number of bytes we need to copy from |
| 3960 | * the H1 buffer. We need to check this against the connection's and |
| 3961 | * the stream's send windows, and to ensure that this fits in the max |
| 3962 | * frame size and in the buffer's available space minus 9 bytes (for |
| 3963 | * the frame header). The connection's flow control is applied last so |
| 3964 | * that we can use a separate list of streams which are immediately |
| 3965 | * unblocked on window opening. Note: we don't implement padding. |
| 3966 | */ |
| 3967 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3968 | if (size > max) |
| 3969 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3970 | |
| 3971 | if (size > h2s->mws) |
| 3972 | size = h2s->mws; |
| 3973 | |
| 3974 | if (size <= 0) { |
| 3975 | h2s->flags |= H2_SF_BLK_SFCTL; |
Olivier Houchard | dddfe31 | 2018-10-10 18:51:00 +0200 | [diff] [blame] | 3976 | if (h2s->send_wait) { |
| 3977 | LIST_DEL(&h2s->list); |
| 3978 | LIST_INIT(&h2s->list); |
| 3979 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3980 | goto end; |
| 3981 | } |
| 3982 | |
| 3983 | if (h2c->mfs && size > h2c->mfs) |
| 3984 | size = h2c->mfs; |
| 3985 | |
| 3986 | if (size + 9 > outbuf.size) { |
| 3987 | /* we have an opportunity for enlarging the too small |
| 3988 | * available space, let's try. |
| 3989 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3990 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3991 | goto realign_again; |
| 3992 | size = outbuf.size - 9; |
| 3993 | } |
| 3994 | |
| 3995 | if (size <= 0) { |
| 3996 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3997 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3998 | goto end; |
| 3999 | } |
| 4000 | |
| 4001 | if (size > h2c->mws) |
| 4002 | size = h2c->mws; |
| 4003 | |
| 4004 | if (size <= 0) { |
| 4005 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4006 | goto end; |
| 4007 | } |
| 4008 | |
| 4009 | /* copy whatever we can */ |
| 4010 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4011 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4012 | if (ret == 1) |
| 4013 | len2 = 0; |
| 4014 | |
| 4015 | if (!ret || len1 + len2 < size) { |
| 4016 | /* FIXME: must normally never happen */ |
| 4017 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4018 | goto end; |
| 4019 | } |
| 4020 | |
| 4021 | /* limit len1/len2 to size */ |
| 4022 | if (len1 + len2 > size) { |
| 4023 | int sub = len1 + len2 - size; |
| 4024 | |
| 4025 | if (len2 > sub) |
| 4026 | len2 -= sub; |
| 4027 | else { |
| 4028 | sub -= len2; |
| 4029 | len2 = 0; |
| 4030 | len1 -= sub; |
| 4031 | } |
| 4032 | } |
| 4033 | |
| 4034 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4035 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4036 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4037 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4038 | |
| 4039 | send_empty: |
| 4040 | /* we may need to add END_STREAM */ |
| 4041 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 4042 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 4043 | * |
| 4044 | * FIXME: what we do here is not correct because we send end_stream |
| 4045 | * before knowing if we'll have to send a HEADERS frame for the |
| 4046 | * trailers. More importantly we're not consuming the trailing CRLF |
| 4047 | * after the end of trailers, so it will be left to the caller to |
| 4048 | * eat it. The right way to do it would be to measure trailers here |
| 4049 | * and to send ES only if there are no trailers. |
| 4050 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4051 | */ |
| 4052 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4053 | !h1m->curr_len || h1m->state >= H1_MSG_DONE) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4054 | es_now = 1; |
| 4055 | |
| 4056 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4057 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4058 | |
| 4059 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4060 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4061 | |
| 4062 | /* commit the H2 response */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4063 | b_add(&h2c->mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4064 | |
| 4065 | /* consume incoming H1 response */ |
| 4066 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4067 | max -= size; |
| 4068 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4069 | total += size; |
| 4070 | h1m->curr_len -= size; |
| 4071 | h2s->mws -= size; |
| 4072 | h2c->mws -= size; |
| 4073 | |
| 4074 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4075 | h1m->state = H1_MSG_CHUNK_CRLF; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4076 | goto new_frame; |
| 4077 | } |
| 4078 | } |
| 4079 | |
| 4080 | if (es_now) { |
| 4081 | if (h2s->st == H2_SS_OPEN) |
| 4082 | h2s->st = H2_SS_HLOC; |
| 4083 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 4084 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4085 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4086 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 4087 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 4088 | total += max; |
| 4089 | ofs += max; |
| 4090 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4091 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 4092 | h1m->state = H1_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 4093 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 4094 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 4095 | h2s->flags |= H2_SF_ES_SENT; |
| 4096 | } |
| 4097 | |
| 4098 | end: |
Dirkjan Bussink | c26c72d | 2018-09-14 14:30:25 +0200 | [diff] [blame] | 4099 | 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] | 4100 | return total; |
| 4101 | } |
| 4102 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4103 | /* Try to send a HEADERS frame matching HTX response present in HTX message |
| 4104 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4105 | * must check the stream's status to detect any error which might have happened |
| 4106 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4107 | * from the message. The htx message is assumed to be valid since produced from |
| 4108 | * the internal code, hence it contains a start line, an optional series of |
| 4109 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4110 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4111 | */ |
| 4112 | static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx) |
| 4113 | { |
| 4114 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4115 | struct h2c *h2c = h2s->h2c; |
| 4116 | struct htx_blk *blk; |
| 4117 | struct htx_blk *blk_end; |
| 4118 | struct buffer outbuf; |
| 4119 | struct htx_sl *sl; |
| 4120 | enum htx_blk_type type; |
| 4121 | int es_now = 0; |
| 4122 | int ret = 0; |
| 4123 | int hdr; |
| 4124 | int idx; |
| 4125 | |
| 4126 | if (h2c_mux_busy(h2c, h2s)) { |
| 4127 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4128 | return 0; |
| 4129 | } |
| 4130 | |
| 4131 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4132 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4133 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4134 | return 0; |
| 4135 | } |
| 4136 | |
| 4137 | /* determine the first block which must not be deleted, blk_end may |
| 4138 | * be NULL if all blocks have to be deleted. |
| 4139 | */ |
| 4140 | idx = htx_get_head(htx); |
| 4141 | blk_end = NULL; |
| 4142 | while (idx != -1) { |
| 4143 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4144 | idx = htx_get_next(htx, idx); |
| 4145 | if (type == HTX_BLK_EOH) { |
| 4146 | if (idx != -1) |
| 4147 | blk_end = htx_get_blk(htx, idx); |
| 4148 | break; |
| 4149 | } |
| 4150 | } |
| 4151 | |
| 4152 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4153 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4154 | ALREADY_CHECKED(sl); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4155 | h2s->status = sl->info.res.status; |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4156 | if (h2s->status < 100 || h2s->status > 999) |
| 4157 | goto fail; |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4158 | |
| 4159 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4160 | hdr = 0; |
| 4161 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4162 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4163 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4164 | blk = htx_get_blk(htx, idx); |
| 4165 | type = htx_get_blk_type(blk); |
| 4166 | |
| 4167 | if (type == HTX_BLK_UNUSED) |
| 4168 | continue; |
| 4169 | |
| 4170 | if (type != HTX_BLK_HDR) |
| 4171 | break; |
| 4172 | |
| 4173 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4174 | goto fail; |
| 4175 | |
| 4176 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4177 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4178 | hdr++; |
| 4179 | } |
| 4180 | |
| 4181 | /* marker for end of headers */ |
| 4182 | list[hdr].n = ist(""); |
| 4183 | |
| 4184 | if (h2s->status == 204 || h2s->status == 304) { |
| 4185 | /* no contents, claim c-len is present and set to zero */ |
| 4186 | es_now = 1; |
| 4187 | } |
| 4188 | |
| 4189 | chunk_reset(&outbuf); |
| 4190 | |
| 4191 | while (1) { |
| 4192 | outbuf.area = b_tail(&h2c->mbuf); |
| 4193 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4194 | outbuf.data = 0; |
| 4195 | |
| 4196 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4197 | break; |
| 4198 | realign_again: |
| 4199 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4200 | } |
| 4201 | |
| 4202 | if (outbuf.size < 9) |
| 4203 | goto full; |
| 4204 | |
| 4205 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4206 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4207 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4208 | outbuf.data = 9; |
| 4209 | |
| 4210 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | aafdf58 | 2018-12-10 18:06:40 +0100 | [diff] [blame] | 4211 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4212 | if (b_space_wraps(&h2c->mbuf)) |
| 4213 | goto realign_again; |
| 4214 | goto full; |
| 4215 | } |
| 4216 | |
| 4217 | /* encode all headers, stop at empty name */ |
| 4218 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4219 | /* these ones do not exist in H2 and must be dropped. */ |
| 4220 | if (isteq(list[hdr].n, ist("connection")) || |
| 4221 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4222 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4223 | isteq(list[hdr].n, ist("upgrade")) || |
| 4224 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4225 | continue; |
| 4226 | |
| 4227 | if (isteq(list[hdr].n, ist(""))) |
| 4228 | break; // end |
| 4229 | |
| 4230 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4231 | /* output full */ |
| 4232 | if (b_space_wraps(&h2c->mbuf)) |
| 4233 | goto realign_again; |
| 4234 | goto full; |
| 4235 | } |
| 4236 | } |
| 4237 | |
| 4238 | /* we may need to add END_STREAM. |
| 4239 | * FIXME: we should also set it when we know for sure that the |
| 4240 | * content-length is zero as well as on 204/304 |
| 4241 | */ |
| 4242 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4243 | es_now = 1; |
| 4244 | |
| 4245 | if (h2s->cs->flags & CS_FL_SHW) |
| 4246 | es_now = 1; |
| 4247 | |
| 4248 | /* update the frame's size */ |
| 4249 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4250 | |
| 4251 | if (es_now) |
| 4252 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4253 | |
| 4254 | /* commit the H2 response */ |
| 4255 | b_add(&h2c->mbuf, outbuf.data); |
| 4256 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4257 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4258 | if (es_now) { |
| 4259 | h2s->flags |= H2_SF_ES_SENT; |
| 4260 | if (h2s->st == H2_SS_OPEN) |
| 4261 | h2s->st = H2_SS_HLOC; |
| 4262 | else |
| 4263 | h2s_close(h2s); |
| 4264 | } |
| 4265 | |
| 4266 | /* OK we could properly deliver the response */ |
| 4267 | |
| 4268 | /* remove all header blocks including the EOH and compute the |
| 4269 | * corresponding size. |
| 4270 | * |
| 4271 | * FIXME: We should remove everything when es_now is set. |
| 4272 | */ |
| 4273 | ret = 0; |
| 4274 | idx = htx_get_head(htx); |
| 4275 | blk = htx_get_blk(htx, idx); |
| 4276 | while (blk != blk_end) { |
| 4277 | ret += htx_get_blksz(blk); |
| 4278 | blk = htx_remove_blk(htx, blk); |
| 4279 | } |
Willy Tarreau | c5753ae | 2018-12-02 12:28:01 +0100 | [diff] [blame] | 4280 | |
| 4281 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4282 | htx_remove_blk(htx, blk_end); |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 4283 | end: |
| 4284 | return ret; |
| 4285 | full: |
| 4286 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4287 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4288 | ret = 0; |
| 4289 | goto end; |
| 4290 | fail: |
| 4291 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4292 | * list etc go here (unrecoverable errors). |
| 4293 | */ |
| 4294 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4295 | ret = 0; |
| 4296 | goto end; |
| 4297 | } |
| 4298 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4299 | /* Try to send a HEADERS frame matching HTX request present in HTX message |
| 4300 | * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller |
| 4301 | * must check the stream's status to detect any error which might have happened |
| 4302 | * subsequently to a successful send. The htx blocks are automatically removed |
| 4303 | * from the message. The htx message is assumed to be valid since produced from |
| 4304 | * the internal code, hence it contains a start line, an optional series of |
| 4305 | * header blocks and an end of header, otherwise an invalid frame could be |
| 4306 | * emitted and the resulting htx message could be left in an inconsistent state. |
| 4307 | */ |
| 4308 | static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx) |
| 4309 | { |
| 4310 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4311 | struct h2c *h2c = h2s->h2c; |
| 4312 | struct htx_blk *blk; |
| 4313 | struct htx_blk *blk_end; |
| 4314 | struct buffer outbuf; |
| 4315 | struct htx_sl *sl; |
| 4316 | struct ist meth, path; |
| 4317 | enum htx_blk_type type; |
| 4318 | int es_now = 0; |
| 4319 | int ret = 0; |
| 4320 | int hdr; |
| 4321 | int idx; |
| 4322 | |
| 4323 | if (h2c_mux_busy(h2c, h2s)) { |
| 4324 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4325 | return 0; |
| 4326 | } |
| 4327 | |
| 4328 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4329 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4330 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4331 | return 0; |
| 4332 | } |
| 4333 | |
| 4334 | /* determine the first block which must not be deleted, blk_end may |
| 4335 | * be NULL if all blocks have to be deleted. |
| 4336 | */ |
| 4337 | idx = htx_get_head(htx); |
| 4338 | blk_end = NULL; |
| 4339 | while (idx != -1) { |
| 4340 | type = htx_get_blk_type(htx_get_blk(htx, idx)); |
| 4341 | idx = htx_get_next(htx, idx); |
| 4342 | if (type == HTX_BLK_EOH) { |
| 4343 | if (idx != -1) |
| 4344 | blk_end = htx_get_blk(htx, idx); |
| 4345 | break; |
| 4346 | } |
| 4347 | } |
| 4348 | |
| 4349 | /* get the start line, we do have one */ |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4350 | sl = htx_get_stline(htx); |
Willy Tarreau | e2778a4 | 2018-12-08 15:30:46 +0100 | [diff] [blame] | 4351 | ALREADY_CHECKED(sl); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4352 | meth = htx_sl_req_meth(sl); |
| 4353 | path = htx_sl_req_uri(sl); |
| 4354 | |
| 4355 | /* and the rest of the headers, that we dump starting at header 0 */ |
| 4356 | hdr = 0; |
| 4357 | |
Willy Tarreau | 8e162ee | 2018-12-06 14:07:27 +0100 | [diff] [blame] | 4358 | idx = htx_get_head(htx); // returns the SL that we skip |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4359 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 4360 | blk = htx_get_blk(htx, idx); |
| 4361 | type = htx_get_blk_type(blk); |
| 4362 | |
| 4363 | if (type == HTX_BLK_UNUSED) |
| 4364 | continue; |
| 4365 | |
| 4366 | if (type != HTX_BLK_HDR) |
| 4367 | break; |
| 4368 | |
| 4369 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4370 | goto fail; |
| 4371 | |
| 4372 | list[hdr].n = htx_get_blk_name(htx, blk); |
| 4373 | list[hdr].v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4374 | hdr++; |
| 4375 | } |
| 4376 | |
| 4377 | /* marker for end of headers */ |
| 4378 | list[hdr].n = ist(""); |
| 4379 | |
| 4380 | chunk_reset(&outbuf); |
| 4381 | |
| 4382 | while (1) { |
| 4383 | outbuf.area = b_tail(&h2c->mbuf); |
| 4384 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4385 | outbuf.data = 0; |
| 4386 | |
| 4387 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4388 | break; |
| 4389 | realign_again: |
| 4390 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4391 | } |
| 4392 | |
| 4393 | if (outbuf.size < 9) |
| 4394 | goto full; |
| 4395 | |
| 4396 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 4397 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 4398 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4399 | outbuf.data = 9; |
| 4400 | |
| 4401 | /* encode the method, which necessarily is the first one */ |
Willy Tarreau | bdabc3a | 2018-12-10 18:25:11 +0100 | [diff] [blame] | 4402 | if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4403 | if (b_space_wraps(&h2c->mbuf)) |
| 4404 | goto realign_again; |
| 4405 | goto full; |
| 4406 | } |
| 4407 | |
| 4408 | /* encode the scheme which is always "https" (or 0x86 for "http") */ |
Willy Tarreau | 7561bcb | 2018-12-10 19:17:06 +0100 | [diff] [blame] | 4409 | if (!hpack_encode_scheme(&outbuf, ist("https"))) { |
| 4410 | /* output full */ |
| 4411 | if (b_space_wraps(&h2c->mbuf)) |
| 4412 | goto realign_again; |
| 4413 | goto full; |
| 4414 | } |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4415 | |
| 4416 | /* encode the path, which necessarily is the second one */ |
Willy Tarreau | 9079981 | 2018-12-10 19:28:38 +0100 | [diff] [blame] | 4417 | if (!hpack_encode_path(&outbuf, path)) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4418 | /* output full */ |
| 4419 | if (b_space_wraps(&h2c->mbuf)) |
| 4420 | goto realign_again; |
| 4421 | goto full; |
| 4422 | } |
| 4423 | |
| 4424 | /* encode all headers, stop at empty name */ |
| 4425 | for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
| 4426 | /* these ones do not exist in H2 and must be dropped. */ |
| 4427 | if (isteq(list[hdr].n, ist("connection")) || |
| 4428 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 4429 | isteq(list[hdr].n, ist("keep-alive")) || |
| 4430 | isteq(list[hdr].n, ist("upgrade")) || |
| 4431 | isteq(list[hdr].n, ist("transfer-encoding"))) |
| 4432 | continue; |
| 4433 | |
| 4434 | if (isteq(list[hdr].n, ist(""))) |
| 4435 | break; // end |
| 4436 | |
| 4437 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 4438 | /* output full */ |
| 4439 | if (b_space_wraps(&h2c->mbuf)) |
| 4440 | goto realign_again; |
| 4441 | goto full; |
| 4442 | } |
| 4443 | } |
| 4444 | |
| 4445 | /* we may need to add END_STREAM if we have no body : |
| 4446 | * - request already closed, or : |
| 4447 | * - no transfer-encoding, and : |
| 4448 | * - no content-length or content-length:0 |
| 4449 | * Fixme: this doesn't take into account CONNECT requests. |
| 4450 | */ |
| 4451 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4452 | es_now = 1; |
| 4453 | |
| 4454 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 4455 | es_now = 1; |
| 4456 | |
| 4457 | if (h2s->cs->flags & CS_FL_SHW) |
| 4458 | es_now = 1; |
| 4459 | |
| 4460 | /* update the frame's size */ |
| 4461 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4462 | |
| 4463 | if (es_now) |
| 4464 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
| 4465 | |
| 4466 | /* commit the H2 response */ |
| 4467 | b_add(&h2c->mbuf, outbuf.data); |
| 4468 | h2s->flags |= H2_SF_HEADERS_SENT; |
| 4469 | h2s->st = H2_SS_OPEN; |
| 4470 | |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 4471 | if (es_now) { |
| 4472 | // trim any possibly pending data (eg: inconsistent content-length) |
| 4473 | h2s->flags |= H2_SF_ES_SENT; |
| 4474 | h2s->st = H2_SS_HLOC; |
| 4475 | } |
| 4476 | |
| 4477 | /* remove all header blocks including the EOH and compute the |
| 4478 | * corresponding size. |
| 4479 | * |
| 4480 | * FIXME: We should remove everything when es_now is set. |
| 4481 | */ |
| 4482 | ret = 0; |
| 4483 | idx = htx_get_head(htx); |
| 4484 | blk = htx_get_blk(htx, idx); |
| 4485 | while (blk != blk_end) { |
| 4486 | ret += htx_get_blksz(blk); |
| 4487 | blk = htx_remove_blk(htx, blk); |
| 4488 | } |
| 4489 | |
| 4490 | if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) |
| 4491 | htx_remove_blk(htx, blk_end); |
| 4492 | |
| 4493 | end: |
| 4494 | return ret; |
| 4495 | full: |
| 4496 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4497 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4498 | ret = 0; |
| 4499 | goto end; |
| 4500 | fail: |
| 4501 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4502 | * list etc go here (unrecoverable errors). |
| 4503 | */ |
| 4504 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4505 | ret = 0; |
| 4506 | goto end; |
| 4507 | } |
| 4508 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4509 | /* 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] | 4510 | * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The |
| 4511 | * caller must check the stream's status to detect any error which might have |
| 4512 | * happened subsequently to a successful send. Returns the number of data bytes |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4513 | * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte. |
| 4514 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4515 | 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] | 4516 | { |
| 4517 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4518 | struct htx *htx; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4519 | struct buffer outbuf; |
| 4520 | size_t total = 0; |
| 4521 | int es_now = 0; |
| 4522 | int bsize; /* htx block size */ |
| 4523 | int fsize; /* h2 frame size */ |
| 4524 | struct htx_blk *blk; |
| 4525 | enum htx_blk_type type; |
| 4526 | int idx; |
| 4527 | |
| 4528 | if (h2c_mux_busy(h2c, h2s)) { |
| 4529 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4530 | goto end; |
| 4531 | } |
| 4532 | |
| 4533 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4534 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4535 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4536 | goto end; |
| 4537 | } |
| 4538 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4539 | htx = htx_from_buf(buf); |
| 4540 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4541 | /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However, |
| 4542 | * while looping, we can meet an HTX_BLK_EOM block that we'll leave to |
| 4543 | * the caller to handle. |
| 4544 | */ |
| 4545 | |
| 4546 | new_frame: |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4547 | if (!count || htx_is_empty(htx)) |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4548 | goto end; |
| 4549 | |
| 4550 | idx = htx_get_head(htx); |
| 4551 | blk = htx_get_blk(htx, idx); |
| 4552 | type = htx_get_blk_type(blk); // DATA or EOD or EOM |
| 4553 | bsize = htx_get_blksz(blk); |
| 4554 | fsize = bsize; |
| 4555 | |
| 4556 | if (type == HTX_BLK_EOD) { |
| 4557 | /* if we have an EOD, we're dealing with chunked data. We may |
| 4558 | * have a set of trailers after us that the caller will want to |
| 4559 | * deal with. Let's simply remove the EOD and return. |
| 4560 | */ |
| 4561 | htx_remove_blk(htx, blk); |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4562 | total++; // EOD counts as one byte |
| 4563 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4564 | goto end; |
| 4565 | } |
Willy Tarreau | 7eeb10a | 2019-01-04 09:28:17 +0100 | [diff] [blame] | 4566 | else if (type == HTX_BLK_EOM) { |
| 4567 | if (h2s->flags & H2_SF_ES_SENT) { |
| 4568 | /* ES already sent */ |
| 4569 | htx_remove_blk(htx, blk); |
| 4570 | total++; // EOM counts as one byte |
| 4571 | count--; |
| 4572 | goto end; |
| 4573 | } |
| 4574 | } |
| 4575 | else if (type != HTX_BLK_DATA) |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4576 | goto end; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4577 | |
| 4578 | /* Perform some optimizations to reduce the number of buffer copies. |
| 4579 | * First, if the mux's buffer is empty and the htx area contains |
| 4580 | * exactly one data block of the same size as the requested count, and |
| 4581 | * this count fits within the frame size, the stream's window size, and |
| 4582 | * the connection's window size, then it's possible to simply swap the |
| 4583 | * caller's buffer with the mux's output buffer and adjust offsets and |
| 4584 | * length to match the entire DATA HTX block in the middle. In this |
| 4585 | * case we perform a true zero-copy operation from end-to-end. This is |
| 4586 | * the situation that happens all the time with large files. Second, if |
| 4587 | * this is not possible, but the mux's output buffer is empty, we still |
| 4588 | * have an opportunity to avoid the copy to the intermediary buffer, by |
| 4589 | * making the intermediary buffer's area point to the output buffer's |
| 4590 | * area. In this case we want to skip the HTX header to make sure that |
| 4591 | * copies remain aligned and that this operation remains possible all |
| 4592 | * the time. This goes for headers, data blocks and any data extracted |
| 4593 | * from the HTX blocks. |
| 4594 | */ |
| 4595 | if (unlikely(fsize == count && |
| 4596 | htx->used == 1 && type == HTX_BLK_DATA && |
| 4597 | fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) { |
| 4598 | void *old_area = h2c->mbuf.area; |
| 4599 | |
| 4600 | if (b_data(&h2c->mbuf)) { |
| 4601 | /* too bad there are data left there. If we have less |
| 4602 | * than 1/4 of the mbuf's size and everything fits, |
| 4603 | * we'll perform a copy anyway. Otherwise we'll pretend |
| 4604 | * the mbuf is full and wait. |
| 4605 | */ |
| 4606 | if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf)) |
| 4607 | goto copy; |
| 4608 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4609 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4610 | goto end; |
| 4611 | } |
| 4612 | |
| 4613 | /* map an H2 frame to the HTX block so that we can put the |
| 4614 | * frame header there. |
| 4615 | */ |
| 4616 | h2c->mbuf.area = buf->area; |
Olivier Houchard | 84cca66 | 2018-12-14 16:28:08 +0100 | [diff] [blame] | 4617 | h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9; |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4618 | h2c->mbuf.data = fsize + 9; |
| 4619 | outbuf.area = b_head(&h2c->mbuf); |
| 4620 | |
| 4621 | /* prepend an H2 DATA frame header just before the DATA block */ |
| 4622 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4623 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4624 | h2_set_frame_size(outbuf.area, fsize); |
| 4625 | |
| 4626 | /* update windows */ |
| 4627 | h2s->mws -= fsize; |
| 4628 | h2c->mws -= fsize; |
| 4629 | |
| 4630 | /* and exchange with our old area */ |
| 4631 | buf->area = old_area; |
| 4632 | buf->data = buf->head = 0; |
| 4633 | total += fsize; |
| 4634 | goto end; |
| 4635 | } |
Willy Tarreau | 2fb1d4c | 2018-12-04 15:28:03 +0100 | [diff] [blame] | 4636 | |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 4637 | copy: |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4638 | /* for DATA and EOM we'll have to emit a frame, even if empty */ |
| 4639 | |
| 4640 | while (1) { |
| 4641 | outbuf.area = b_tail(&h2c->mbuf); |
| 4642 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4643 | outbuf.data = 0; |
| 4644 | |
| 4645 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4646 | break; |
| 4647 | realign_again: |
| 4648 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4649 | } |
| 4650 | |
| 4651 | if (outbuf.size < 9) { |
| 4652 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4653 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4654 | goto end; |
| 4655 | } |
| 4656 | |
| 4657 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 4658 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 4659 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4660 | outbuf.data = 9; |
| 4661 | |
| 4662 | /* we have in <fsize> the exact number of bytes we need to copy from |
| 4663 | * the HTX buffer. We need to check this against the connection's and |
| 4664 | * the stream's send windows, and to ensure that this fits in the max |
| 4665 | * frame size and in the buffer's available space minus 9 bytes (for |
| 4666 | * the frame header). The connection's flow control is applied last so |
| 4667 | * that we can use a separate list of streams which are immediately |
| 4668 | * unblocked on window opening. Note: we don't implement padding. |
| 4669 | */ |
| 4670 | |
| 4671 | /* EOM is presented with bsize==1 but would lead to the emission of an |
| 4672 | * empty frame, thus we force it to zero here. |
| 4673 | */ |
| 4674 | if (type == HTX_BLK_EOM) |
| 4675 | bsize = fsize = 0; |
| 4676 | |
| 4677 | if (!fsize) |
| 4678 | goto send_empty; |
| 4679 | |
| 4680 | if (h2s->mws <= 0) { |
| 4681 | h2s->flags |= H2_SF_BLK_SFCTL; |
| 4682 | if (h2s->send_wait) { |
| 4683 | LIST_DEL(&h2s->list); |
| 4684 | LIST_INIT(&h2s->list); |
| 4685 | } |
| 4686 | goto end; |
| 4687 | } |
| 4688 | |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4689 | if (fsize > count) |
| 4690 | fsize = count; |
| 4691 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4692 | if (fsize > h2s->mws) |
| 4693 | fsize = h2s->mws; // >0 |
| 4694 | |
| 4695 | if (h2c->mfs && fsize > h2c->mfs) |
| 4696 | fsize = h2c->mfs; // >0 |
| 4697 | |
| 4698 | if (fsize + 9 > outbuf.size) { |
| 4699 | /* we have an opportunity for enlarging the too small |
| 4700 | * available space, let's try. |
| 4701 | * FIXME: is this really interesting to do? Maybe we'll |
| 4702 | * spend lots of time realigning instead of using two |
| 4703 | * frames. |
| 4704 | */ |
| 4705 | if (b_space_wraps(&h2c->mbuf)) |
| 4706 | goto realign_again; |
| 4707 | fsize = outbuf.size - 9; |
| 4708 | |
| 4709 | if (fsize <= 0) { |
| 4710 | /* no need to send an empty frame here */ |
| 4711 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4712 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4713 | goto end; |
| 4714 | } |
| 4715 | } |
| 4716 | |
| 4717 | if (h2c->mws <= 0) { |
| 4718 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 4719 | goto end; |
| 4720 | } |
| 4721 | |
| 4722 | if (fsize > h2c->mws) |
| 4723 | fsize = h2c->mws; |
| 4724 | |
| 4725 | /* now let's copy this this into the output buffer */ |
| 4726 | memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize); |
Willy Tarreau | 0f799ca | 2018-12-04 15:20:11 +0100 | [diff] [blame] | 4727 | h2s->mws -= fsize; |
| 4728 | h2c->mws -= fsize; |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4729 | count -= fsize; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4730 | |
| 4731 | send_empty: |
| 4732 | /* update the frame's size */ |
| 4733 | h2_set_frame_size(outbuf.area, fsize); |
| 4734 | |
| 4735 | /* FIXME: for now we only set the ES flag on empty DATA frames, once |
| 4736 | * meeting EOM. We should optimize this later. |
| 4737 | */ |
| 4738 | if (type == HTX_BLK_EOM) { |
Willy Tarreau | ee57376 | 2018-12-04 15:25:57 +0100 | [diff] [blame] | 4739 | total++; // EOM counts as one byte |
| 4740 | count--; |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 4741 | es_now = 1; |
| 4742 | } |
| 4743 | |
| 4744 | if (es_now) |
| 4745 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
| 4746 | |
| 4747 | /* commit the H2 response */ |
| 4748 | b_add(&h2c->mbuf, fsize + 9); |
| 4749 | |
| 4750 | /* consume incoming HTX block, including EOM */ |
| 4751 | total += fsize; |
| 4752 | if (fsize == bsize) { |
| 4753 | htx_remove_blk(htx, blk); |
| 4754 | if (fsize) |
| 4755 | goto new_frame; |
| 4756 | } else { |
| 4757 | /* we've truncated this block */ |
| 4758 | htx_cut_data_blk(htx, blk, fsize); |
| 4759 | } |
| 4760 | |
| 4761 | if (es_now) { |
| 4762 | if (h2s->st == H2_SS_OPEN) |
| 4763 | h2s->st = H2_SS_HLOC; |
| 4764 | else |
| 4765 | h2s_close(h2s); |
| 4766 | |
| 4767 | h2s->flags |= H2_SF_ES_SENT; |
| 4768 | } |
| 4769 | |
| 4770 | end: |
| 4771 | return total; |
| 4772 | } |
| 4773 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 4774 | /* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in |
| 4775 | * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes |
| 4776 | * processed. The caller must check the stream's status to detect any error |
| 4777 | * which might have happened subsequently to a successful send. The htx blocks |
| 4778 | * are automatically removed from the message. The htx message is assumed to be |
| 4779 | * valid since produced from the internal code. Processing stops when meeting |
| 4780 | * the EOM, which is also removed. All trailers are processed at once and sent |
| 4781 | * as a single frame. The ES flag is always set. |
| 4782 | */ |
| 4783 | static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx) |
| 4784 | { |
| 4785 | struct http_hdr list[MAX_HTTP_HDR]; |
| 4786 | struct h2c *h2c = h2s->h2c; |
| 4787 | struct htx_blk *blk; |
| 4788 | struct htx_blk *blk_end; |
| 4789 | struct buffer outbuf; |
| 4790 | struct h1m h1m; |
| 4791 | enum htx_blk_type type; |
| 4792 | uint32_t size; |
| 4793 | int ret = 0; |
| 4794 | int hdr; |
| 4795 | int idx; |
| 4796 | void *start; |
| 4797 | |
| 4798 | if (h2c_mux_busy(h2c, h2s)) { |
| 4799 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 4800 | goto end; |
| 4801 | } |
| 4802 | |
| 4803 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
| 4804 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 4805 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4806 | goto end; |
| 4807 | } |
| 4808 | |
| 4809 | /* The principle is that we parse each and every trailers block using |
| 4810 | * the H1 headers parser, and append it to the list. We don't proceed |
| 4811 | * until EOM is met. blk_end will point to the EOM block. |
| 4812 | */ |
| 4813 | hdr = 0; |
| 4814 | memset(list, 0, sizeof(list)); |
| 4815 | blk_end = NULL; |
| 4816 | |
| 4817 | for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) { |
| 4818 | blk = htx_get_blk(htx, idx); |
| 4819 | type = htx_get_blk_type(blk); |
| 4820 | |
| 4821 | if (type == HTX_BLK_UNUSED) |
| 4822 | continue; |
| 4823 | |
| 4824 | if (type != HTX_BLK_TLR) { |
| 4825 | if (type == HTX_BLK_EOM) |
| 4826 | blk_end = blk; |
| 4827 | break; |
| 4828 | } |
| 4829 | |
| 4830 | if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) |
| 4831 | goto fail; |
| 4832 | |
| 4833 | size = htx_get_blksz(blk); |
| 4834 | start = htx_get_blk_ptr(htx, blk); |
| 4835 | |
| 4836 | h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER; |
| 4837 | h1m.err_pos = 0; |
| 4838 | ret = h1_headers_to_hdr_list(start, start + size, |
| 4839 | list + hdr, sizeof(list)/sizeof(list[0]) - hdr, |
| 4840 | &h1m, NULL); |
| 4841 | if (ret < 0) |
| 4842 | goto fail; |
| 4843 | |
| 4844 | /* ret == 0 if an incomplete trailers block was found (missing |
| 4845 | * empty line), or > 0 if it was found. We have to continue on |
| 4846 | * incomplete messages because the trailers block might be |
| 4847 | * incomplete. |
| 4848 | */ |
| 4849 | |
| 4850 | /* search the new end */ |
| 4851 | while (hdr <= sizeof(list)/sizeof(list[0])) { |
| 4852 | if (!list[hdr].n.len) |
| 4853 | break; |
| 4854 | hdr++; |
| 4855 | } |
| 4856 | } |
| 4857 | |
| 4858 | if (!blk_end) |
| 4859 | goto end; // end not found yet |
| 4860 | |
| 4861 | if (!hdr) |
| 4862 | goto done; |
| 4863 | |
| 4864 | chunk_reset(&outbuf); |
| 4865 | |
| 4866 | while (1) { |
| 4867 | outbuf.area = b_tail(&h2c->mbuf); |
| 4868 | outbuf.size = b_contig_space(&h2c->mbuf); |
| 4869 | outbuf.data = 0; |
| 4870 | |
| 4871 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
| 4872 | break; |
| 4873 | realign_again: |
| 4874 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
| 4875 | } |
| 4876 | |
| 4877 | if (outbuf.size < 9) |
| 4878 | goto full; |
| 4879 | |
| 4880 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */ |
| 4881 | memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5); |
| 4882 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 4883 | outbuf.data = 9; |
| 4884 | |
| 4885 | /* encode status, which necessarily is the first one */ |
| 4886 | if (!hpack_encode_int_status(&outbuf, h2s->status)) { |
| 4887 | if (b_space_wraps(&h2c->mbuf)) |
| 4888 | goto realign_again; |
| 4889 | goto full; |
| 4890 | } |
| 4891 | |
| 4892 | /* encode all headers */ |
| 4893 | for (idx = 0; idx < hdr; idx++) { |
| 4894 | /* these ones do not exist in H2 or must not appear in |
| 4895 | * trailers and must be dropped. |
| 4896 | */ |
| 4897 | if (isteq(list[idx].n, ist("host")) || |
| 4898 | isteq(list[idx].n, ist("content-length")) || |
| 4899 | isteq(list[idx].n, ist("connection")) || |
| 4900 | isteq(list[idx].n, ist("proxy-connection")) || |
| 4901 | isteq(list[idx].n, ist("keep-alive")) || |
| 4902 | isteq(list[idx].n, ist("upgrade")) || |
| 4903 | isteq(list[idx].n, ist("te")) || |
| 4904 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 4905 | continue; |
| 4906 | |
| 4907 | if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) { |
| 4908 | /* output full */ |
| 4909 | if (b_space_wraps(&h2c->mbuf)) |
| 4910 | goto realign_again; |
| 4911 | goto full; |
| 4912 | } |
| 4913 | } |
| 4914 | |
| 4915 | /* update the frame's size */ |
| 4916 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
| 4917 | |
| 4918 | /* commit the H2 response */ |
| 4919 | b_add(&h2c->mbuf, outbuf.data); |
| 4920 | h2s->flags |= H2_SF_ES_SENT; |
| 4921 | |
| 4922 | if (h2s->st == H2_SS_OPEN) |
| 4923 | h2s->st = H2_SS_HLOC; |
| 4924 | else |
| 4925 | h2s_close(h2s); |
| 4926 | |
| 4927 | /* OK we could properly deliver the response */ |
| 4928 | done: |
| 4929 | /* remove all header blocks including EOM and compute the corresponding size. */ |
| 4930 | ret = 0; |
| 4931 | idx = htx_get_head(htx); |
| 4932 | blk = htx_get_blk(htx, idx); |
| 4933 | while (blk != blk_end) { |
| 4934 | ret += htx_get_blksz(blk); |
| 4935 | blk = htx_remove_blk(htx, blk); |
| 4936 | } |
| 4937 | blk = htx_remove_blk(htx, blk); |
| 4938 | end: |
| 4939 | return ret; |
| 4940 | full: |
| 4941 | h2c->flags |= H2_CF_MUX_MFULL; |
| 4942 | h2s->flags |= H2_SF_BLK_MROOM; |
| 4943 | ret = 0; |
| 4944 | goto end; |
| 4945 | fail: |
| 4946 | /* unparsable HTX messages, too large ones to be produced in the local |
| 4947 | * list etc go here (unrecoverable errors). |
| 4948 | */ |
| 4949 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 4950 | ret = 0; |
| 4951 | goto end; |
| 4952 | } |
| 4953 | |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4954 | /* Called from the upper layer, to subscribe to events, such as being able to send */ |
| 4955 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 4956 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4957 | struct wait_event *sw; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4958 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 4959 | struct h2c *h2c = h2s->h2c; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4960 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4961 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 4962 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4963 | if (!(sw->events & SUB_RETRY_RECV)) { |
| 4964 | sw->events |= SUB_RETRY_RECV; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 4965 | sw->handle = h2s; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4966 | h2s->recv_wait = sw; |
Olivier Houchard | 4cf7fb1 | 2018-08-02 19:23:05 +0200 | [diff] [blame] | 4967 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4968 | event_type &= ~SUB_RETRY_RECV; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4969 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4970 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4971 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4972 | if (!(sw->events & SUB_RETRY_SEND)) { |
| 4973 | sw->events |= SUB_RETRY_SEND; |
Olivier Houchard | 8ae735d | 2018-09-11 18:24:28 +0200 | [diff] [blame] | 4974 | sw->handle = h2s; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4975 | h2s->send_wait = sw; |
| 4976 | if (!(h2s->flags & H2_SF_BLK_SFCTL)) { |
| 4977 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 4978 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 4979 | else |
| 4980 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 4981 | } |
Olivier Houchard | e1c6dbc | 2018-08-01 17:06:43 +0200 | [diff] [blame] | 4982 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4983 | event_type &= ~SUB_RETRY_SEND; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4984 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4985 | if (event_type != 0) |
| 4986 | return -1; |
| 4987 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 4988 | |
| 4989 | |
| 4990 | } |
| 4991 | |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4992 | static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 4993 | { |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4994 | struct wait_event *sw; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4995 | struct h2s *h2s = cs->ctx; |
| 4996 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 4997 | if (event_type & SUB_RETRY_RECV) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 4998 | sw = param; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 4999 | if (h2s->recv_wait == sw) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5000 | sw->events &= ~SUB_RETRY_RECV; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5001 | h2s->recv_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5002 | } |
| 5003 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5004 | if (event_type & SUB_RETRY_SEND) { |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5005 | sw = param; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5006 | if (h2s->send_wait == sw) { |
| 5007 | LIST_DEL(&h2s->list); |
| 5008 | LIST_INIT(&h2s->list); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5009 | sw->events &= ~SUB_RETRY_SEND; |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5010 | h2s->send_wait = NULL; |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5011 | } |
| 5012 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5013 | if (event_type & SUB_CALL_UNSUBSCRIBE) { |
| 5014 | sw = param; |
| 5015 | if (h2s->send_wait == sw) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5016 | sw->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5017 | h2s->send_wait = NULL; |
Olivier Houchard | f29cd5c | 2018-12-20 11:56:28 +0100 | [diff] [blame] | 5018 | LIST_DEL(&h2s->list); |
| 5019 | LIST_INIT(&h2s->list); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5020 | } |
| 5021 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5022 | return 0; |
| 5023 | } |
| 5024 | |
| 5025 | |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5026 | /* Called from the upper layer, to receive data */ |
| 5027 | static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 5028 | { |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5029 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5030 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5031 | struct htx *h2s_htx = NULL; |
| 5032 | struct htx *buf_htx = NULL; |
| 5033 | struct htx_ret htx_ret; |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5034 | size_t ret = 0; |
| 5035 | |
| 5036 | /* transfer possibly pending data to the upper layer */ |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5037 | if (h2c->proxy->options2 & PR_O2_USE_HTX) { |
| 5038 | /* in HTX mode we ignore the count argument */ |
| 5039 | h2s_htx = htx_from_buf(&h2s->rxbuf); |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5040 | if (htx_is_empty(h2s_htx)) { |
| 5041 | if (cs->flags & CS_FL_REOS) |
| 5042 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5043 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5044 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5045 | goto end; |
Olivier Houchard | 56b0348 | 2018-12-10 16:09:53 +0100 | [diff] [blame] | 5046 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5047 | |
| 5048 | buf_htx = htx_from_buf(buf); |
| 5049 | count = htx_free_space(buf_htx); |
| 5050 | |
Willy Tarreau | 0c22fa7 | 2018-12-04 15:21:35 +0100 | [diff] [blame] | 5051 | 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] | 5052 | |
| 5053 | buf_htx->extra = h2s_htx->extra; |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5054 | htx_to_buf(buf_htx, buf); |
| 5055 | htx_to_buf(h2s_htx, &h2s->rxbuf); |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5056 | ret = htx_ret.ret; |
| 5057 | } |
| 5058 | else { |
| 5059 | ret = b_xfer(buf, &h2s->rxbuf, count); |
| 5060 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5061 | |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5062 | if (b_data(&h2s->rxbuf)) |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5063 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5064 | else { |
Olivier Houchard | d247be0 | 2018-12-06 16:22:29 +0100 | [diff] [blame] | 5065 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5066 | if (cs->flags & CS_FL_REOS) |
| 5067 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 71748cb | 2018-12-17 14:16:46 +0100 | [diff] [blame] | 5068 | if (cs->flags & CS_FL_ERR_PENDING) |
| 5069 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 638b799 | 2018-08-16 15:41:52 +0200 | [diff] [blame] | 5070 | if (b_size(&h2s->rxbuf)) { |
| 5071 | b_free(&h2s->rxbuf); |
| 5072 | offer_buffers(NULL, tasks_run_queue); |
| 5073 | } |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5074 | } |
| 5075 | |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5076 | if (ret && h2c->dsi == h2s->id) { |
| 5077 | /* demux is blocking on this stream's buffer */ |
| 5078 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 872e2fa | 2019-01-03 08:27:41 +0100 | [diff] [blame] | 5079 | h2c_restart_reading(h2c); |
Willy Tarreau | 082f559 | 2018-11-25 08:03:32 +0100 | [diff] [blame] | 5080 | } |
Willy Tarreau | 86724e2 | 2018-12-01 23:19:43 +0100 | [diff] [blame] | 5081 | end: |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5082 | return ret; |
| 5083 | } |
| 5084 | |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5085 | static void h2_stop_senders(struct h2c *h2c) |
| 5086 | { |
| 5087 | struct h2s *h2s, *h2s_back; |
| 5088 | |
| 5089 | list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) { |
| 5090 | /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */ |
| 5091 | if (h2c->msi == h2s_id(h2s)) |
| 5092 | continue; |
| 5093 | LIST_DEL(&h2s->list); |
| 5094 | LIST_INIT(&h2s->list); |
| 5095 | task_remove_from_task_list((struct task *)h2s->send_wait->task); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5096 | h2s->send_wait->events |= SUB_RETRY_SEND; |
| 5097 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5098 | LIST_ADD(&h2c->send_list, &h2s->list); |
| 5099 | } |
| 5100 | } |
| 5101 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5102 | /* Called from the upper layer, to send data */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 5103 | 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] | 5104 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5105 | struct h2s *h2s = cs->ctx; |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5106 | size_t orig_count = count; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 5107 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5108 | size_t ret; |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5109 | struct htx *htx; |
| 5110 | struct htx_blk *blk; |
| 5111 | enum htx_blk_type btype; |
| 5112 | uint32_t bsize; |
| 5113 | int32_t idx; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5114 | |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5115 | if (h2s->send_wait) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5116 | h2s->send_wait->events &= ~SUB_CALL_UNSUBSCRIBE; |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5117 | h2s->send_wait = NULL; |
| 5118 | LIST_DEL(&h2s->list); |
| 5119 | LIST_INIT(&h2s->list); |
| 5120 | } |
Willy Tarreau | 6bf641a | 2018-10-08 09:43:03 +0200 | [diff] [blame] | 5121 | if (h2s->h2c->st0 < H2_CS_FRAME_H) |
| 5122 | return 0; |
| 5123 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5124 | /* htx will be enough to decide if we're using HTX or legacy */ |
| 5125 | htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL; |
| 5126 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5127 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 5128 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 5129 | |
Willy Tarreau | 751f2d0 | 2018-10-05 09:35:00 +0200 | [diff] [blame] | 5130 | if (h2s->id == 0) { |
| 5131 | int32_t id = h2c_get_next_sid(h2s->h2c); |
| 5132 | |
| 5133 | if (id < 0) { |
| 5134 | cs->ctx = NULL; |
| 5135 | cs->flags |= CS_FL_ERROR; |
| 5136 | h2s_destroy(h2s); |
| 5137 | return 0; |
| 5138 | } |
| 5139 | |
| 5140 | eb32_delete(&h2s->by_id); |
| 5141 | h2s->by_id.key = h2s->id = id; |
| 5142 | h2s->h2c->max_id = id; |
| 5143 | eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id); |
| 5144 | } |
| 5145 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5146 | if (htx) { |
Willy Tarreau | c14999b | 2018-12-06 14:09:09 +0100 | [diff] [blame] | 5147 | while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) && |
| 5148 | count && !htx_is_empty(htx)) { |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5149 | idx = htx_get_head(htx); |
| 5150 | blk = htx_get_blk(htx, idx); |
| 5151 | btype = htx_get_blk_type(blk); |
| 5152 | bsize = htx_get_blksz(blk); |
| 5153 | |
| 5154 | switch (btype) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5155 | case HTX_BLK_REQ_SL: |
| 5156 | /* start-line before headers */ |
| 5157 | ret = h2s_htx_bck_make_req_headers(h2s, htx); |
| 5158 | if (ret > 0) { |
| 5159 | total += ret; |
| 5160 | count -= ret; |
| 5161 | if (ret < bsize) |
| 5162 | goto done; |
| 5163 | } |
| 5164 | break; |
| 5165 | |
Willy Tarreau | 115e83b | 2018-12-01 19:17:53 +0100 | [diff] [blame] | 5166 | case HTX_BLK_RES_SL: |
| 5167 | /* start-line before headers */ |
| 5168 | ret = h2s_htx_frt_make_resp_headers(h2s, htx); |
| 5169 | if (ret > 0) { |
| 5170 | total += ret; |
| 5171 | count -= ret; |
| 5172 | if (ret < bsize) |
| 5173 | goto done; |
| 5174 | } |
| 5175 | break; |
| 5176 | |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5177 | case HTX_BLK_DATA: |
| 5178 | case HTX_BLK_EOD: |
| 5179 | case HTX_BLK_EOM: |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5180 | /* all these cause the emission of a DATA frame (possibly empty). |
| 5181 | * This EOM necessarily is one before trailers, as the EOM following |
| 5182 | * trailers would have been consumed by the trailers parser. |
| 5183 | */ |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5184 | ret = h2s_htx_frt_make_resp_data(h2s, buf, count); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5185 | if (ret > 0) { |
Willy Tarreau | 98de12a | 2018-12-12 07:03:00 +0100 | [diff] [blame] | 5186 | htx = htx_from_buf(buf); |
Willy Tarreau | 0c535fd | 2018-12-01 19:25:56 +0100 | [diff] [blame] | 5187 | total += ret; |
| 5188 | count -= ret; |
| 5189 | if (ret < bsize) |
| 5190 | goto done; |
| 5191 | } |
| 5192 | break; |
| 5193 | |
Willy Tarreau | 1bb812f | 2019-01-04 10:56:26 +0100 | [diff] [blame] | 5194 | case HTX_BLK_TLR: |
| 5195 | /* This is the first trailers block, all the subsequent ones AND |
| 5196 | * the EOM will be swallowed by the parser. |
| 5197 | */ |
| 5198 | ret = h2s_htx_make_trailers(h2s, htx); |
| 5199 | if (ret > 0) { |
| 5200 | total += ret; |
| 5201 | count -= ret; |
| 5202 | if (ret < bsize) |
| 5203 | goto done; |
| 5204 | } |
| 5205 | break; |
| 5206 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5207 | default: |
| 5208 | htx_remove_blk(htx, blk); |
| 5209 | total += bsize; |
| 5210 | count -= bsize; |
| 5211 | break; |
| 5212 | } |
| 5213 | } |
| 5214 | goto done; |
| 5215 | } |
| 5216 | |
| 5217 | /* legacy transfer mode */ |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5218 | while (h2s->h1m.state < H1_MSG_DONE && count) { |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 5219 | if (h2s->h1m.state <= H1_MSG_LAST_LF) { |
Willy Tarreau | 8073969 | 2018-10-05 11:35:57 +0200 | [diff] [blame] | 5220 | if (h2s->h2c->flags & H2_CF_IS_BACK) |
| 5221 | ret = -1; |
| 5222 | else |
| 5223 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5224 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5225 | else if (h2s->h1m.state < H1_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5226 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5227 | } |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5228 | else if (h2s->h1m.state == H1_MSG_TRAILERS) { |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5229 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5230 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5231 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 5232 | if (unlikely((int)ret <= 0)) { |
| 5233 | if ((int)ret < 0) |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5234 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 5235 | break; |
| 5236 | } |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 5237 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5238 | total += count; |
| 5239 | count = 0; |
Willy Tarreau | a40704a | 2018-09-11 13:52:04 +0200 | [diff] [blame] | 5240 | h2s->h1m.state = H1_MSG_DONE; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 5241 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 5242 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5243 | else { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5244 | cs_set_error(cs); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5245 | break; |
| 5246 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5247 | |
| 5248 | total += ret; |
| 5249 | count -= ret; |
| 5250 | |
| 5251 | if (h2s->st >= H2_SS_ERROR) |
| 5252 | break; |
| 5253 | |
| 5254 | if (h2s->flags & H2_SF_BLK_ANY) |
| 5255 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5256 | } |
| 5257 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5258 | done: |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5259 | if (h2s->st >= H2_SS_ERROR) { |
| 5260 | /* trim any possibly pending data after we close (extra CR-LF, |
| 5261 | * unprocessed trailers, abnormal extra data, ...) |
| 5262 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 5263 | total += count; |
| 5264 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 5265 | } |
| 5266 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5267 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 5268 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | ec988c7 | 2018-12-19 18:00:29 +0100 | [diff] [blame] | 5269 | cs_set_error(cs); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 5270 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 5271 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 5272 | } |
| 5273 | |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5274 | if (htx) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 5275 | htx_to_buf(htx, buf); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5276 | } else { |
| 5277 | b_del(buf, total); |
| 5278 | } |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5279 | |
| 5280 | /* The mux is full, cancel the pending tasks */ |
| 5281 | if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) || |
| 5282 | (h2s->flags & H2_SF_BLK_MBUSY)) |
| 5283 | h2_stop_senders(h2s->h2c); |
Willy Tarreau | bcd3bb3 | 2018-12-01 18:59:00 +0100 | [diff] [blame] | 5284 | |
Olivier Houchard | 8122a8d | 2018-12-03 19:13:29 +0100 | [diff] [blame] | 5285 | /* If we're running HTX, and we read the whole buffer, then pretend |
| 5286 | * we read exactly what the caller specified, as with HTX the caller |
| 5287 | * will always give the buffer size, instead of the amount of data |
| 5288 | * available. |
| 5289 | */ |
| 5290 | if (htx && !b_data(buf)) |
| 5291 | total = orig_count; |
| 5292 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5293 | if (total > 0) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 5294 | if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5295 | tasklet_wakeup(h2s->h2c->wait_event.task); |
Olivier Houchard | d846c26 | 2018-10-19 17:24:29 +0200 | [diff] [blame] | 5296 | |
Olivier Houchard | 7505f94 | 2018-08-21 18:10:44 +0200 | [diff] [blame] | 5297 | } |
Olivier Houchard | 6dea2ee | 2018-12-19 18:16:17 +0100 | [diff] [blame] | 5298 | /* If we're waiting for flow control, and we got a shutr on the |
| 5299 | * connection, we will never be unlocked, so add an error on |
| 5300 | * the conn_stream. |
| 5301 | */ |
| 5302 | if (conn_xprt_read0_pending(h2s->h2c->conn) && |
| 5303 | !b_data(&h2s->h2c->dbuf) && |
| 5304 | (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) { |
| 5305 | if (cs->flags & CS_FL_EOS) |
| 5306 | cs->flags |= CS_FL_ERROR; |
| 5307 | else |
| 5308 | cs->flags |= CS_FL_ERR_PENDING; |
| 5309 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 5310 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5311 | } |
| 5312 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5313 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5314 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5315 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 5316 | struct h2c *h2c = conn->ctx; |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5317 | struct h2s *h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5318 | struct eb32_node *node; |
| 5319 | int fctl_cnt = 0; |
| 5320 | int send_cnt = 0; |
| 5321 | int tree_cnt = 0; |
| 5322 | int orph_cnt = 0; |
| 5323 | |
| 5324 | if (!h2c) |
| 5325 | return; |
| 5326 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5327 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5328 | fctl_cnt++; |
| 5329 | |
Olivier Houchard | fa8aa86 | 2018-10-10 18:25:41 +0200 | [diff] [blame] | 5330 | list_for_each_entry(h2s, &h2c->send_list, list) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5331 | send_cnt++; |
| 5332 | |
Willy Tarreau | 3af3771 | 2018-12-18 14:34:41 +0100 | [diff] [blame] | 5333 | h2s = NULL; |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5334 | node = eb32_first(&h2c->streams_by_id); |
| 5335 | while (node) { |
| 5336 | h2s = container_of(node, struct h2s, by_id); |
| 5337 | tree_cnt++; |
| 5338 | if (!h2s->cs) |
| 5339 | orph_cnt++; |
| 5340 | node = eb32_next(node); |
| 5341 | } |
| 5342 | |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5343 | chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x" |
| 5344 | " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d" |
| 5345 | " .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] | 5346 | h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags, |
| 5347 | 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] | 5348 | h2c->wait_event.events, h2c->dsi, |
Willy Tarreau | 987c063 | 2018-12-18 10:32:05 +0100 | [diff] [blame] | 5349 | (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf), |
| 5350 | (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), |
| 5351 | h2c->msi, |
| 5352 | (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf), |
| 5353 | (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf)); |
| 5354 | |
| 5355 | if (h2s) { |
| 5356 | chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p", |
| 5357 | h2s, h2s->id, h2s->flags, |
| 5358 | (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf), |
| 5359 | (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf), |
| 5360 | h2s->cs); |
| 5361 | if (h2s->cs) |
| 5362 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 5363 | h2s->cs->flags, h2s->cs->data); |
| 5364 | } |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5365 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5366 | |
| 5367 | /*******************************************************/ |
| 5368 | /* functions below are dedicated to the config parsers */ |
| 5369 | /*******************************************************/ |
| 5370 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5371 | /* config parser for global "tune.h2.header-table-size" */ |
| 5372 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 5373 | struct proxy *defpx, const char *file, int line, |
| 5374 | char **err) |
| 5375 | { |
| 5376 | if (too_many_args(1, args, err, NULL)) |
| 5377 | return -1; |
| 5378 | |
| 5379 | h2_settings_header_table_size = atoi(args[1]); |
| 5380 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 5381 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 5382 | return -1; |
| 5383 | } |
| 5384 | return 0; |
| 5385 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5386 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5387 | /* config parser for global "tune.h2.initial-window-size" */ |
| 5388 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 5389 | struct proxy *defpx, const char *file, int line, |
| 5390 | char **err) |
| 5391 | { |
| 5392 | if (too_many_args(1, args, err, NULL)) |
| 5393 | return -1; |
| 5394 | |
| 5395 | h2_settings_initial_window_size = atoi(args[1]); |
| 5396 | if (h2_settings_initial_window_size < 0) { |
| 5397 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5398 | return -1; |
| 5399 | } |
| 5400 | return 0; |
| 5401 | } |
| 5402 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5403 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 5404 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 5405 | struct proxy *defpx, const char *file, int line, |
| 5406 | char **err) |
| 5407 | { |
| 5408 | if (too_many_args(1, args, err, NULL)) |
| 5409 | return -1; |
| 5410 | |
| 5411 | h2_settings_max_concurrent_streams = atoi(args[1]); |
| 5412 | if (h2_settings_max_concurrent_streams < 0) { |
| 5413 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 5414 | return -1; |
| 5415 | } |
| 5416 | return 0; |
| 5417 | } |
| 5418 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5419 | |
| 5420 | /****************************************/ |
| 5421 | /* MUX initialization and instanciation */ |
| 5422 | /***************************************/ |
| 5423 | |
| 5424 | /* The mux operations */ |
Willy Tarreau | 680b2bd | 2018-11-27 07:30:17 +0100 | [diff] [blame] | 5425 | static const struct mux_ops h2_ops = { |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5426 | .init = h2_init, |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 5427 | .wake = h2_wake, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5428 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 511efea | 2018-08-16 15:30:32 +0200 | [diff] [blame] | 5429 | .rcv_buf = h2_rcv_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 5430 | .subscribe = h2_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 5431 | .unsubscribe = h2_unsubscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5432 | .attach = h2_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 5433 | .get_first_cs = h2_get_first_cs, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5434 | .detach = h2_detach, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 5435 | .destroy = h2_destroy, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 5436 | .avail_streams = h2_avail_streams, |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 5437 | .max_streams = h2_max_streams, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5438 | .shutr = h2_shutr, |
| 5439 | .shutw = h2_shutw, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 5440 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 5441 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5442 | .name = "H2", |
| 5443 | }; |
| 5444 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 5445 | /* PROTO selection : this mux registers PROTO token "h2" */ |
| 5446 | static struct mux_proto_list mux_proto_h2 = |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5447 | { .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] | 5448 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5449 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2); |
| 5450 | |
Willy Tarreau | f895727 | 2018-10-03 10:25:20 +0200 | [diff] [blame] | 5451 | static struct mux_proto_list mux_proto_h2_htx = |
| 5452 | { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops }; |
| 5453 | |
| 5454 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx); |
| 5455 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5456 | /* config keyword parsers */ |
| 5457 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 5458 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 5459 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 5460 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 5461 | { 0, NULL, NULL } |
| 5462 | }}; |
| 5463 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 5464 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |