1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_SPDY_SPDY_SESSION_H_ 6 #define NET_SPDY_SPDY_SESSION_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <map> 12 #include <memory> 13 #include <set> 14 #include <string> 15 #include <vector> 16 17 #include "base/containers/circular_deque.h" 18 #include "base/gtest_prod_util.h" 19 #include "base/memory/raw_ptr.h" 20 #include "base/memory/ref_counted.h" 21 #include "base/memory/scoped_refptr.h" 22 #include "base/memory/weak_ptr.h" 23 #include "base/strings/string_piece.h" 24 #include "base/time/time.h" 25 #include "base/timer/timer.h" 26 #include "base/values.h" 27 #include "net/base/completion_once_callback.h" 28 #include "net/base/host_port_pair.h" 29 #include "net/base/io_buffer.h" 30 #include "net/base/load_states.h" 31 #include "net/base/load_timing_info.h" 32 #include "net/base/net_errors.h" 33 #include "net/base/net_export.h" 34 #include "net/base/network_change_notifier.h" 35 #include "net/base/request_priority.h" 36 #include "net/log/net_log_source.h" 37 #include "net/socket/client_socket_pool.h" 38 #include "net/socket/next_proto.h" 39 #include "net/socket/ssl_client_socket.h" 40 #include "net/socket/stream_socket.h" 41 #include "net/spdy/buffered_spdy_framer.h" 42 #include "net/spdy/http2_priority_dependencies.h" 43 #include "net/spdy/http2_push_promise_index.h" 44 #include "net/spdy/multiplexed_session.h" 45 #include "net/spdy/server_push_delegate.h" 46 #include "net/spdy/spdy_buffer.h" 47 #include "net/spdy/spdy_session_pool.h" 48 #include "net/spdy/spdy_stream.h" 49 #include "net/spdy/spdy_write_queue.h" 50 #include "net/ssl/ssl_config_service.h" 51 #include "net/third_party/quiche/src/quiche/spdy/core/http2_header_block.h" 52 #include "net/third_party/quiche/src/quiche/spdy/core/spdy_alt_svc_wire_format.h" 53 #include "net/third_party/quiche/src/quiche/spdy/core/spdy_framer.h" 54 #include "net/third_party/quiche/src/quiche/spdy/core/spdy_protocol.h" 55 #include "net/traffic_annotation/network_traffic_annotation.h" 56 #include "third_party/abseil-cpp/absl/types/optional.h" 57 #include "url/gurl.h" 58 #include "url/origin.h" 59 #include "url/scheme_host_port.h" 60 61 namespace net { 62 63 namespace test { 64 class SpdyStreamTest; 65 } 66 67 // TLS and other layers will chunk data at 16KB. Making the max frame size too 68 // small will lead to increased CPU/byte cost and overhead on both client/server 69 // due to excessive frames to process. Making this larger has diminishing 70 // returns as the data will be chunked elsewhere. We also want to ensure we are 71 // >= 2860B (~2* MSS => 2 packets) to avoid delayed ACKs. We will also account 72 // for the frame header size of 9B to prevent fragmentation when this is added. 73 // As a result we will use a 16KB - 9B max data frame size. 74 const int kMaxSpdyFrameChunkSize = (16 * 1024) - 9; 75 76 // Default value of spdy::SETTINGS_INITIAL_WINDOW_SIZE per protocol 77 // specification. A session is always created with this initial window size. 78 const int32_t kDefaultInitialWindowSize = 65535; 79 80 // Maximum number of concurrent streams we will create, unless the server 81 // sends a SETTINGS frame with a different value. 82 const size_t kInitialMaxConcurrentStreams = 100; 83 84 // If more than this many bytes have been read or more than that many 85 // milliseconds have passed, return ERR_IO_PENDING from ReadLoop. 86 const int kYieldAfterBytesRead = 32 * 1024; 87 const int kYieldAfterDurationMilliseconds = 20; 88 89 // First and last valid stream IDs. As we always act as the client, 90 // start at 1 for the first stream id. 91 const spdy::SpdyStreamId kFirstStreamId = 1; 92 const spdy::SpdyStreamId kLastStreamId = 0x7fffffff; 93 94 // Maximum number of capped frames that can be queued at any time. 95 // We measured how many queued capped frames were ever in the 96 // SpdyWriteQueue at one given time between 2019-08 and 2020-02. 97 // The numbers showed that in 99.94% of cases it would always 98 // stay below 10, and that it would exceed 1000 only in 99 // 10^-8 of cases. Therefore we picked 10000 as a number that will 100 // virtually never be hit in practice, while still preventing an 101 // attacker from growing this queue unboundedly. 102 const int kSpdySessionMaxQueuedCappedFrames = 10000; 103 104 // Default time to delay sending small receive window updates (can be 105 // configured through SetTimeToBufferSmallWindowUpdates()). Usually window 106 // updates are sent when half of the receive window has been processed by 107 // the client but in the case of a client that consumes the data slowly, 108 // this strategy alone would make servers consider the connection or stream 109 // idle. 110 constexpr base::TimeDelta kDefaultTimeToBufferSmallWindowUpdates = 111 base::Seconds(5); 112 113 class NetLog; 114 class NetworkQualityEstimator; 115 class SpdyStream; 116 class SSLInfo; 117 class TransportSecurityState; 118 119 // NOTE: There is an enum called SpdyProtocolErrorDetails2 (also with numeric 120 // suffixes) in tools/metrics/histograms/enums.xml. Be sure to add new values 121 // there also. 122 enum SpdyProtocolErrorDetails { 123 // http2::Http2DecoderAdapter::SpdyFramerError mappings. 124 SPDY_ERROR_NO_ERROR = 0, 125 SPDY_ERROR_INVALID_STREAM_ID = 38, 126 SPDY_ERROR_INVALID_CONTROL_FRAME = 1, 127 SPDY_ERROR_CONTROL_PAYLOAD_TOO_LARGE = 2, 128 SPDY_ERROR_DECOMPRESS_FAILURE = 5, 129 SPDY_ERROR_INVALID_PADDING = 39, 130 SPDY_ERROR_INVALID_DATA_FRAME_FLAGS = 8, 131 SPDY_ERROR_UNEXPECTED_FRAME = 31, 132 SPDY_ERROR_INTERNAL_FRAMER_ERROR = 41, 133 SPDY_ERROR_INVALID_CONTROL_FRAME_SIZE = 37, 134 SPDY_ERROR_OVERSIZED_PAYLOAD = 40, 135 136 // HttpDecoder or HttpDecoderAdapter error. 137 SPDY_ERROR_HPACK_INDEX_VARINT_ERROR = 43, 138 SPDY_ERROR_HPACK_NAME_LENGTH_VARINT_ERROR = 44, 139 SPDY_ERROR_HPACK_VALUE_LENGTH_VARINT_ERROR = 45, 140 SPDY_ERROR_HPACK_NAME_TOO_LONG = 46, 141 SPDY_ERROR_HPACK_VALUE_TOO_LONG = 47, 142 SPDY_ERROR_HPACK_NAME_HUFFMAN_ERROR = 48, 143 SPDY_ERROR_HPACK_VALUE_HUFFMAN_ERROR = 49, 144 SPDY_ERROR_HPACK_MISSING_DYNAMIC_TABLE_SIZE_UPDATE = 50, 145 SPDY_ERROR_HPACK_INVALID_INDEX = 51, 146 SPDY_ERROR_HPACK_INVALID_NAME_INDEX = 52, 147 SPDY_ERROR_HPACK_DYNAMIC_TABLE_SIZE_UPDATE_NOT_ALLOWED = 53, 148 SPDY_ERROR_HPACK_INITIAL_DYNAMIC_TABLE_SIZE_UPDATE_IS_ABOVE_LOW_WATER_MARK = 149 54, 150 SPDY_ERROR_HPACK_DYNAMIC_TABLE_SIZE_UPDATE_IS_ABOVE_ACKNOWLEDGED_SETTING = 55, 151 SPDY_ERROR_HPACK_TRUNCATED_BLOCK = 56, 152 SPDY_ERROR_HPACK_FRAGMENT_TOO_LONG = 57, 153 SPDY_ERROR_HPACK_COMPRESSED_HEADER_SIZE_EXCEEDS_LIMIT = 58, 154 SPDY_ERROR_STOP_PROCESSING = 59, 155 // spdy::SpdyErrorCode mappings. 156 STATUS_CODE_NO_ERROR = 41, 157 STATUS_CODE_PROTOCOL_ERROR = 11, 158 STATUS_CODE_INTERNAL_ERROR = 16, 159 STATUS_CODE_FLOW_CONTROL_ERROR = 17, 160 STATUS_CODE_SETTINGS_TIMEOUT = 32, 161 STATUS_CODE_STREAM_CLOSED = 12, 162 STATUS_CODE_FRAME_SIZE_ERROR = 21, 163 STATUS_CODE_REFUSED_STREAM = 13, 164 STATUS_CODE_CANCEL = 15, 165 STATUS_CODE_COMPRESSION_ERROR = 42, 166 STATUS_CODE_CONNECT_ERROR = 33, 167 STATUS_CODE_ENHANCE_YOUR_CALM = 34, 168 STATUS_CODE_INADEQUATE_SECURITY = 35, 169 STATUS_CODE_HTTP_1_1_REQUIRED = 36, 170 // Deprecated SpdyRstStrreamStatus mappings. 171 STATUS_CODE_UNSUPPORTED_VERSION = 14, 172 STATUS_CODE_STREAM_IN_USE = 18, 173 STATUS_CODE_STREAM_ALREADY_CLOSED = 19, 174 175 // SpdySession errors 176 PROTOCOL_ERROR_UNEXPECTED_PING = 22, 177 PROTOCOL_ERROR_RST_STREAM_FOR_NON_ACTIVE_STREAM = 23, 178 PROTOCOL_ERROR_SPDY_COMPRESSION_FAILURE = 24, 179 PROTOCOL_ERROR_REQUEST_FOR_SECURE_CONTENT_OVER_INSECURE_SESSION = 25, 180 PROTOCOL_ERROR_SYN_REPLY_NOT_RECEIVED = 26, 181 PROTOCOL_ERROR_INVALID_WINDOW_UPDATE_SIZE = 27, 182 PROTOCOL_ERROR_RECEIVE_WINDOW_VIOLATION = 28, 183 184 // Next free value. 185 NUM_SPDY_PROTOCOL_ERROR_DETAILS = 60, 186 }; 187 SpdyProtocolErrorDetails NET_EXPORT_PRIVATE MapFramerErrorToProtocolError( 188 http2::Http2DecoderAdapter::SpdyFramerError error); 189 Error NET_EXPORT_PRIVATE 190 MapFramerErrorToNetError(http2::Http2DecoderAdapter::SpdyFramerError error); 191 SpdyProtocolErrorDetails NET_EXPORT_PRIVATE 192 MapRstStreamStatusToProtocolError(spdy::SpdyErrorCode error_code); 193 spdy::SpdyErrorCode NET_EXPORT_PRIVATE MapNetErrorToGoAwayStatus(Error err); 194 195 // There is an enum of the same name in tools/metrics/histograms/enums.xml. 196 // Be sure to add new values there also. 197 enum class SpdyPushedStreamFate { 198 kTooManyPushedStreams = 0, 199 kTimeout = 1, 200 kPromisedStreamIdParityError = 2, 201 kAssociatedStreamIdParityError = 3, 202 kStreamIdOutOfOrder = 4, 203 kGoingAway = 5, 204 kInvalidUrl = 6, 205 kInactiveAssociatedStream = 7, 206 kNonHttpSchemeFromTrustedProxy = 8, 207 kNonHttpsPushedScheme = 9, 208 kNonHttpsAssociatedScheme = 10, 209 kCertificateMismatch = 11, 210 kDuplicateUrl = 12, 211 kClientRequestNotRange = 13, 212 kPushedRequestNotRange = 14, 213 kRangeMismatch = 15, 214 kVaryMismatch = 16, 215 kAcceptedNoVary = 17, 216 kAcceptedMatchingVary = 18, 217 kPushDisabled = 19, 218 kAlreadyInCache = 20, 219 kUnsupportedStatusCode = 21, 220 kMaxValue = kUnsupportedStatusCode 221 }; 222 223 // If these compile asserts fail then SpdyProtocolErrorDetails needs 224 // to be updated with new values, as do the mapping functions above. 225 static_assert(28 == http2::Http2DecoderAdapter::LAST_ERROR, 226 "SpdyProtocolErrorDetails / Spdy Errors mismatch"); 227 static_assert(13 == spdy::SpdyErrorCode::ERROR_CODE_MAX, 228 "SpdyProtocolErrorDetails / spdy::SpdyErrorCode mismatch"); 229 230 // A helper class used to manage a request to create a stream. 231 class NET_EXPORT_PRIVATE SpdyStreamRequest { 232 public: 233 SpdyStreamRequest(); 234 235 SpdyStreamRequest(const SpdyStreamRequest&) = delete; 236 SpdyStreamRequest& operator=(const SpdyStreamRequest&) = delete; 237 238 // Calls CancelRequest(). 239 ~SpdyStreamRequest(); 240 241 // Returns the time when ConfirmHandshake() completed, if this request had to 242 // wait for ConfirmHandshake(). confirm_handshake_end()243 base::TimeTicks confirm_handshake_end() const { 244 return confirm_handshake_end_; 245 } 246 247 // Starts the request to create a stream. If OK is returned, then 248 // ReleaseStream() may be called. If ERR_IO_PENDING is returned, 249 // then when the stream is created, |callback| will be called, at 250 // which point ReleaseStream() may be called. Otherwise, the stream 251 // is not created, an error is returned, and ReleaseStream() may not 252 // be called. 253 // 254 // If |can_send_early| is true, this request is allowed to be sent over 255 // TLS 1.3 0RTT without confirming the handshake. 256 // 257 // If OK is returned, must not be called again without 258 // ReleaseStream() being called first. If ERR_IO_PENDING is 259 // returned, must not be called again without CancelRequest() or 260 // ReleaseStream() being called first. Otherwise, in case of an 261 // immediate error, this may be called again. 262 int StartRequest(SpdyStreamType type, 263 const base::WeakPtr<SpdySession>& session, 264 const GURL& url, 265 bool can_send_early, 266 RequestPriority priority, 267 const SocketTag& socket_tag, 268 const NetLogWithSource& net_log, 269 CompletionOnceCallback callback, 270 const NetworkTrafficAnnotationTag& traffic_annotation, 271 bool detect_broken_connection = false, 272 base::TimeDelta heartbeat_interval = base::Seconds(0)); 273 274 // Cancels any pending stream creation request. May be called 275 // repeatedly. 276 void CancelRequest(); 277 278 // Transfers the created stream (guaranteed to not be NULL) to the 279 // caller. Must be called at most once after StartRequest() returns 280 // OK or |callback| is called with OK. The caller must immediately 281 // set a delegate for the returned stream (except for test code). 282 base::WeakPtr<SpdyStream> ReleaseStream(); 283 284 // Changes the priority of the stream, or changes the priority of the queued 285 // request in the session. 286 void SetPriority(RequestPriority priority); 287 traffic_annotation()288 const NetworkTrafficAnnotationTag traffic_annotation() const { 289 return NetworkTrafficAnnotationTag(traffic_annotation_); 290 } 291 292 private: 293 friend class SpdySession; 294 295 void OnConfirmHandshakeComplete(int rv); 296 297 // Called by |session_| when the stream attempt has finished 298 // successfully. 299 void OnRequestCompleteSuccess(const base::WeakPtr<SpdyStream>& stream); 300 301 // Called by |session_| when the stream attempt has finished with an 302 // error. Also called with ERR_ABORTED if |session_| is destroyed 303 // while the stream attempt is still pending. 304 void OnRequestCompleteFailure(int rv); 305 306 // Accessors called by |session_|. type()307 SpdyStreamType type() const { return type_; } url()308 const GURL& url() const { return url_; } priority()309 RequestPriority priority() const { return priority_; } net_log()310 const NetLogWithSource& net_log() const { return net_log_; } 311 312 void Reset(); 313 314 SpdyStreamType type_; 315 base::WeakPtr<SpdySession> session_; 316 base::WeakPtr<SpdyStream> stream_; 317 GURL url_; 318 RequestPriority priority_; 319 SocketTag socket_tag_; 320 NetLogWithSource net_log_; 321 CompletionOnceCallback callback_; 322 MutableNetworkTrafficAnnotationTag traffic_annotation_; 323 base::TimeTicks confirm_handshake_end_; 324 bool detect_broken_connection_; 325 base::TimeDelta heartbeat_interval_; 326 327 base::WeakPtrFactory<SpdyStreamRequest> weak_ptr_factory_{this}; 328 }; 329 330 class NET_EXPORT SpdySession 331 : public BufferedSpdyFramerVisitorInterface, 332 public spdy::SpdyFramerDebugVisitorInterface, 333 public MultiplexedSession, 334 public HigherLayeredPool, 335 public NetworkChangeNotifier::DefaultNetworkActiveObserver, 336 public Http2PushPromiseIndex::Delegate { 337 public: 338 // TODO(akalin): Use base::TickClock when it becomes available. 339 typedef base::TimeTicks (*TimeFunc)(); 340 341 // Returns true if |new_hostname| can be pooled into an existing connection to 342 // |old_hostname| associated with |ssl_info|. 343 static bool CanPool( 344 TransportSecurityState* transport_security_state, 345 const SSLInfo& ssl_info, 346 const SSLConfigService& ssl_config_service, 347 const std::string& old_hostname, 348 const std::string& new_hostname, 349 const net::NetworkAnonymizationKey& network_anonymization_key); 350 351 // Create a new SpdySession. 352 // |spdy_session_key| is the host/port that this session connects to, privacy 353 // and proxy configuration settings that it's using. 354 // |net_log| is the NetLog that we log network events to. 355 SpdySession(const SpdySessionKey& spdy_session_key, 356 HttpServerProperties* http_server_properties, 357 TransportSecurityState* transport_security_state, 358 SSLConfigService* ssl_config_service, 359 const quic::ParsedQuicVersionVector& quic_supported_versions, 360 bool enable_sending_initial_data, 361 bool enable_ping_based_connection_checking, 362 bool is_http_enabled, 363 bool is_quic_enabled, 364 size_t session_max_recv_window_size, 365 int session_max_queued_capped_frames, 366 const spdy::SettingsMap& initial_settings, 367 bool enable_http2_settings_grease, 368 const absl::optional<SpdySessionPool::GreasedHttp2Frame>& 369 greased_http2_frame, 370 bool http2_end_stream_with_data_frame, 371 bool enable_priority_update, 372 TimeFunc time_func, 373 ServerPushDelegate* push_delegate, 374 NetworkQualityEstimator* network_quality_estimator, 375 NetLog* net_log); 376 377 ~SpdySession() override; 378 host_port_pair()379 const HostPortPair& host_port_pair() const { 380 return spdy_session_key_.host_port_proxy_pair().first; 381 } host_port_proxy_pair()382 const HostPortProxyPair& host_port_proxy_pair() const { 383 return spdy_session_key_.host_port_proxy_pair(); 384 } spdy_session_key()385 const SpdySessionKey& spdy_session_key() const { return spdy_session_key_; } 386 387 // Get a pushed stream for a given |url| with stream ID |pushed_stream_id|. 388 // The caller must have already claimed the stream from Http2PushPromiseIndex. 389 // |pushed_stream_id| must not be kNoPushedStreamFound. 390 // 391 // Returns ERR_CONNECTION_CLOSED if the connection is being closed. 392 // Returns ERR_HTTP2_PUSHED_STREAM_NOT_AVAILABLE if the pushed stream is not 393 // available any longer, for example, if the server has reset it. 394 // Returns OK if the stream is still available, and returns the stream in 395 // |*spdy_stream|. If the stream is still open, updates its priority to 396 // |priority|. 397 int GetPushedStream(const GURL& url, 398 spdy::SpdyStreamId pushed_stream_id, 399 RequestPriority priority, 400 raw_ptr<SpdyStream>* spdy_stream); 401 402 // Called when the pushed stream should be cancelled. If the pushed stream is 403 // not claimed and active, sends RST to the server to cancel the stream. 404 void CancelPush(const GURL& url); 405 406 // Initialize the session with the given connection. 407 // 408 // |pool| is the SpdySessionPool that owns us. Its lifetime must 409 // strictly be greater than |this|. 410 // 411 // The session begins reading from |client_socket_handle| on a subsequent 412 // event loop iteration, so the SpdySession may close immediately afterwards 413 // if the first read of |client_socket_handle| fails. 414 void InitializeWithSocketHandle( 415 std::unique_ptr<ClientSocketHandle> client_socket_handle, 416 SpdySessionPool* pool); 417 418 // Just like InitializeWithSocketHandle(), but for use when the session is not 419 // on top of a socket pool, but instead directly on top of a socket, which the 420 // session has sole ownership of, and is responsible for deleting directly 421 // itself. 422 void InitializeWithSocket(std::unique_ptr<StreamSocket> stream_socket, 423 const LoadTimingInfo::ConnectTiming& connect_timing, 424 SpdySessionPool* pool); 425 426 // Parse ALPS application_data from TLS handshake. 427 // Returns OK on success. Return a net error code on failure, and closes the 428 // connection with the same error code. 429 int ParseAlps(); 430 431 // Check to see if this SPDY session can support an additional domain. 432 // If the session is un-authenticated, then this call always returns true. 433 // For SSL-based sessions, verifies that the server certificate in use by 434 // this session provides authentication for the domain and no client 435 // certificate or channel ID was sent to the original server during the SSL 436 // handshake. NOTE: This function can have false negatives on some 437 // platforms. 438 // TODO(wtc): rename this function and the Net.SpdyIPPoolDomainMatch 439 // histogram because this function does more than verifying domain 440 // authentication now. 441 bool VerifyDomainAuthentication(const std::string& domain) const; 442 443 // Pushes the given producer into the write queue for 444 // |stream|. |stream| is guaranteed to be activated before the 445 // producer is used to produce its frame. 446 void EnqueueStreamWrite(const base::WeakPtr<SpdyStream>& stream, 447 spdy::SpdyFrameType frame_type, 448 std::unique_ptr<SpdyBufferProducer> producer); 449 450 // Returns true if this session is configured to send greased HTTP/2 frames. 451 // For more details on greased frames, see 452 // https://tools.ietf.org/html/draft-bishop-httpbis-grease-00. 453 bool GreasedFramesEnabled() const; 454 455 // Returns true if HEADERS frames on request streams should not have the 456 // END_STREAM flag set, but instead an empty DATA frame with END_STREAM should 457 // be sent afterwards to close the stream. Does not apply to bidirectional or 458 // proxy streams. EndStreamWithDataFrame()459 bool EndStreamWithDataFrame() const { 460 return http2_end_stream_with_data_frame_; 461 } 462 463 // Send greased frame, that is, a frame of reserved type. 464 void EnqueueGreasedFrame(const base::WeakPtr<SpdyStream>& stream); 465 466 // Returns whether HTTP/2 style priority information (stream dependency and 467 // weight fields in HEADERS frames, and PRIORITY frames) should be sent. True 468 // unless |enable_priority_update_| is true and 469 // SETTINGS_DEPRECATE_HTTP2_PRIORITIES with value 1 has been received from 470 // server. In particular, if it returns false, it will always return false 471 // afterwards. 472 bool ShouldSendHttp2Priority() const; 473 474 // Returns whether PRIORITY_UPDATE frames should be sent. False if 475 // |enable_priority_update_| is false. Otherwise, true before SETTINGS frame 476 // is received from server, and true after SETTINGS frame is received if it 477 // contained SETTINGS_DEPRECATE_HTTP2_PRIORITIES with value 1. In particular, 478 // if it returns false, it will always return false afterwards. 479 bool ShouldSendPriorityUpdate() const; 480 481 // Runs the handshake to completion to confirm the handshake with the server. 482 // If ERR_IO_PENDING is returned, then when the handshake is confirmed, 483 // |callback| will be called. 484 int ConfirmHandshake(CompletionOnceCallback callback); 485 486 // Creates and returns a HEADERS frame for |stream_id|. 487 std::unique_ptr<spdy::SpdySerializedFrame> CreateHeaders( 488 spdy::SpdyStreamId stream_id, 489 RequestPriority priority, 490 spdy::SpdyControlFlags flags, 491 spdy::Http2HeaderBlock headers, 492 NetLogSource source_dependency); 493 494 // Creates and returns a SpdyBuffer holding a data frame with the given data. 495 // Sets |*effective_len| to number of bytes sent, and |*end_stream| to the 496 // value of the END_STREAM (also known as fin) flag. Returns nullptr if 497 // session is draining or if session or stream is stalled by flow control. 498 std::unique_ptr<SpdyBuffer> CreateDataBuffer(spdy::SpdyStreamId stream_id, 499 IOBuffer* data, 500 int len, 501 spdy::SpdyDataFlags flags, 502 int* effective_len, 503 bool* end_stream); 504 505 // Send PRIORITY frames according to the new priority of an existing stream. 506 void UpdateStreamPriority(SpdyStream* stream, 507 RequestPriority old_priority, 508 RequestPriority new_priority); 509 510 // Close the stream with the given ID, which must exist and be 511 // active. Note that that stream may hold the last reference to the 512 // session. 513 void CloseActiveStream(spdy::SpdyStreamId stream_id, int status); 514 515 // Close the given created stream, which must exist but not yet be 516 // active. Note that |stream| may hold the last reference to the 517 // session. 518 void CloseCreatedStream(const base::WeakPtr<SpdyStream>& stream, int status); 519 520 // Send a RST_STREAM frame with the given status code and close the 521 // stream with the given ID, which must exist and be active. Note 522 // that that stream may hold the last reference to the session. 523 void ResetStream(spdy::SpdyStreamId stream_id, 524 int error, 525 const std::string& description); 526 527 // Check if a stream is active. 528 bool IsStreamActive(spdy::SpdyStreamId stream_id) const; 529 530 // The LoadState is used for informing the user of the current network 531 // status, such as "resolving host", "connecting", etc. 532 LoadState GetLoadState() const; 533 534 // MultiplexedSession methods: 535 int GetRemoteEndpoint(IPEndPoint* endpoint) override; 536 bool GetSSLInfo(SSLInfo* ssl_info) const override; 537 base::StringPiece GetAcceptChViaAlps( 538 const url::SchemeHostPort& scheme_host_port) const override; 539 540 // Returns true if ALPN was negotiated for the underlying socket. 541 bool WasAlpnNegotiated() const; 542 543 // Returns the protocol negotiated via ALPN for the underlying socket. 544 NextProto GetNegotiatedProtocol() const; 545 546 // Send a WINDOW_UPDATE frame for a stream. Called by a stream 547 // whenever receive window size is increased. 548 void SendStreamWindowUpdate(spdy::SpdyStreamId stream_id, 549 uint32_t delta_window_size); 550 551 // Configure the amount of time that small receive window updates should 552 // be accumulated over (defaults to kDefaultTimeToBufferSmallWindowUpdates). SetTimeToBufferSmallWindowUpdates(const base::TimeDelta buffer_time)553 void SetTimeToBufferSmallWindowUpdates(const base::TimeDelta buffer_time) { 554 time_to_buffer_small_window_updates_ = buffer_time; 555 } 556 557 // Returns the configured time that small receive window updates should 558 // be accumulated over. TimeToBufferSmallWindowUpdates()559 base::TimeDelta TimeToBufferSmallWindowUpdates() const { 560 return time_to_buffer_small_window_updates_; 561 } 562 563 // Accessors for the session's availability state. IsAvailable()564 bool IsAvailable() const { return availability_state_ == STATE_AVAILABLE; } IsGoingAway()565 bool IsGoingAway() const { return availability_state_ == STATE_GOING_AWAY; } IsDraining()566 bool IsDraining() const { return availability_state_ == STATE_DRAINING; } 567 568 // Closes this session. This will close all active streams and mark 569 // the session as permanently closed. Callers must assume that the 570 // session is destroyed after this is called. (However, it may not 571 // be destroyed right away, e.g. when a SpdySession function is 572 // present in the call stack.) 573 // 574 // |err| should be < ERR_IO_PENDING; this function is intended to be 575 // called on error. 576 // |description| indicates the reason for the error. 577 void CloseSessionOnError(Error err, const std::string& description); 578 579 // Mark this session as unavailable, meaning that it will not be used to 580 // service new streams. Unlike when a GOAWAY frame is received, this function 581 // will not close any streams. 582 void MakeUnavailable(); 583 584 // Closes all active streams with stream id's greater than 585 // |last_good_stream_id|, as well as any created or pending 586 // streams. Must be called only when |availability_state_| >= 587 // STATE_GOING_AWAY. After this function, DcheckGoingAway() will 588 // pass. May be called multiple times. 589 void StartGoingAway(spdy::SpdyStreamId last_good_stream_id, Error status); 590 591 // Must be called only when going away (i.e., DcheckGoingAway() 592 // passes). If there are no more active streams and the session 593 // isn't closed yet, close it. 594 void MaybeFinishGoingAway(); 595 596 // Retrieves information on the current state of the SPDY session as a 597 // Value. 598 base::Value::Dict GetInfoAsValue() const; 599 600 // Indicates whether the session is being reused after having successfully 601 // used to send/receive data in the past or if the underlying socket was idle 602 // before being used for a SPDY session. 603 bool IsReused() const; 604 605 // Returns true if the underlying transport socket ever had any reads or 606 // writes. WasEverUsed()607 bool WasEverUsed() const { return socket_->WasEverUsed(); } 608 609 // Returns the load timing information from the perspective of the given 610 // stream. If it's not the first stream, the connection is considered reused 611 // for that stream. 612 // 613 // This uses a different notion of reuse than IsReused(). This function 614 // sets |socket_reused| to false only if |stream_id| is the ID of the first 615 // stream using the session. IsReused(), on the other hand, indicates if the 616 // session has been used to send/receive data at all. 617 bool GetLoadTimingInfo(spdy::SpdyStreamId stream_id, 618 LoadTimingInfo* load_timing_info) const; 619 620 // Returns true if session is currently active. is_active()621 bool is_active() const { 622 return !active_streams_.empty() || !created_streams_.empty(); 623 } 624 625 // True if the server supports WebSocket protocol. support_websocket()626 bool support_websocket() const { return support_websocket_; } 627 628 // Returns true if no stream in the session can send data due to 629 // session flow control. IsSendStalled()630 bool IsSendStalled() const { return session_send_window_size_ == 0; } 631 net_log()632 const NetLogWithSource& net_log() const { return net_log_; } 633 634 int GetPeerAddress(IPEndPoint* address) const; 635 int GetLocalAddress(IPEndPoint* address) const; 636 637 // Adds |alias| to set of aliases associated with this session. 638 void AddPooledAlias(const SpdySessionKey& alias_key); 639 640 // Removes |alias| from set of aliases associated with this session. 641 void RemovePooledAlias(const SpdySessionKey& alias_key); 642 643 // Returns the set of aliases associated with this session. pooled_aliases()644 const std::set<SpdySessionKey>& pooled_aliases() const { 645 return pooled_aliases_; 646 } 647 648 // https://http2.github.io/http2-spec/#TLSUsage mandates minimum security 649 // standards for TLS. 650 bool HasAcceptableTransportSecurity() const; 651 652 // Must be used only by |pool_| (including |pool_.push_promise_index_|). 653 base::WeakPtr<SpdySession> GetWeakPtr(); 654 655 // HigherLayeredPool implementation: 656 bool CloseOneIdleConnection() override; 657 658 // Http2PushPromiseIndex::Delegate implementation: 659 bool ValidatePushedStream(spdy::SpdyStreamId stream_id, 660 const GURL& url, 661 const HttpRequestInfo& request_info, 662 const SpdySessionKey& key) const override; 663 base::WeakPtr<SpdySession> GetWeakPtrToSession() override; 664 665 // Change this session's socket tag to |new_tag|. Returns true on success. 666 bool ChangeSocketTag(const SocketTag& new_tag); 667 668 // Whether connection status monitoring is active or not. 669 bool IsBrokenConnectionDetectionEnabled() const; 670 671 static void RecordSpdyPushedStreamFateHistogram(SpdyPushedStreamFate value); 672 673 private: 674 friend class test::SpdyStreamTest; 675 friend class base::RefCounted<SpdySession>; 676 friend class HttpNetworkTransactionTest; 677 friend class HttpProxyClientSocketPoolTest; 678 friend class SpdyHttpStreamTest; 679 friend class SpdyNetworkTransactionTest; 680 friend class SpdyProxyClientSocketTest; 681 friend class SpdySessionPoolTest; 682 friend class SpdySessionTest; 683 friend class SpdyStreamRequest; 684 685 FRIEND_TEST_ALL_PREFIXES(RecordPushedStreamHistogramTest, VaryResponseHeader); 686 687 using PendingStreamRequestQueue = 688 base::circular_deque<base::WeakPtr<SpdyStreamRequest>>; 689 using ActiveStreamMap = std::map<spdy::SpdyStreamId, SpdyStream*>; 690 using CreatedStreamSet = std::set<SpdyStream*>; 691 692 enum AvailabilityState { 693 // The session is available in its socket pool and can be used 694 // freely. 695 STATE_AVAILABLE, 696 // The session can process data on existing streams but will 697 // refuse to create new ones. 698 STATE_GOING_AWAY, 699 // The session is draining its write queue in preparation of closing. 700 // Further writes will not be queued, and further reads will not be issued 701 // (though the remainder of a current read may be processed). The session 702 // will be destroyed by its write loop once the write queue is drained. 703 STATE_DRAINING, 704 }; 705 706 enum ReadState { 707 READ_STATE_DO_READ, 708 READ_STATE_DO_READ_COMPLETE, 709 }; 710 711 enum WriteState { 712 // There is no in-flight write and the write queue is empty. 713 WRITE_STATE_IDLE, 714 WRITE_STATE_DO_WRITE, 715 WRITE_STATE_DO_WRITE_COMPLETE, 716 }; 717 718 // Has the shared logic for the other two Initialize methods that call it. 719 void InitializeInternal(SpdySessionPool* pool); 720 721 // Called by SpdyStreamRequest to start a request to create a 722 // stream. If OK is returned, then |stream| will be filled in with a 723 // valid stream. If ERR_IO_PENDING is returned, then 724 // |request->OnRequestComplete{Success,Failure}()| will be called 725 // when the stream is created (unless it is cancelled). Otherwise, 726 // no stream is created and the error is returned. 727 int TryCreateStream(const base::WeakPtr<SpdyStreamRequest>& request, 728 base::WeakPtr<SpdyStream>* stream); 729 730 // Actually create a stream into |stream|. Returns OK if successful; 731 // otherwise, returns an error and |stream| is not filled. 732 int CreateStream(const SpdyStreamRequest& request, 733 base::WeakPtr<SpdyStream>* stream); 734 735 // Called by SpdyStreamRequest to remove |request| from the stream 736 // creation queue. Returns whether a request was removed from the queue. 737 bool CancelStreamRequest(const base::WeakPtr<SpdyStreamRequest>& request); 738 739 // Removes |request| from the stream creation queue and reinserts it into the 740 // queue at the new |priority|. 741 void ChangeStreamRequestPriority( 742 const base::WeakPtr<SpdyStreamRequest>& request, 743 RequestPriority priority); 744 745 // Returns the next pending stream request to process, or NULL if 746 // there is none. 747 base::WeakPtr<SpdyStreamRequest> GetNextPendingStreamRequest(); 748 749 // Called when there is room to create more streams (e.g., a stream 750 // was closed). Processes as many pending stream requests as 751 // possible. 752 void ProcessPendingStreamRequests(); 753 754 void TryCreatePushStream(spdy::SpdyStreamId stream_id, 755 spdy::SpdyStreamId associated_stream_id, 756 spdy::Http2HeaderBlock headers); 757 758 // Close the stream pointed to by the given iterator. Note that that 759 // stream may hold the last reference to the session. 760 void CloseActiveStreamIterator(ActiveStreamMap::iterator it, int status); 761 762 // Close the stream pointed to by the given iterator. Note that that 763 // stream may hold the last reference to the session. 764 void CloseCreatedStreamIterator(CreatedStreamSet::iterator it, int status); 765 766 // Calls EnqueueResetStreamFrame() and then 767 // CloseActiveStreamIterator(). 768 void ResetStreamIterator(ActiveStreamMap::iterator it, 769 int status, 770 const std::string& description); 771 772 // Send a RST_STREAM frame with the given parameters. There should 773 // either be no active stream with the given ID, or that active 774 // stream should be closed shortly after this function is called. 775 void EnqueueResetStreamFrame(spdy::SpdyStreamId stream_id, 776 RequestPriority priority, 777 spdy::SpdyErrorCode error_code, 778 const std::string& description); 779 780 // Send a PRIORITY frame with the given parameters. 781 void EnqueuePriorityFrame(spdy::SpdyStreamId stream_id, 782 spdy::SpdyStreamId dependency_id, 783 int weight, 784 bool exclusive); 785 786 // Calls DoReadLoop. Use this function instead of DoReadLoop when 787 // posting a task to pump the read loop. 788 void PumpReadLoop(ReadState expected_read_state, int result); 789 790 // Advance the ReadState state machine. |expected_read_state| is the 791 // expected starting read state. 792 // 793 // This function must always be called via PumpReadLoop(). 794 int DoReadLoop(ReadState expected_read_state, int result); 795 // The implementations of the states of the ReadState state machine. 796 int DoRead(); 797 int DoReadComplete(int result); 798 799 // Calls DoWriteLoop. If |availability_state_| is STATE_DRAINING and no 800 // writes remain, the session is removed from the session pool and 801 // destroyed. 802 // 803 // Use this function instead of DoWriteLoop when posting a task to 804 // pump the write loop. 805 void PumpWriteLoop(WriteState expected_write_state, int result); 806 807 // Iff the write loop is not currently active, posts a callback into 808 // PumpWriteLoop(). 809 void MaybePostWriteLoop(); 810 811 // Advance the WriteState state machine. |expected_write_state| is 812 // the expected starting write state. 813 // 814 // This function must always be called via PumpWriteLoop(). 815 int DoWriteLoop(WriteState expected_write_state, int result); 816 // The implementations of the states of the WriteState state machine. 817 int DoWrite(); 818 int DoWriteComplete(int result); 819 820 void NotifyRequestsOfConfirmation(int rv); 821 822 // TODO(akalin): Rename the Send* and Write* functions below to 823 // Enqueue*. 824 825 // Send initial data. Called when a connection is successfully 826 // established in InitializeWithSocket() and 827 // |enable_sending_initial_data_| is true. 828 void SendInitialData(); 829 830 // Handle SETTING. Either when we send settings, or when we receive a 831 // SETTINGS control frame, update our SpdySession accordingly. 832 void HandleSetting(uint32_t id, uint32_t value); 833 834 // Adjust the send window size of all ActiveStreams and PendingStreamRequests. 835 void UpdateStreamsSendWindowSize(int32_t delta_window_size); 836 837 // Checks the connection status in an energy efficient manner: 838 // * If the radio is in full power mode, send the PING immediately 839 // * If the radio is in standby, record the event and send the PING once the 840 // radio wakes up 841 // The radio status check is currently only implemented for Android devices, 842 // on all other platforms the radio is assumed to be always active (i.e., no 843 // batching happens). 844 void MaybeCheckConnectionStatus(); 845 // Always checks the connection status and schedules the next check. 846 void CheckConnectionStatus(); 847 // Send PING frame if all previous PING frames have been ACKed, 848 // all posted CheckPingStatus() tasks have been executed, 849 // and too long time has passed since last read from server. 850 void MaybeSendPrefacePing(); 851 852 // Send a single WINDOW_UPDATE frame. 853 void SendWindowUpdateFrame(spdy::SpdyStreamId stream_id, 854 uint32_t delta_window_size, 855 RequestPriority priority); 856 857 // Send the PING frame. 858 void WritePingFrame(spdy::SpdyPingId unique_id, bool is_ack); 859 860 // Post a CheckPingStatus call after delay. Don't post if there is already 861 // CheckPingStatus running. 862 void PlanToCheckPingStatus(); 863 864 // Check the status of the connection. It calls |CloseSessionOnError| if we 865 // haven't received any data in |kHungInterval| time period. 866 void CheckPingStatus(base::TimeTicks last_check_time); 867 868 // Get a new stream id. 869 spdy::SpdyStreamId GetNewStreamId(); 870 871 // Pushes the given frame with the given priority into the write 872 // queue for the session. 873 void EnqueueSessionWrite(RequestPriority priority, 874 spdy::SpdyFrameType frame_type, 875 std::unique_ptr<spdy::SpdySerializedFrame> frame); 876 877 // Puts |producer| associated with |stream| onto the write queue 878 // with the given priority. 879 void EnqueueWrite(RequestPriority priority, 880 spdy::SpdyFrameType frame_type, 881 std::unique_ptr<SpdyBufferProducer> producer, 882 const base::WeakPtr<SpdyStream>& stream, 883 const NetworkTrafficAnnotationTag& traffic_annotation); 884 885 // Inserts a newly-created stream into |created_streams_|. 886 void InsertCreatedStream(std::unique_ptr<SpdyStream> stream); 887 888 // Activates |stream| (which must be in |created_streams_|) by 889 // assigning it an ID and returns it. 890 std::unique_ptr<SpdyStream> ActivateCreatedStream(SpdyStream* stream); 891 892 // Inserts a newly-activated stream into |active_streams_|. 893 void InsertActivatedStream(std::unique_ptr<SpdyStream> stream); 894 895 // Remove all internal references to |stream|, call OnClose() on it, 896 // and process any pending stream requests before deleting it. Note 897 // that |stream| may hold the last reference to the session. 898 void DeleteStream(std::unique_ptr<SpdyStream> stream, int status); 899 900 void RecordHistograms(); 901 void RecordProtocolErrorHistogram(SpdyProtocolErrorDetails details); 902 static void RecordPushedStreamVaryResponseHeaderHistogram( 903 const spdy::Http2HeaderBlock& headers); 904 905 // DCHECKs that |availability_state_| >= STATE_GOING_AWAY, that 906 // there are no pending stream creation requests, and that there are 907 // no created streams. 908 void DcheckGoingAway() const; 909 910 // Calls DcheckGoingAway(), then DCHECKs that |availability_state_| 911 // == STATE_DRAINING, |error_on_close_| has a valid value, and that there 912 // are no active streams or unclaimed pushed streams. 913 void DcheckDraining() const; 914 915 // If the session is already draining, does nothing. Otherwise, moves 916 // the session to the draining state. 917 void DoDrainSession(Error err, const std::string& description); 918 919 // Called right before closing a (possibly-inactive) stream for a 920 // reason other than being requested to by the stream. 921 void LogAbandonedStream(SpdyStream* stream, Error status); 922 923 // Called right before closing an active stream for a reason other 924 // than being requested to by the stream. 925 void LogAbandonedActiveStream(ActiveStreamMap::const_iterator it, 926 Error status); 927 928 // Invokes a user callback for stream creation. We provide this method so it 929 // can be deferred to the MessageLoop, so we avoid re-entrancy problems. 930 void CompleteStreamRequest( 931 const base::WeakPtr<SpdyStreamRequest>& pending_request); 932 933 // Cancel pushed stream with |stream_id|, if still unclaimed. Identifying a 934 // pushed stream by GURL instead of stream ID could result in incorrect 935 // behavior if a pushed stream was claimed but later another stream was pushed 936 // for the same GURL. 937 void CancelPushedStreamIfUnclaimed(spdy::SpdyStreamId stream_id); 938 939 // BufferedSpdyFramerVisitorInterface: 940 void OnError( 941 http2::Http2DecoderAdapter::SpdyFramerError spdy_framer_error) override; 942 void OnStreamError(spdy::SpdyStreamId stream_id, 943 const std::string& description) override; 944 void OnPing(spdy::SpdyPingId unique_id, bool is_ack) override; 945 void OnRstStream(spdy::SpdyStreamId stream_id, 946 spdy::SpdyErrorCode error_code) override; 947 void OnGoAway(spdy::SpdyStreamId last_accepted_stream_id, 948 spdy::SpdyErrorCode error_code, 949 base::StringPiece debug_data) override; 950 void OnDataFrameHeader(spdy::SpdyStreamId stream_id, 951 size_t length, 952 bool fin) override; 953 void OnStreamFrameData(spdy::SpdyStreamId stream_id, 954 const char* data, 955 size_t len) override; 956 void OnStreamEnd(spdy::SpdyStreamId stream_id) override; 957 void OnStreamPadding(spdy::SpdyStreamId stream_id, size_t len) override; 958 void OnSettings() override; 959 void OnSettingsAck() override; 960 void OnSetting(spdy::SpdySettingsId id, uint32_t value) override; 961 void OnSettingsEnd() override; 962 void OnWindowUpdate(spdy::SpdyStreamId stream_id, 963 int delta_window_size) override; 964 void OnPushPromise(spdy::SpdyStreamId stream_id, 965 spdy::SpdyStreamId promised_stream_id, 966 spdy::Http2HeaderBlock headers) override; 967 void OnHeaders(spdy::SpdyStreamId stream_id, 968 bool has_priority, 969 int weight, 970 spdy::SpdyStreamId parent_stream_id, 971 bool exclusive, 972 bool fin, 973 spdy::Http2HeaderBlock headers, 974 base::TimeTicks recv_first_byte_time) override; 975 void OnAltSvc(spdy::SpdyStreamId stream_id, 976 base::StringPiece origin, 977 const spdy::SpdyAltSvcWireFormat::AlternativeServiceVector& 978 altsvc_vector) override; 979 bool OnUnknownFrame(spdy::SpdyStreamId stream_id, 980 uint8_t frame_type) override; 981 982 // spdy::SpdyFramerDebugVisitorInterface 983 void OnSendCompressedFrame(spdy::SpdyStreamId stream_id, 984 spdy::SpdyFrameType type, 985 size_t payload_len, 986 size_t frame_len) override; 987 void OnReceiveCompressedFrame(spdy::SpdyStreamId stream_id, 988 spdy::SpdyFrameType type, 989 size_t frame_len) override; 990 991 // Called when bytes are consumed from a SpdyBuffer for a DATA frame 992 // that is to be written or is being written. Increases the send 993 // window size accordingly if some or all of the SpdyBuffer is being 994 // discarded. 995 // 996 // If session flow control is turned off, this must not be called. 997 void OnWriteBufferConsumed(size_t frame_payload_size, 998 size_t consume_size, 999 SpdyBuffer::ConsumeSource consume_source); 1000 1001 // Called when the radio goes into full power mode. Currently implemented only 1002 // for Android devices. 1003 void OnDefaultNetworkActive() override; 1004 1005 // Called by OnWindowUpdate() (which is in turn called by the 1006 // framer) to increase this session's send window size by 1007 // |delta_window_size| from a WINDOW_UPDATE frome, which must be at 1008 // least 1. If |delta_window_size| would cause this session's send 1009 // window size to overflow, does nothing. 1010 // 1011 // If session flow control is turned off, this must not be called. 1012 void IncreaseSendWindowSize(int delta_window_size); 1013 1014 // If session flow control is turned on, called by CreateDataFrame() 1015 // (which is in turn called by a stream) to decrease this session's 1016 // send window size by |delta_window_size|, which must be at least 1 1017 // and at most kMaxSpdyFrameChunkSize. |delta_window_size| must not 1018 // cause this session's send window size to go negative. 1019 // 1020 // If session flow control is turned off, this must not be called. 1021 void DecreaseSendWindowSize(int32_t delta_window_size); 1022 1023 // Called when bytes are consumed by the delegate from a SpdyBuffer 1024 // containing received data. Increases the receive window size 1025 // accordingly. 1026 // 1027 // If session flow control is turned off, this must not be called. 1028 void OnReadBufferConsumed(size_t consume_size, 1029 SpdyBuffer::ConsumeSource consume_source); 1030 1031 // Called by OnReadBufferConsume to increase this session's receive 1032 // window size by |delta_window_size|, which must be at least 1 and 1033 // must not cause this session's receive window size to overflow, 1034 // possibly also sending a WINDOW_UPDATE frame. Also called during 1035 // initialization to set the initial receive window size. 1036 // 1037 // If session flow control is turned off, this must not be called. 1038 void IncreaseRecvWindowSize(int32_t delta_window_size); 1039 1040 // Called by OnStreamFrameData (which is in turn called by the 1041 // framer) to decrease this session's receive window size by 1042 // |delta_window_size|, which must be at least 1 and must not cause 1043 // this session's receive window size to go negative. 1044 // 1045 // If session flow control is turned off, this must not be called. 1046 void DecreaseRecvWindowSize(int32_t delta_window_size); 1047 1048 // Queue a send-stalled stream for possibly resuming once we're not 1049 // send-stalled anymore. 1050 void QueueSendStalledStream(const SpdyStream& stream); 1051 1052 // Go through the queue of send-stalled streams and try to resume as 1053 // many as possible. 1054 void ResumeSendStalledStreams(); 1055 1056 // Returns the next stream to possibly resume, or 0 if the queue is 1057 // empty. 1058 spdy::SpdyStreamId PopStreamToPossiblyResume(); 1059 1060 // Enables connection status monitoring, causing the session to periodically 1061 // send a PING frame. 1062 // This must be called at most once for each stream requiring it. If called, 1063 // MaybeDisableBrokenConnectionDetection() will need to be called before 1064 // closing the requesting stream. 1065 // Note: `heartbeat_interval` should be considered a suggestion. The 1066 // implementation, for example, could either: 1067 // * Avoid sending a PING, if one has recently been transmitted or is 1068 // already in flight 1069 // * Delay sending a PING, to avoid waking up the radio on mobile platforms 1070 // Only the first value of `heartbeat_interval` is taken into account. 1071 void EnableBrokenConnectionDetection(base::TimeDelta heartbeat_interval); 1072 1073 // Requests to disable connection status monitoring. The service is disabled 1074 // only if no other active stream also requires it (an internal counter keeps 1075 // track of that). 1076 // This must be called once for each stream that requested it previously. 1077 void MaybeDisableBrokenConnectionDetection(); 1078 1079 // Whether Do{Read,Write}Loop() is in the call stack. Useful for 1080 // making sure we don't destroy ourselves prematurely in that case. 1081 bool in_io_loop_ = false; 1082 1083 // The key used to identify this session. 1084 SpdySessionKey spdy_session_key_; 1085 1086 // Set set of SpdySessionKeys for which this session has serviced 1087 // requests. 1088 std::set<SpdySessionKey> pooled_aliases_; 1089 1090 // |pool_| owns us, therefore its lifetime must exceed ours. 1091 raw_ptr<SpdySessionPool> pool_ = nullptr; 1092 raw_ptr<HttpServerProperties> http_server_properties_; 1093 1094 raw_ptr<TransportSecurityState> transport_security_state_; 1095 raw_ptr<SSLConfigService> ssl_config_service_; 1096 1097 // One of these two owns the socket for this session, which is stored in 1098 // |socket_|. If |client_socket_handle_| is non-null, this session is on top 1099 // of a socket in a socket pool. If |owned_stream_socket_| is non-null, this 1100 // session is directly on top of a socket, which is not in a socket pool. 1101 std::unique_ptr<ClientSocketHandle> client_socket_handle_; 1102 std::unique_ptr<StreamSocket> owned_stream_socket_; 1103 1104 // This is non-null only if |owned_stream_socket_| is non-null. 1105 std::unique_ptr<LoadTimingInfo::ConnectTiming> connect_timing_; 1106 1107 // The socket for this session. 1108 raw_ptr<StreamSocket> socket_ = nullptr; 1109 1110 // The read buffer used to read data from the socket. 1111 // Non-null if there is a Read() pending. 1112 scoped_refptr<IOBuffer> read_buffer_; 1113 1114 spdy::SpdyStreamId stream_hi_water_mark_; // The next stream id to use. 1115 1116 // Used to ensure the server increments push stream ids correctly. 1117 spdy::SpdyStreamId last_accepted_push_stream_id_ = 0; 1118 1119 // Queue, for each priority, of pending stream requests that have 1120 // not yet been satisfied. 1121 PendingStreamRequestQueue pending_create_stream_queues_[NUM_PRIORITIES]; 1122 1123 // Map from stream id to all active streams. Streams are active in the sense 1124 // that they have a consumer (typically SpdyNetworkTransaction and regardless 1125 // of whether or not there is currently any ongoing IO [might be waiting for 1126 // the server to start pushing the stream]) or there are still network events 1127 // incoming even though the consumer has already gone away (cancellation). 1128 // 1129 // |active_streams_| owns all its SpdyStream objects. 1130 // 1131 // TODO(willchan): Perhaps we should separate out cancelled streams and move 1132 // them into a separate ActiveStreamMap, and not deliver network events to 1133 // them? 1134 ActiveStreamMap active_streams_; 1135 1136 // Not owned. |push_delegate_| outlives the session and handles server pushes 1137 // received by session. 1138 raw_ptr<ServerPushDelegate> push_delegate_; 1139 1140 // Set of all created streams but that have not yet sent any frames. 1141 // 1142 // |created_streams_| owns all its SpdyStream objects. 1143 CreatedStreamSet created_streams_; 1144 1145 // Number of pushed streams. All active streams are stored in 1146 // |active_streams_|, but it's better to know the number of push streams 1147 // without traversing the whole collection. 1148 size_t num_pushed_streams_ = 0u; 1149 1150 // Number of active pushed streams in |active_streams_|, i.e. not in reserved 1151 // remote state. Streams in reserved state are not counted towards any 1152 // concurrency limits. 1153 size_t num_active_pushed_streams_ = 0u; 1154 1155 // Number of bytes that has been pushed by the server. 1156 uint64_t bytes_pushed_count_ = 0u; 1157 1158 // Number of bytes that has been pushed by the server but never claimed. 1159 uint64_t bytes_pushed_and_unclaimed_count_ = 0u; 1160 1161 // The write queue. 1162 SpdyWriteQueue write_queue_; 1163 1164 // Data for the frame we are currently sending. 1165 1166 // The buffer we're currently writing. 1167 std::unique_ptr<SpdyBuffer> in_flight_write_; 1168 // The type of the frame in |in_flight_write_|. 1169 spdy::SpdyFrameType in_flight_write_frame_type_ = spdy::SpdyFrameType::DATA; 1170 // The size of the frame in |in_flight_write_|. 1171 size_t in_flight_write_frame_size_ = 0; 1172 // The stream to notify when |in_flight_write_| has been written to 1173 // the socket completely. 1174 base::WeakPtr<SpdyStream> in_flight_write_stream_; 1175 1176 // Traffic annotation for the write in progress. 1177 MutableNetworkTrafficAnnotationTag in_flight_write_traffic_annotation_; 1178 1179 // Spdy Frame state. 1180 std::unique_ptr<BufferedSpdyFramer> buffered_spdy_framer_; 1181 1182 // The state variables. 1183 AvailabilityState availability_state_ = STATE_AVAILABLE; 1184 ReadState read_state_ = READ_STATE_DO_READ; 1185 WriteState write_state_ = WRITE_STATE_IDLE; 1186 1187 // If the session is closing (i.e., |availability_state_| is STATE_DRAINING), 1188 // then |error_on_close_| holds the error with which it was closed, which 1189 // may be OK (upon a polite GOAWAY) or an error < ERR_IO_PENDING otherwise. 1190 // Initialized to OK. 1191 Error error_on_close_ = OK; 1192 1193 // Settings that are sent in the initial SETTINGS frame 1194 // (if |enable_sending_initial_data_| is true), 1195 // and also control SpdySession parameters like initial receive window size 1196 // and maximum HPACK dynamic table size. 1197 const spdy::SettingsMap initial_settings_; 1198 1199 // If true, a setting parameter with reserved identifier will be sent in every 1200 // initial SETTINGS frame, see 1201 // https://tools.ietf.org/html/draft-bishop-httpbis-grease-00. 1202 // The setting identifier and value will be drawn independently for each 1203 // connection to prevent tracking of the client. 1204 const bool enable_http2_settings_grease_; 1205 1206 // If set, an HTTP/2 frame with a reserved frame type will be sent after 1207 // every HTTP/2 SETTINGS frame and before every HTTP/2 DATA frame. See 1208 // https://tools.ietf.org/html/draft-bishop-httpbis-grease-00. 1209 const absl::optional<SpdySessionPool::GreasedHttp2Frame> greased_http2_frame_; 1210 1211 // If set, the HEADERS frame carrying a request without body will not have the 1212 // END_STREAM flag set. The stream will be closed by a subsequent empty DATA 1213 // frame with END_STREAM. Does not affect bidirectional or proxy streams. 1214 // If unset, the HEADERS frame will have the END_STREAM flag set on. 1215 // This is useful in conjuction with |greased_http2_frame_| so that a frame 1216 // of reserved type can be sent out even on requests without a body. 1217 const bool http2_end_stream_with_data_frame_; 1218 1219 // If true, enable sending PRIORITY_UPDATE frames until SETTINGS frame 1220 // arrives. After SETTINGS frame arrives, do not send PRIORITY_UPDATE frames 1221 // any longer if SETTINGS_DEPRECATE_HTTP2_PRIORITIES is missing or has zero 0, 1222 // but continue and also stop sending HTTP/2-style priority information in 1223 // HEADERS frames and PRIORITY frames if it has value 1. 1224 const bool enable_priority_update_; 1225 1226 // The value of the last received SETTINGS_DEPRECATE_HTTP2_PRIORITIES, with 0 1227 // mapping to false and 1 to true. Initial value is false. 1228 bool deprecate_http2_priorities_ = false; 1229 1230 // True if at least one SETTINGS frame has been received. 1231 bool settings_frame_received_ = false; 1232 1233 // The callbacks to notify a request that the handshake has been confirmed. 1234 std::vector<CompletionOnceCallback> waiting_for_confirmation_callbacks_; 1235 1236 // True if there is an ongoing handshake confirmation with outstanding 1237 // requests. 1238 bool in_confirm_handshake_ = false; 1239 1240 // Limits 1241 size_t max_concurrent_streams_; 1242 // TODO(https://crbug.com/1426477): Remove. 1243 size_t max_concurrent_pushed_streams_; 1244 1245 // Some statistics counters for the session. 1246 int streams_initiated_count_ = 0; 1247 1248 // TODO(https://crbug.com/1426477): Remove. 1249 int streams_pushed_count_ = 0; 1250 int streams_pushed_and_claimed_count_ = 0; 1251 1252 int streams_abandoned_count_ = 0; 1253 1254 // True if there has been a ping sent for which we have not received a 1255 // response yet. There is always at most one ping in flight. 1256 bool ping_in_flight_ = false; 1257 1258 // Triggers periodic connection status checks. 1259 base::OneShotTimer heartbeat_timer_; 1260 1261 // Period used by the connection status monitoring mechanism. 1262 base::TimeDelta heartbeat_interval_; 1263 1264 // True if the connection status should be checked once the radio wakes up. 1265 bool check_connection_on_radio_wakeup_ = false; 1266 1267 // This is the next ping_id (unique_id) to be sent in PING frame. 1268 spdy::SpdyPingId next_ping_id_ = 1; 1269 1270 // This is the last time we have sent a PING. 1271 base::TimeTicks last_ping_sent_time_; 1272 1273 // This is the last time we had read activity in the session. 1274 base::TimeTicks last_read_time_; 1275 1276 // This is the length of the last compressed frame. 1277 size_t last_compressed_frame_len_ = 0; 1278 1279 // True if there is a CheckPingStatus() task posted on the message loop. 1280 bool check_ping_status_pending_ = false; 1281 1282 // Current send window size. Zero unless session flow control is turned on. 1283 int32_t session_send_window_size_ = 0; 1284 1285 // Maximum receive window size. Each time a WINDOW_UPDATE is sent, it 1286 // restores the receive window size to this value. Zero unless session flow 1287 // control is turned on. 1288 int32_t session_max_recv_window_size_; 1289 1290 // Maximum number of capped frames that can be queued at any time. 1291 // Every time we try to enqueue a capped frame, we check that there aren't 1292 // more than this amount already queued, and close the connection if so. 1293 int session_max_queued_capped_frames_; 1294 1295 // Number of active requests which asked for connection status monitoring. 1296 int broken_connection_detection_requests_ = 0; 1297 1298 // Sum of |session_unacked_recv_window_bytes_| and current receive window 1299 // size. Zero unless session flow control is turned on. 1300 // TODO(bnc): Rename or change semantics so that |window_size_| is actual 1301 // window size. 1302 int32_t session_recv_window_size_ = 0; 1303 1304 // When bytes are consumed, SpdyIOBuffer destructor calls back to SpdySession, 1305 // and this member keeps count of them until the corresponding WINDOW_UPDATEs 1306 // are sent. Zero unless session flow control is turned on. 1307 int32_t session_unacked_recv_window_bytes_ = 0; 1308 1309 // Time of the last WINDOW_UPDATE for the receive window. 1310 base::TimeTicks last_recv_window_update_; 1311 1312 // Time to accumilate small receive window updates for. 1313 base::TimeDelta time_to_buffer_small_window_updates_; 1314 1315 // Initial send window size for this session's streams. Can be 1316 // changed by an arriving SETTINGS frame. Newly created streams use 1317 // this value for the initial send window size. 1318 int32_t stream_initial_send_window_size_; 1319 1320 // The maximum HPACK dynamic table size the server is allowed to set. 1321 uint32_t max_header_table_size_; 1322 1323 // Initial receive window size for this session's streams. There are 1324 // plans to add a command line switch that would cause a SETTINGS 1325 // frame with window size announcement to be sent on startup. Newly 1326 // created streams will use this value for the initial receive 1327 // window size. 1328 int32_t stream_max_recv_window_size_; 1329 1330 // A queue of stream IDs that have been send-stalled at some point 1331 // in the past. 1332 base::circular_deque<spdy::SpdyStreamId> 1333 stream_send_unstall_queue_[NUM_PRIORITIES]; 1334 1335 NetLogWithSource net_log_; 1336 1337 // Versions of QUIC which may be used. 1338 const quic::ParsedQuicVersionVector quic_supported_versions_; 1339 1340 // Outside of tests, these should always be true. 1341 const bool enable_sending_initial_data_; 1342 const bool enable_ping_based_connection_checking_; 1343 1344 const bool is_http2_enabled_; 1345 const bool is_quic_enabled_; 1346 1347 // TODO(https://crbug.com/1426477): Remove. 1348 // If true, accept pushed streams from server. 1349 // If false, reset pushed streams immediately. 1350 const bool enable_push_; 1351 1352 // True if the server has advertised WebSocket support via 1353 // spdy::SETTINGS_ENABLE_CONNECT_PROTOCOL, see 1354 // https://tools.ietf.org/html/draft-ietf-httpbis-h2-websockets-00. 1355 bool support_websocket_ = false; 1356 1357 // |connection_at_risk_of_loss_time_| is an optimization to avoid sending 1358 // wasteful preface pings (when we just got some data). 1359 // 1360 // If it is zero (the most conservative figure), then we always send the 1361 // preface ping (when none are in flight). 1362 // 1363 // It is common for TCP/IP sessions to time out in about 3-5 minutes. 1364 // Certainly if it has been more than 3 minutes, we do want to send a preface 1365 // ping. 1366 // 1367 // We don't think any connection will time out in under about 10 seconds. So 1368 // this might as well be set to something conservative like 10 seconds. Later, 1369 // we could adjust it to send fewer pings perhaps. 1370 base::TimeDelta connection_at_risk_of_loss_time_; 1371 1372 // The amount of time that we are willing to tolerate with no activity (of any 1373 // form), while there is a ping in flight, before we declare the connection to 1374 // be hung. TODO(rtenneti): When hung, instead of resetting connection, race 1375 // to build a new connection, and see if that completes before we (finally) 1376 // get a PING response (http://crbug.com/127812). 1377 base::TimeDelta hung_interval_; 1378 1379 TimeFunc time_func_; 1380 1381 Http2PriorityDependencies priority_dependency_state_; 1382 1383 // Map of origin to Accept-CH header field values received via ALPS. 1384 base::flat_map<url::SchemeHostPort, std::string> 1385 accept_ch_entries_received_via_alps_; 1386 1387 // Network quality estimator to which the ping RTTs should be reported. May be 1388 // nullptr. 1389 raw_ptr<NetworkQualityEstimator> network_quality_estimator_; 1390 1391 // Used for accessing the SpdySession from asynchronous tasks. An asynchronous 1392 // must check if its WeakPtr<SpdySession> is valid before accessing it, to 1393 // correctly handle the case where it became unavailable and was deleted. 1394 base::WeakPtrFactory<SpdySession> weak_factory_{this}; 1395 }; 1396 1397 } // namespace net 1398 1399 #endif // NET_SPDY_SPDY_SESSION_H_ 1400