QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/quic/core/http/quic_spdy_session.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <cstdint> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 9 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | #include <utility> |
| 11 | |
bnc | 3fc60df | 2019-07-17 11:55:10 -0700 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/core/http/http_constants.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/quic/core/http/quic_headers_stream.h" |
| 14 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
renjietang | c04c85f | 2019-07-25 14:07:27 -0700 | [diff] [blame] | 15 | #include "net/third_party/quiche/src/quic/core/quic_versions.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 16 | #include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h" |
| 17 | #include "net/third_party/quiche/src/quic/platform/api/quic_fallthrough.h" |
| 18 | #include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h" |
| 19 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
| 20 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
| 21 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
| 22 | #include "net/third_party/quiche/src/quic/platform/api/quic_str_cat.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 23 | #include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h" |
| 24 | #include "net/third_party/quiche/src/spdy/core/http2_frame_decoder_adapter.h" |
| 25 | |
| 26 | using http2::Http2DecoderAdapter; |
| 27 | using spdy::HpackEntry; |
| 28 | using spdy::HpackHeaderTable; |
| 29 | using spdy::Http2WeightToSpdy3Priority; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 30 | using spdy::Spdy3PriorityToHttp2Weight; |
| 31 | using spdy::SpdyErrorCode; |
| 32 | using spdy::SpdyFramer; |
| 33 | using spdy::SpdyFramerDebugVisitorInterface; |
| 34 | using spdy::SpdyFramerVisitorInterface; |
| 35 | using spdy::SpdyFrameType; |
| 36 | using spdy::SpdyHeaderBlock; |
| 37 | using spdy::SpdyHeadersHandlerInterface; |
| 38 | using spdy::SpdyHeadersIR; |
| 39 | using spdy::SpdyKnownSettingsId; |
| 40 | using spdy::SpdyPingId; |
| 41 | using spdy::SpdyPriority; |
| 42 | using spdy::SpdyPriorityIR; |
| 43 | using spdy::SpdyPushPromiseIR; |
| 44 | using spdy::SpdySerializedFrame; |
| 45 | using spdy::SpdySettingsId; |
| 46 | using spdy::SpdySettingsIR; |
| 47 | using spdy::SpdyStreamId; |
| 48 | |
| 49 | namespace quic { |
| 50 | |
| 51 | namespace { |
| 52 | |
renjietang | bb1c489 | 2019-05-24 15:58:44 -0700 | [diff] [blame] | 53 | // TODO(renjietang): remove this once HTTP/3 error codes are adopted. |
| 54 | const uint16_t kHttpUnknownStreamType = 0x0D; |
| 55 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 56 | class HeaderTableDebugVisitor : public HpackHeaderTable::DebugVisitorInterface { |
| 57 | public: |
| 58 | HeaderTableDebugVisitor(const QuicClock* clock, |
| 59 | std::unique_ptr<QuicHpackDebugVisitor> visitor) |
| 60 | : clock_(clock), headers_stream_hpack_visitor_(std::move(visitor)) {} |
| 61 | HeaderTableDebugVisitor(const HeaderTableDebugVisitor&) = delete; |
| 62 | HeaderTableDebugVisitor& operator=(const HeaderTableDebugVisitor&) = delete; |
| 63 | |
| 64 | int64_t OnNewEntry(const HpackEntry& entry) override { |
| 65 | QUIC_DVLOG(1) << entry.GetDebugString(); |
| 66 | return (clock_->ApproximateNow() - QuicTime::Zero()).ToMicroseconds(); |
| 67 | } |
| 68 | |
| 69 | void OnUseEntry(const HpackEntry& entry) override { |
| 70 | const QuicTime::Delta elapsed( |
| 71 | clock_->ApproximateNow() - |
| 72 | QuicTime::Delta::FromMicroseconds(entry.time_added()) - |
| 73 | QuicTime::Zero()); |
| 74 | QUIC_DVLOG(1) << entry.GetDebugString() << " " << elapsed.ToMilliseconds() |
| 75 | << " ms"; |
| 76 | headers_stream_hpack_visitor_->OnUseEntry(elapsed); |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | const QuicClock* clock_; |
| 81 | std::unique_ptr<QuicHpackDebugVisitor> headers_stream_hpack_visitor_; |
| 82 | }; |
| 83 | |
| 84 | } // namespace |
| 85 | |
| 86 | // A SpdyFramerVisitor that passes HEADERS frames to the QuicSpdyStream, and |
| 87 | // closes the connection if any unexpected frames are received. |
| 88 | class QuicSpdySession::SpdyFramerVisitor |
| 89 | : public SpdyFramerVisitorInterface, |
| 90 | public SpdyFramerDebugVisitorInterface { |
| 91 | public: |
| 92 | explicit SpdyFramerVisitor(QuicSpdySession* session) : session_(session) {} |
| 93 | SpdyFramerVisitor(const SpdyFramerVisitor&) = delete; |
| 94 | SpdyFramerVisitor& operator=(const SpdyFramerVisitor&) = delete; |
| 95 | |
| 96 | SpdyHeadersHandlerInterface* OnHeaderFrameStart( |
| 97 | SpdyStreamId /* stream_id */) override { |
renjietang | 1e3ee62 | 2019-07-31 14:01:46 -0700 | [diff] [blame] | 98 | DCHECK(!VersionUsesQpack(session_->transport_version())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 99 | return &header_list_; |
| 100 | } |
| 101 | |
| 102 | void OnHeaderFrameEnd(SpdyStreamId /* stream_id */) override { |
renjietang | 1e3ee62 | 2019-07-31 14:01:46 -0700 | [diff] [blame] | 103 | DCHECK(!VersionUsesQpack(session_->transport_version())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 104 | if (session_->IsConnected()) { |
| 105 | session_->OnHeaderList(header_list_); |
| 106 | } |
| 107 | header_list_.Clear(); |
| 108 | } |
| 109 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 110 | void OnStreamFrameData(SpdyStreamId /*stream_id*/, |
| 111 | const char* /*data*/, |
| 112 | size_t /*len*/) override { |
renjietang | 1e3ee62 | 2019-07-31 14:01:46 -0700 | [diff] [blame] | 113 | DCHECK(!VersionUsesQpack(session_->transport_version())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 114 | CloseConnection("SPDY DATA frame received.", |
| 115 | QUIC_INVALID_HEADERS_STREAM_DATA); |
| 116 | } |
| 117 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 118 | void OnStreamEnd(SpdyStreamId /*stream_id*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 119 | // The framer invokes OnStreamEnd after processing a frame that had the fin |
| 120 | // bit set. |
| 121 | } |
| 122 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 123 | void OnStreamPadding(SpdyStreamId /*stream_id*/, size_t /*len*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 124 | CloseConnection("SPDY frame padding received.", |
| 125 | QUIC_INVALID_HEADERS_STREAM_DATA); |
| 126 | } |
| 127 | |
| 128 | void OnError(Http2DecoderAdapter::SpdyFramerError error) override { |
| 129 | QuicErrorCode code = QUIC_INVALID_HEADERS_STREAM_DATA; |
| 130 | switch (error) { |
| 131 | case Http2DecoderAdapter::SpdyFramerError::SPDY_DECOMPRESS_FAILURE: |
| 132 | code = QUIC_HEADERS_STREAM_DATA_DECOMPRESS_FAILURE; |
| 133 | break; |
| 134 | default: |
| 135 | break; |
| 136 | } |
| 137 | CloseConnection( |
| 138 | QuicStrCat("SPDY framing error: ", |
| 139 | Http2DecoderAdapter::SpdyFramerErrorToString(error)), |
| 140 | code); |
| 141 | } |
| 142 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 143 | void OnDataFrameHeader(SpdyStreamId /*stream_id*/, |
| 144 | size_t /*length*/, |
| 145 | bool /*fin*/) override { |
renjietang | 1e3ee62 | 2019-07-31 14:01:46 -0700 | [diff] [blame] | 146 | DCHECK(!VersionUsesQpack(session_->transport_version())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 147 | CloseConnection("SPDY DATA frame received.", |
| 148 | QUIC_INVALID_HEADERS_STREAM_DATA); |
| 149 | } |
| 150 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 151 | void OnRstStream(SpdyStreamId /*stream_id*/, |
| 152 | SpdyErrorCode /*error_code*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 153 | CloseConnection("SPDY RST_STREAM frame received.", |
| 154 | QUIC_INVALID_HEADERS_STREAM_DATA); |
| 155 | } |
| 156 | |
| 157 | void OnSetting(SpdySettingsId id, uint32_t value) override { |
renjietang | 1e3ee62 | 2019-07-31 14:01:46 -0700 | [diff] [blame] | 158 | DCHECK(!VersionUsesQpack(session_->transport_version())); |
bnc | 5b182b9 | 2019-07-30 11:00:15 -0700 | [diff] [blame] | 159 | session_->OnSetting(id, value); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 160 | } |
| 161 | |
renjietang | 1e3ee62 | 2019-07-31 14:01:46 -0700 | [diff] [blame] | 162 | void OnSettingsEnd() override { |
| 163 | DCHECK(!VersionUsesQpack(session_->transport_version())); |
| 164 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 165 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 166 | void OnPing(SpdyPingId /*unique_id*/, bool /*is_ack*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 167 | CloseConnection("SPDY PING frame received.", |
| 168 | QUIC_INVALID_HEADERS_STREAM_DATA); |
| 169 | } |
| 170 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 171 | void OnGoAway(SpdyStreamId /*last_accepted_stream_id*/, |
| 172 | SpdyErrorCode /*error_code*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 173 | CloseConnection("SPDY GOAWAY frame received.", |
| 174 | QUIC_INVALID_HEADERS_STREAM_DATA); |
| 175 | } |
| 176 | |
| 177 | void OnHeaders(SpdyStreamId stream_id, |
| 178 | bool has_priority, |
| 179 | int weight, |
fayang | 944cfbc | 2019-07-31 09:15:00 -0700 | [diff] [blame] | 180 | SpdyStreamId parent_stream_id, |
| 181 | bool exclusive, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 182 | bool fin, |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 183 | bool /*end*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 184 | if (!session_->IsConnected()) { |
| 185 | return; |
| 186 | } |
| 187 | |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 188 | if (VersionUsesQpack(session_->connection()->transport_version())) { |
| 189 | CloseConnection("HEADERS frame not allowed on headers stream.", |
| 190 | QUIC_INVALID_HEADERS_STREAM_DATA); |
| 191 | return; |
| 192 | } |
| 193 | |
fayang | 944cfbc | 2019-07-31 09:15:00 -0700 | [diff] [blame] | 194 | if (session_->use_http2_priority_write_scheduler()) { |
| 195 | session_->OnHeaders( |
| 196 | stream_id, has_priority, |
| 197 | spdy::SpdyStreamPrecedence(parent_stream_id, weight, exclusive), fin); |
| 198 | QUIC_RELOADABLE_FLAG_COUNT_N(quic_use_http2_priority_write_scheduler, 1, |
| 199 | 3); |
| 200 | return; |
| 201 | } |
| 202 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 203 | SpdyPriority priority = |
| 204 | has_priority ? Http2WeightToSpdy3Priority(weight) : 0; |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 205 | session_->OnHeaders(stream_id, has_priority, |
| 206 | spdy::SpdyStreamPrecedence(priority), fin); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 207 | } |
| 208 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 209 | void OnWindowUpdate(SpdyStreamId /*stream_id*/, |
| 210 | int /*delta_window_size*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 211 | CloseConnection("SPDY WINDOW_UPDATE frame received.", |
| 212 | QUIC_INVALID_HEADERS_STREAM_DATA); |
| 213 | } |
| 214 | |
| 215 | void OnPushPromise(SpdyStreamId stream_id, |
| 216 | SpdyStreamId promised_stream_id, |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 217 | bool /*end*/) override { |
renjietang | 1e3ee62 | 2019-07-31 14:01:46 -0700 | [diff] [blame] | 218 | DCHECK(!VersionUsesQpack(session_->transport_version())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 219 | if (!session_->supports_push_promise()) { |
| 220 | CloseConnection("PUSH_PROMISE not supported.", |
| 221 | QUIC_INVALID_HEADERS_STREAM_DATA); |
| 222 | return; |
| 223 | } |
| 224 | if (!session_->IsConnected()) { |
| 225 | return; |
| 226 | } |
renjietang | cd8fab3 | 2019-06-17 16:22:16 -0700 | [diff] [blame] | 227 | session_->OnPushPromise(stream_id, promised_stream_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 228 | } |
| 229 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 230 | void OnContinuation(SpdyStreamId /*stream_id*/, bool /*end*/) override {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 231 | |
| 232 | void OnPriority(SpdyStreamId stream_id, |
fayang | 944cfbc | 2019-07-31 09:15:00 -0700 | [diff] [blame] | 233 | SpdyStreamId parent_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 234 | int weight, |
fayang | 944cfbc | 2019-07-31 09:15:00 -0700 | [diff] [blame] | 235 | bool exclusive) override { |
renjietang | 1e3ee62 | 2019-07-31 14:01:46 -0700 | [diff] [blame] | 236 | DCHECK(!VersionUsesQpack(session_->transport_version())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 237 | if (session_->connection()->transport_version() <= QUIC_VERSION_39) { |
| 238 | CloseConnection("SPDY PRIORITY frame received.", |
| 239 | QUIC_INVALID_HEADERS_STREAM_DATA); |
| 240 | return; |
| 241 | } |
| 242 | if (!session_->IsConnected()) { |
| 243 | return; |
| 244 | } |
fayang | 944cfbc | 2019-07-31 09:15:00 -0700 | [diff] [blame] | 245 | if (session_->use_http2_priority_write_scheduler()) { |
| 246 | session_->OnPriority( |
| 247 | stream_id, spdy::SpdyStreamPrecedence(parent_id, weight, exclusive)); |
| 248 | QUIC_RELOADABLE_FLAG_COUNT_N(quic_use_http2_priority_write_scheduler, 2, |
| 249 | 3); |
| 250 | return; |
| 251 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 252 | SpdyPriority priority = Http2WeightToSpdy3Priority(weight); |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 253 | session_->OnPriority(stream_id, spdy::SpdyStreamPrecedence(priority)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 254 | } |
| 255 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 256 | bool OnUnknownFrame(SpdyStreamId /*stream_id*/, |
| 257 | uint8_t /*frame_type*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 258 | CloseConnection("Unknown frame type received.", |
| 259 | QUIC_INVALID_HEADERS_STREAM_DATA); |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | // SpdyFramerDebugVisitorInterface implementation |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 264 | void OnSendCompressedFrame(SpdyStreamId /*stream_id*/, |
| 265 | SpdyFrameType /*type*/, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 266 | size_t payload_len, |
| 267 | size_t frame_len) override { |
| 268 | if (payload_len == 0) { |
| 269 | QUIC_BUG << "Zero payload length."; |
| 270 | return; |
| 271 | } |
| 272 | int compression_pct = 100 - (100 * frame_len) / payload_len; |
| 273 | QUIC_DVLOG(1) << "Net.QuicHpackCompressionPercentage: " << compression_pct; |
| 274 | } |
| 275 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 276 | void OnReceiveCompressedFrame(SpdyStreamId /*stream_id*/, |
| 277 | SpdyFrameType /*type*/, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 278 | size_t frame_len) override { |
| 279 | if (session_->IsConnected()) { |
| 280 | session_->OnCompressedFrameSize(frame_len); |
| 281 | } |
| 282 | } |
| 283 | |
bnc | 2bda409 | 2019-07-24 09:32:39 -0700 | [diff] [blame] | 284 | void set_max_header_list_size(size_t max_header_list_size) { |
| 285 | header_list_.set_max_header_list_size(max_header_list_size); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | private: |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 289 | void CloseConnection(const std::string& details, QuicErrorCode code) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 290 | if (session_->IsConnected()) { |
| 291 | session_->CloseConnectionWithDetails(code, details); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | private: |
| 296 | QuicSpdySession* session_; |
| 297 | QuicHeaderList header_list_; |
| 298 | }; |
| 299 | |
| 300 | QuicHpackDebugVisitor::QuicHpackDebugVisitor() {} |
| 301 | |
| 302 | QuicHpackDebugVisitor::~QuicHpackDebugVisitor() {} |
| 303 | |
| 304 | QuicSpdySession::QuicSpdySession( |
| 305 | QuicConnection* connection, |
| 306 | QuicSession::Visitor* visitor, |
| 307 | const QuicConfig& config, |
| 308 | const ParsedQuicVersionVector& supported_versions) |
| 309 | : QuicSession(connection, visitor, config, supported_versions), |
renjietang | c04c85f | 2019-07-25 14:07:27 -0700 | [diff] [blame] | 310 | send_control_stream_(nullptr), |
| 311 | receive_control_stream_(nullptr), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 312 | max_inbound_header_list_size_(kDefaultMaxUncompressedHeaderSize), |
renjietang | 3a1bb80 | 2019-06-11 10:42:41 -0700 | [diff] [blame] | 313 | max_outbound_header_list_size_(kDefaultMaxUncompressedHeaderSize), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 314 | server_push_enabled_(true), |
| 315 | stream_id_( |
| 316 | QuicUtils::GetInvalidStreamId(connection->transport_version())), |
| 317 | promised_stream_id_( |
| 318 | QuicUtils::GetInvalidStreamId(connection->transport_version())), |
| 319 | fin_(false), |
| 320 | frame_len_(0), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 321 | supports_push_promise_(perspective() == Perspective::IS_CLIENT), |
| 322 | spdy_framer_(SpdyFramer::ENABLE_COMPRESSION), |
| 323 | spdy_framer_visitor_(new SpdyFramerVisitor(this)) { |
| 324 | h2_deframer_.set_visitor(spdy_framer_visitor_.get()); |
| 325 | h2_deframer_.set_debug_visitor(spdy_framer_visitor_.get()); |
| 326 | spdy_framer_.set_debug_visitor(spdy_framer_visitor_.get()); |
| 327 | } |
| 328 | |
| 329 | QuicSpdySession::~QuicSpdySession() { |
| 330 | // Set the streams' session pointers in closed and dynamic stream lists |
| 331 | // to null to avoid subsequent use of this session. |
| 332 | for (auto& stream : *closed_streams()) { |
| 333 | static_cast<QuicSpdyStream*>(stream.get())->ClearSession(); |
| 334 | } |
| 335 | for (auto const& kv : zombie_streams()) { |
| 336 | static_cast<QuicSpdyStream*>(kv.second.get())->ClearSession(); |
| 337 | } |
renjietang | 55d182a | 2019-07-12 10:26:25 -0700 | [diff] [blame] | 338 | for (auto const& kv : stream_map()) { |
renjietang | b663b86 | 2019-07-08 16:02:39 -0700 | [diff] [blame] | 339 | if (!kv.second->is_static()) { |
| 340 | static_cast<QuicSpdyStream*>(kv.second.get())->ClearSession(); |
renjietang | 2da2afb | 2019-05-13 17:22:14 -0700 | [diff] [blame] | 341 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
| 345 | void QuicSpdySession::Initialize() { |
| 346 | QuicSession::Initialize(); |
| 347 | |
renjietang | 118c8ac | 2019-07-30 11:43:59 -0700 | [diff] [blame] | 348 | if (!VersionUsesQpack(connection()->transport_version())) { |
dschinazi | 552accc | 2019-06-17 17:07:34 -0700 | [diff] [blame] | 349 | if (perspective() == Perspective::IS_SERVER) { |
| 350 | set_largest_peer_created_stream_id( |
| 351 | QuicUtils::GetHeadersStreamId(connection()->transport_version())); |
| 352 | } else { |
| 353 | QuicStreamId headers_stream_id = GetNextOutgoingBidirectionalStreamId(); |
| 354 | DCHECK_EQ(headers_stream_id, QuicUtils::GetHeadersStreamId( |
| 355 | connection()->transport_version())); |
| 356 | } |
renjietang | 118c8ac | 2019-07-30 11:43:59 -0700 | [diff] [blame] | 357 | auto headers_stream = QuicMakeUnique<QuicHeadersStream>((this)); |
| 358 | DCHECK_EQ(QuicUtils::GetHeadersStreamId(connection()->transport_version()), |
| 359 | headers_stream->id()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 360 | |
renjietang | 118c8ac | 2019-07-30 11:43:59 -0700 | [diff] [blame] | 361 | headers_stream_ = headers_stream.get(); |
| 362 | RegisterStaticStream(std::move(headers_stream), |
| 363 | /*stream_already_counted = */ false); |
| 364 | } else { |
renjietang | 8a2df8f | 2019-08-07 10:43:52 -0700 | [diff] [blame^] | 365 | qpack_encoder_ = QuicMakeUnique<QpackEncoder>(this); |
| 366 | qpack_encoder_->set_qpack_stream_sender_delegate( |
| 367 | &encoder_stream_sender_delegate_); |
renjietang | c2aa5cb | 2019-06-20 12:22:53 -0700 | [diff] [blame] | 368 | qpack_decoder_ = |
bnc | 4c664c5 | 2019-08-04 18:14:12 -0700 | [diff] [blame] | 369 | QuicMakeUnique<QpackDecoder>(kDefaultQpackMaxDynamicTableCapacity, |
renjietang | 8a2df8f | 2019-08-07 10:43:52 -0700 | [diff] [blame^] | 370 | /* maximum_blocked_streams = */ 0, this); |
| 371 | qpack_decoder_->set_qpack_stream_sender_delegate( |
| 372 | &decoder_stream_sender_delegate_); |
bnc | 4c664c5 | 2019-08-04 18:14:12 -0700 | [diff] [blame] | 373 | // TODO(b/112770235): Set sensible limit on maximum number of blocked |
| 374 | // streams. |
bnc | bdd303e | 2019-07-09 05:33:17 -0700 | [diff] [blame] | 375 | // TODO(b/112770235): Send SETTINGS_QPACK_MAX_TABLE_CAPACITY with value |
bnc | 4c664c5 | 2019-08-04 18:14:12 -0700 | [diff] [blame] | 376 | // kDefaultQpackMaxDynamicTableCapacity, and SETTINGS_QPACK_BLOCKED_STREAMS |
| 377 | // with limit on maximum number of blocked streams. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 378 | } |
| 379 | |
renjietang | b663b86 | 2019-07-08 16:02:39 -0700 | [diff] [blame] | 380 | if (VersionHasStreamType(connection()->transport_version())) { |
renjietang | c04c85f | 2019-07-25 14:07:27 -0700 | [diff] [blame] | 381 | MaybeInitializeHttp3UnidirectionalStreams(); |
renjietang | fbeb5bf | 2019-04-19 15:06:20 -0700 | [diff] [blame] | 382 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 383 | |
bnc | 2bda409 | 2019-07-24 09:32:39 -0700 | [diff] [blame] | 384 | spdy_framer_visitor_->set_max_header_list_size(max_inbound_header_list_size_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 385 | |
| 386 | // Limit HPACK buffering to 2x header list size limit. |
bnc | 2bda409 | 2019-07-24 09:32:39 -0700 | [diff] [blame] | 387 | h2_deframer_.GetHpackDecoder()->set_max_decode_buffer_size_bytes( |
| 388 | 2 * max_inbound_header_list_size_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 389 | } |
| 390 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 391 | void QuicSpdySession::OnDecoderStreamError(QuicStringPiece /*error_message*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 392 | DCHECK(VersionUsesQpack(connection()->transport_version())); |
| 393 | |
| 394 | // TODO(112770235): Signal connection error on decoder stream errors. |
| 395 | QUIC_NOTREACHED(); |
| 396 | } |
| 397 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 398 | void QuicSpdySession::OnEncoderStreamError(QuicStringPiece /*error_message*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 399 | DCHECK(VersionUsesQpack(connection()->transport_version())); |
| 400 | |
| 401 | // TODO(112770235): Signal connection error on encoder stream errors. |
| 402 | QUIC_NOTREACHED(); |
| 403 | } |
| 404 | |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 405 | void QuicSpdySession::OnStreamHeadersPriority( |
| 406 | QuicStreamId stream_id, |
| 407 | const spdy::SpdyStreamPrecedence& precedence) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 408 | QuicSpdyStream* stream = GetSpdyDataStream(stream_id); |
| 409 | if (!stream) { |
| 410 | // It's quite possible to receive headers after a stream has been reset. |
| 411 | return; |
| 412 | } |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 413 | stream->OnStreamHeadersPriority(precedence); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | void QuicSpdySession::OnStreamHeaderList(QuicStreamId stream_id, |
| 417 | bool fin, |
| 418 | size_t frame_len, |
| 419 | const QuicHeaderList& header_list) { |
rch | da26cdb | 2019-05-17 11:57:37 -0700 | [diff] [blame] | 420 | if (IsStaticStream(stream_id)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 421 | connection()->CloseConnection( |
| 422 | QUIC_INVALID_HEADERS_STREAM_DATA, "stream is static", |
| 423 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 424 | return; |
| 425 | } |
| 426 | QuicSpdyStream* stream = GetSpdyDataStream(stream_id); |
| 427 | if (stream == nullptr) { |
| 428 | // The stream no longer exists, but trailing headers may contain the final |
| 429 | // byte offset necessary for flow control and open stream accounting. |
| 430 | size_t final_byte_offset = 0; |
| 431 | for (const auto& header : header_list) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 432 | const std::string& header_key = header.first; |
| 433 | const std::string& header_value = header.second; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 434 | if (header_key == kFinalOffsetHeaderKey) { |
| 435 | if (!QuicTextUtils::StringToSizeT(header_value, &final_byte_offset)) { |
| 436 | connection()->CloseConnection( |
| 437 | QUIC_INVALID_HEADERS_STREAM_DATA, |
| 438 | "Trailers are malformed (no final offset)", |
| 439 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 440 | return; |
| 441 | } |
bnc | 5de8705 | 2019-05-03 14:21:53 -0700 | [diff] [blame] | 442 | QUIC_DVLOG(1) << "Received final byte offset in trailers for stream " |
| 443 | << stream_id << ", which no longer exists."; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 444 | OnFinalByteOffsetReceived(stream_id, final_byte_offset); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | // It's quite possible to receive headers after a stream has been reset. |
| 449 | return; |
| 450 | } |
| 451 | stream->OnStreamHeaderList(fin, frame_len, header_list); |
| 452 | } |
| 453 | |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 454 | void QuicSpdySession::OnPriorityFrame( |
| 455 | QuicStreamId stream_id, |
| 456 | const spdy::SpdyStreamPrecedence& precedence) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 457 | QuicSpdyStream* stream = GetSpdyDataStream(stream_id); |
| 458 | if (!stream) { |
| 459 | // It's quite possible to receive a PRIORITY frame after a stream has been |
| 460 | // reset. |
| 461 | return; |
| 462 | } |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 463 | stream->OnPriorityFrame(precedence); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | size_t QuicSpdySession::ProcessHeaderData(const struct iovec& iov) { |
| 467 | return h2_deframer_.ProcessInput(static_cast<char*>(iov.iov_base), |
| 468 | iov.iov_len); |
| 469 | } |
| 470 | |
| 471 | size_t QuicSpdySession::WriteHeadersOnHeadersStream( |
| 472 | QuicStreamId id, |
| 473 | SpdyHeaderBlock headers, |
| 474 | bool fin, |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 475 | const spdy::SpdyStreamPrecedence& precedence, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 476 | QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 477 | DCHECK(!VersionUsesQpack(connection()->transport_version())); |
| 478 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 479 | return WriteHeadersOnHeadersStreamImpl( |
| 480 | id, std::move(headers), fin, |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 481 | /* parent_stream_id = */ 0, |
| 482 | Spdy3PriorityToHttp2Weight(precedence.spdy3_priority()), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 483 | /* exclusive = */ false, std::move(ack_listener)); |
| 484 | } |
| 485 | |
| 486 | size_t QuicSpdySession::WritePriority(QuicStreamId id, |
| 487 | QuicStreamId parent_stream_id, |
| 488 | int weight, |
| 489 | bool exclusive) { |
renjietang | 118c8ac | 2019-07-30 11:43:59 -0700 | [diff] [blame] | 490 | DCHECK(!VersionUsesQpack(connection()->transport_version())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 491 | if (connection()->transport_version() <= QUIC_VERSION_39) { |
| 492 | return 0; |
| 493 | } |
| 494 | SpdyPriorityIR priority_frame(id, parent_stream_id, weight, exclusive); |
| 495 | SpdySerializedFrame frame(spdy_framer_.SerializeFrame(priority_frame)); |
renjietang | fbeb5bf | 2019-04-19 15:06:20 -0700 | [diff] [blame] | 496 | headers_stream()->WriteOrBufferData( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 497 | QuicStringPiece(frame.data(), frame.size()), false, nullptr); |
| 498 | return frame.size(); |
| 499 | } |
| 500 | |
renjietang | 7498c8c | 2019-07-02 19:28:42 -0700 | [diff] [blame] | 501 | void QuicSpdySession::WriteH3Priority(const PriorityFrame& priority) { |
| 502 | DCHECK(VersionHasStreamType(connection()->transport_version())); |
| 503 | DCHECK(perspective() == Perspective::IS_CLIENT) |
| 504 | << "Server must not send priority"; |
| 505 | |
| 506 | send_control_stream_->WritePriority(priority); |
| 507 | } |
| 508 | |
renjietang | f4f4712 | 2019-07-22 12:08:53 -0700 | [diff] [blame] | 509 | void QuicSpdySession::WritePushPromise(QuicStreamId original_stream_id, |
| 510 | QuicStreamId promised_stream_id, |
| 511 | SpdyHeaderBlock headers) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 512 | if (perspective() == Perspective::IS_CLIENT) { |
| 513 | QUIC_BUG << "Client shouldn't send PUSH_PROMISE"; |
renjietang | f4f4712 | 2019-07-22 12:08:53 -0700 | [diff] [blame] | 514 | return; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 515 | } |
| 516 | |
renjietang | 3c3dfb7 | 2019-07-26 11:55:52 -0700 | [diff] [blame] | 517 | if (!VersionHasStreamType(connection()->transport_version())) { |
| 518 | SpdyPushPromiseIR push_promise(original_stream_id, promised_stream_id, |
| 519 | std::move(headers)); |
| 520 | // PUSH_PROMISE must not be the last frame sent out, at least followed by |
| 521 | // response headers. |
| 522 | push_promise.set_fin(false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 523 | |
renjietang | 3c3dfb7 | 2019-07-26 11:55:52 -0700 | [diff] [blame] | 524 | SpdySerializedFrame frame(spdy_framer_.SerializeFrame(push_promise)); |
| 525 | headers_stream()->WriteOrBufferData( |
| 526 | QuicStringPiece(frame.data(), frame.size()), false, nullptr); |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | // Encode header list. |
| 531 | std::string encoded_headers = |
| 532 | qpack_encoder_->EncodeHeaderList(original_stream_id, &headers); |
| 533 | PushPromiseFrame frame; |
| 534 | frame.push_id = promised_stream_id; |
| 535 | frame.headers = encoded_headers; |
| 536 | QuicSpdyStream* stream = GetSpdyDataStream(original_stream_id); |
| 537 | stream->WritePushPromise(frame); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 538 | } |
| 539 | |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 540 | void QuicSpdySession::SendMaxHeaderListSize(size_t value) { |
renjietang | 3a1bb80 | 2019-06-11 10:42:41 -0700 | [diff] [blame] | 541 | if (VersionHasStreamType(connection()->transport_version())) { |
renjietang | 7498c8c | 2019-07-02 19:28:42 -0700 | [diff] [blame] | 542 | send_control_stream_->SendSettingsFrame(); |
renjietang | 3a1bb80 | 2019-06-11 10:42:41 -0700 | [diff] [blame] | 543 | return; |
| 544 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 545 | SpdySettingsIR settings_frame; |
| 546 | settings_frame.AddSetting(SETTINGS_MAX_HEADER_LIST_SIZE, value); |
| 547 | |
| 548 | SpdySerializedFrame frame(spdy_framer_.SerializeFrame(settings_frame)); |
renjietang | fbeb5bf | 2019-04-19 15:06:20 -0700 | [diff] [blame] | 549 | headers_stream()->WriteOrBufferData( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 550 | QuicStringPiece(frame.data(), frame.size()), false, nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | QpackEncoder* QuicSpdySession::qpack_encoder() { |
| 554 | DCHECK(VersionUsesQpack(connection()->transport_version())); |
| 555 | |
| 556 | return qpack_encoder_.get(); |
| 557 | } |
| 558 | |
| 559 | QpackDecoder* QuicSpdySession::qpack_decoder() { |
| 560 | DCHECK(VersionUsesQpack(connection()->transport_version())); |
| 561 | |
| 562 | return qpack_decoder_.get(); |
| 563 | } |
| 564 | |
| 565 | QuicSpdyStream* QuicSpdySession::GetSpdyDataStream( |
| 566 | const QuicStreamId stream_id) { |
renjietang | 880d243 | 2019-07-16 13:14:37 -0700 | [diff] [blame] | 567 | QuicStream* stream = nullptr; |
| 568 | if (GetQuicReloadableFlag(quic_inline_getorcreatedynamicstream) && |
| 569 | GetQuicReloadableFlag(quic_handle_staticness_for_spdy_stream)) { |
| 570 | QUIC_RELOADABLE_FLAG_COUNT(quic_inline_getorcreatedynamicstream); |
| 571 | stream = GetOrCreateStream(stream_id); |
| 572 | } else { |
| 573 | stream = GetOrCreateDynamicStream(stream_id); |
| 574 | } |
renjietang | 495d597 | 2019-07-10 14:51:28 -0700 | [diff] [blame] | 575 | if (GetQuicReloadableFlag(quic_handle_staticness_for_spdy_stream) && stream && |
| 576 | stream->is_static()) { |
| 577 | QUIC_RELOADABLE_FLAG_COUNT(quic_handle_staticness_for_spdy_stream); |
| 578 | QUIC_BUG << "GetSpdyDataStream returns static stream"; |
| 579 | connection()->CloseConnection( |
| 580 | QUIC_INVALID_STREAM_ID, "stream is static", |
| 581 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 582 | return nullptr; |
| 583 | } |
rch | da26cdb | 2019-05-17 11:57:37 -0700 | [diff] [blame] | 584 | DCHECK(!stream || !stream->is_static()); |
| 585 | return static_cast<QuicSpdyStream*>(stream); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | void QuicSpdySession::OnCryptoHandshakeEvent(CryptoHandshakeEvent event) { |
| 589 | QuicSession::OnCryptoHandshakeEvent(event); |
| 590 | if (event == HANDSHAKE_CONFIRMED && config()->SupportMaxHeaderListSize()) { |
| 591 | SendMaxHeaderListSize(max_inbound_header_list_size_); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | // True if there are open HTTP requests. |
| 596 | bool QuicSpdySession::ShouldKeepConnectionAlive() const { |
| 597 | // Change to check if there are open HTTP requests. |
| 598 | // When IETF QUIC control and QPACK streams are used, those will need to be |
| 599 | // subtracted from this count to ensure only request streams are counted. |
| 600 | return GetNumOpenDynamicStreams() > 0; |
| 601 | } |
| 602 | |
renjietang | e76b2da | 2019-05-13 14:50:23 -0700 | [diff] [blame] | 603 | bool QuicSpdySession::UsesPendingStreams() const { |
bnc | 36c4728 | 2019-06-21 05:17:59 -0700 | [diff] [blame] | 604 | // QuicSpdySession supports PendingStreams, therefore this method should |
| 605 | // eventually just return true. However, pending streams can only be used if |
| 606 | // unidirectional stream type is supported. |
| 607 | return VersionHasStreamType(connection()->transport_version()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | size_t QuicSpdySession::WriteHeadersOnHeadersStreamImpl( |
| 611 | QuicStreamId id, |
| 612 | spdy::SpdyHeaderBlock headers, |
| 613 | bool fin, |
| 614 | QuicStreamId parent_stream_id, |
| 615 | int weight, |
| 616 | bool exclusive, |
| 617 | QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener) { |
renjietang | 2abedac | 2019-05-20 14:04:50 -0700 | [diff] [blame] | 618 | DCHECK(!VersionUsesQpack(connection()->transport_version())); |
| 619 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 620 | SpdyHeadersIR headers_frame(id, std::move(headers)); |
| 621 | headers_frame.set_fin(fin); |
| 622 | if (perspective() == Perspective::IS_CLIENT) { |
| 623 | headers_frame.set_has_priority(true); |
| 624 | headers_frame.set_parent_stream_id(parent_stream_id); |
| 625 | headers_frame.set_weight(weight); |
| 626 | headers_frame.set_exclusive(exclusive); |
| 627 | } |
| 628 | SpdySerializedFrame frame(spdy_framer_.SerializeFrame(headers_frame)); |
renjietang | fbeb5bf | 2019-04-19 15:06:20 -0700 | [diff] [blame] | 629 | headers_stream()->WriteOrBufferData( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 630 | QuicStringPiece(frame.data(), frame.size()), false, |
| 631 | std::move(ack_listener)); |
| 632 | return frame.size(); |
| 633 | } |
| 634 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 635 | void QuicSpdySession::OnPromiseHeaderList( |
| 636 | QuicStreamId /*stream_id*/, |
| 637 | QuicStreamId /*promised_stream_id*/, |
| 638 | size_t /*frame_len*/, |
| 639 | const QuicHeaderList& /*header_list*/) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 640 | std::string error = |
| 641 | "OnPromiseHeaderList should be overridden in client code."; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 642 | QUIC_BUG << error; |
| 643 | connection()->CloseConnection(QUIC_INTERNAL_ERROR, error, |
| 644 | ConnectionCloseBehavior::SILENT_CLOSE); |
| 645 | } |
| 646 | |
bnc | 5b182b9 | 2019-07-30 11:00:15 -0700 | [diff] [blame] | 647 | void QuicSpdySession::OnSetting(uint64_t id, uint64_t value) { |
| 648 | if (VersionHasStreamType(connection()->transport_version())) { |
| 649 | // SETTINGS frame received on the control stream. |
| 650 | switch (id) { |
| 651 | case SETTINGS_QPACK_MAX_TABLE_CAPACITY: |
| 652 | QUIC_DVLOG(1) |
| 653 | << "SETTINGS_QPACK_MAX_TABLE_CAPACITY received with value " |
| 654 | << value; |
bnc | a0c8f5a | 2019-08-04 11:49:03 -0700 | [diff] [blame] | 655 | qpack_encoder_->SetMaximumDynamicTableCapacity(value); |
bnc | 5b182b9 | 2019-07-30 11:00:15 -0700 | [diff] [blame] | 656 | break; |
| 657 | case SETTINGS_MAX_HEADER_LIST_SIZE: |
| 658 | QUIC_DVLOG(1) << "SETTINGS_MAX_HEADER_LIST_SIZE received with value " |
| 659 | << value; |
| 660 | max_outbound_header_list_size_ = value; |
| 661 | break; |
| 662 | case SETTINGS_QPACK_BLOCKED_STREAMS: |
| 663 | QUIC_DVLOG(1) << "SETTINGS_QPACK_BLOCKED_STREAMS received with value " |
| 664 | << value; |
bnc | a0c8f5a | 2019-08-04 11:49:03 -0700 | [diff] [blame] | 665 | qpack_encoder_->SetMaximumBlockedStreams(value); |
bnc | 5b182b9 | 2019-07-30 11:00:15 -0700 | [diff] [blame] | 666 | break; |
| 667 | case SETTINGS_NUM_PLACEHOLDERS: |
| 668 | QUIC_DVLOG(1) << "SETTINGS_NUM_PLACEHOLDERS received with value " |
| 669 | << value; |
| 670 | // TODO: Support placeholder setting. |
| 671 | break; |
| 672 | default: |
| 673 | QUIC_DVLOG(1) << "Unknown setting identifier " << id |
| 674 | << " received with value " << value; |
| 675 | // Ignore unknown settings. |
| 676 | break; |
| 677 | } |
| 678 | return; |
| 679 | } |
| 680 | |
| 681 | // SETTINGS frame received on the headers stream. |
| 682 | switch (id) { |
| 683 | case spdy::SETTINGS_HEADER_TABLE_SIZE: |
| 684 | QUIC_DVLOG(1) << "SETTINGS_HEADER_TABLE_SIZE received with value " |
| 685 | << value; |
| 686 | spdy_framer_.UpdateHeaderEncoderTableSize(value); |
| 687 | break; |
| 688 | case spdy::SETTINGS_ENABLE_PUSH: |
| 689 | if (perspective() == Perspective::IS_SERVER) { |
| 690 | // See rfc7540, Section 6.5.2. |
| 691 | if (value > 1) { |
| 692 | QUIC_DLOG(ERROR) << "Invalid value " << value |
| 693 | << " received for SETTINGS_ENABLE_PUSH."; |
| 694 | if (IsConnected()) { |
| 695 | CloseConnectionWithDetails( |
| 696 | QUIC_INVALID_HEADERS_STREAM_DATA, |
| 697 | QuicStrCat("Invalid value for SETTINGS_ENABLE_PUSH: ", value)); |
| 698 | } |
| 699 | return; |
| 700 | } |
| 701 | QUIC_DVLOG(1) << "SETTINGS_ENABLE_PUSH received with value " << value; |
| 702 | server_push_enabled_ = value; |
| 703 | break; |
| 704 | } else { |
| 705 | QUIC_DLOG(ERROR) |
| 706 | << "Invalid SETTINGS_ENABLE_PUSH received by client with value " |
| 707 | << value; |
| 708 | if (IsConnected()) { |
| 709 | CloseConnectionWithDetails( |
| 710 | QUIC_INVALID_HEADERS_STREAM_DATA, |
| 711 | QuicStrCat("Unsupported field of HTTP/2 SETTINGS frame: ", id)); |
| 712 | } |
| 713 | } |
| 714 | break; |
| 715 | // TODO(fayang): Need to support SETTINGS_MAX_HEADER_LIST_SIZE when |
| 716 | // clients are actually sending it. |
| 717 | case spdy::SETTINGS_MAX_HEADER_LIST_SIZE: |
| 718 | QUIC_DVLOG(1) << "SETTINGS_MAX_HEADER_LIST_SIZE received with value " |
| 719 | << value; |
| 720 | break; |
| 721 | default: |
| 722 | QUIC_DLOG(ERROR) << "Unknown setting identifier " << id |
| 723 | << " received with value " << value; |
| 724 | if (IsConnected()) { |
| 725 | CloseConnectionWithDetails( |
| 726 | QUIC_INVALID_HEADERS_STREAM_DATA, |
| 727 | QuicStrCat("Unsupported field of HTTP/2 SETTINGS frame: ", id)); |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 732 | bool QuicSpdySession::ShouldReleaseHeadersStreamSequencerBuffer() { |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | void QuicSpdySession::OnHeaders(SpdyStreamId stream_id, |
| 737 | bool has_priority, |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 738 | const spdy::SpdyStreamPrecedence& precedence, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 739 | bool fin) { |
| 740 | if (has_priority) { |
| 741 | if (perspective() == Perspective::IS_CLIENT) { |
| 742 | CloseConnectionWithDetails(QUIC_INVALID_HEADERS_STREAM_DATA, |
| 743 | "Server must not send priorities."); |
| 744 | return; |
| 745 | } |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 746 | OnStreamHeadersPriority(stream_id, precedence); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 747 | } else { |
| 748 | if (perspective() == Perspective::IS_SERVER) { |
| 749 | CloseConnectionWithDetails(QUIC_INVALID_HEADERS_STREAM_DATA, |
| 750 | "Client must send priorities."); |
| 751 | return; |
| 752 | } |
| 753 | } |
| 754 | DCHECK_EQ(QuicUtils::GetInvalidStreamId(connection()->transport_version()), |
| 755 | stream_id_); |
| 756 | DCHECK_EQ(QuicUtils::GetInvalidStreamId(connection()->transport_version()), |
| 757 | promised_stream_id_); |
| 758 | stream_id_ = stream_id; |
| 759 | fin_ = fin; |
| 760 | } |
| 761 | |
| 762 | void QuicSpdySession::OnPushPromise(SpdyStreamId stream_id, |
renjietang | cd8fab3 | 2019-06-17 16:22:16 -0700 | [diff] [blame] | 763 | SpdyStreamId promised_stream_id) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 764 | DCHECK_EQ(QuicUtils::GetInvalidStreamId(connection()->transport_version()), |
| 765 | stream_id_); |
| 766 | DCHECK_EQ(QuicUtils::GetInvalidStreamId(connection()->transport_version()), |
| 767 | promised_stream_id_); |
| 768 | stream_id_ = stream_id; |
| 769 | promised_stream_id_ = promised_stream_id; |
| 770 | } |
| 771 | |
| 772 | // TODO (wangyix): Why is SpdyStreamId used instead of QuicStreamId? |
| 773 | // This occurs in many places in this file. |
| 774 | void QuicSpdySession::OnPriority(SpdyStreamId stream_id, |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 775 | const spdy::SpdyStreamPrecedence& precedence) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 776 | if (perspective() == Perspective::IS_CLIENT) { |
| 777 | CloseConnectionWithDetails(QUIC_INVALID_HEADERS_STREAM_DATA, |
| 778 | "Server must not send PRIORITY frames."); |
| 779 | return; |
| 780 | } |
fayang | 476683a | 2019-07-25 12:42:16 -0700 | [diff] [blame] | 781 | OnPriorityFrame(stream_id, precedence); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | void QuicSpdySession::OnHeaderList(const QuicHeaderList& header_list) { |
| 785 | QUIC_DVLOG(1) << "Received header list for stream " << stream_id_ << ": " |
| 786 | << header_list.DebugString(); |
renjietang | 3c3dfb7 | 2019-07-26 11:55:52 -0700 | [diff] [blame] | 787 | // This code path is only executed for push promise in IETF QUIC. |
| 788 | if (VersionUsesQpack(connection()->transport_version())) { |
| 789 | DCHECK(promised_stream_id_ != |
| 790 | QuicUtils::GetInvalidStreamId(connection()->transport_version())); |
| 791 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 792 | if (promised_stream_id_ == |
| 793 | QuicUtils::GetInvalidStreamId(connection()->transport_version())) { |
| 794 | OnStreamHeaderList(stream_id_, fin_, frame_len_, header_list); |
| 795 | } else { |
| 796 | OnPromiseHeaderList(stream_id_, promised_stream_id_, frame_len_, |
| 797 | header_list); |
| 798 | } |
| 799 | // Reset state for the next frame. |
| 800 | promised_stream_id_ = |
| 801 | QuicUtils::GetInvalidStreamId(connection()->transport_version()); |
| 802 | stream_id_ = QuicUtils::GetInvalidStreamId(connection()->transport_version()); |
| 803 | fin_ = false; |
| 804 | frame_len_ = 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | void QuicSpdySession::OnCompressedFrameSize(size_t frame_len) { |
| 808 | frame_len_ += frame_len; |
| 809 | } |
| 810 | |
| 811 | void QuicSpdySession::SetHpackEncoderDebugVisitor( |
| 812 | std::unique_ptr<QuicHpackDebugVisitor> visitor) { |
| 813 | spdy_framer_.SetEncoderHeaderTableDebugVisitor( |
| 814 | std::unique_ptr<HeaderTableDebugVisitor>(new HeaderTableDebugVisitor( |
| 815 | connection()->helper()->GetClock(), std::move(visitor)))); |
| 816 | } |
| 817 | |
| 818 | void QuicSpdySession::SetHpackDecoderDebugVisitor( |
| 819 | std::unique_ptr<QuicHpackDebugVisitor> visitor) { |
| 820 | h2_deframer_.SetDecoderHeaderTableDebugVisitor( |
| 821 | QuicMakeUnique<HeaderTableDebugVisitor>( |
| 822 | connection()->helper()->GetClock(), std::move(visitor))); |
| 823 | } |
| 824 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 825 | void QuicSpdySession::CloseConnectionWithDetails(QuicErrorCode error, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 826 | const std::string& details) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 827 | connection()->CloseConnection( |
| 828 | error, details, ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 829 | } |
| 830 | |
QUICHE team | c2653c4 | 2019-03-08 13:30:06 -0800 | [diff] [blame] | 831 | bool QuicSpdySession::HasActiveRequestStreams() const { |
renjietang | 901cdcd | 2019-08-02 15:13:16 -0700 | [diff] [blame] | 832 | if (GetQuicReloadableFlag(quic_active_streams_never_negative)) { |
| 833 | QUIC_RELOADABLE_FLAG_COUNT(quic_active_streams_never_negative); |
| 834 | DCHECK(static_cast<size_t>(stream_map().size()) >= |
| 835 | num_incoming_static_streams() + num_outgoing_static_streams()); |
| 836 | return stream_map().size() - num_incoming_static_streams() - |
| 837 | num_outgoing_static_streams() > |
| 838 | 0; |
| 839 | } |
renjietang | 55d182a | 2019-07-12 10:26:25 -0700 | [diff] [blame] | 840 | if (static_cast<size_t>(stream_map().size()) > |
renjietang | fbeb5bf | 2019-04-19 15:06:20 -0700 | [diff] [blame] | 841 | num_incoming_static_streams() + num_outgoing_static_streams()) { |
renjietang | 55d182a | 2019-07-12 10:26:25 -0700 | [diff] [blame] | 842 | return stream_map().size() - num_incoming_static_streams() - |
renjietang | fbeb5bf | 2019-04-19 15:06:20 -0700 | [diff] [blame] | 843 | num_outgoing_static_streams() > |
| 844 | 0; |
| 845 | } |
| 846 | return false; |
QUICHE team | c2653c4 | 2019-03-08 13:30:06 -0800 | [diff] [blame] | 847 | } |
| 848 | |
renjietang | bb1c489 | 2019-05-24 15:58:44 -0700 | [diff] [blame] | 849 | bool QuicSpdySession::ProcessPendingStream(PendingStream* pending) { |
| 850 | DCHECK(VersionHasStreamType(connection()->transport_version())); |
renjietang | a553da0 | 2019-06-24 11:57:04 -0700 | [diff] [blame] | 851 | DCHECK(connection()->connected()); |
renjietang | 0c55886 | 2019-05-08 13:26:23 -0700 | [diff] [blame] | 852 | struct iovec iov; |
| 853 | if (!pending->sequencer()->GetReadableRegion(&iov)) { |
renjietang | bb1c489 | 2019-05-24 15:58:44 -0700 | [diff] [blame] | 854 | // The first byte hasn't been received yet. |
| 855 | return false; |
renjietang | 0c55886 | 2019-05-08 13:26:23 -0700 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | QuicDataReader reader(static_cast<char*>(iov.iov_base), iov.iov_len); |
renjietang | bb1c489 | 2019-05-24 15:58:44 -0700 | [diff] [blame] | 859 | uint8_t stream_type_length = reader.PeekVarInt62Length(); |
renjietang | 0c55886 | 2019-05-08 13:26:23 -0700 | [diff] [blame] | 860 | uint64_t stream_type = 0; |
| 861 | if (!reader.ReadVarInt62(&stream_type)) { |
renjietang | bb1c489 | 2019-05-24 15:58:44 -0700 | [diff] [blame] | 862 | return false; |
renjietang | 0c55886 | 2019-05-08 13:26:23 -0700 | [diff] [blame] | 863 | } |
renjietang | bb1c489 | 2019-05-24 15:58:44 -0700 | [diff] [blame] | 864 | pending->MarkConsumed(stream_type_length); |
renjietang | 0c55886 | 2019-05-08 13:26:23 -0700 | [diff] [blame] | 865 | |
renjietang | 0c55886 | 2019-05-08 13:26:23 -0700 | [diff] [blame] | 866 | switch (stream_type) { |
renjietang | 3a1bb80 | 2019-06-11 10:42:41 -0700 | [diff] [blame] | 867 | case kControlStream: { // HTTP/3 control stream. |
| 868 | auto receive_stream = QuicMakeUnique<QuicReceiveControlStream>(pending); |
| 869 | receive_control_stream_ = receive_stream.get(); |
renjietang | 0e9980b | 2019-07-11 12:00:21 -0700 | [diff] [blame] | 870 | RegisterStaticStream(std::move(receive_stream), |
| 871 | /*stream_already_counted = */ true); |
renjietang | 3a1bb80 | 2019-06-11 10:42:41 -0700 | [diff] [blame] | 872 | receive_control_stream_->SetUnblocked(); |
| 873 | return true; |
| 874 | } |
renjietang | bb1c489 | 2019-05-24 15:58:44 -0700 | [diff] [blame] | 875 | case kServerPushStream: { // Push Stream. |
renjietang | baea59c | 2019-05-29 15:08:14 -0700 | [diff] [blame] | 876 | QuicSpdyStream* stream = CreateIncomingStream(pending); |
renjietang | bb1c489 | 2019-05-24 15:58:44 -0700 | [diff] [blame] | 877 | stream->SetUnblocked(); |
| 878 | return true; |
| 879 | } |
| 880 | case kQpackEncoderStream: // QPACK encoder stream. |
renjietang | 0c55886 | 2019-05-08 13:26:23 -0700 | [diff] [blame] | 881 | // TODO(bnc): Create QPACK encoder stream. |
| 882 | break; |
renjietang | bb1c489 | 2019-05-24 15:58:44 -0700 | [diff] [blame] | 883 | case kQpackDecoderStream: // QPACK decoder stream. |
renjietang | 0c55886 | 2019-05-08 13:26:23 -0700 | [diff] [blame] | 884 | // TODO(bnc): Create QPACK decoder stream. |
| 885 | break; |
| 886 | default: |
renjietang | bb1c489 | 2019-05-24 15:58:44 -0700 | [diff] [blame] | 887 | SendStopSending(kHttpUnknownStreamType, pending->id()); |
renjietang | 0c55886 | 2019-05-08 13:26:23 -0700 | [diff] [blame] | 888 | } |
renjietang | bb1c489 | 2019-05-24 15:58:44 -0700 | [diff] [blame] | 889 | return false; |
renjietang | 0c55886 | 2019-05-08 13:26:23 -0700 | [diff] [blame] | 890 | } |
| 891 | |
renjietang | c04c85f | 2019-07-25 14:07:27 -0700 | [diff] [blame] | 892 | void QuicSpdySession::MaybeInitializeHttp3UnidirectionalStreams() { |
| 893 | DCHECK(VersionHasStreamType(connection()->transport_version())); |
| 894 | if (!send_control_stream_ && CanOpenNextOutgoingUnidirectionalStream()) { |
| 895 | auto send_control = QuicMakeUnique<QuicSendControlStream>( |
| 896 | GetNextOutgoingUnidirectionalStreamId(), this, |
| 897 | max_inbound_header_list_size_); |
| 898 | send_control_stream_ = send_control.get(); |
| 899 | RegisterStaticStream(std::move(send_control), |
| 900 | /*stream_already_counted = */ false); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | void QuicSpdySession::OnCanCreateNewOutgoingStream(bool unidirectional) { |
| 905 | if (unidirectional && |
| 906 | VersionHasStreamType(connection()->transport_version())) { |
| 907 | MaybeInitializeHttp3UnidirectionalStreams(); |
| 908 | } |
| 909 | } |
| 910 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 911 | } // namespace quic |