| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #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 <deque> | |
| 12 #include <map> | |
| 13 #include <memory> | |
| 14 #include <set> | |
| 15 #include <vector> | |
| 16 | |
| 17 #include "base/gtest_prod_util.h" | |
| 18 #include "base/macros.h" | |
| 19 #include "base/memory/ref_counted.h" | |
| 20 #include "base/memory/weak_ptr.h" | |
| 21 #include "base/time/time.h" | |
| 22 #include "net/base/host_port_pair.h" | |
| 23 #include "net/base/io_buffer.h" | |
| 24 #include "net/base/load_states.h" | |
| 25 #include "net/base/net_errors.h" | |
| 26 #include "net/base/net_export.h" | |
| 27 #include "net/base/request_priority.h" | |
| 28 #include "net/log/net_log_source.h" | |
| 29 #include "net/socket/client_socket_handle.h" | |
| 30 #include "net/socket/client_socket_pool.h" | |
| 31 #include "net/socket/next_proto.h" | |
| 32 #include "net/socket/ssl_client_socket.h" | |
| 33 #include "net/socket/stream_socket.h" | |
| 34 #include "net/spdy/buffered_spdy_framer.h" | |
| 35 #include "net/spdy/http2_priority_dependencies.h" | |
| 36 #include "net/spdy/multiplexed_session.h" | |
| 37 #include "net/spdy/platform/api/spdy_string.h" | |
| 38 #include "net/spdy/platform/api/spdy_string_piece.h" | |
| 39 #include "net/spdy/server_push_delegate.h" | |
| 40 #include "net/spdy/spdy_alt_svc_wire_format.h" | |
| 41 #include "net/spdy/spdy_buffer.h" | |
| 42 #include "net/spdy/spdy_framer.h" | |
| 43 #include "net/spdy/spdy_header_block.h" | |
| 44 #include "net/spdy/spdy_protocol.h" | |
| 45 #include "net/spdy/spdy_session_pool.h" | |
| 46 #include "net/spdy/spdy_stream.h" | |
| 47 #include "net/spdy/spdy_write_queue.h" | |
| 48 #include "net/ssl/ssl_config_service.h" | |
| 49 #include "url/gurl.h" | |
| 50 #include "url/scheme_host_port.h" | |
| 51 | |
| 52 namespace net { | |
| 53 | |
| 54 namespace test { | |
| 55 class SpdyStreamTest; | |
| 56 } | |
| 57 | |
| 58 // This is somewhat arbitrary and not really fixed, but it will always work | |
| 59 // reasonably with ethernet. Chop the world into 2-packet chunks. This is | |
| 60 // somewhat arbitrary, but is reasonably small and ensures that we elicit | |
| 61 // ACKs quickly from TCP (because TCP tries to only ACK every other packet). | |
| 62 const int kMss = 1430; | |
| 63 // The 8 is the size of the SPDY frame header. | |
| 64 const int kMaxSpdyFrameChunkSize = (2 * kMss) - 8; | |
| 65 | |
| 66 // Default value of SETTINGS_INITIAL_WINDOW_SIZE per protocol specification. | |
| 67 // A session is always created with this initial window size. | |
| 68 const int32_t kDefaultInitialWindowSize = 65535; | |
| 69 | |
| 70 // Maximum number of concurrent streams we will create, unless the server | |
| 71 // sends a SETTINGS frame with a different value. | |
| 72 const size_t kInitialMaxConcurrentStreams = 100; | |
| 73 | |
| 74 // If more than this many bytes have been read or more than that many | |
| 75 // milliseconds have passed, return ERR_IO_PENDING from ReadLoop. | |
| 76 const int kYieldAfterBytesRead = 32 * 1024; | |
| 77 const int kYieldAfterDurationMilliseconds = 20; | |
| 78 | |
| 79 // First and last valid stream IDs. As we always act as the client, | |
| 80 // start at 1 for the first stream id. | |
| 81 const SpdyStreamId kFirstStreamId = 1; | |
| 82 const SpdyStreamId kLastStreamId = 0x7fffffff; | |
| 83 | |
| 84 struct LoadTimingInfo; | |
| 85 class NetLog; | |
| 86 class NetLogWithSource; | |
| 87 class ProxyDelegate; | |
| 88 class SpdyStream; | |
| 89 class SSLInfo; | |
| 90 class TransportSecurityState; | |
| 91 | |
| 92 // NOTE: There's an enum of the same name (also with numeric suffixes) | |
| 93 // in histograms.xml. Be sure to add new values there also. | |
| 94 enum SpdyProtocolErrorDetails { | |
| 95 // SpdyFramer::SpdyFramerError mappings. | |
| 96 SPDY_ERROR_NO_ERROR = 0, | |
| 97 SPDY_ERROR_INVALID_STREAM_ID = 38, | |
| 98 SPDY_ERROR_INVALID_CONTROL_FRAME = 1, | |
| 99 SPDY_ERROR_CONTROL_PAYLOAD_TOO_LARGE = 2, | |
| 100 SPDY_ERROR_ZLIB_INIT_FAILURE = 3, | |
| 101 SPDY_ERROR_UNSUPPORTED_VERSION = 4, | |
| 102 SPDY_ERROR_DECOMPRESS_FAILURE = 5, | |
| 103 SPDY_ERROR_COMPRESS_FAILURE = 6, | |
| 104 SPDY_ERROR_GOAWAY_FRAME_CORRUPT = 29, | |
| 105 SPDY_ERROR_RST_STREAM_FRAME_CORRUPT = 30, | |
| 106 SPDY_ERROR_INVALID_PADDING = 39, | |
| 107 SPDY_ERROR_INVALID_DATA_FRAME_FLAGS = 8, | |
| 108 SPDY_ERROR_INVALID_CONTROL_FRAME_FLAGS = 9, | |
| 109 SPDY_ERROR_UNEXPECTED_FRAME = 31, | |
| 110 SPDY_ERROR_INTERNAL_FRAMER_ERROR = 41, | |
| 111 SPDY_ERROR_INVALID_CONTROL_FRAME_SIZE = 37, | |
| 112 SPDY_ERROR_OVERSIZED_PAYLOAD = 40, | |
| 113 // SpdyErrorCode mappings. | |
| 114 STATUS_CODE_NO_ERROR = 41, | |
| 115 STATUS_CODE_PROTOCOL_ERROR = 11, | |
| 116 STATUS_CODE_INTERNAL_ERROR = 16, | |
| 117 STATUS_CODE_FLOW_CONTROL_ERROR = 17, | |
| 118 STATUS_CODE_SETTINGS_TIMEOUT = 32, | |
| 119 STATUS_CODE_STREAM_CLOSED = 12, | |
| 120 STATUS_CODE_FRAME_SIZE_ERROR = 21, | |
| 121 STATUS_CODE_REFUSED_STREAM = 13, | |
| 122 STATUS_CODE_CANCEL = 15, | |
| 123 STATUS_CODE_COMPRESSION_ERROR = 42, | |
| 124 STATUS_CODE_CONNECT_ERROR = 33, | |
| 125 STATUS_CODE_ENHANCE_YOUR_CALM = 34, | |
| 126 STATUS_CODE_INADEQUATE_SECURITY = 35, | |
| 127 STATUS_CODE_HTTP_1_1_REQUIRED = 36, | |
| 128 // Deprecated SpdyRstStrreamStatus mappings. | |
| 129 STATUS_CODE_UNSUPPORTED_VERSION = 14, | |
| 130 STATUS_CODE_STREAM_IN_USE = 18, | |
| 131 STATUS_CODE_STREAM_ALREADY_CLOSED = 19, | |
| 132 | |
| 133 // SpdySession errors | |
| 134 PROTOCOL_ERROR_UNEXPECTED_PING = 22, | |
| 135 PROTOCOL_ERROR_RST_STREAM_FOR_NON_ACTIVE_STREAM = 23, | |
| 136 PROTOCOL_ERROR_SPDY_COMPRESSION_FAILURE = 24, | |
| 137 PROTOCOL_ERROR_REQUEST_FOR_SECURE_CONTENT_OVER_INSECURE_SESSION = 25, | |
| 138 PROTOCOL_ERROR_SYN_REPLY_NOT_RECEIVED = 26, | |
| 139 PROTOCOL_ERROR_INVALID_WINDOW_UPDATE_SIZE = 27, | |
| 140 PROTOCOL_ERROR_RECEIVE_WINDOW_VIOLATION = 28, | |
| 141 | |
| 142 // Next free value. | |
| 143 NUM_SPDY_PROTOCOL_ERROR_DETAILS = 43, | |
| 144 }; | |
| 145 SpdyProtocolErrorDetails NET_EXPORT_PRIVATE | |
| 146 MapFramerErrorToProtocolError(SpdyFramer::SpdyFramerError error); | |
| 147 Error NET_EXPORT_PRIVATE | |
| 148 MapFramerErrorToNetError(SpdyFramer::SpdyFramerError error); | |
| 149 SpdyProtocolErrorDetails NET_EXPORT_PRIVATE | |
| 150 MapRstStreamStatusToProtocolError(SpdyErrorCode error_code); | |
| 151 SpdyErrorCode NET_EXPORT_PRIVATE MapNetErrorToGoAwayStatus(Error err); | |
| 152 | |
| 153 // If these compile asserts fail then SpdyProtocolErrorDetails needs | |
| 154 // to be updated with new values, as do the mapping functions above. | |
| 155 static_assert(17 == SpdyFramer::LAST_ERROR, | |
| 156 "SpdyProtocolErrorDetails / Spdy Errors mismatch"); | |
| 157 static_assert(13 == SpdyErrorCode::ERROR_CODE_MAX, | |
| 158 "SpdyProtocolErrorDetails / SpdyErrorCode mismatch"); | |
| 159 | |
| 160 // A helper class used to manage a request to create a stream. | |
| 161 class NET_EXPORT_PRIVATE SpdyStreamRequest { | |
| 162 public: | |
| 163 SpdyStreamRequest(); | |
| 164 // Calls CancelRequest(). | |
| 165 ~SpdyStreamRequest(); | |
| 166 | |
| 167 // Starts the request to create a stream. If OK is returned, then | |
| 168 // ReleaseStream() may be called. If ERR_IO_PENDING is returned, | |
| 169 // then when the stream is created, |callback| will be called, at | |
| 170 // which point ReleaseStream() may be called. Otherwise, the stream | |
| 171 // is not created, an error is returned, and ReleaseStream() may not | |
| 172 // be called. | |
| 173 // | |
| 174 // If OK is returned, must not be called again without | |
| 175 // ReleaseStream() being called first. If ERR_IO_PENDING is | |
| 176 // returned, must not be called again without CancelRequest() or | |
| 177 // ReleaseStream() being called first. Otherwise, in case of an | |
| 178 // immediate error, this may be called again. | |
| 179 int StartRequest(SpdyStreamType type, | |
| 180 const base::WeakPtr<SpdySession>& session, | |
| 181 const GURL& url, | |
| 182 RequestPriority priority, | |
| 183 const NetLogWithSource& net_log, | |
| 184 const CompletionCallback& callback); | |
| 185 | |
| 186 // Cancels any pending stream creation request. May be called | |
| 187 // repeatedly. | |
| 188 void CancelRequest(); | |
| 189 | |
| 190 // Transfers the created stream (guaranteed to not be NULL) to the | |
| 191 // caller. Must be called at most once after StartRequest() returns | |
| 192 // OK or |callback| is called with OK. The caller must immediately | |
| 193 // set a delegate for the returned stream (except for test code). | |
| 194 base::WeakPtr<SpdyStream> ReleaseStream(); | |
| 195 | |
| 196 // Returns the estimate of dynamically allocated memory in bytes. | |
| 197 size_t EstimateMemoryUsage() const; | |
| 198 | |
| 199 private: | |
| 200 friend class SpdySession; | |
| 201 | |
| 202 // Called by |session_| when the stream attempt has finished | |
| 203 // successfully. | |
| 204 void OnRequestCompleteSuccess(const base::WeakPtr<SpdyStream>& stream); | |
| 205 | |
| 206 // Called by |session_| when the stream attempt has finished with an | |
| 207 // error. Also called with ERR_ABORTED if |session_| is destroyed | |
| 208 // while the stream attempt is still pending. | |
| 209 void OnRequestCompleteFailure(int rv); | |
| 210 | |
| 211 // Accessors called by |session_|. | |
| 212 SpdyStreamType type() const { return type_; } | |
| 213 const GURL& url() const { return url_; } | |
| 214 RequestPriority priority() const { return priority_; } | |
| 215 const NetLogWithSource& net_log() const { return net_log_; } | |
| 216 | |
| 217 void Reset(); | |
| 218 | |
| 219 SpdyStreamType type_; | |
| 220 base::WeakPtr<SpdySession> session_; | |
| 221 base::WeakPtr<SpdyStream> stream_; | |
| 222 GURL url_; | |
| 223 RequestPriority priority_; | |
| 224 NetLogWithSource net_log_; | |
| 225 CompletionCallback callback_; | |
| 226 | |
| 227 base::WeakPtrFactory<SpdyStreamRequest> weak_ptr_factory_; | |
| 228 | |
| 229 DISALLOW_COPY_AND_ASSIGN(SpdyStreamRequest); | |
| 230 }; | |
| 231 | |
| 232 class NET_EXPORT SpdySession : public BufferedSpdyFramerVisitorInterface, | |
| 233 public SpdyFramerDebugVisitorInterface, | |
| 234 public MultiplexedSession, | |
| 235 public HigherLayeredPool { | |
| 236 public: | |
| 237 // TODO(akalin): Use base::TickClock when it becomes available. | |
| 238 typedef base::TimeTicks (*TimeFunc)(void); | |
| 239 | |
| 240 // Container class for unclaimed pushed streams on a SpdySession. Guarantees | |
| 241 // that |spdy_session_.pool_| gets notified every time a stream is pushed or | |
| 242 // an unclaimed pushed stream is claimed. | |
| 243 class UnclaimedPushedStreamContainer { | |
| 244 public: | |
| 245 struct PushedStreamInfo { | |
| 246 PushedStreamInfo() : stream_id(0) {} | |
| 247 PushedStreamInfo(SpdyStreamId stream_id, base::TimeTicks creation_time) | |
| 248 : stream_id(stream_id), creation_time(creation_time) {} | |
| 249 ~PushedStreamInfo() {} | |
| 250 size_t EstimateMemoryUsage() const { return 0; } | |
| 251 | |
| 252 SpdyStreamId stream_id; | |
| 253 base::TimeTicks creation_time; | |
| 254 }; | |
| 255 using PushedStreamMap = std::map<GURL, PushedStreamInfo>; | |
| 256 using iterator = PushedStreamMap::iterator; | |
| 257 using const_iterator = PushedStreamMap::const_iterator; | |
| 258 | |
| 259 UnclaimedPushedStreamContainer() = delete; | |
| 260 explicit UnclaimedPushedStreamContainer(SpdySession* spdy_session); | |
| 261 ~UnclaimedPushedStreamContainer(); | |
| 262 | |
| 263 bool empty() const { return streams_.empty(); } | |
| 264 size_t size() const { return streams_.size(); } | |
| 265 const_iterator begin() const { return streams_.begin(); } | |
| 266 const_iterator end() const { return streams_.end(); } | |
| 267 const_iterator find(const GURL& url) const { return streams_.find(url); } | |
| 268 size_t count(const GURL& url) const { return streams_.count(url); } | |
| 269 const_iterator lower_bound(const GURL& url) const { | |
| 270 return streams_.lower_bound(url); | |
| 271 } | |
| 272 | |
| 273 size_t erase(const GURL& url); | |
| 274 iterator erase(const_iterator it); | |
| 275 iterator insert(const_iterator position, | |
| 276 const GURL& url, | |
| 277 SpdyStreamId stream_id, | |
| 278 const base::TimeTicks& creation_time); | |
| 279 | |
| 280 size_t EstimateMemoryUsage() const; | |
| 281 | |
| 282 private: | |
| 283 SpdySession* spdy_session_; | |
| 284 | |
| 285 // (Bijective) map from the URL to the ID of the streams that have | |
| 286 // already started to be pushed by the server, but do not have | |
| 287 // consumers yet. Contains a subset of |active_streams_|. | |
| 288 PushedStreamMap streams_; | |
| 289 }; | |
| 290 | |
| 291 // Returns true if |new_hostname| can be pooled into an existing connection to | |
| 292 // |old_hostname| associated with |ssl_info|. | |
| 293 static bool CanPool(TransportSecurityState* transport_security_state, | |
| 294 const SSLInfo& ssl_info, | |
| 295 const SpdyString& old_hostname, | |
| 296 const SpdyString& new_hostname); | |
| 297 | |
| 298 // Create a new SpdySession. | |
| 299 // |spdy_session_key| is the host/port that this session connects to, privacy | |
| 300 // and proxy configuration settings that it's using. | |
| 301 // |session| is the HttpNetworkSession. |net_log| is the NetLog that we log | |
| 302 // network events to. | |
| 303 SpdySession(const SpdySessionKey& spdy_session_key, | |
| 304 HttpServerProperties* http_server_properties, | |
| 305 TransportSecurityState* transport_security_state, | |
| 306 bool enable_sending_initial_data, | |
| 307 bool enable_ping_based_connection_checking, | |
| 308 size_t session_max_recv_window_size, | |
| 309 const SettingsMap& initial_settings, | |
| 310 TimeFunc time_func, | |
| 311 ServerPushDelegate* push_delegate, | |
| 312 ProxyDelegate* proxy_delegate, | |
| 313 NetLog* net_log); | |
| 314 | |
| 315 ~SpdySession() override; | |
| 316 | |
| 317 const HostPortPair& host_port_pair() const { | |
| 318 return spdy_session_key_.host_port_proxy_pair().first; | |
| 319 } | |
| 320 const HostPortProxyPair& host_port_proxy_pair() const { | |
| 321 return spdy_session_key_.host_port_proxy_pair(); | |
| 322 } | |
| 323 const SpdySessionKey& spdy_session_key() const { | |
| 324 return spdy_session_key_; | |
| 325 } | |
| 326 // Get a pushed stream for a given |url|. If the server initiates a | |
| 327 // stream, it might already exist for a given path. The server | |
| 328 // might also not have initiated the stream yet, but indicated it | |
| 329 // will via X-Associated-Content. Returns OK if a stream was found | |
| 330 // and put into |spdy_stream|, or if one was not found but it is | |
| 331 // okay to create a new stream (in which case |spdy_stream| is | |
| 332 // reset). Returns an error (not ERR_IO_PENDING) otherwise, and | |
| 333 // resets |spdy_stream|. | |
| 334 // | |
| 335 // If a stream was found and the stream is still open, the priority | |
| 336 // of that stream is updated to match |priority|. | |
| 337 int GetPushStream(const GURL& url, | |
| 338 RequestPriority priority, | |
| 339 SpdyStream** spdy_stream, | |
| 340 const NetLogWithSource& stream_net_log); | |
| 341 | |
| 342 // Called when the pushed stream should be cancelled. If the pushed stream is | |
| 343 // not claimed and active, sends RST to the server to cancel the stream. | |
| 344 void CancelPush(const GURL& url); | |
| 345 | |
| 346 // Initialize the session with the given connection. |is_secure| | |
| 347 // must indicate whether |connection| uses an SSL socket or not; it | |
| 348 // is usually true, but it can be false for testing or when SPDY is | |
| 349 // configured to work with non-secure sockets. | |
| 350 // | |
| 351 // |pool| is the SpdySessionPool that owns us. Its lifetime must | |
| 352 // strictly be greater than |this|. | |
| 353 // | |
| 354 // The session begins reading from |connection| on a subsequent event loop | |
| 355 // iteration, so the SpdySession may close immediately afterwards if the first | |
| 356 // read of |connection| fails. | |
| 357 void InitializeWithSocket(std::unique_ptr<ClientSocketHandle> connection, | |
| 358 SpdySessionPool* pool, | |
| 359 bool is_secure); | |
| 360 | |
| 361 // Check to see if this SPDY session can support an additional domain. | |
| 362 // If the session is un-authenticated, then this call always returns true. | |
| 363 // For SSL-based sessions, verifies that the server certificate in use by | |
| 364 // this session provides authentication for the domain and no client | |
| 365 // certificate or channel ID was sent to the original server during the SSL | |
| 366 // handshake. NOTE: This function can have false negatives on some | |
| 367 // platforms. | |
| 368 // TODO(wtc): rename this function and the Net.SpdyIPPoolDomainMatch | |
| 369 // histogram because this function does more than verifying domain | |
| 370 // authentication now. | |
| 371 bool VerifyDomainAuthentication(const SpdyString& domain); | |
| 372 | |
| 373 // Pushes the given producer into the write queue for | |
| 374 // |stream|. |stream| is guaranteed to be activated before the | |
| 375 // producer is used to produce its frame. | |
| 376 void EnqueueStreamWrite(const base::WeakPtr<SpdyStream>& stream, | |
| 377 SpdyFrameType frame_type, | |
| 378 std::unique_ptr<SpdyBufferProducer> producer); | |
| 379 | |
| 380 // Creates and returns a HEADERS frame for |stream_id|. | |
| 381 std::unique_ptr<SpdySerializedFrame> CreateHeaders( | |
| 382 SpdyStreamId stream_id, | |
| 383 RequestPriority priority, | |
| 384 SpdyControlFlags flags, | |
| 385 SpdyHeaderBlock headers, | |
| 386 NetLogSource source_dependency); | |
| 387 | |
| 388 // Creates and returns a SpdyBuffer holding a data frame with the | |
| 389 // given data. May return NULL if stalled by flow control. | |
| 390 std::unique_ptr<SpdyBuffer> CreateDataBuffer(SpdyStreamId stream_id, | |
| 391 IOBuffer* data, | |
| 392 int len, | |
| 393 SpdyDataFlags flags); | |
| 394 | |
| 395 // Close the stream with the given ID, which must exist and be | |
| 396 // active. Note that that stream may hold the last reference to the | |
| 397 // session. | |
| 398 void CloseActiveStream(SpdyStreamId stream_id, int status); | |
| 399 | |
| 400 // Close the given created stream, which must exist but not yet be | |
| 401 // active. Note that |stream| may hold the last reference to the | |
| 402 // session. | |
| 403 void CloseCreatedStream(const base::WeakPtr<SpdyStream>& stream, int status); | |
| 404 | |
| 405 // Send a RST_STREAM frame with the given status code and close the | |
| 406 // stream with the given ID, which must exist and be active. Note | |
| 407 // that that stream may hold the last reference to the session. | |
| 408 void ResetStream(SpdyStreamId stream_id, | |
| 409 SpdyErrorCode error_code, | |
| 410 const SpdyString& description); | |
| 411 | |
| 412 // Check if a stream is active. | |
| 413 bool IsStreamActive(SpdyStreamId stream_id) const; | |
| 414 | |
| 415 // The LoadState is used for informing the user of the current network | |
| 416 // status, such as "resolving host", "connecting", etc. | |
| 417 LoadState GetLoadState() const; | |
| 418 | |
| 419 // Returns server infomation in the form of (scheme/host/port). | |
| 420 url::SchemeHostPort GetServer(); | |
| 421 | |
| 422 // MultiplexedSession methods: | |
| 423 bool GetRemoteEndpoint(IPEndPoint* endpoint) override; | |
| 424 bool GetSSLInfo(SSLInfo* ssl_info) const override; | |
| 425 Error GetTokenBindingSignature(crypto::ECPrivateKey* key, | |
| 426 TokenBindingType tb_type, | |
| 427 std::vector<uint8_t>* out) override; | |
| 428 | |
| 429 // Returns true if ALPN was negotiated for the underlying socket. | |
| 430 bool WasAlpnNegotiated() const; | |
| 431 | |
| 432 // Returns the protocol negotiated via ALPN for the underlying socket. | |
| 433 NextProto GetNegotiatedProtocol() const; | |
| 434 | |
| 435 // Send a WINDOW_UPDATE frame for a stream. Called by a stream | |
| 436 // whenever receive window size is increased. | |
| 437 void SendStreamWindowUpdate(SpdyStreamId stream_id, | |
| 438 uint32_t delta_window_size); | |
| 439 | |
| 440 // Accessors for the session's availability state. | |
| 441 bool IsAvailable() const { return availability_state_ == STATE_AVAILABLE; } | |
| 442 bool IsGoingAway() const { return availability_state_ == STATE_GOING_AWAY; } | |
| 443 bool IsDraining() const { return availability_state_ == STATE_DRAINING; } | |
| 444 | |
| 445 // Closes this session. This will close all active streams and mark | |
| 446 // the session as permanently closed. Callers must assume that the | |
| 447 // session is destroyed after this is called. (However, it may not | |
| 448 // be destroyed right away, e.g. when a SpdySession function is | |
| 449 // present in the call stack.) | |
| 450 // | |
| 451 // |err| should be < ERR_IO_PENDING; this function is intended to be | |
| 452 // called on error. | |
| 453 // |description| indicates the reason for the error. | |
| 454 void CloseSessionOnError(Error err, const SpdyString& description); | |
| 455 | |
| 456 // Mark this session as unavailable, meaning that it will not be used to | |
| 457 // service new streams. Unlike when a GOAWAY frame is received, this function | |
| 458 // will not close any streams. | |
| 459 void MakeUnavailable(); | |
| 460 | |
| 461 // Closes all active streams with stream id's greater than | |
| 462 // |last_good_stream_id|, as well as any created or pending | |
| 463 // streams. Must be called only when |availability_state_| >= | |
| 464 // STATE_GOING_AWAY. After this function, DcheckGoingAway() will | |
| 465 // pass. May be called multiple times. | |
| 466 void StartGoingAway(SpdyStreamId last_good_stream_id, Error status); | |
| 467 | |
| 468 // Must be called only when going away (i.e., DcheckGoingAway() | |
| 469 // passes). If there are no more active streams and the session | |
| 470 // isn't closed yet, close it. | |
| 471 void MaybeFinishGoingAway(); | |
| 472 | |
| 473 // Retrieves information on the current state of the SPDY session as a | |
| 474 // Value. | |
| 475 std::unique_ptr<base::Value> GetInfoAsValue() const; | |
| 476 | |
| 477 // Indicates whether the session is being reused after having successfully | |
| 478 // used to send/receive data in the past or if the underlying socket was idle | |
| 479 // before being used for a SPDY session. | |
| 480 bool IsReused() const; | |
| 481 | |
| 482 // Returns true if the underlying transport socket ever had any reads or | |
| 483 // writes. | |
| 484 bool WasEverUsed() const { | |
| 485 return connection_->socket()->WasEverUsed(); | |
| 486 } | |
| 487 | |
| 488 // Returns the load timing information from the perspective of the given | |
| 489 // stream. If it's not the first stream, the connection is considered reused | |
| 490 // for that stream. | |
| 491 // | |
| 492 // This uses a different notion of reuse than IsReused(). This function | |
| 493 // sets |socket_reused| to false only if |stream_id| is the ID of the first | |
| 494 // stream using the session. IsReused(), on the other hand, indicates if the | |
| 495 // session has been used to send/receive data at all. | |
| 496 bool GetLoadTimingInfo(SpdyStreamId stream_id, | |
| 497 LoadTimingInfo* load_timing_info) const; | |
| 498 | |
| 499 // Returns true if session is not currently active | |
| 500 bool is_active() const { | |
| 501 return !active_streams_.empty() || !created_streams_.empty(); | |
| 502 } | |
| 503 | |
| 504 // Access to the number of active and pending streams. These are primarily | |
| 505 // available for testing and diagnostics. | |
| 506 size_t num_active_streams() const { return active_streams_.size(); } | |
| 507 size_t num_unclaimed_pushed_streams() const; | |
| 508 size_t num_created_streams() const { return created_streams_.size(); } | |
| 509 size_t count_unclaimed_pushed_streams_for_url(const GURL& url) const; | |
| 510 | |
| 511 size_t num_pushed_streams() const { return num_pushed_streams_; } | |
| 512 size_t num_active_pushed_streams() const { | |
| 513 return num_active_pushed_streams_; | |
| 514 } | |
| 515 | |
| 516 size_t pending_create_stream_queue_size(RequestPriority priority) const { | |
| 517 DCHECK_GE(priority, MINIMUM_PRIORITY); | |
| 518 DCHECK_LE(priority, MAXIMUM_PRIORITY); | |
| 519 return pending_create_stream_queues_[priority].size(); | |
| 520 } | |
| 521 | |
| 522 // Returns the current |stream_initial_send_window_size_|. | |
| 523 int32_t stream_initial_send_window_size() const { | |
| 524 return stream_initial_send_window_size_; | |
| 525 } | |
| 526 | |
| 527 // Returns true if no stream in the session can send data due to | |
| 528 // session flow control. | |
| 529 bool IsSendStalled() const { return session_send_window_size_ == 0; } | |
| 530 | |
| 531 const NetLogWithSource& net_log() const { return net_log_; } | |
| 532 | |
| 533 int GetPeerAddress(IPEndPoint* address) const; | |
| 534 int GetLocalAddress(IPEndPoint* address) const; | |
| 535 | |
| 536 // Adds |alias| to set of aliases associated with this session. | |
| 537 void AddPooledAlias(const SpdySessionKey& alias_key); | |
| 538 | |
| 539 // Removes |alias| from set of aliases associated with this session. | |
| 540 void RemovePooledAlias(const SpdySessionKey& alias_key); | |
| 541 | |
| 542 // Returns the set of aliases associated with this session. | |
| 543 const std::set<SpdySessionKey>& pooled_aliases() const { | |
| 544 return pooled_aliases_; | |
| 545 } | |
| 546 | |
| 547 size_t GetDataFrameMinimumSize() const { | |
| 548 return buffered_spdy_framer_->GetDataFrameMinimumSize(); | |
| 549 } | |
| 550 | |
| 551 size_t GetFrameHeaderSize() const { | |
| 552 return buffered_spdy_framer_->GetFrameHeaderSize(); | |
| 553 } | |
| 554 | |
| 555 size_t GetFrameMinimumSize() const { | |
| 556 return buffered_spdy_framer_->GetFrameMinimumSize(); | |
| 557 } | |
| 558 | |
| 559 size_t GetFrameMaximumSize() const { | |
| 560 return buffered_spdy_framer_->GetFrameMaximumSize(); | |
| 561 } | |
| 562 | |
| 563 size_t GetDataFrameMaximumPayload() const { | |
| 564 return buffered_spdy_framer_->GetDataFrameMaximumPayload(); | |
| 565 } | |
| 566 | |
| 567 // https://http2.github.io/http2-spec/#TLSUsage mandates minimum security | |
| 568 // standards for TLS. | |
| 569 bool HasAcceptableTransportSecurity() const; | |
| 570 | |
| 571 // Must be used only by |pool_|. | |
| 572 base::WeakPtr<SpdySession> GetWeakPtr(); | |
| 573 | |
| 574 // HigherLayeredPool implementation: | |
| 575 bool CloseOneIdleConnection() override; | |
| 576 | |
| 577 // Dumps memory allocation stats to |stats|. Sets |*is_session_active| to | |
| 578 // indicate whether session is active. | |
| 579 // |stats| can be assumed as being default initialized upon entry. | |
| 580 // Implementation overrides fields in |stats|. | |
| 581 // Returns the estimate of dynamically allocated memory in bytes, which | |
| 582 // includes the size attributed to the underlying socket. | |
| 583 size_t DumpMemoryStats(StreamSocket::SocketMemoryStats* stats, | |
| 584 bool* is_session_active) const; | |
| 585 | |
| 586 private: | |
| 587 friend class test::SpdyStreamTest; | |
| 588 friend class base::RefCounted<SpdySession>; | |
| 589 friend class HttpNetworkTransactionTest; | |
| 590 friend class HttpProxyClientSocketPoolTest; | |
| 591 friend class SpdyHttpStreamTest; | |
| 592 friend class SpdyNetworkTransactionTest; | |
| 593 friend class SpdyProxyClientSocketTest; | |
| 594 friend class SpdySessionTest; | |
| 595 friend class SpdyStreamRequest; | |
| 596 | |
| 597 // Allow tests to access our innards for testing purposes. | |
| 598 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, ClientPing); | |
| 599 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, FailedPing); | |
| 600 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, GetActivePushStream); | |
| 601 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, DeleteExpiredPushStreams); | |
| 602 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, MetricsCollectionOnPushStreams); | |
| 603 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, CancelPushBeforeClaimed); | |
| 604 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, CancelPushAfterSessionGoesAway); | |
| 605 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, CancelPushAfterExpired); | |
| 606 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, ProtocolNegotiation); | |
| 607 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, ClearSettings); | |
| 608 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, AdjustRecvWindowSize); | |
| 609 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, AdjustSendWindowSize); | |
| 610 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, SessionFlowControlInactiveStream); | |
| 611 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, SessionFlowControlPadding); | |
| 612 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, | |
| 613 SessionFlowControlTooMuchDataTwoDataFrames); | |
| 614 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, | |
| 615 StreamFlowControlTooMuchDataTwoDataFrames); | |
| 616 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, SessionFlowControlNoReceiveLeaks); | |
| 617 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, SessionFlowControlNoSendLeaks); | |
| 618 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, SessionFlowControlEndToEnd); | |
| 619 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, StreamIdSpaceExhausted); | |
| 620 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, MaxConcurrentStreamsZero); | |
| 621 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, UnstallRacesWithStreamCreation); | |
| 622 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, GoAwayOnSessionFlowControlError); | |
| 623 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, | |
| 624 RejectPushedStreamExceedingConcurrencyLimit); | |
| 625 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, IgnoreReservedRemoteStreamsCount); | |
| 626 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, | |
| 627 CancelReservedStreamOnHeadersReceived); | |
| 628 FRIEND_TEST_ALL_PREFIXES(SpdySessionTest, RejectInvalidUnknownFrames); | |
| 629 FRIEND_TEST_ALL_PREFIXES(SpdySessionPoolTest, IPAddressChanged); | |
| 630 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest, | |
| 631 ServerPushValidCrossOrigin); | |
| 632 FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest, | |
| 633 ServerPushValidCrossOriginWithOpenSession); | |
| 634 | |
| 635 typedef std::deque<base::WeakPtr<SpdyStreamRequest>> | |
| 636 PendingStreamRequestQueue; | |
| 637 typedef std::map<SpdyStreamId, SpdyStream*> ActiveStreamMap; | |
| 638 typedef std::set<SpdyStream*> CreatedStreamSet; | |
| 639 | |
| 640 enum AvailabilityState { | |
| 641 // The session is available in its socket pool and can be used | |
| 642 // freely. | |
| 643 STATE_AVAILABLE, | |
| 644 // The session can process data on existing streams but will | |
| 645 // refuse to create new ones. | |
| 646 STATE_GOING_AWAY, | |
| 647 // The session is draining its write queue in preparation of closing. | |
| 648 // Further writes will not be queued, and further reads will not be issued | |
| 649 // (though the remainder of a current read may be processed). The session | |
| 650 // will be destroyed by its write loop once the write queue is drained. | |
| 651 STATE_DRAINING, | |
| 652 }; | |
| 653 | |
| 654 enum ReadState { | |
| 655 READ_STATE_DO_READ, | |
| 656 READ_STATE_DO_READ_COMPLETE, | |
| 657 }; | |
| 658 | |
| 659 enum WriteState { | |
| 660 // There is no in-flight write and the write queue is empty. | |
| 661 WRITE_STATE_IDLE, | |
| 662 WRITE_STATE_DO_WRITE, | |
| 663 WRITE_STATE_DO_WRITE_COMPLETE, | |
| 664 }; | |
| 665 | |
| 666 // Called by SpdyStreamRequest to start a request to create a | |
| 667 // stream. If OK is returned, then |stream| will be filled in with a | |
| 668 // valid stream. If ERR_IO_PENDING is returned, then | |
| 669 // |request->OnRequestComplete{Success,Failure}()| will be called | |
| 670 // when the stream is created (unless it is cancelled). Otherwise, | |
| 671 // no stream is created and the error is returned. | |
| 672 int TryCreateStream(const base::WeakPtr<SpdyStreamRequest>& request, | |
| 673 base::WeakPtr<SpdyStream>* stream); | |
| 674 | |
| 675 // Actually create a stream into |stream|. Returns OK if successful; | |
| 676 // otherwise, returns an error and |stream| is not filled. | |
| 677 int CreateStream(const SpdyStreamRequest& request, | |
| 678 base::WeakPtr<SpdyStream>* stream); | |
| 679 | |
| 680 // Called by SpdyStreamRequest to remove |request| from the stream | |
| 681 // creation queue. | |
| 682 void CancelStreamRequest(const base::WeakPtr<SpdyStreamRequest>& request); | |
| 683 | |
| 684 // Returns the next pending stream request to process, or NULL if | |
| 685 // there is none. | |
| 686 base::WeakPtr<SpdyStreamRequest> GetNextPendingStreamRequest(); | |
| 687 | |
| 688 // Called when there is room to create more streams (e.g., a stream | |
| 689 // was closed). Processes as many pending stream requests as | |
| 690 // possible. | |
| 691 void ProcessPendingStreamRequests(); | |
| 692 | |
| 693 void TryCreatePushStream(SpdyStreamId stream_id, | |
| 694 SpdyStreamId associated_stream_id, | |
| 695 SpdyHeaderBlock headers); | |
| 696 | |
| 697 // Close the stream pointed to by the given iterator. Note that that | |
| 698 // stream may hold the last reference to the session. | |
| 699 void CloseActiveStreamIterator(ActiveStreamMap::iterator it, int status); | |
| 700 | |
| 701 // Close the stream pointed to by the given iterator. Note that that | |
| 702 // stream may hold the last reference to the session. | |
| 703 void CloseCreatedStreamIterator(CreatedStreamSet::iterator it, int status); | |
| 704 | |
| 705 // Calls EnqueueResetStreamFrame() and then | |
| 706 // CloseActiveStreamIterator(). | |
| 707 void ResetStreamIterator(ActiveStreamMap::iterator it, | |
| 708 SpdyErrorCode error_code, | |
| 709 const SpdyString& description); | |
| 710 | |
| 711 // Send a RST_STREAM frame with the given parameters. There should | |
| 712 // either be no active stream with the given ID, or that active | |
| 713 // stream should be closed shortly after this function is called. | |
| 714 void EnqueueResetStreamFrame(SpdyStreamId stream_id, | |
| 715 RequestPriority priority, | |
| 716 SpdyErrorCode error_code, | |
| 717 const SpdyString& description); | |
| 718 | |
| 719 // Send a PRIORITY frame with the given parameters. | |
| 720 void EnqueuePriorityFrame(SpdyStreamId stream_id, | |
| 721 SpdyStreamId dependency_id, | |
| 722 int weight, | |
| 723 bool exclusive); | |
| 724 | |
| 725 // Calls DoReadLoop. Use this function instead of DoReadLoop when | |
| 726 // posting a task to pump the read loop. | |
| 727 void PumpReadLoop(ReadState expected_read_state, int result); | |
| 728 | |
| 729 // Advance the ReadState state machine. |expected_read_state| is the | |
| 730 // expected starting read state. | |
| 731 // | |
| 732 // This function must always be called via PumpReadLoop(). | |
| 733 int DoReadLoop(ReadState expected_read_state, int result); | |
| 734 // The implementations of the states of the ReadState state machine. | |
| 735 int DoRead(); | |
| 736 int DoReadComplete(int result); | |
| 737 | |
| 738 // Calls DoWriteLoop. If |availability_state_| is STATE_DRAINING and no | |
| 739 // writes remain, the session is removed from the session pool and | |
| 740 // destroyed. | |
| 741 // | |
| 742 // Use this function instead of DoWriteLoop when posting a task to | |
| 743 // pump the write loop. | |
| 744 void PumpWriteLoop(WriteState expected_write_state, int result); | |
| 745 | |
| 746 // Iff the write loop is not currently active, posts a callback into | |
| 747 // PumpWriteLoop(). | |
| 748 void MaybePostWriteLoop(); | |
| 749 | |
| 750 // Advance the WriteState state machine. |expected_write_state| is | |
| 751 // the expected starting write state. | |
| 752 // | |
| 753 // This function must always be called via PumpWriteLoop(). | |
| 754 int DoWriteLoop(WriteState expected_write_state, int result); | |
| 755 // The implementations of the states of the WriteState state machine. | |
| 756 int DoWrite(); | |
| 757 int DoWriteComplete(int result); | |
| 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 // Helper method to send a SETTINGS frame. | |
| 768 void SendSettings(const SettingsMap& settings); | |
| 769 | |
| 770 // Handle SETTING. Either when we send settings, or when we receive a | |
| 771 // SETTINGS control frame, update our SpdySession accordingly. | |
| 772 void HandleSetting(uint32_t id, uint32_t value); | |
| 773 | |
| 774 // Adjust the send window size of all ActiveStreams and PendingStreamRequests. | |
| 775 void UpdateStreamsSendWindowSize(int32_t delta_window_size); | |
| 776 | |
| 777 // Send the PING (preface-PING) frame. | |
| 778 void SendPrefacePingIfNoneInFlight(); | |
| 779 | |
| 780 // Send PING if there are no PINGs in flight and we haven't heard from server. | |
| 781 void SendPrefacePing(); | |
| 782 | |
| 783 // Send a single WINDOW_UPDATE frame. | |
| 784 void SendWindowUpdateFrame(SpdyStreamId stream_id, | |
| 785 uint32_t delta_window_size, | |
| 786 RequestPriority priority); | |
| 787 | |
| 788 // Send the PING frame. | |
| 789 void WritePingFrame(SpdyPingId unique_id, bool is_ack); | |
| 790 | |
| 791 // Post a CheckPingStatus call after delay. Don't post if there is already | |
| 792 // CheckPingStatus running. | |
| 793 void PlanToCheckPingStatus(); | |
| 794 | |
| 795 // Check the status of the connection. It calls |CloseSessionOnError| if we | |
| 796 // haven't received any data in |kHungInterval| time period. | |
| 797 void CheckPingStatus(base::TimeTicks last_check_time); | |
| 798 | |
| 799 // Get a new stream id. | |
| 800 SpdyStreamId GetNewStreamId(); | |
| 801 | |
| 802 // Pushes the given frame with the given priority into the write | |
| 803 // queue for the session. | |
| 804 void EnqueueSessionWrite(RequestPriority priority, | |
| 805 SpdyFrameType frame_type, | |
| 806 std::unique_ptr<SpdySerializedFrame> frame); | |
| 807 | |
| 808 // Puts |producer| associated with |stream| onto the write queue | |
| 809 // with the given priority. | |
| 810 void EnqueueWrite(RequestPriority priority, | |
| 811 SpdyFrameType frame_type, | |
| 812 std::unique_ptr<SpdyBufferProducer> producer, | |
| 813 const base::WeakPtr<SpdyStream>& stream); | |
| 814 | |
| 815 // Inserts a newly-created stream into |created_streams_|. | |
| 816 void InsertCreatedStream(std::unique_ptr<SpdyStream> stream); | |
| 817 | |
| 818 // Activates |stream| (which must be in |created_streams_|) by | |
| 819 // assigning it an ID and returns it. | |
| 820 std::unique_ptr<SpdyStream> ActivateCreatedStream(SpdyStream* stream); | |
| 821 | |
| 822 // Inserts a newly-activated stream into |active_streams_|. | |
| 823 void InsertActivatedStream(std::unique_ptr<SpdyStream> stream); | |
| 824 | |
| 825 // Remove all internal references to |stream|, call OnClose() on it, | |
| 826 // and process any pending stream requests before deleting it. Note | |
| 827 // that |stream| may hold the last reference to the session. | |
| 828 void DeleteStream(std::unique_ptr<SpdyStream> stream, int status); | |
| 829 | |
| 830 // Returns the stream id of the push stream if it is not claimed yet, or 0 | |
| 831 // otherwise. | |
| 832 SpdyStreamId GetStreamIdForPush(const GURL& url); | |
| 833 | |
| 834 // Check if we have a pending pushed-stream for this url | |
| 835 // Returns the stream if found (and returns it from the pending | |
| 836 // list). Returns NULL otherwise. | |
| 837 SpdyStream* GetActivePushStream(const GURL& url); | |
| 838 | |
| 839 void RecordPingRTTHistogram(base::TimeDelta duration); | |
| 840 void RecordHistograms(); | |
| 841 void RecordProtocolErrorHistogram(SpdyProtocolErrorDetails details); | |
| 842 | |
| 843 // DCHECKs that |availability_state_| >= STATE_GOING_AWAY, that | |
| 844 // there are no pending stream creation requests, and that there are | |
| 845 // no created streams. | |
| 846 void DcheckGoingAway() const; | |
| 847 | |
| 848 // Calls DcheckGoingAway(), then DCHECKs that |availability_state_| | |
| 849 // == STATE_DRAINING, |error_on_close_| has a valid value, and that there | |
| 850 // are no active streams or unclaimed pushed streams. | |
| 851 void DcheckDraining() const; | |
| 852 | |
| 853 // If the session is already draining, does nothing. Otherwise, moves | |
| 854 // the session to the draining state. | |
| 855 void DoDrainSession(Error err, const SpdyString& description); | |
| 856 | |
| 857 // Called right before closing a (possibly-inactive) stream for a | |
| 858 // reason other than being requested to by the stream. | |
| 859 void LogAbandonedStream(SpdyStream* stream, Error status); | |
| 860 | |
| 861 // Called right before closing an active stream for a reason other | |
| 862 // than being requested to by the stream. | |
| 863 void LogAbandonedActiveStream(ActiveStreamMap::const_iterator it, | |
| 864 Error status); | |
| 865 | |
| 866 // Invokes a user callback for stream creation. We provide this method so it | |
| 867 // can be deferred to the MessageLoop, so we avoid re-entrancy problems. | |
| 868 void CompleteStreamRequest( | |
| 869 const base::WeakPtr<SpdyStreamRequest>& pending_request); | |
| 870 | |
| 871 // Remove old unclaimed pushed streams. | |
| 872 void DeleteExpiredPushedStreams(); | |
| 873 | |
| 874 // BufferedSpdyFramerVisitorInterface: | |
| 875 void OnError(SpdyFramer::SpdyFramerError spdy_framer_error) override; | |
| 876 void OnStreamError(SpdyStreamId stream_id, | |
| 877 const SpdyString& description) override; | |
| 878 void OnPing(SpdyPingId unique_id, bool is_ack) override; | |
| 879 void OnRstStream(SpdyStreamId stream_id, SpdyErrorCode error_code) override; | |
| 880 void OnGoAway(SpdyStreamId last_accepted_stream_id, | |
| 881 SpdyErrorCode error_code, | |
| 882 SpdyStringPiece debug_data) override; | |
| 883 void OnDataFrameHeader(SpdyStreamId stream_id, | |
| 884 size_t length, | |
| 885 bool fin) override; | |
| 886 void OnStreamFrameData(SpdyStreamId stream_id, | |
| 887 const char* data, | |
| 888 size_t len) override; | |
| 889 void OnStreamEnd(SpdyStreamId stream_id) override; | |
| 890 void OnStreamPadding(SpdyStreamId stream_id, size_t len) override; | |
| 891 void OnSettings() override; | |
| 892 void OnSetting(SpdySettingsIds id, uint32_t value) override; | |
| 893 void OnWindowUpdate(SpdyStreamId stream_id, int delta_window_size) override; | |
| 894 void OnPushPromise(SpdyStreamId stream_id, | |
| 895 SpdyStreamId promised_stream_id, | |
| 896 SpdyHeaderBlock headers) override; | |
| 897 void OnHeaders(SpdyStreamId stream_id, | |
| 898 bool has_priority, | |
| 899 int weight, | |
| 900 SpdyStreamId parent_stream_id, | |
| 901 bool exclusive, | |
| 902 bool fin, | |
| 903 SpdyHeaderBlock headers) override; | |
| 904 void OnAltSvc(SpdyStreamId stream_id, | |
| 905 SpdyStringPiece origin, | |
| 906 const SpdyAltSvcWireFormat::AlternativeServiceVector& | |
| 907 altsvc_vector) override; | |
| 908 bool OnUnknownFrame(SpdyStreamId stream_id, uint8_t frame_type) override; | |
| 909 | |
| 910 // SpdyFramerDebugVisitorInterface | |
| 911 void OnSendCompressedFrame(SpdyStreamId stream_id, | |
| 912 SpdyFrameType type, | |
| 913 size_t payload_len, | |
| 914 size_t frame_len) override; | |
| 915 void OnReceiveCompressedFrame(SpdyStreamId stream_id, | |
| 916 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 by OnWindowUpdate() (which is in turn called by the | |
| 930 // framer) to increase this session's send window size by | |
| 931 // |delta_window_size| from a WINDOW_UPDATE frome, which must be at | |
| 932 // least 1. If |delta_window_size| would cause this session's send | |
| 933 // window size to overflow, does nothing. | |
| 934 // | |
| 935 // If session flow control is turned off, this must not be called. | |
| 936 void IncreaseSendWindowSize(int delta_window_size); | |
| 937 | |
| 938 // If session flow control is turned on, called by CreateDataFrame() | |
| 939 // (which is in turn called by a stream) to decrease this session's | |
| 940 // send window size by |delta_window_size|, which must be at least 1 | |
| 941 // and at most kMaxSpdyFrameChunkSize. |delta_window_size| must not | |
| 942 // cause this session's send window size to go negative. | |
| 943 // | |
| 944 // If session flow control is turned off, this must not be called. | |
| 945 void DecreaseSendWindowSize(int32_t delta_window_size); | |
| 946 | |
| 947 // Called when bytes are consumed by the delegate from a SpdyBuffer | |
| 948 // containing received data. Increases the receive window size | |
| 949 // accordingly. | |
| 950 // | |
| 951 // If session flow control is turned off, this must not be called. | |
| 952 void OnReadBufferConsumed(size_t consume_size, | |
| 953 SpdyBuffer::ConsumeSource consume_source); | |
| 954 | |
| 955 // Called by OnReadBufferConsume to increase this session's receive | |
| 956 // window size by |delta_window_size|, which must be at least 1 and | |
| 957 // must not cause this session's receive window size to overflow, | |
| 958 // possibly also sending a WINDOW_UPDATE frame. Also called during | |
| 959 // initialization to set the initial receive window size. | |
| 960 // | |
| 961 // If session flow control is turned off, this must not be called. | |
| 962 void IncreaseRecvWindowSize(int32_t delta_window_size); | |
| 963 | |
| 964 // Called by OnStreamFrameData (which is in turn called by the | |
| 965 // framer) to decrease this session's receive window size by | |
| 966 // |delta_window_size|, which must be at least 1 and must not cause | |
| 967 // this session's receive window size to go negative. | |
| 968 // | |
| 969 // If session flow control is turned off, this must not be called. | |
| 970 void DecreaseRecvWindowSize(int32_t delta_window_size); | |
| 971 | |
| 972 // Queue a send-stalled stream for possibly resuming once we're not | |
| 973 // send-stalled anymore. | |
| 974 void QueueSendStalledStream(const SpdyStream& stream); | |
| 975 | |
| 976 // Go through the queue of send-stalled streams and try to resume as | |
| 977 // many as possible. | |
| 978 void ResumeSendStalledStreams(); | |
| 979 | |
| 980 // Returns the next stream to possibly resume, or 0 if the queue is | |
| 981 // empty. | |
| 982 SpdyStreamId PopStreamToPossiblyResume(); | |
| 983 | |
| 984 // -------------------------- | |
| 985 // Helper methods for testing | |
| 986 // -------------------------- | |
| 987 | |
| 988 void set_connection_at_risk_of_loss_time(base::TimeDelta duration) { | |
| 989 connection_at_risk_of_loss_time_ = duration; | |
| 990 } | |
| 991 | |
| 992 void set_hung_interval(base::TimeDelta duration) { | |
| 993 hung_interval_ = duration; | |
| 994 } | |
| 995 | |
| 996 void set_max_concurrent_pushed_streams(size_t value) { | |
| 997 max_concurrent_pushed_streams_ = value; | |
| 998 } | |
| 999 | |
| 1000 int64_t pings_in_flight() const { return pings_in_flight_; } | |
| 1001 | |
| 1002 SpdyPingId next_ping_id() const { return next_ping_id_; } | |
| 1003 | |
| 1004 base::TimeTicks last_activity_time() const { return last_activity_time_; } | |
| 1005 | |
| 1006 bool check_ping_status_pending() const { return check_ping_status_pending_; } | |
| 1007 | |
| 1008 // Whether Do{Read,Write}Loop() is in the call stack. Useful for | |
| 1009 // making sure we don't destroy ourselves prematurely in that case. | |
| 1010 bool in_io_loop_; | |
| 1011 | |
| 1012 // The key used to identify this session. | |
| 1013 const SpdySessionKey spdy_session_key_; | |
| 1014 | |
| 1015 // Set set of SpdySessionKeys for which this session has serviced | |
| 1016 // requests. | |
| 1017 std::set<SpdySessionKey> pooled_aliases_; | |
| 1018 | |
| 1019 // |pool_| owns us, therefore its lifetime must exceed ours. | |
| 1020 SpdySessionPool* pool_; | |
| 1021 HttpServerProperties* http_server_properties_; | |
| 1022 | |
| 1023 TransportSecurityState* transport_security_state_; | |
| 1024 | |
| 1025 // The socket handle for this session. | |
| 1026 std::unique_ptr<ClientSocketHandle> connection_; | |
| 1027 | |
| 1028 // The read buffer used to read data from the socket. | |
| 1029 // Non-null if there is a Read() pending. | |
| 1030 scoped_refptr<IOBuffer> read_buffer_; | |
| 1031 | |
| 1032 SpdyStreamId stream_hi_water_mark_; // The next stream id to use. | |
| 1033 | |
| 1034 // Used to ensure the server increments push stream ids correctly. | |
| 1035 SpdyStreamId last_accepted_push_stream_id_; | |
| 1036 | |
| 1037 // Queue, for each priority, of pending stream requests that have | |
| 1038 // not yet been satisfied. | |
| 1039 PendingStreamRequestQueue pending_create_stream_queues_[NUM_PRIORITIES]; | |
| 1040 | |
| 1041 // Map from stream id to all active streams. Streams are active in the sense | |
| 1042 // that they have a consumer (typically SpdyNetworkTransaction and regardless | |
| 1043 // of whether or not there is currently any ongoing IO [might be waiting for | |
| 1044 // the server to start pushing the stream]) or there are still network events | |
| 1045 // incoming even though the consumer has already gone away (cancellation). | |
| 1046 // | |
| 1047 // |active_streams_| owns all its SpdyStream objects. | |
| 1048 // | |
| 1049 // TODO(willchan): Perhaps we should separate out cancelled streams and move | |
| 1050 // them into a separate ActiveStreamMap, and not deliver network events to | |
| 1051 // them? | |
| 1052 ActiveStreamMap active_streams_; | |
| 1053 | |
| 1054 UnclaimedPushedStreamContainer unclaimed_pushed_streams_; | |
| 1055 | |
| 1056 // Not owned. |push_delegate_| outlives the session and handles server pushes | |
| 1057 // received by session. | |
| 1058 ServerPushDelegate* push_delegate_; | |
| 1059 | |
| 1060 // Set of all created streams but that have not yet sent any frames. | |
| 1061 // | |
| 1062 // |created_streams_| owns all its SpdyStream objects. | |
| 1063 CreatedStreamSet created_streams_; | |
| 1064 | |
| 1065 // Number of pushed streams. All active streams are stored in | |
| 1066 // |active_streams_|, but it's better to know the number of push streams | |
| 1067 // without traversing the whole collection. | |
| 1068 size_t num_pushed_streams_; | |
| 1069 | |
| 1070 // Number of active pushed streams in |active_streams_|, i.e. not in reserved | |
| 1071 // remote state. Streams in reserved state are not counted towards any | |
| 1072 // concurrency limits. | |
| 1073 size_t num_active_pushed_streams_; | |
| 1074 | |
| 1075 // Number of bytes that has been pushed by the server. | |
| 1076 uint64_t bytes_pushed_count_; | |
| 1077 | |
| 1078 // Number of bytes that has been pushed by the server but never claimed. | |
| 1079 uint64_t bytes_pushed_and_unclaimed_count_; | |
| 1080 | |
| 1081 // The write queue. | |
| 1082 SpdyWriteQueue write_queue_; | |
| 1083 | |
| 1084 // Data for the frame we are currently sending. | |
| 1085 | |
| 1086 // The buffer we're currently writing. | |
| 1087 std::unique_ptr<SpdyBuffer> in_flight_write_; | |
| 1088 // The type of the frame in |in_flight_write_|. | |
| 1089 SpdyFrameType in_flight_write_frame_type_; | |
| 1090 // The size of the frame in |in_flight_write_|. | |
| 1091 size_t in_flight_write_frame_size_; | |
| 1092 // The stream to notify when |in_flight_write_| has been written to | |
| 1093 // the socket completely. | |
| 1094 base::WeakPtr<SpdyStream> in_flight_write_stream_; | |
| 1095 | |
| 1096 // Flag if we're using an SSL connection for this SpdySession. | |
| 1097 bool is_secure_; | |
| 1098 | |
| 1099 // Spdy Frame state. | |
| 1100 std::unique_ptr<BufferedSpdyFramer> buffered_spdy_framer_; | |
| 1101 | |
| 1102 // The state variables. | |
| 1103 AvailabilityState availability_state_; | |
| 1104 ReadState read_state_; | |
| 1105 WriteState write_state_; | |
| 1106 | |
| 1107 // If the session is closing (i.e., |availability_state_| is STATE_DRAINING), | |
| 1108 // then |error_on_close_| holds the error with which it was closed, which | |
| 1109 // may be OK (upon a polite GOAWAY) or an error < ERR_IO_PENDING otherwise. | |
| 1110 // Initialized to OK. | |
| 1111 Error error_on_close_; | |
| 1112 | |
| 1113 // Settings that are sent in the initial SETTINGS frame | |
| 1114 // (if |enable_sending_initial_data_| is true), | |
| 1115 // and also control SpdySession parameters like initial receive window size | |
| 1116 // and maximum HPACK dynamic table size. | |
| 1117 const SettingsMap initial_settings_; | |
| 1118 | |
| 1119 // Limits | |
| 1120 size_t max_concurrent_streams_; | |
| 1121 size_t max_concurrent_pushed_streams_; | |
| 1122 | |
| 1123 // Some statistics counters for the session. | |
| 1124 int streams_initiated_count_; | |
| 1125 int streams_pushed_count_; | |
| 1126 int streams_pushed_and_claimed_count_; | |
| 1127 int streams_abandoned_count_; | |
| 1128 | |
| 1129 // Count of all pings on the wire, for which we have not gotten a response. | |
| 1130 int64_t pings_in_flight_; | |
| 1131 | |
| 1132 // This is the next ping_id (unique_id) to be sent in PING frame. | |
| 1133 SpdyPingId next_ping_id_; | |
| 1134 | |
| 1135 // This is the last time we have sent a PING. | |
| 1136 base::TimeTicks last_ping_sent_time_; | |
| 1137 | |
| 1138 // This is the last time we had activity in the session. | |
| 1139 base::TimeTicks last_activity_time_; | |
| 1140 | |
| 1141 // This is the length of the last compressed frame. | |
| 1142 size_t last_compressed_frame_len_; | |
| 1143 | |
| 1144 // This is the next time that unclaimed push streams should be checked for | |
| 1145 // expirations. | |
| 1146 base::TimeTicks next_unclaimed_push_stream_sweep_time_; | |
| 1147 | |
| 1148 // Indicate if we have already scheduled a delayed task to check the ping | |
| 1149 // status. | |
| 1150 bool check_ping_status_pending_; | |
| 1151 | |
| 1152 // Current send window size. Zero unless session flow control is turned on. | |
| 1153 int32_t session_send_window_size_; | |
| 1154 | |
| 1155 // Maximum receive window size. Each time a WINDOW_UPDATE is sent, it | |
| 1156 // restores the receive window size to this value. Zero unless session flow | |
| 1157 // control is turned on. | |
| 1158 int32_t session_max_recv_window_size_; | |
| 1159 | |
| 1160 // Sum of |session_unacked_recv_window_bytes_| and current receive window | |
| 1161 // size. Zero unless session flow control is turned on. | |
| 1162 // TODO(bnc): Rename or change semantics so that |window_size_| is actual | |
| 1163 // window size. | |
| 1164 int32_t session_recv_window_size_; | |
| 1165 | |
| 1166 // When bytes are consumed, SpdyIOBuffer destructor calls back to SpdySession, | |
| 1167 // and this member keeps count of them until the corresponding WINDOW_UPDATEs | |
| 1168 // are sent. Zero unless session flow control is turned on. | |
| 1169 int32_t session_unacked_recv_window_bytes_; | |
| 1170 | |
| 1171 // Initial send window size for this session's streams. Can be | |
| 1172 // changed by an arriving SETTINGS frame. Newly created streams use | |
| 1173 // this value for the initial send window size. | |
| 1174 int32_t stream_initial_send_window_size_; | |
| 1175 | |
| 1176 // The maximum HPACK dynamic table size the server is allowed to set. | |
| 1177 uint32_t max_header_table_size_; | |
| 1178 | |
| 1179 // Initial receive window size for this session's streams. There are | |
| 1180 // plans to add a command line switch that would cause a SETTINGS | |
| 1181 // frame with window size announcement to be sent on startup. Newly | |
| 1182 // created streams will use this value for the initial receive | |
| 1183 // window size. | |
| 1184 int32_t stream_max_recv_window_size_; | |
| 1185 | |
| 1186 // A queue of stream IDs that have been send-stalled at some point | |
| 1187 // in the past. | |
| 1188 std::deque<SpdyStreamId> stream_send_unstall_queue_[NUM_PRIORITIES]; | |
| 1189 | |
| 1190 NetLogWithSource net_log_; | |
| 1191 | |
| 1192 // Outside of tests, these should always be true. | |
| 1193 bool enable_sending_initial_data_; | |
| 1194 bool enable_ping_based_connection_checking_; | |
| 1195 | |
| 1196 // |connection_at_risk_of_loss_time_| is an optimization to avoid sending | |
| 1197 // wasteful preface pings (when we just got some data). | |
| 1198 // | |
| 1199 // If it is zero (the most conservative figure), then we always send the | |
| 1200 // preface ping (when none are in flight). | |
| 1201 // | |
| 1202 // It is common for TCP/IP sessions to time out in about 3-5 minutes. | |
| 1203 // Certainly if it has been more than 3 minutes, we do want to send a preface | |
| 1204 // ping. | |
| 1205 // | |
| 1206 // We don't think any connection will time out in under about 10 seconds. So | |
| 1207 // this might as well be set to something conservative like 10 seconds. Later, | |
| 1208 // we could adjust it to send fewer pings perhaps. | |
| 1209 base::TimeDelta connection_at_risk_of_loss_time_; | |
| 1210 | |
| 1211 // The amount of time that we are willing to tolerate with no activity (of any | |
| 1212 // form), while there is a ping in flight, before we declare the connection to | |
| 1213 // be hung. TODO(rtenneti): When hung, instead of resetting connection, race | |
| 1214 // to build a new connection, and see if that completes before we (finally) | |
| 1215 // get a PING response (http://crbug.com/127812). | |
| 1216 base::TimeDelta hung_interval_; | |
| 1217 | |
| 1218 // The |proxy_delegate_| verifies that a given proxy is a trusted SPDY proxy, | |
| 1219 // which is allowed to push resources from origins that are different from | |
| 1220 // those of their associated streams. May be nullptr. | |
| 1221 ProxyDelegate* proxy_delegate_; | |
| 1222 | |
| 1223 TimeFunc time_func_; | |
| 1224 | |
| 1225 Http2PriorityDependencies priority_dependency_state_; | |
| 1226 | |
| 1227 // Used for posting asynchronous IO tasks. We use this even though | |
| 1228 // SpdySession is refcounted because we don't need to keep the SpdySession | |
| 1229 // alive if the last reference is within a RunnableMethod. Just revoke the | |
| 1230 // method. | |
| 1231 base::WeakPtrFactory<SpdySession> weak_factory_; | |
| 1232 }; | |
| 1233 | |
| 1234 } // namespace net | |
| 1235 | |
| 1236 #endif // NET_SPDY_SPDY_SESSION_H_ | |
| OLD | NEW |