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