Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 | #include "net/spdy/spdy_session.h" |
| 6 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 7 | #include <algorithm> |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 8 | #include <map> |
| 9 | |
| 10 | #include "base/basictypes.h" |
| 11 | #include "base/bind.h" |
| 12 | #include "base/compiler_specific.h" |
| 13 | #include "base/logging.h" |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 14 | #include "base/message_loop/message_loop.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 15 | #include "base/metrics/field_trial.h" |
| 16 | #include "base/metrics/histogram.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 17 | #include "base/metrics/sparse_histogram.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 18 | #include "base/metrics/stats_counters.h" |
| 19 | #include "base/stl_util.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 20 | #include "base/strings/string_number_conversions.h" |
Torne (Richard Coles) | 5e3f23d | 2013-06-11 16:24:11 +0100 | [diff] [blame] | 21 | #include "base/strings/string_util.h" |
| 22 | #include "base/strings/stringprintf.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 23 | #include "base/strings/utf_string_conversions.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 24 | #include "base/time/time.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 25 | #include "base/values.h" |
| 26 | #include "crypto/ec_private_key.h" |
| 27 | #include "crypto/ec_signature_creator.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 28 | #include "net/base/connection_type_histograms.h" |
| 29 | #include "net/base/net_log.h" |
| 30 | #include "net/base/net_util.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 31 | #include "net/cert/asn1_util.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 32 | #include "net/http/http_network_session.h" |
| 33 | #include "net/http/http_server_properties.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 34 | #include "net/spdy/spdy_buffer_producer.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 35 | #include "net/spdy/spdy_credential_builder.h" |
| 36 | #include "net/spdy/spdy_frame_builder.h" |
| 37 | #include "net/spdy/spdy_http_utils.h" |
| 38 | #include "net/spdy/spdy_protocol.h" |
| 39 | #include "net/spdy/spdy_session_pool.h" |
| 40 | #include "net/spdy/spdy_stream.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 41 | #include "net/ssl/server_bound_cert_service.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 42 | |
| 43 | namespace net { |
| 44 | |
| 45 | namespace { |
| 46 | |
| 47 | const int kReadBufferSize = 8 * 1024; |
| 48 | const int kDefaultConnectionAtRiskOfLossSeconds = 10; |
| 49 | const int kHungIntervalSeconds = 10; |
| 50 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 51 | // Always start at 1 for the first stream id. |
| 52 | const SpdyStreamId kFirstStreamId = 1; |
| 53 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 54 | // Minimum seconds that unclaimed pushed streams will be kept in memory. |
| 55 | const int kMinPushedStreamLifetimeSeconds = 300; |
| 56 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 57 | base::Value* NetLogSpdySynCallback(const SpdyHeaderBlock* headers, |
| 58 | bool fin, |
| 59 | bool unidirectional, |
| 60 | SpdyStreamId stream_id, |
| 61 | SpdyStreamId associated_stream, |
| 62 | NetLog::LogLevel /* log_level */) { |
| 63 | base::DictionaryValue* dict = new base::DictionaryValue(); |
| 64 | base::ListValue* headers_list = new base::ListValue(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 65 | for (SpdyHeaderBlock::const_iterator it = headers->begin(); |
| 66 | it != headers->end(); ++it) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 67 | headers_list->Append(new base::StringValue(base::StringPrintf( |
| 68 | "%s: %s", it->first.c_str(), |
| 69 | (ShouldShowHttpHeaderValue( |
| 70 | it->first) ? it->second : "[elided]").c_str()))); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 71 | } |
| 72 | dict->SetBoolean("fin", fin); |
| 73 | dict->SetBoolean("unidirectional", unidirectional); |
| 74 | dict->Set("headers", headers_list); |
| 75 | dict->SetInteger("stream_id", stream_id); |
| 76 | if (associated_stream) |
| 77 | dict->SetInteger("associated_stream", associated_stream); |
| 78 | return dict; |
| 79 | } |
| 80 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 81 | base::Value* NetLogSpdyCredentialCallback(size_t slot, |
| 82 | const std::string* origin, |
| 83 | NetLog::LogLevel /* log_level */) { |
| 84 | base::DictionaryValue* dict = new base::DictionaryValue(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 85 | dict->SetInteger("slot", slot); |
| 86 | dict->SetString("origin", *origin); |
| 87 | return dict; |
| 88 | } |
| 89 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 90 | base::Value* NetLogSpdySessionCloseCallback(int net_error, |
| 91 | const std::string* description, |
| 92 | NetLog::LogLevel /* log_level */) { |
| 93 | base::DictionaryValue* dict = new base::DictionaryValue(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 94 | dict->SetInteger("net_error", net_error); |
| 95 | dict->SetString("description", *description); |
| 96 | return dict; |
| 97 | } |
| 98 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 99 | base::Value* NetLogSpdySessionCallback(const HostPortProxyPair* host_pair, |
| 100 | NetLog::LogLevel /* log_level */) { |
| 101 | base::DictionaryValue* dict = new base::DictionaryValue(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 102 | dict->SetString("host", host_pair->first.ToString()); |
| 103 | dict->SetString("proxy", host_pair->second.ToPacString()); |
| 104 | return dict; |
| 105 | } |
| 106 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 107 | base::Value* NetLogSpdySettingsCallback(const HostPortPair& host_port_pair, |
| 108 | bool clear_persisted, |
| 109 | NetLog::LogLevel /* log_level */) { |
| 110 | base::DictionaryValue* dict = new base::DictionaryValue(); |
| 111 | dict->SetString("host", host_port_pair.ToString()); |
| 112 | dict->SetBoolean("clear_persisted", clear_persisted); |
| 113 | return dict; |
| 114 | } |
| 115 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 116 | base::Value* NetLogSpdySettingCallback(SpdySettingsIds id, |
| 117 | SpdySettingsFlags flags, |
| 118 | uint32 value, |
| 119 | NetLog::LogLevel /* log_level */) { |
| 120 | base::DictionaryValue* dict = new base::DictionaryValue(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 121 | dict->SetInteger("id", id); |
| 122 | dict->SetInteger("flags", flags); |
| 123 | dict->SetInteger("value", value); |
| 124 | return dict; |
| 125 | } |
| 126 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 127 | base::Value* NetLogSpdySendSettingsCallback(const SettingsMap* settings, |
| 128 | NetLog::LogLevel /* log_level */) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 129 | base::DictionaryValue* dict = new base::DictionaryValue(); |
| 130 | base::ListValue* settings_list = new base::ListValue(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 131 | for (SettingsMap::const_iterator it = settings->begin(); |
| 132 | it != settings->end(); ++it) { |
| 133 | const SpdySettingsIds id = it->first; |
| 134 | const SpdySettingsFlags flags = it->second.first; |
| 135 | const uint32 value = it->second.second; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 136 | settings_list->Append(new base::StringValue( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 137 | base::StringPrintf("[id:%u flags:%u value:%u]", id, flags, value))); |
| 138 | } |
| 139 | dict->Set("settings", settings_list); |
| 140 | return dict; |
| 141 | } |
| 142 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 143 | base::Value* NetLogSpdyWindowUpdateFrameCallback( |
| 144 | SpdyStreamId stream_id, |
| 145 | uint32 delta, |
| 146 | NetLog::LogLevel /* log_level */) { |
| 147 | base::DictionaryValue* dict = new base::DictionaryValue(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 148 | dict->SetInteger("stream_id", static_cast<int>(stream_id)); |
| 149 | dict->SetInteger("delta", delta); |
| 150 | return dict; |
| 151 | } |
| 152 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 153 | base::Value* NetLogSpdySessionWindowUpdateCallback( |
| 154 | int32 delta, |
| 155 | int32 window_size, |
| 156 | NetLog::LogLevel /* log_level */) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 157 | base::DictionaryValue* dict = new base::DictionaryValue(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 158 | dict->SetInteger("delta", delta); |
| 159 | dict->SetInteger("window_size", window_size); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 160 | return dict; |
| 161 | } |
| 162 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 163 | base::Value* NetLogSpdyDataCallback(SpdyStreamId stream_id, |
| 164 | int size, |
| 165 | bool fin, |
| 166 | NetLog::LogLevel /* log_level */) { |
| 167 | base::DictionaryValue* dict = new base::DictionaryValue(); |
| 168 | dict->SetInteger("stream_id", static_cast<int>(stream_id)); |
| 169 | dict->SetInteger("size", size); |
| 170 | dict->SetBoolean("fin", fin); |
| 171 | return dict; |
| 172 | } |
| 173 | |
| 174 | base::Value* NetLogSpdyRstCallback(SpdyStreamId stream_id, |
| 175 | int status, |
| 176 | const std::string* description, |
| 177 | NetLog::LogLevel /* log_level */) { |
| 178 | base::DictionaryValue* dict = new base::DictionaryValue(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 179 | dict->SetInteger("stream_id", static_cast<int>(stream_id)); |
| 180 | dict->SetInteger("status", status); |
| 181 | dict->SetString("description", *description); |
| 182 | return dict; |
| 183 | } |
| 184 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 185 | base::Value* NetLogSpdyPingCallback(uint32 unique_id, |
| 186 | const char* type, |
| 187 | NetLog::LogLevel /* log_level */) { |
| 188 | base::DictionaryValue* dict = new base::DictionaryValue(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 189 | dict->SetInteger("unique_id", unique_id); |
| 190 | dict->SetString("type", type); |
| 191 | return dict; |
| 192 | } |
| 193 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 194 | base::Value* NetLogSpdyGoAwayCallback(SpdyStreamId last_stream_id, |
| 195 | int active_streams, |
| 196 | int unclaimed_streams, |
| 197 | SpdyGoAwayStatus status, |
| 198 | NetLog::LogLevel /* log_level */) { |
| 199 | base::DictionaryValue* dict = new base::DictionaryValue(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 200 | dict->SetInteger("last_accepted_stream_id", |
| 201 | static_cast<int>(last_stream_id)); |
| 202 | dict->SetInteger("active_streams", active_streams); |
| 203 | dict->SetInteger("unclaimed_streams", unclaimed_streams); |
| 204 | dict->SetInteger("status", static_cast<int>(status)); |
| 205 | return dict; |
| 206 | } |
| 207 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 208 | // The maximum number of concurrent streams we will ever create. Even if |
| 209 | // the server permits more, we will never exceed this limit. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 210 | const size_t kMaxConcurrentStreamLimit = 256; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 211 | |
| 212 | } // namespace |
| 213 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 214 | SpdyStreamRequest::SpdyStreamRequest() { |
| 215 | Reset(); |
| 216 | } |
| 217 | |
| 218 | SpdyStreamRequest::~SpdyStreamRequest() { |
| 219 | CancelRequest(); |
| 220 | } |
| 221 | |
| 222 | int SpdyStreamRequest::StartRequest( |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 223 | SpdyStreamType type, |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 224 | const base::WeakPtr<SpdySession>& session, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 225 | const GURL& url, |
| 226 | RequestPriority priority, |
| 227 | const BoundNetLog& net_log, |
| 228 | const CompletionCallback& callback) { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 229 | DCHECK(session.get()); |
| 230 | DCHECK(!session_.get()); |
| 231 | DCHECK(!stream_.get()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 232 | DCHECK(callback_.is_null()); |
| 233 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 234 | type_ = type; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 235 | session_ = session; |
| 236 | url_ = url; |
| 237 | priority_ = priority; |
| 238 | net_log_ = net_log; |
| 239 | callback_ = callback; |
| 240 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 241 | base::WeakPtr<SpdyStream> stream; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 242 | int rv = session->TryCreateStream(this, &stream); |
| 243 | if (rv == OK) { |
| 244 | Reset(); |
| 245 | stream_ = stream; |
| 246 | } |
| 247 | return rv; |
| 248 | } |
| 249 | |
| 250 | void SpdyStreamRequest::CancelRequest() { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 251 | if (session_.get()) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 252 | session_->CancelStreamRequest(this); |
| 253 | Reset(); |
| 254 | } |
| 255 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 256 | base::WeakPtr<SpdyStream> SpdyStreamRequest::ReleaseStream() { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 257 | DCHECK(!session_.get()); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 258 | base::WeakPtr<SpdyStream> stream = stream_; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 259 | DCHECK(stream.get()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 260 | Reset(); |
| 261 | return stream; |
| 262 | } |
| 263 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 264 | void SpdyStreamRequest::OnRequestCompleteSuccess( |
| 265 | base::WeakPtr<SpdyStream>* stream) { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 266 | DCHECK(session_.get()); |
| 267 | DCHECK(!stream_.get()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 268 | DCHECK(!callback_.is_null()); |
| 269 | CompletionCallback callback = callback_; |
| 270 | Reset(); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 271 | DCHECK(*stream); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 272 | stream_ = *stream; |
| 273 | callback.Run(OK); |
| 274 | } |
| 275 | |
| 276 | void SpdyStreamRequest::OnRequestCompleteFailure(int rv) { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 277 | DCHECK(session_.get()); |
| 278 | DCHECK(!stream_.get()); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 279 | DCHECK(!callback_.is_null()); |
| 280 | CompletionCallback callback = callback_; |
| 281 | Reset(); |
| 282 | DCHECK_NE(rv, OK); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 283 | callback.Run(rv); |
| 284 | } |
| 285 | |
| 286 | void SpdyStreamRequest::Reset() { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 287 | type_ = SPDY_BIDIRECTIONAL_STREAM; |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 288 | session_.reset(); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 289 | stream_.reset(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 290 | url_ = GURL(); |
| 291 | priority_ = MINIMUM_PRIORITY; |
| 292 | net_log_ = BoundNetLog(); |
| 293 | callback_.Reset(); |
| 294 | } |
| 295 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 296 | SpdySession::ActiveStreamInfo::ActiveStreamInfo() |
| 297 | : stream(NULL), |
| 298 | waiting_for_syn_reply(false) {} |
| 299 | |
| 300 | SpdySession::ActiveStreamInfo::ActiveStreamInfo(SpdyStream* stream) |
| 301 | : stream(stream), |
| 302 | waiting_for_syn_reply(stream->type() != SPDY_PUSH_STREAM) {} |
| 303 | |
| 304 | SpdySession::ActiveStreamInfo::~ActiveStreamInfo() {} |
| 305 | |
| 306 | SpdySession::PushedStreamInfo::PushedStreamInfo() : stream_id(0) {} |
| 307 | |
| 308 | SpdySession::PushedStreamInfo::PushedStreamInfo( |
| 309 | SpdyStreamId stream_id, |
| 310 | base::TimeTicks creation_time) |
| 311 | : stream_id(stream_id), |
| 312 | creation_time(creation_time) {} |
| 313 | |
| 314 | SpdySession::PushedStreamInfo::~PushedStreamInfo() {} |
| 315 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 316 | SpdySession::SpdySession( |
| 317 | const SpdySessionKey& spdy_session_key, |
| 318 | const base::WeakPtr<HttpServerProperties>& http_server_properties, |
| 319 | bool verify_domain_authentication, |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 320 | bool enable_sending_initial_data, |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 321 | bool enable_credential_frames, |
| 322 | bool enable_compression, |
| 323 | bool enable_ping_based_connection_checking, |
| 324 | NextProto default_protocol, |
| 325 | size_t stream_initial_recv_window_size, |
| 326 | size_t initial_max_concurrent_streams, |
| 327 | size_t max_concurrent_streams_limit, |
| 328 | TimeFunc time_func, |
| 329 | const HostPortPair& trusted_spdy_proxy, |
| 330 | NetLog* net_log) |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 331 | : weak_factory_(this), |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 332 | in_io_loop_(false), |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 333 | spdy_session_key_(spdy_session_key), |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 334 | pool_(NULL), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 335 | http_server_properties_(http_server_properties), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 336 | read_buffer_(new IOBuffer(kReadBufferSize)), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 337 | stream_hi_water_mark_(kFirstStreamId), |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 338 | in_flight_write_frame_type_(DATA), |
| 339 | in_flight_write_frame_size_(0), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 340 | is_secure_(false), |
| 341 | certificate_error_code_(OK), |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 342 | availability_state_(STATE_AVAILABLE), |
| 343 | read_state_(READ_STATE_DO_READ), |
| 344 | write_state_(WRITE_STATE_IDLE), |
| 345 | error_on_close_(OK), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 346 | max_concurrent_streams_(initial_max_concurrent_streams == 0 ? |
| 347 | kInitialMaxConcurrentStreams : |
| 348 | initial_max_concurrent_streams), |
| 349 | max_concurrent_streams_limit_(max_concurrent_streams_limit == 0 ? |
| 350 | kMaxConcurrentStreamLimit : |
| 351 | max_concurrent_streams_limit), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 352 | streams_initiated_count_(0), |
| 353 | streams_pushed_count_(0), |
| 354 | streams_pushed_and_claimed_count_(0), |
| 355 | streams_abandoned_count_(0), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 356 | total_bytes_received_(0), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 357 | sent_settings_(false), |
| 358 | received_settings_(false), |
| 359 | stalled_streams_(0), |
| 360 | pings_in_flight_(0), |
| 361 | next_ping_id_(1), |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 362 | last_activity_time_(time_func()), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 363 | check_ping_status_pending_(false), |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 364 | send_connection_header_prefix_(false), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 365 | flow_control_state_(FLOW_CONTROL_NONE), |
| 366 | stream_initial_send_window_size_(kSpdyStreamInitialWindowSize), |
| 367 | stream_initial_recv_window_size_(stream_initial_recv_window_size == 0 ? |
| 368 | kDefaultInitialRecvWindowSize : |
| 369 | stream_initial_recv_window_size), |
| 370 | session_send_window_size_(0), |
| 371 | session_recv_window_size_(0), |
| 372 | session_unacked_recv_window_bytes_(0), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 373 | net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SPDY_SESSION)), |
| 374 | verify_domain_authentication_(verify_domain_authentication), |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 375 | enable_sending_initial_data_(enable_sending_initial_data), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 376 | enable_credential_frames_(enable_credential_frames), |
| 377 | enable_compression_(enable_compression), |
| 378 | enable_ping_based_connection_checking_( |
| 379 | enable_ping_based_connection_checking), |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 380 | protocol_(default_protocol), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 381 | credential_state_(SpdyCredentialState::kDefaultNumSlots), |
| 382 | connection_at_risk_of_loss_time_( |
| 383 | base::TimeDelta::FromSeconds(kDefaultConnectionAtRiskOfLossSeconds)), |
| 384 | hung_interval_( |
| 385 | base::TimeDelta::FromSeconds(kHungIntervalSeconds)), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 386 | trusted_spdy_proxy_(trusted_spdy_proxy), |
| 387 | time_func_(time_func) { |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 388 | // TODO(akalin): Change this to kProtoSPDYMinimumVersion once we |
| 389 | // stop supporting SPDY/1. |
| 390 | DCHECK_GE(protocol_, kProtoSPDY2); |
| 391 | DCHECK_LE(protocol_, kProtoSPDYMaximumVersion); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 392 | DCHECK(HttpStreamFactory::spdy_enabled()); |
| 393 | net_log_.BeginEvent( |
| 394 | NetLog::TYPE_SPDY_SESSION, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 395 | base::Bind(&NetLogSpdySessionCallback, &host_port_proxy_pair())); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 396 | next_unclaimed_push_stream_sweep_time_ = time_func_() + |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 397 | base::TimeDelta::FromSeconds(kMinPushedStreamLifetimeSeconds); |
| 398 | // TODO(mbelshe): consider randomization of the stream_hi_water_mark. |
| 399 | } |
| 400 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 401 | SpdySession::~SpdySession() { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 402 | CHECK(!in_io_loop_); |
| 403 | DCHECK(!pool_); |
| 404 | DcheckClosed(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 405 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 406 | // TODO(akalin): Check connection->is_initialized() instead. This |
| 407 | // requires re-working CreateFakeSpdySession(), though. |
| 408 | DCHECK(connection_->socket()); |
| 409 | // With SPDY we can't recycle sockets. |
| 410 | connection_->socket()->Disconnect(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 411 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 412 | RecordHistograms(); |
| 413 | |
| 414 | net_log_.EndEvent(NetLog::TYPE_SPDY_SESSION); |
| 415 | } |
| 416 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 417 | Error SpdySession::InitializeWithSocket( |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 418 | scoped_ptr<ClientSocketHandle> connection, |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 419 | SpdySessionPool* pool, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 420 | bool is_secure, |
| 421 | int certificate_error_code) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 422 | CHECK(!in_io_loop_); |
| 423 | DCHECK_EQ(availability_state_, STATE_AVAILABLE); |
| 424 | DCHECK_EQ(read_state_, READ_STATE_DO_READ); |
| 425 | DCHECK_EQ(write_state_, WRITE_STATE_IDLE); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 426 | DCHECK(!connection_); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 427 | |
| 428 | DCHECK(certificate_error_code == OK || |
| 429 | certificate_error_code < ERR_IO_PENDING); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 430 | // TODO(akalin): Check connection->is_initialized() instead. This |
| 431 | // requires re-working CreateFakeSpdySession(), though. |
| 432 | DCHECK(connection->socket()); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 433 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 434 | base::StatsCounter spdy_sessions("spdy.sessions"); |
| 435 | spdy_sessions.Increment(); |
| 436 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 437 | connection_ = connection.Pass(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 438 | is_secure_ = is_secure; |
| 439 | certificate_error_code_ = certificate_error_code; |
| 440 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 441 | NextProto protocol_negotiated = |
| 442 | connection_->socket()->GetNegotiatedProtocol(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 443 | if (protocol_negotiated != kProtoUnknown) { |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 444 | protocol_ = protocol_negotiated; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 445 | } |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 446 | // TODO(akalin): Change this to kProtoSPDYMinimumVersion once we |
| 447 | // stop supporting SPDY/1. |
| 448 | DCHECK_GE(protocol_, kProtoSPDY2); |
| 449 | DCHECK_LE(protocol_, kProtoSPDYMaximumVersion); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 450 | |
| 451 | SSLClientSocket* ssl_socket = GetSSLClientSocket(); |
| 452 | if (ssl_socket && ssl_socket->WasChannelIDSent()) { |
| 453 | // According to the SPDY spec, the credential associated with the TLS |
| 454 | // connection is stored in slot[1]. |
| 455 | credential_state_.SetHasCredential(GURL("https://" + |
| 456 | host_port_pair().ToString())); |
| 457 | } |
| 458 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 459 | if (protocol_ == kProtoHTTP2Draft04) |
| 460 | send_connection_header_prefix_ = true; |
| 461 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 462 | if (protocol_ >= kProtoSPDY31) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 463 | flow_control_state_ = FLOW_CONTROL_STREAM_AND_SESSION; |
| 464 | session_send_window_size_ = kSpdySessionInitialWindowSize; |
| 465 | session_recv_window_size_ = kSpdySessionInitialWindowSize; |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 466 | } else if (protocol_ >= kProtoSPDY3) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 467 | flow_control_state_ = FLOW_CONTROL_STREAM; |
| 468 | } else { |
| 469 | flow_control_state_ = FLOW_CONTROL_NONE; |
| 470 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 471 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 472 | buffered_spdy_framer_.reset( |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 473 | new BufferedSpdyFramer(NextProtoToSpdyMajorVersion(protocol_), |
| 474 | enable_compression_)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 475 | buffered_spdy_framer_->set_visitor(this); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 476 | buffered_spdy_framer_->set_debug_visitor(this); |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 477 | UMA_HISTOGRAM_ENUMERATION("Net.SpdyVersion", protocol_, kProtoMaximumVersion); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 478 | #if defined(SPDY_PROXY_AUTH_ORIGIN) |
| 479 | UMA_HISTOGRAM_BOOLEAN("Net.SpdySessions_DataReductionProxy", |
| 480 | host_port_pair().Equals(HostPortPair::FromURL( |
| 481 | GURL(SPDY_PROXY_AUTH_ORIGIN)))); |
| 482 | #endif |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 483 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 484 | net_log_.AddEvent( |
| 485 | NetLog::TYPE_SPDY_SESSION_INITIALIZED, |
| 486 | connection_->socket()->NetLog().source().ToEventParametersCallback()); |
| 487 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 488 | int error = DoReadLoop(READ_STATE_DO_READ, OK); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 489 | if (error == ERR_IO_PENDING) |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 490 | error = OK; |
| 491 | if (error == OK) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 492 | DCHECK_NE(availability_state_, STATE_CLOSED); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 493 | connection_->AddLayeredPool(this); |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 494 | if (enable_sending_initial_data_) |
| 495 | SendInitialData(); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 496 | pool_ = pool; |
| 497 | } else { |
| 498 | DcheckClosed(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 499 | } |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 500 | return static_cast<Error>(error); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | bool SpdySession::VerifyDomainAuthentication(const std::string& domain) { |
| 504 | if (!verify_domain_authentication_) |
| 505 | return true; |
| 506 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 507 | if (availability_state_ == STATE_CLOSED) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 508 | return false; |
| 509 | |
| 510 | SSLInfo ssl_info; |
| 511 | bool was_npn_negotiated; |
| 512 | NextProto protocol_negotiated = kProtoUnknown; |
| 513 | if (!GetSSLInfo(&ssl_info, &was_npn_negotiated, &protocol_negotiated)) |
| 514 | return true; // This is not a secure session, so all domains are okay. |
| 515 | |
| 516 | return !ssl_info.client_cert_sent && |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 517 | (enable_credential_frames_ || !ssl_info.channel_id_sent || |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 518 | ServerBoundCertService::GetDomainForHost(domain) == |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 519 | ServerBoundCertService::GetDomainForHost(host_port_pair().host())) && |
| 520 | ssl_info.cert->VerifyNameMatch(domain); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 523 | int SpdySession::GetPushStream( |
| 524 | const GURL& url, |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 525 | base::WeakPtr<SpdyStream>* stream, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 526 | const BoundNetLog& stream_net_log) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 527 | CHECK(!in_io_loop_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 528 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 529 | stream->reset(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 530 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 531 | // TODO(akalin): Add unit test exercising this code path. |
| 532 | if (availability_state_ == STATE_CLOSED) |
| 533 | return ERR_CONNECTION_CLOSED; |
| 534 | |
| 535 | Error err = TryAccessStream(url); |
| 536 | if (err != OK) |
| 537 | return err; |
| 538 | |
| 539 | *stream = GetActivePushStream(url); |
| 540 | if (*stream) { |
| 541 | DCHECK_LT(streams_pushed_and_claimed_count_, streams_pushed_count_); |
| 542 | streams_pushed_and_claimed_count_++; |
| 543 | } |
| 544 | return OK; |
| 545 | } |
| 546 | |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 547 | // {,Try}CreateStream() and TryAccessStream() can be called with |
| 548 | // |in_io_loop_| set if a stream is being created in response to |
| 549 | // another being closed due to received data. |
| 550 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 551 | Error SpdySession::TryAccessStream(const GURL& url) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 552 | DCHECK_NE(availability_state_, STATE_CLOSED); |
| 553 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 554 | if (is_secure_ && certificate_error_code_ != OK && |
| 555 | (url.SchemeIs("https") || url.SchemeIs("wss"))) { |
| 556 | RecordProtocolErrorHistogram( |
| 557 | PROTOCOL_ERROR_REQUEST_FOR_SECURE_CONTENT_OVER_INSECURE_SESSION); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 558 | CloseSessionResult result = DoCloseSession( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 559 | static_cast<Error>(certificate_error_code_), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 560 | "Tried to get SPDY stream for secure content over an unauthenticated " |
| 561 | "session."); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 562 | DCHECK_EQ(result, SESSION_CLOSED_AND_REMOVED); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 563 | return ERR_SPDY_PROTOCOL_ERROR; |
| 564 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 565 | return OK; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 568 | int SpdySession::TryCreateStream(SpdyStreamRequest* request, |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 569 | base::WeakPtr<SpdyStream>* stream) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 570 | CHECK(request); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 571 | |
| 572 | if (availability_state_ == STATE_GOING_AWAY) |
| 573 | return ERR_FAILED; |
| 574 | |
| 575 | // TODO(akalin): Add unit test exercising this code path. |
| 576 | if (availability_state_ == STATE_CLOSED) |
| 577 | return ERR_CONNECTION_CLOSED; |
| 578 | |
| 579 | Error err = TryAccessStream(request->url()); |
| 580 | if (err != OK) |
| 581 | return err; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 582 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 583 | if (!max_concurrent_streams_ || |
| 584 | (active_streams_.size() + created_streams_.size() < |
| 585 | max_concurrent_streams_)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 586 | return CreateStream(*request, stream); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | stalled_streams_++; |
| 590 | net_log().AddEvent(NetLog::TYPE_SPDY_SESSION_STALLED_MAX_STREAMS); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 591 | pending_create_stream_queues_[request->priority()].push_back(request); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 592 | return ERR_IO_PENDING; |
| 593 | } |
| 594 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 595 | int SpdySession::CreateStream(const SpdyStreamRequest& request, |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 596 | base::WeakPtr<SpdyStream>* stream) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 597 | DCHECK_GE(request.priority(), MINIMUM_PRIORITY); |
| 598 | DCHECK_LT(request.priority(), NUM_PRIORITIES); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 599 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 600 | if (availability_state_ == STATE_GOING_AWAY) |
| 601 | return ERR_FAILED; |
| 602 | |
| 603 | // TODO(akalin): Add unit test exercising this code path. |
| 604 | if (availability_state_ == STATE_CLOSED) |
| 605 | return ERR_CONNECTION_CLOSED; |
| 606 | |
| 607 | Error err = TryAccessStream(request.url()); |
| 608 | if (err != OK) { |
| 609 | // This should have been caught in TryCreateStream(). |
| 610 | NOTREACHED(); |
| 611 | return err; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 614 | DCHECK(connection_->socket()); |
| 615 | DCHECK(connection_->socket()->IsConnected()); |
| 616 | if (connection_->socket()) { |
| 617 | UMA_HISTOGRAM_BOOLEAN("Net.SpdySession.CreateStreamWithSocketConnected", |
| 618 | connection_->socket()->IsConnected()); |
| 619 | if (!connection_->socket()->IsConnected()) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 620 | CloseSessionResult result = DoCloseSession( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 621 | ERR_CONNECTION_CLOSED, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 622 | "Tried to create SPDY stream for a closed socket connection."); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 623 | DCHECK_EQ(result, SESSION_CLOSED_AND_REMOVED); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 624 | return ERR_CONNECTION_CLOSED; |
| 625 | } |
| 626 | } |
| 627 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 628 | scoped_ptr<SpdyStream> new_stream( |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 629 | new SpdyStream(request.type(), GetWeakPtr(), request.url(), |
| 630 | request.priority(), |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 631 | stream_initial_send_window_size_, |
| 632 | stream_initial_recv_window_size_, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 633 | request.net_log())); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 634 | *stream = new_stream->GetWeakPtr(); |
| 635 | InsertCreatedStream(new_stream.Pass()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 636 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 637 | UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 638 | "Net.SpdyPriorityCount", |
| 639 | static_cast<int>(request.priority()), 0, 10, 11); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 640 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 641 | return OK; |
| 642 | } |
| 643 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 644 | void SpdySession::CancelStreamRequest(SpdyStreamRequest* request) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 645 | CHECK(request); |
| 646 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 647 | if (DCHECK_IS_ON()) { |
| 648 | // |request| should not be in a queue not matching its priority. |
| 649 | for (int i = 0; i < NUM_PRIORITIES; ++i) { |
| 650 | if (request->priority() == i) |
| 651 | continue; |
| 652 | PendingStreamRequestQueue* queue = &pending_create_stream_queues_[i]; |
| 653 | DCHECK(std::find(queue->begin(), queue->end(), request) == queue->end()); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | PendingStreamRequestQueue* queue = |
| 658 | &pending_create_stream_queues_[request->priority()]; |
| 659 | // Remove |request| from |queue| while preserving the order of the |
| 660 | // other elements. |
| 661 | PendingStreamRequestQueue::iterator it = |
| 662 | std::find(queue->begin(), queue->end(), request); |
| 663 | if (it != queue->end()) { |
| 664 | it = queue->erase(it); |
| 665 | // |request| should be in the queue at most once, and if it is |
| 666 | // present, should not be pending completion. |
| 667 | DCHECK(std::find(it, queue->end(), request) == queue->end()); |
| 668 | DCHECK(!ContainsKey(pending_stream_request_completions_, |
| 669 | request)); |
| 670 | return; |
| 671 | } |
| 672 | |
| 673 | pending_stream_request_completions_.erase(request); |
| 674 | } |
| 675 | |
| 676 | void SpdySession::ProcessPendingStreamRequests() { |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 677 | // Like |max_concurrent_streams_|, 0 means infinite for |
| 678 | // |max_requests_to_process|. |
| 679 | size_t max_requests_to_process = 0; |
| 680 | if (max_concurrent_streams_ != 0) { |
| 681 | max_requests_to_process = |
| 682 | max_concurrent_streams_ - |
| 683 | (active_streams_.size() + created_streams_.size()); |
| 684 | } |
| 685 | for (size_t i = 0; |
| 686 | max_requests_to_process == 0 || i < max_requests_to_process; ++i) { |
| 687 | bool processed_request = false; |
| 688 | for (int j = NUM_PRIORITIES - 1; j >= MINIMUM_PRIORITY; --j) { |
| 689 | if (pending_create_stream_queues_[j].empty()) |
| 690 | continue; |
| 691 | |
| 692 | SpdyStreamRequest* pending_request = |
| 693 | pending_create_stream_queues_[j].front(); |
| 694 | CHECK(pending_request); |
| 695 | pending_create_stream_queues_[j].pop_front(); |
| 696 | processed_request = true; |
| 697 | DCHECK(!ContainsKey(pending_stream_request_completions_, |
| 698 | pending_request)); |
| 699 | pending_stream_request_completions_.insert(pending_request); |
| 700 | base::MessageLoop::current()->PostTask( |
| 701 | FROM_HERE, |
| 702 | base::Bind(&SpdySession::CompleteStreamRequest, |
| 703 | weak_factory_.GetWeakPtr(), pending_request)); |
| 704 | break; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 705 | } |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 706 | if (!processed_request) |
| 707 | break; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 711 | bool SpdySession::NeedsCredentials() const { |
| 712 | if (!is_secure_) |
| 713 | return false; |
| 714 | SSLClientSocket* ssl_socket = GetSSLClientSocket(); |
| 715 | if (ssl_socket->GetNegotiatedProtocol() < kProtoSPDY3) |
| 716 | return false; |
| 717 | return ssl_socket->WasChannelIDSent(); |
| 718 | } |
| 719 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 720 | void SpdySession::AddPooledAlias(const SpdySessionKey& alias_key) { |
| 721 | pooled_aliases_.insert(alias_key); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | int SpdySession::GetProtocolVersion() const { |
| 725 | DCHECK(buffered_spdy_framer_.get()); |
| 726 | return buffered_spdy_framer_->protocol_version(); |
| 727 | } |
| 728 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 729 | base::WeakPtr<SpdySession> SpdySession::GetWeakPtr() { |
| 730 | return weak_factory_.GetWeakPtr(); |
| 731 | } |
| 732 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 733 | bool SpdySession::CloseOneIdleConnection() { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 734 | CHECK(!in_io_loop_); |
| 735 | DCHECK_NE(availability_state_, STATE_CLOSED); |
| 736 | DCHECK(pool_); |
| 737 | if (!active_streams_.empty()) |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 738 | return false; |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 739 | CloseSessionResult result = |
| 740 | DoCloseSession(ERR_CONNECTION_CLOSED, "Closing one idle connection."); |
| 741 | if (result != SESSION_CLOSED_AND_REMOVED) { |
| 742 | NOTREACHED(); |
| 743 | return false; |
| 744 | } |
| 745 | return true; |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 746 | } |
| 747 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 748 | void SpdySession::EnqueueStreamWrite( |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 749 | const base::WeakPtr<SpdyStream>& stream, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 750 | SpdyFrameType frame_type, |
| 751 | scoped_ptr<SpdyBufferProducer> producer) { |
| 752 | DCHECK(frame_type == HEADERS || |
| 753 | frame_type == DATA || |
| 754 | frame_type == CREDENTIAL || |
| 755 | frame_type == SYN_STREAM); |
| 756 | EnqueueWrite(stream->priority(), frame_type, producer.Pass(), stream); |
| 757 | } |
| 758 | |
| 759 | scoped_ptr<SpdyFrame> SpdySession::CreateSynStream( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 760 | SpdyStreamId stream_id, |
| 761 | RequestPriority priority, |
| 762 | uint8 credential_slot, |
| 763 | SpdyControlFlags flags, |
| 764 | const SpdyHeaderBlock& headers) { |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 765 | ActiveStreamMap::const_iterator it = active_streams_.find(stream_id); |
| 766 | CHECK(it != active_streams_.end()); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 767 | CHECK_EQ(it->second.stream->stream_id(), stream_id); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 768 | |
| 769 | SendPrefacePingIfNoneInFlight(); |
| 770 | |
| 771 | DCHECK(buffered_spdy_framer_.get()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 772 | scoped_ptr<SpdyFrame> syn_frame( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 773 | buffered_spdy_framer_->CreateSynStream( |
| 774 | stream_id, 0, |
| 775 | ConvertRequestPriorityToSpdyPriority(priority, GetProtocolVersion()), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 776 | credential_slot, flags, enable_compression_, &headers)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 777 | |
| 778 | base::StatsCounter spdy_requests("spdy.requests"); |
| 779 | spdy_requests.Increment(); |
| 780 | streams_initiated_count_++; |
| 781 | |
| 782 | if (net_log().IsLoggingAllEvents()) { |
| 783 | net_log().AddEvent( |
| 784 | NetLog::TYPE_SPDY_SESSION_SYN_STREAM, |
| 785 | base::Bind(&NetLogSpdySynCallback, &headers, |
| 786 | (flags & CONTROL_FLAG_FIN) != 0, |
| 787 | (flags & CONTROL_FLAG_UNIDIRECTIONAL) != 0, |
| 788 | stream_id, 0)); |
| 789 | } |
| 790 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 791 | return syn_frame.Pass(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 794 | int SpdySession::CreateCredentialFrame( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 795 | const std::string& origin, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 796 | const std::string& key, |
| 797 | const std::string& cert, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 798 | RequestPriority priority, |
| 799 | scoped_ptr<SpdyFrame>* credential_frame) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 800 | DCHECK(is_secure_); |
| 801 | SSLClientSocket* ssl_socket = GetSSLClientSocket(); |
| 802 | DCHECK(ssl_socket); |
| 803 | DCHECK(ssl_socket->WasChannelIDSent()); |
| 804 | |
| 805 | SpdyCredential credential; |
| 806 | std::string tls_unique; |
| 807 | ssl_socket->GetTLSUniqueChannelBinding(&tls_unique); |
| 808 | size_t slot = credential_state_.SetHasCredential(GURL(origin)); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 809 | int rv = SpdyCredentialBuilder::Build(tls_unique, key, cert, slot, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 810 | &credential); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 811 | DCHECK_NE(rv, ERR_IO_PENDING); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 812 | if (rv != OK) |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 813 | return rv; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 814 | |
| 815 | DCHECK(buffered_spdy_framer_.get()); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 816 | credential_frame->reset( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 817 | buffered_spdy_framer_->CreateCredentialFrame(credential)); |
| 818 | |
| 819 | if (net_log().IsLoggingAllEvents()) { |
| 820 | net_log().AddEvent( |
| 821 | NetLog::TYPE_SPDY_SESSION_SEND_CREDENTIAL, |
| 822 | base::Bind(&NetLogSpdyCredentialCallback, credential.slot, &origin)); |
| 823 | } |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 824 | return OK; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 827 | scoped_ptr<SpdyBuffer> SpdySession::CreateDataBuffer(SpdyStreamId stream_id, |
| 828 | IOBuffer* data, |
| 829 | int len, |
| 830 | SpdyDataFlags flags) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 831 | if (availability_state_ == STATE_CLOSED) { |
| 832 | NOTREACHED(); |
| 833 | return scoped_ptr<SpdyBuffer>(); |
| 834 | } |
| 835 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 836 | ActiveStreamMap::const_iterator it = active_streams_.find(stream_id); |
| 837 | CHECK(it != active_streams_.end()); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 838 | SpdyStream* stream = it->second.stream; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 839 | CHECK_EQ(stream->stream_id(), stream_id); |
| 840 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 841 | if (len < 0) { |
| 842 | NOTREACHED(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 843 | return scoped_ptr<SpdyBuffer>(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 846 | int effective_len = std::min(len, kMaxSpdyFrameChunkSize); |
| 847 | |
| 848 | bool send_stalled_by_stream = |
| 849 | (flow_control_state_ >= FLOW_CONTROL_STREAM) && |
| 850 | (stream->send_window_size() <= 0); |
| 851 | bool send_stalled_by_session = IsSendStalled(); |
| 852 | |
| 853 | // NOTE: There's an enum of the same name in histograms.xml. |
| 854 | enum SpdyFrameFlowControlState { |
| 855 | SEND_NOT_STALLED, |
| 856 | SEND_STALLED_BY_STREAM, |
| 857 | SEND_STALLED_BY_SESSION, |
| 858 | SEND_STALLED_BY_STREAM_AND_SESSION, |
| 859 | }; |
| 860 | |
| 861 | SpdyFrameFlowControlState frame_flow_control_state = SEND_NOT_STALLED; |
| 862 | if (send_stalled_by_stream) { |
| 863 | if (send_stalled_by_session) { |
| 864 | frame_flow_control_state = SEND_STALLED_BY_STREAM_AND_SESSION; |
| 865 | } else { |
| 866 | frame_flow_control_state = SEND_STALLED_BY_STREAM; |
| 867 | } |
| 868 | } else if (send_stalled_by_session) { |
| 869 | frame_flow_control_state = SEND_STALLED_BY_SESSION; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 872 | if (flow_control_state_ == FLOW_CONTROL_STREAM) { |
| 873 | UMA_HISTOGRAM_ENUMERATION( |
| 874 | "Net.SpdyFrameStreamFlowControlState", |
| 875 | frame_flow_control_state, |
| 876 | SEND_STALLED_BY_STREAM + 1); |
| 877 | } else if (flow_control_state_ == FLOW_CONTROL_STREAM_AND_SESSION) { |
| 878 | UMA_HISTOGRAM_ENUMERATION( |
| 879 | "Net.SpdyFrameStreamAndSessionFlowControlState", |
| 880 | frame_flow_control_state, |
| 881 | SEND_STALLED_BY_STREAM_AND_SESSION + 1); |
| 882 | } |
| 883 | |
| 884 | // Obey send window size of the stream if stream flow control is |
| 885 | // enabled. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 886 | if (flow_control_state_ >= FLOW_CONTROL_STREAM) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 887 | if (send_stalled_by_stream) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 888 | stream->set_send_stalled_by_flow_control(true); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 889 | // Even though we're currently stalled only by the stream, we |
| 890 | // might end up being stalled by the session also. |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 891 | QueueSendStalledStream(*stream); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 892 | net_log().AddEvent( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 893 | NetLog::TYPE_SPDY_SESSION_STREAM_STALLED_BY_STREAM_SEND_WINDOW, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 894 | NetLog::IntegerCallback("stream_id", stream_id)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 895 | return scoped_ptr<SpdyBuffer>(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 898 | effective_len = std::min(effective_len, stream->send_window_size()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 901 | // Obey send window size of the session if session flow control is |
| 902 | // enabled. |
| 903 | if (flow_control_state_ == FLOW_CONTROL_STREAM_AND_SESSION) { |
| 904 | if (send_stalled_by_session) { |
| 905 | stream->set_send_stalled_by_flow_control(true); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 906 | QueueSendStalledStream(*stream); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 907 | net_log().AddEvent( |
| 908 | NetLog::TYPE_SPDY_SESSION_STREAM_STALLED_BY_SESSION_SEND_WINDOW, |
| 909 | NetLog::IntegerCallback("stream_id", stream_id)); |
| 910 | return scoped_ptr<SpdyBuffer>(); |
| 911 | } |
| 912 | |
| 913 | effective_len = std::min(effective_len, session_send_window_size_); |
| 914 | } |
| 915 | |
| 916 | DCHECK_GE(effective_len, 0); |
| 917 | |
| 918 | // Clear FIN flag if only some of the data will be in the data |
| 919 | // frame. |
| 920 | if (effective_len < len) |
| 921 | flags = static_cast<SpdyDataFlags>(flags & ~DATA_FLAG_FIN); |
| 922 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 923 | if (net_log().IsLoggingAllEvents()) { |
| 924 | net_log().AddEvent( |
| 925 | NetLog::TYPE_SPDY_SESSION_SEND_DATA, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 926 | base::Bind(&NetLogSpdyDataCallback, stream_id, effective_len, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 927 | (flags & DATA_FLAG_FIN) != 0)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | // Send PrefacePing for DATA_FRAMEs with nonzero payload size. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 931 | if (effective_len > 0) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 932 | SendPrefacePingIfNoneInFlight(); |
| 933 | |
| 934 | // TODO(mbelshe): reduce memory copies here. |
| 935 | DCHECK(buffered_spdy_framer_.get()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 936 | scoped_ptr<SpdyFrame> frame( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 937 | buffered_spdy_framer_->CreateDataFrame( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 938 | stream_id, data->data(), |
| 939 | static_cast<uint32>(effective_len), flags)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 940 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 941 | scoped_ptr<SpdyBuffer> data_buffer(new SpdyBuffer(frame.Pass())); |
| 942 | |
| 943 | if (flow_control_state_ == FLOW_CONTROL_STREAM_AND_SESSION) { |
| 944 | DecreaseSendWindowSize(static_cast<int32>(effective_len)); |
| 945 | data_buffer->AddConsumeCallback( |
| 946 | base::Bind(&SpdySession::OnWriteBufferConsumed, |
| 947 | weak_factory_.GetWeakPtr(), |
| 948 | static_cast<size_t>(effective_len))); |
| 949 | } |
| 950 | |
| 951 | return data_buffer.Pass(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 954 | void SpdySession::CloseActiveStream(SpdyStreamId stream_id, int status) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 955 | DCHECK_NE(stream_id, 0u); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 956 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 957 | ActiveStreamMap::iterator it = active_streams_.find(stream_id); |
| 958 | if (it == active_streams_.end()) { |
| 959 | NOTREACHED(); |
| 960 | return; |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 961 | } |
| 962 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 963 | CloseActiveStreamIterator(it, status); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 966 | void SpdySession::CloseCreatedStream( |
| 967 | const base::WeakPtr<SpdyStream>& stream, int status) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 968 | DCHECK_EQ(stream->stream_id(), 0u); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 969 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 970 | CreatedStreamSet::iterator it = created_streams_.find(stream.get()); |
| 971 | if (it == created_streams_.end()) { |
| 972 | NOTREACHED(); |
| 973 | return; |
| 974 | } |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 975 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 976 | CloseCreatedStreamIterator(it, status); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | void SpdySession::ResetStream(SpdyStreamId stream_id, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 980 | SpdyRstStreamStatus status, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 981 | const std::string& description) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 982 | DCHECK_NE(stream_id, 0u); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 983 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 984 | ActiveStreamMap::iterator it = active_streams_.find(stream_id); |
| 985 | if (it == active_streams_.end()) { |
| 986 | NOTREACHED(); |
| 987 | return; |
| 988 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 989 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 990 | ResetStreamIterator(it, status, description); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | bool SpdySession::IsStreamActive(SpdyStreamId stream_id) const { |
| 994 | return ContainsKey(active_streams_, stream_id); |
| 995 | } |
| 996 | |
| 997 | LoadState SpdySession::GetLoadState() const { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 998 | // Just report that we're idle since the session could be doing |
| 999 | // many things concurrently. |
| 1000 | return LOAD_STATE_IDLE; |
| 1001 | } |
| 1002 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1003 | void SpdySession::CloseActiveStreamIterator(ActiveStreamMap::iterator it, |
| 1004 | int status) { |
| 1005 | // TODO(mbelshe): We should send a RST_STREAM control frame here |
| 1006 | // so that the server can cancel a large send. |
| 1007 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1008 | scoped_ptr<SpdyStream> owned_stream(it->second.stream); |
| 1009 | active_streams_.erase(it); |
| 1010 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1011 | // TODO(akalin): When SpdyStream was ref-counted (and |
| 1012 | // |unclaimed_pushed_streams_| held scoped_refptr<SpdyStream>), this |
| 1013 | // was only done when status was not OK. This meant that pushed |
| 1014 | // streams can still be claimed after they're closed. This is |
| 1015 | // probably something that we still want to support, although server |
| 1016 | // push is hardly used. Write tests for this and fix this. (See |
| 1017 | // http://crbug.com/261712 .) |
| 1018 | if (owned_stream->type() == SPDY_PUSH_STREAM) |
| 1019 | unclaimed_pushed_streams_.erase(owned_stream->url()); |
| 1020 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1021 | DeleteStream(owned_stream.Pass(), status); |
| 1022 | } |
| 1023 | |
| 1024 | void SpdySession::CloseCreatedStreamIterator(CreatedStreamSet::iterator it, |
| 1025 | int status) { |
| 1026 | scoped_ptr<SpdyStream> owned_stream(*it); |
| 1027 | created_streams_.erase(it); |
| 1028 | DeleteStream(owned_stream.Pass(), status); |
| 1029 | } |
| 1030 | |
| 1031 | void SpdySession::ResetStreamIterator(ActiveStreamMap::iterator it, |
| 1032 | SpdyRstStreamStatus status, |
| 1033 | const std::string& description) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 1034 | // Send the RST_STREAM frame first as CloseActiveStreamIterator() |
| 1035 | // may close us. |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1036 | SpdyStreamId stream_id = it->first; |
| 1037 | RequestPriority priority = it->second.stream->priority(); |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 1038 | EnqueueResetStreamFrame(stream_id, priority, status, description); |
| 1039 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1040 | // Removes any pending writes for the stream except for possibly an |
| 1041 | // in-flight one. |
| 1042 | CloseActiveStreamIterator(it, ERR_SPDY_PROTOCOL_ERROR); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1043 | } |
| 1044 | |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 1045 | void SpdySession::EnqueueResetStreamFrame(SpdyStreamId stream_id, |
| 1046 | RequestPriority priority, |
| 1047 | SpdyRstStreamStatus status, |
| 1048 | const std::string& description) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1049 | DCHECK_NE(stream_id, 0u); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1050 | |
| 1051 | net_log().AddEvent( |
| 1052 | NetLog::TYPE_SPDY_SESSION_SEND_RST_STREAM, |
| 1053 | base::Bind(&NetLogSpdyRstCallback, stream_id, status, &description)); |
| 1054 | |
| 1055 | DCHECK(buffered_spdy_framer_.get()); |
| 1056 | scoped_ptr<SpdyFrame> rst_frame( |
| 1057 | buffered_spdy_framer_->CreateRstStream(stream_id, status)); |
| 1058 | |
| 1059 | EnqueueSessionWrite(priority, RST_STREAM, rst_frame.Pass()); |
| 1060 | RecordProtocolErrorHistogram( |
| 1061 | static_cast<SpdyProtocolErrorDetails>(status + STATUS_CODE_INVALID)); |
| 1062 | } |
| 1063 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1064 | void SpdySession::PumpReadLoop(ReadState expected_read_state, int result) { |
| 1065 | CHECK(!in_io_loop_); |
| 1066 | DCHECK_NE(availability_state_, STATE_CLOSED); |
| 1067 | DCHECK_EQ(read_state_, expected_read_state); |
| 1068 | |
| 1069 | result = DoReadLoop(expected_read_state, result); |
| 1070 | |
| 1071 | if (availability_state_ == STATE_CLOSED) { |
| 1072 | DCHECK_EQ(result, error_on_close_); |
| 1073 | DCHECK_LT(error_on_close_, ERR_IO_PENDING); |
| 1074 | RemoveFromPool(); |
| 1075 | return; |
| 1076 | } |
| 1077 | |
| 1078 | DCHECK(result == OK || result == ERR_IO_PENDING); |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 1079 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1080 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1081 | int SpdySession::DoReadLoop(ReadState expected_read_state, int result) { |
| 1082 | CHECK(!in_io_loop_); |
| 1083 | DCHECK_NE(availability_state_, STATE_CLOSED); |
| 1084 | DCHECK_EQ(read_state_, expected_read_state); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1085 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1086 | in_io_loop_ = true; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1087 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1088 | int bytes_read_without_yielding = 0; |
| 1089 | |
| 1090 | // Loop until the session is closed, the read becomes blocked, or |
| 1091 | // the read limit is exceeded. |
| 1092 | while (true) { |
| 1093 | switch (read_state_) { |
| 1094 | case READ_STATE_DO_READ: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1095 | DCHECK_EQ(result, OK); |
| 1096 | result = DoRead(); |
| 1097 | break; |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1098 | case READ_STATE_DO_READ_COMPLETE: |
| 1099 | if (result > 0) |
| 1100 | bytes_read_without_yielding += result; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1101 | result = DoReadComplete(result); |
| 1102 | break; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1103 | default: |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1104 | NOTREACHED() << "read_state_: " << read_state_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1105 | break; |
| 1106 | } |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1107 | |
| 1108 | if (availability_state_ == STATE_CLOSED) { |
| 1109 | DCHECK_EQ(result, error_on_close_); |
| 1110 | DCHECK_LT(result, ERR_IO_PENDING); |
| 1111 | break; |
| 1112 | } |
| 1113 | |
| 1114 | if (result == ERR_IO_PENDING) |
| 1115 | break; |
| 1116 | |
| 1117 | if (bytes_read_without_yielding > kMaxReadBytesWithoutYielding) { |
| 1118 | read_state_ = READ_STATE_DO_READ; |
| 1119 | base::MessageLoop::current()->PostTask( |
| 1120 | FROM_HERE, |
| 1121 | base::Bind(&SpdySession::PumpReadLoop, |
| 1122 | weak_factory_.GetWeakPtr(), READ_STATE_DO_READ, OK)); |
| 1123 | result = ERR_IO_PENDING; |
| 1124 | break; |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | CHECK(in_io_loop_); |
| 1129 | in_io_loop_ = false; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1130 | |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 1131 | return result; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | int SpdySession::DoRead() { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1135 | CHECK(in_io_loop_); |
| 1136 | DCHECK_NE(availability_state_, STATE_CLOSED); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1137 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 1138 | CHECK(connection_); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1139 | CHECK(connection_->socket()); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1140 | read_state_ = READ_STATE_DO_READ_COMPLETE; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1141 | return connection_->socket()->Read( |
| 1142 | read_buffer_.get(), |
| 1143 | kReadBufferSize, |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1144 | base::Bind(&SpdySession::PumpReadLoop, |
| 1145 | weak_factory_.GetWeakPtr(), READ_STATE_DO_READ_COMPLETE)); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | int SpdySession::DoReadComplete(int result) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1149 | CHECK(in_io_loop_); |
| 1150 | DCHECK_NE(availability_state_, STATE_CLOSED); |
| 1151 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1152 | // Parse a frame. For now this code requires that the frame fit into our |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1153 | // buffer (kReadBufferSize). |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1154 | // TODO(mbelshe): support arbitrarily large frames! |
| 1155 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1156 | if (result == 0) { |
| 1157 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdySession.BytesRead.EOF", |
| 1158 | total_bytes_received_, 1, 100000000, 50); |
| 1159 | CloseSessionResult close_session_result = |
| 1160 | DoCloseSession(ERR_CONNECTION_CLOSED, "Connection closed"); |
| 1161 | DCHECK_EQ(close_session_result, SESSION_CLOSED_BUT_NOT_REMOVED); |
| 1162 | DCHECK_EQ(availability_state_, STATE_CLOSED); |
| 1163 | DCHECK_EQ(error_on_close_, ERR_CONNECTION_CLOSED); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1164 | return ERR_CONNECTION_CLOSED; |
| 1165 | } |
| 1166 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1167 | if (result < 0) { |
| 1168 | CloseSessionResult close_session_result = |
| 1169 | DoCloseSession(static_cast<Error>(result), "result is < 0."); |
| 1170 | DCHECK_EQ(close_session_result, SESSION_CLOSED_BUT_NOT_REMOVED); |
| 1171 | DCHECK_EQ(availability_state_, STATE_CLOSED); |
| 1172 | DCHECK_EQ(error_on_close_, result); |
| 1173 | return result; |
| 1174 | } |
| 1175 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1176 | total_bytes_received_ += result; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1177 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1178 | last_activity_time_ = time_func_(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1179 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1180 | DCHECK(buffered_spdy_framer_.get()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1181 | char* data = read_buffer_->data(); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1182 | while (result > 0) { |
| 1183 | uint32 bytes_processed = buffered_spdy_framer_->ProcessInput(data, result); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1184 | result -= bytes_processed; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1185 | data += bytes_processed; |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1186 | |
| 1187 | if (availability_state_ == STATE_CLOSED) { |
| 1188 | DCHECK_LT(error_on_close_, ERR_IO_PENDING); |
| 1189 | return error_on_close_; |
| 1190 | } |
| 1191 | |
| 1192 | DCHECK_EQ(buffered_spdy_framer_->error_code(), SpdyFramer::SPDY_NO_ERROR); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1195 | read_state_ = READ_STATE_DO_READ; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1196 | return OK; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1199 | void SpdySession::PumpWriteLoop(WriteState expected_write_state, int result) { |
| 1200 | CHECK(!in_io_loop_); |
| 1201 | DCHECK_NE(availability_state_, STATE_CLOSED); |
| 1202 | DCHECK_EQ(write_state_, expected_write_state); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1203 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1204 | result = DoWriteLoop(expected_write_state, result); |
| 1205 | |
| 1206 | if (availability_state_ == STATE_CLOSED) { |
| 1207 | DCHECK_EQ(result, error_on_close_); |
| 1208 | DCHECK_LT(error_on_close_, ERR_IO_PENDING); |
| 1209 | RemoveFromPool(); |
| 1210 | return; |
| 1211 | } |
| 1212 | |
| 1213 | DCHECK(result == OK || result == ERR_IO_PENDING); |
| 1214 | } |
| 1215 | |
| 1216 | int SpdySession::DoWriteLoop(WriteState expected_write_state, int result) { |
| 1217 | CHECK(!in_io_loop_); |
| 1218 | DCHECK_NE(availability_state_, STATE_CLOSED); |
| 1219 | DCHECK_NE(write_state_, WRITE_STATE_IDLE); |
| 1220 | DCHECK_EQ(write_state_, expected_write_state); |
| 1221 | |
| 1222 | in_io_loop_ = true; |
| 1223 | |
| 1224 | // Loop until the session is closed or the write becomes blocked. |
| 1225 | while (true) { |
| 1226 | switch (write_state_) { |
| 1227 | case WRITE_STATE_DO_WRITE: |
| 1228 | DCHECK_EQ(result, OK); |
| 1229 | result = DoWrite(); |
| 1230 | break; |
| 1231 | case WRITE_STATE_DO_WRITE_COMPLETE: |
| 1232 | result = DoWriteComplete(result); |
| 1233 | break; |
| 1234 | case WRITE_STATE_IDLE: |
| 1235 | default: |
| 1236 | NOTREACHED() << "write_state_: " << write_state_; |
| 1237 | break; |
| 1238 | } |
| 1239 | |
| 1240 | if (availability_state_ == STATE_CLOSED) { |
| 1241 | DCHECK_EQ(result, error_on_close_); |
| 1242 | DCHECK_LT(result, ERR_IO_PENDING); |
| 1243 | break; |
| 1244 | } |
| 1245 | |
| 1246 | if (write_state_ == WRITE_STATE_IDLE) { |
| 1247 | DCHECK_EQ(result, ERR_IO_PENDING); |
| 1248 | break; |
| 1249 | } |
| 1250 | |
| 1251 | if (result == ERR_IO_PENDING) |
| 1252 | break; |
| 1253 | } |
| 1254 | |
| 1255 | CHECK(in_io_loop_); |
| 1256 | in_io_loop_ = false; |
| 1257 | |
| 1258 | return result; |
| 1259 | } |
| 1260 | |
| 1261 | int SpdySession::DoWrite() { |
| 1262 | CHECK(in_io_loop_); |
| 1263 | DCHECK_NE(availability_state_, STATE_CLOSED); |
| 1264 | |
| 1265 | DCHECK(buffered_spdy_framer_); |
| 1266 | if (in_flight_write_) { |
| 1267 | DCHECK_GT(in_flight_write_->GetRemainingSize(), 0u); |
| 1268 | } else { |
| 1269 | // Grab the next frame to send. |
| 1270 | SpdyFrameType frame_type = DATA; |
| 1271 | scoped_ptr<SpdyBufferProducer> producer; |
| 1272 | base::WeakPtr<SpdyStream> stream; |
| 1273 | if (!write_queue_.Dequeue(&frame_type, &producer, &stream)) { |
| 1274 | write_state_ = WRITE_STATE_IDLE; |
| 1275 | return ERR_IO_PENDING; |
| 1276 | } |
| 1277 | |
| 1278 | if (stream.get()) |
| 1279 | DCHECK(!stream->IsClosed()); |
| 1280 | |
| 1281 | // Activate the stream only when sending the SYN_STREAM frame to |
| 1282 | // guarantee monotonically-increasing stream IDs. |
| 1283 | if (frame_type == SYN_STREAM) { |
| 1284 | if (stream.get() && stream->stream_id() == 0) { |
| 1285 | scoped_ptr<SpdyStream> owned_stream = |
| 1286 | ActivateCreatedStream(stream.get()); |
| 1287 | InsertActivatedStream(owned_stream.Pass()); |
| 1288 | } else { |
| 1289 | NOTREACHED(); |
| 1290 | return ERR_UNEXPECTED; |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | in_flight_write_ = producer->ProduceBuffer(); |
| 1295 | if (!in_flight_write_) { |
| 1296 | NOTREACHED(); |
| 1297 | return ERR_UNEXPECTED; |
| 1298 | } |
| 1299 | in_flight_write_frame_type_ = frame_type; |
| 1300 | in_flight_write_frame_size_ = in_flight_write_->GetRemainingSize(); |
| 1301 | DCHECK_GE(in_flight_write_frame_size_, |
| 1302 | buffered_spdy_framer_->GetFrameMinimumSize()); |
| 1303 | in_flight_write_stream_ = stream; |
| 1304 | } |
| 1305 | |
| 1306 | write_state_ = WRITE_STATE_DO_WRITE_COMPLETE; |
| 1307 | |
| 1308 | // Explicitly store in a scoped_refptr<IOBuffer> to avoid problems |
| 1309 | // with Socket implementations that don't store their IOBuffer |
| 1310 | // argument in a scoped_refptr<IOBuffer> (see crbug.com/232345). |
| 1311 | scoped_refptr<IOBuffer> write_io_buffer = |
| 1312 | in_flight_write_->GetIOBufferForRemainingData(); |
| 1313 | return connection_->socket()->Write( |
| 1314 | write_io_buffer.get(), |
| 1315 | in_flight_write_->GetRemainingSize(), |
| 1316 | base::Bind(&SpdySession::PumpWriteLoop, |
| 1317 | weak_factory_.GetWeakPtr(), WRITE_STATE_DO_WRITE_COMPLETE)); |
| 1318 | } |
| 1319 | |
| 1320 | int SpdySession::DoWriteComplete(int result) { |
| 1321 | CHECK(in_io_loop_); |
| 1322 | DCHECK_NE(availability_state_, STATE_CLOSED); |
| 1323 | DCHECK_NE(result, ERR_IO_PENDING); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1324 | DCHECK_GT(in_flight_write_->GetRemainingSize(), 0u); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1325 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1326 | last_activity_time_ = time_func_(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1327 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1328 | if (result < 0) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1329 | DCHECK_NE(result, ERR_IO_PENDING); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1330 | in_flight_write_.reset(); |
| 1331 | in_flight_write_frame_type_ = DATA; |
| 1332 | in_flight_write_frame_size_ = 0; |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 1333 | in_flight_write_stream_.reset(); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1334 | CloseSessionResult close_session_result = |
| 1335 | DoCloseSession(static_cast<Error>(result), "Write error"); |
| 1336 | DCHECK_EQ(close_session_result, SESSION_CLOSED_BUT_NOT_REMOVED); |
| 1337 | DCHECK_EQ(availability_state_, STATE_CLOSED); |
| 1338 | DCHECK_EQ(error_on_close_, result); |
| 1339 | return result; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1340 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1341 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1342 | // It should not be possible to have written more bytes than our |
| 1343 | // in_flight_write_. |
| 1344 | DCHECK_LE(static_cast<size_t>(result), |
| 1345 | in_flight_write_->GetRemainingSize()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1346 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1347 | if (result > 0) { |
| 1348 | in_flight_write_->Consume(static_cast<size_t>(result)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1349 | |
| 1350 | // We only notify the stream when we've fully written the pending frame. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1351 | if (in_flight_write_->GetRemainingSize() == 0) { |
| 1352 | // It is possible that the stream was cancelled while we were |
| 1353 | // writing to the socket. |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 1354 | if (in_flight_write_stream_.get()) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1355 | DCHECK_GT(in_flight_write_frame_size_, 0u); |
| 1356 | in_flight_write_stream_->OnFrameWriteComplete( |
| 1357 | in_flight_write_frame_type_, |
| 1358 | in_flight_write_frame_size_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | // Cleanup the write which just completed. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1362 | in_flight_write_.reset(); |
| 1363 | in_flight_write_frame_type_ = DATA; |
| 1364 | in_flight_write_frame_size_ = 0; |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 1365 | in_flight_write_stream_.reset(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1366 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1367 | } |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1368 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1369 | write_state_ = WRITE_STATE_DO_WRITE; |
| 1370 | return OK; |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 1371 | } |
| 1372 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1373 | void SpdySession::DcheckGoingAway() const { |
| 1374 | DCHECK_GE(availability_state_, STATE_GOING_AWAY); |
| 1375 | if (DCHECK_IS_ON()) { |
| 1376 | for (int i = 0; i < NUM_PRIORITIES; ++i) { |
| 1377 | DCHECK(pending_create_stream_queues_[i].empty()); |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 1378 | } |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 1379 | } |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1380 | DCHECK(pending_stream_request_completions_.empty()); |
| 1381 | DCHECK(created_streams_.empty()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1384 | void SpdySession::DcheckClosed() const { |
| 1385 | DcheckGoingAway(); |
| 1386 | DCHECK_EQ(availability_state_, STATE_CLOSED); |
| 1387 | DCHECK_LT(error_on_close_, ERR_IO_PENDING); |
| 1388 | DCHECK(active_streams_.empty()); |
| 1389 | DCHECK(unclaimed_pushed_streams_.empty()); |
| 1390 | DCHECK(write_queue_.IsEmpty()); |
| 1391 | } |
| 1392 | |
| 1393 | void SpdySession::StartGoingAway(SpdyStreamId last_good_stream_id, |
| 1394 | Error status) { |
| 1395 | DCHECK_GE(availability_state_, STATE_GOING_AWAY); |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 1396 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1397 | // The loops below are carefully written to avoid reentrancy problems. |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 1398 | // |
| 1399 | // TODO(akalin): Any of the functions below can cause |this| to be |
| 1400 | // deleted, so handle that below (and add tests for it). |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1401 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1402 | for (int i = 0; i < NUM_PRIORITIES; ++i) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1403 | PendingStreamRequestQueue queue; |
| 1404 | queue.swap(pending_create_stream_queues_[i]); |
| 1405 | for (PendingStreamRequestQueue::const_iterator it = queue.begin(); |
| 1406 | it != queue.end(); ++it) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 1407 | CHECK(*it); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 1408 | (*it)->OnRequestCompleteFailure(ERR_ABORTED); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1409 | } |
| 1410 | } |
| 1411 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1412 | PendingStreamRequestCompletionSet pending_completions; |
| 1413 | pending_completions.swap(pending_stream_request_completions_); |
| 1414 | for (PendingStreamRequestCompletionSet::const_iterator it = |
| 1415 | pending_completions.begin(); |
| 1416 | it != pending_completions.end(); ++it) { |
| 1417 | (*it)->OnRequestCompleteFailure(ERR_ABORTED); |
| 1418 | } |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 1419 | |
| 1420 | while (true) { |
| 1421 | ActiveStreamMap::iterator it = |
| 1422 | active_streams_.lower_bound(last_good_stream_id + 1); |
| 1423 | if (it == active_streams_.end()) |
| 1424 | break; |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1425 | LogAbandonedActiveStream(it, status); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1426 | CloseActiveStreamIterator(it, status); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | while (!created_streams_.empty()) { |
| 1430 | CreatedStreamSet::iterator it = created_streams_.begin(); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 1431 | LogAbandonedStream(*it, status); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1432 | CloseCreatedStreamIterator(it, status); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1435 | write_queue_.RemovePendingWritesForStreamsAfter(last_good_stream_id); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1436 | |
| 1437 | DcheckGoingAway(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1438 | } |
| 1439 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1440 | void SpdySession::MaybeFinishGoingAway() { |
| 1441 | DcheckGoingAway(); |
| 1442 | if (active_streams_.empty() && availability_state_ != STATE_CLOSED) { |
| 1443 | CloseSessionResult result = |
| 1444 | DoCloseSession(ERR_CONNECTION_CLOSED, "Finished going away"); |
| 1445 | DCHECK_NE(result, SESSION_ALREADY_CLOSED); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1446 | } |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1447 | } |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1448 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1449 | SpdySession::CloseSessionResult SpdySession::DoCloseSession( |
| 1450 | Error err, |
| 1451 | const std::string& description) { |
| 1452 | DCHECK_LT(err, ERR_IO_PENDING); |
| 1453 | |
| 1454 | if (availability_state_ == STATE_CLOSED) |
| 1455 | return SESSION_ALREADY_CLOSED; |
| 1456 | |
| 1457 | net_log_.AddEvent( |
| 1458 | NetLog::TYPE_SPDY_SESSION_CLOSE, |
| 1459 | base::Bind(&NetLogSpdySessionCloseCallback, err, &description)); |
| 1460 | |
| 1461 | UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SpdySession.ClosedOnError", -err); |
| 1462 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdySession.BytesRead.OtherErrors", |
| 1463 | total_bytes_received_, 1, 100000000, 50); |
| 1464 | |
| 1465 | // |pool_| will be NULL when |InitializeWithSocket()| is in the |
| 1466 | // call stack. |
| 1467 | if (pool_ && availability_state_ != STATE_GOING_AWAY) |
| 1468 | pool_->MakeSessionUnavailable(GetWeakPtr()); |
| 1469 | |
| 1470 | availability_state_ = STATE_CLOSED; |
| 1471 | error_on_close_ = err; |
| 1472 | |
| 1473 | StartGoingAway(0, err); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1474 | write_queue_.Clear(); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1475 | |
| 1476 | DcheckClosed(); |
| 1477 | |
| 1478 | if (in_io_loop_) |
| 1479 | return SESSION_CLOSED_BUT_NOT_REMOVED; |
| 1480 | |
| 1481 | RemoveFromPool(); |
| 1482 | return SESSION_CLOSED_AND_REMOVED; |
| 1483 | } |
| 1484 | |
| 1485 | void SpdySession::RemoveFromPool() { |
| 1486 | DcheckClosed(); |
| 1487 | CHECK(pool_); |
| 1488 | |
| 1489 | SpdySessionPool* pool = pool_; |
| 1490 | pool_ = NULL; |
| 1491 | pool->RemoveUnavailableSession(GetWeakPtr()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 1494 | void SpdySession::LogAbandonedStream(SpdyStream* stream, Error status) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1495 | DCHECK(stream); |
| 1496 | std::string description = base::StringPrintf( |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1497 | "ABANDONED (stream_id=%d): ", stream->stream_id()) + |
| 1498 | stream->url().spec(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1499 | stream->LogStreamError(status, description); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1500 | // We don't increment the streams abandoned counter here. If the |
| 1501 | // stream isn't active (i.e., it hasn't written anything to the wire |
| 1502 | // yet) then it's as if it never existed. If it is active, then |
| 1503 | // LogAbandonedActiveStream() will increment the counters. |
| 1504 | } |
| 1505 | |
| 1506 | void SpdySession::LogAbandonedActiveStream(ActiveStreamMap::const_iterator it, |
| 1507 | Error status) { |
| 1508 | DCHECK_GT(it->first, 0u); |
| 1509 | LogAbandonedStream(it->second.stream, status); |
| 1510 | ++streams_abandoned_count_; |
| 1511 | base::StatsCounter abandoned_streams("spdy.abandoned_streams"); |
| 1512 | abandoned_streams.Increment(); |
| 1513 | if (it->second.stream->type() == SPDY_PUSH_STREAM && |
| 1514 | unclaimed_pushed_streams_.find(it->second.stream->url()) != |
| 1515 | unclaimed_pushed_streams_.end()) { |
| 1516 | base::StatsCounter abandoned_push_streams("spdy.abandoned_push_streams"); |
| 1517 | abandoned_push_streams.Increment(); |
| 1518 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | int SpdySession::GetNewStreamId() { |
| 1522 | int id = stream_hi_water_mark_; |
| 1523 | stream_hi_water_mark_ += 2; |
| 1524 | if (stream_hi_water_mark_ > 0x7fff) |
| 1525 | stream_hi_water_mark_ = 1; |
| 1526 | return id; |
| 1527 | } |
| 1528 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1529 | void SpdySession::CloseSessionOnError(Error err, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1530 | const std::string& description) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1531 | // We may be called from anywhere, so we can't expect a particular |
| 1532 | // return value. |
| 1533 | ignore_result(DoCloseSession(err, description)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1536 | base::Value* SpdySession::GetInfoAsValue() const { |
| 1537 | base::DictionaryValue* dict = new base::DictionaryValue(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1538 | |
| 1539 | dict->SetInteger("source_id", net_log_.source().id); |
| 1540 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 1541 | dict->SetString("host_port_pair", host_port_pair().ToString()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1542 | if (!pooled_aliases_.empty()) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1543 | base::ListValue* alias_list = new base::ListValue(); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 1544 | for (std::set<SpdySessionKey>::const_iterator it = |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1545 | pooled_aliases_.begin(); |
| 1546 | it != pooled_aliases_.end(); it++) { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 1547 | alias_list->Append(new base::StringValue( |
| 1548 | it->host_port_pair().ToString())); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1549 | } |
| 1550 | dict->Set("aliases", alias_list); |
| 1551 | } |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 1552 | dict->SetString("proxy", host_port_proxy_pair().second.ToURI()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1553 | |
| 1554 | dict->SetInteger("active_streams", active_streams_.size()); |
| 1555 | |
| 1556 | dict->SetInteger("unclaimed_pushed_streams", |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1557 | unclaimed_pushed_streams_.size()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1558 | |
| 1559 | dict->SetBoolean("is_secure", is_secure_); |
| 1560 | |
| 1561 | dict->SetString("protocol_negotiated", |
| 1562 | SSLClientSocket::NextProtoToString( |
| 1563 | connection_->socket()->GetNegotiatedProtocol())); |
| 1564 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1565 | dict->SetInteger("error", error_on_close_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1566 | dict->SetInteger("max_concurrent_streams", max_concurrent_streams_); |
| 1567 | |
| 1568 | dict->SetInteger("streams_initiated_count", streams_initiated_count_); |
| 1569 | dict->SetInteger("streams_pushed_count", streams_pushed_count_); |
| 1570 | dict->SetInteger("streams_pushed_and_claimed_count", |
| 1571 | streams_pushed_and_claimed_count_); |
| 1572 | dict->SetInteger("streams_abandoned_count", streams_abandoned_count_); |
| 1573 | DCHECK(buffered_spdy_framer_.get()); |
| 1574 | dict->SetInteger("frames_received", buffered_spdy_framer_->frames_received()); |
| 1575 | |
| 1576 | dict->SetBoolean("sent_settings", sent_settings_); |
| 1577 | dict->SetBoolean("received_settings", received_settings_); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1578 | |
| 1579 | dict->SetInteger("send_window_size", session_send_window_size_); |
| 1580 | dict->SetInteger("recv_window_size", session_recv_window_size_); |
| 1581 | dict->SetInteger("unacked_recv_window_bytes", |
| 1582 | session_unacked_recv_window_bytes_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1583 | return dict; |
| 1584 | } |
| 1585 | |
| 1586 | bool SpdySession::IsReused() const { |
| 1587 | return buffered_spdy_framer_->frames_received() > 0; |
| 1588 | } |
| 1589 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1590 | bool SpdySession::GetLoadTimingInfo(SpdyStreamId stream_id, |
| 1591 | LoadTimingInfo* load_timing_info) const { |
| 1592 | return connection_->GetLoadTimingInfo(stream_id != kFirstStreamId, |
| 1593 | load_timing_info); |
| 1594 | } |
| 1595 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1596 | int SpdySession::GetPeerAddress(IPEndPoint* address) const { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1597 | int rv = ERR_SOCKET_NOT_CONNECTED; |
| 1598 | if (connection_->socket()) { |
| 1599 | rv = connection_->socket()->GetPeerAddress(address); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1600 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1601 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1602 | UMA_HISTOGRAM_BOOLEAN("Net.SpdySessionSocketNotConnectedGetPeerAddress", |
| 1603 | rv == ERR_SOCKET_NOT_CONNECTED); |
| 1604 | |
| 1605 | return rv; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | int SpdySession::GetLocalAddress(IPEndPoint* address) const { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1609 | int rv = ERR_SOCKET_NOT_CONNECTED; |
| 1610 | if (connection_->socket()) { |
| 1611 | rv = connection_->socket()->GetLocalAddress(address); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1612 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1613 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1614 | UMA_HISTOGRAM_BOOLEAN("Net.SpdySessionSocketNotConnectedGetLocalAddress", |
| 1615 | rv == ERR_SOCKET_NOT_CONNECTED); |
| 1616 | |
| 1617 | return rv; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1620 | void SpdySession::EnqueueSessionWrite(RequestPriority priority, |
| 1621 | SpdyFrameType frame_type, |
| 1622 | scoped_ptr<SpdyFrame> frame) { |
| 1623 | DCHECK(frame_type == RST_STREAM || |
| 1624 | frame_type == SETTINGS || |
| 1625 | frame_type == WINDOW_UPDATE || |
| 1626 | frame_type == PING); |
| 1627 | EnqueueWrite( |
| 1628 | priority, frame_type, |
| 1629 | scoped_ptr<SpdyBufferProducer>( |
| 1630 | new SimpleBufferProducer( |
| 1631 | scoped_ptr<SpdyBuffer>(new SpdyBuffer(frame.Pass())))), |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 1632 | base::WeakPtr<SpdyStream>()); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1633 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1634 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1635 | void SpdySession::EnqueueWrite(RequestPriority priority, |
| 1636 | SpdyFrameType frame_type, |
| 1637 | scoped_ptr<SpdyBufferProducer> producer, |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 1638 | const base::WeakPtr<SpdyStream>& stream) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1639 | if (availability_state_ == STATE_CLOSED) |
| 1640 | return; |
| 1641 | |
| 1642 | bool was_idle = write_queue_.IsEmpty(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1643 | write_queue_.Enqueue(priority, frame_type, producer.Pass(), stream); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1644 | if (write_state_ == WRITE_STATE_IDLE) { |
| 1645 | DCHECK(was_idle); |
| 1646 | DCHECK(!in_flight_write_); |
| 1647 | write_state_ = WRITE_STATE_DO_WRITE; |
| 1648 | base::MessageLoop::current()->PostTask( |
| 1649 | FROM_HERE, |
| 1650 | base::Bind(&SpdySession::PumpWriteLoop, |
| 1651 | weak_factory_.GetWeakPtr(), WRITE_STATE_DO_WRITE, OK)); |
| 1652 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 1655 | void SpdySession::InsertCreatedStream(scoped_ptr<SpdyStream> stream) { |
| 1656 | DCHECK_EQ(stream->stream_id(), 0u); |
| 1657 | DCHECK(created_streams_.find(stream.get()) == created_streams_.end()); |
| 1658 | created_streams_.insert(stream.release()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 1661 | scoped_ptr<SpdyStream> SpdySession::ActivateCreatedStream(SpdyStream* stream) { |
| 1662 | DCHECK_EQ(stream->stream_id(), 0u); |
| 1663 | DCHECK(created_streams_.find(stream) != created_streams_.end()); |
| 1664 | stream->set_stream_id(GetNewStreamId()); |
| 1665 | scoped_ptr<SpdyStream> owned_stream(stream); |
| 1666 | created_streams_.erase(stream); |
| 1667 | return owned_stream.Pass(); |
| 1668 | } |
| 1669 | |
| 1670 | void SpdySession::InsertActivatedStream(scoped_ptr<SpdyStream> stream) { |
| 1671 | SpdyStreamId stream_id = stream->stream_id(); |
| 1672 | DCHECK_NE(stream_id, 0u); |
| 1673 | std::pair<ActiveStreamMap::iterator, bool> result = |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1674 | active_streams_.insert( |
| 1675 | std::make_pair(stream_id, ActiveStreamInfo(stream.get()))); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 1676 | if (result.second) { |
| 1677 | ignore_result(stream.release()); |
| 1678 | } else { |
| 1679 | NOTREACHED(); |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | void SpdySession::DeleteStream(scoped_ptr<SpdyStream> stream, int status) { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 1684 | if (in_flight_write_stream_.get() == stream.get()) { |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 1685 | // If we're deleting the stream for the in-flight write, we still |
| 1686 | // need to let the write complete, so we clear |
| 1687 | // |in_flight_write_stream_| and let the write finish on its own |
| 1688 | // without notifying |in_flight_write_stream_|. |
| 1689 | in_flight_write_stream_.reset(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 1692 | write_queue_.RemovePendingWritesForStream(stream->GetWeakPtr()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1693 | |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 1694 | // |stream->OnClose()| may end up closing |this|, so detect that. |
| 1695 | base::WeakPtr<SpdySession> weak_this = GetWeakPtr(); |
| 1696 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1697 | stream->OnClose(status); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 1698 | |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 1699 | if (!weak_this) |
| 1700 | return; |
| 1701 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1702 | switch (availability_state_) { |
| 1703 | case STATE_AVAILABLE: |
| 1704 | ProcessPendingStreamRequests(); |
| 1705 | break; |
| 1706 | case STATE_GOING_AWAY: |
| 1707 | DcheckGoingAway(); |
| 1708 | MaybeFinishGoingAway(); |
| 1709 | break; |
| 1710 | case STATE_CLOSED: |
| 1711 | // Do nothing. |
| 1712 | break; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1713 | } |
| 1714 | } |
| 1715 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1716 | base::WeakPtr<SpdyStream> SpdySession::GetActivePushStream(const GURL& url) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1717 | base::StatsCounter used_push_streams("spdy.claimed_push_streams"); |
| 1718 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1719 | PushedStreamMap::iterator unclaimed_it = unclaimed_pushed_streams_.find(url); |
| 1720 | if (unclaimed_it == unclaimed_pushed_streams_.end()) |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1721 | return base::WeakPtr<SpdyStream>(); |
| 1722 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1723 | SpdyStreamId stream_id = unclaimed_it->second.stream_id; |
| 1724 | unclaimed_pushed_streams_.erase(unclaimed_it); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1725 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1726 | ActiveStreamMap::iterator active_it = active_streams_.find(stream_id); |
| 1727 | if (active_it == active_streams_.end()) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1728 | NOTREACHED(); |
| 1729 | return base::WeakPtr<SpdyStream>(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1730 | } |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1731 | |
| 1732 | net_log_.AddEvent(NetLog::TYPE_SPDY_STREAM_ADOPTED_PUSH_STREAM); |
| 1733 | used_push_streams.Increment(); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1734 | return active_it->second.stream->GetWeakPtr(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | bool SpdySession::GetSSLInfo(SSLInfo* ssl_info, |
| 1738 | bool* was_npn_negotiated, |
| 1739 | NextProto* protocol_negotiated) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1740 | *was_npn_negotiated = connection_->socket()->WasNpnNegotiated(); |
| 1741 | *protocol_negotiated = connection_->socket()->GetNegotiatedProtocol(); |
| 1742 | return connection_->socket()->GetSSLInfo(ssl_info); |
| 1743 | } |
| 1744 | |
| 1745 | bool SpdySession::GetSSLCertRequestInfo( |
| 1746 | SSLCertRequestInfo* cert_request_info) { |
| 1747 | if (!is_secure_) |
| 1748 | return false; |
| 1749 | GetSSLClientSocket()->GetSSLCertRequestInfo(cert_request_info); |
| 1750 | return true; |
| 1751 | } |
| 1752 | |
| 1753 | ServerBoundCertService* SpdySession::GetServerBoundCertService() const { |
| 1754 | if (!is_secure_) |
| 1755 | return NULL; |
| 1756 | return GetSSLClientSocket()->GetServerBoundCertService(); |
| 1757 | } |
| 1758 | |
| 1759 | void SpdySession::OnError(SpdyFramer::SpdyError error_code) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1760 | CHECK(in_io_loop_); |
| 1761 | |
| 1762 | if (availability_state_ == STATE_CLOSED) |
| 1763 | return; |
| 1764 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1765 | RecordProtocolErrorHistogram( |
| 1766 | static_cast<SpdyProtocolErrorDetails>(error_code)); |
| 1767 | std::string description = base::StringPrintf( |
| 1768 | "SPDY_ERROR error_code: %d.", error_code); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1769 | CloseSessionResult result = |
| 1770 | DoCloseSession(ERR_SPDY_PROTOCOL_ERROR, description); |
| 1771 | DCHECK_EQ(result, SESSION_CLOSED_BUT_NOT_REMOVED); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | void SpdySession::OnStreamError(SpdyStreamId stream_id, |
| 1775 | const std::string& description) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1776 | CHECK(in_io_loop_); |
| 1777 | |
| 1778 | if (availability_state_ == STATE_CLOSED) |
| 1779 | return; |
| 1780 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1781 | ActiveStreamMap::iterator it = active_streams_.find(stream_id); |
| 1782 | if (it == active_streams_.end()) { |
| 1783 | // We still want to send a frame to reset the stream even if we |
| 1784 | // don't know anything about it. |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 1785 | EnqueueResetStreamFrame( |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1786 | stream_id, IDLE, RST_STREAM_PROTOCOL_ERROR, description); |
| 1787 | return; |
| 1788 | } |
| 1789 | |
| 1790 | ResetStreamIterator(it, RST_STREAM_PROTOCOL_ERROR, description); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | void SpdySession::OnStreamFrameData(SpdyStreamId stream_id, |
| 1794 | const char* data, |
| 1795 | size_t len, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1796 | bool fin) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1797 | CHECK(in_io_loop_); |
| 1798 | |
| 1799 | if (availability_state_ == STATE_CLOSED) |
| 1800 | return; |
| 1801 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1802 | DCHECK_LT(len, 1u << 24); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1803 | if (net_log().IsLoggingAllEvents()) { |
| 1804 | net_log().AddEvent( |
| 1805 | NetLog::TYPE_SPDY_SESSION_RECV_DATA, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1806 | base::Bind(&NetLogSpdyDataCallback, stream_id, len, fin)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1809 | ActiveStreamMap::iterator it = active_streams_.find(stream_id); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1810 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1811 | // By the time data comes in, the stream may already be inactive. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1812 | if (it == active_streams_.end()) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1813 | return; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1814 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1815 | SpdyStream* stream = it->second.stream; |
| 1816 | CHECK_EQ(stream->stream_id(), stream_id); |
| 1817 | |
| 1818 | if (it->second.waiting_for_syn_reply) { |
| 1819 | const std::string& error = "Data received before SYN_REPLY."; |
| 1820 | stream->LogStreamError(ERR_SPDY_PROTOCOL_ERROR, error); |
| 1821 | ResetStreamIterator(it, RST_STREAM_PROTOCOL_ERROR, error); |
| 1822 | return; |
| 1823 | } |
| 1824 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1825 | scoped_ptr<SpdyBuffer> buffer; |
| 1826 | if (data) { |
| 1827 | DCHECK_GT(len, 0u); |
| 1828 | buffer.reset(new SpdyBuffer(data, len)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1829 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1830 | if (flow_control_state_ == FLOW_CONTROL_STREAM_AND_SESSION) { |
| 1831 | DecreaseRecvWindowSize(static_cast<int32>(len)); |
| 1832 | buffer->AddConsumeCallback( |
| 1833 | base::Bind(&SpdySession::OnReadBufferConsumed, |
| 1834 | weak_factory_.GetWeakPtr())); |
| 1835 | } |
| 1836 | } else { |
| 1837 | DCHECK_EQ(len, 0u); |
| 1838 | } |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1839 | stream->OnDataReceived(buffer.Pass()); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | void SpdySession::OnSettings(bool clear_persisted) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1843 | CHECK(in_io_loop_); |
| 1844 | |
| 1845 | if (availability_state_ == STATE_CLOSED) |
| 1846 | return; |
| 1847 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 1848 | if (clear_persisted) |
| 1849 | http_server_properties_->ClearSpdySettings(host_port_pair()); |
| 1850 | |
| 1851 | if (net_log_.IsLoggingAllEvents()) { |
| 1852 | net_log_.AddEvent( |
| 1853 | NetLog::TYPE_SPDY_SESSION_RECV_SETTINGS, |
| 1854 | base::Bind(&NetLogSpdySettingsCallback, host_port_pair(), |
| 1855 | clear_persisted)); |
| 1856 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | void SpdySession::OnSetting(SpdySettingsIds id, |
| 1860 | uint8 flags, |
| 1861 | uint32 value) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1862 | CHECK(in_io_loop_); |
| 1863 | |
| 1864 | if (availability_state_ == STATE_CLOSED) |
| 1865 | return; |
| 1866 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1867 | HandleSetting(id, value); |
| 1868 | http_server_properties_->SetSpdySetting( |
| 1869 | host_port_pair(), |
| 1870 | id, |
| 1871 | static_cast<SpdySettingsFlags>(flags), |
| 1872 | value); |
| 1873 | received_settings_ = true; |
| 1874 | |
| 1875 | // Log the setting. |
| 1876 | net_log_.AddEvent( |
| 1877 | NetLog::TYPE_SPDY_SESSION_RECV_SETTING, |
| 1878 | base::Bind(&NetLogSpdySettingCallback, |
| 1879 | id, static_cast<SpdySettingsFlags>(flags), value)); |
| 1880 | } |
| 1881 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 1882 | void SpdySession::OnSendCompressedFrame( |
| 1883 | SpdyStreamId stream_id, |
| 1884 | SpdyFrameType type, |
| 1885 | size_t payload_len, |
| 1886 | size_t frame_len) { |
| 1887 | if (type != SYN_STREAM) |
| 1888 | return; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1889 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 1890 | DCHECK(buffered_spdy_framer_.get()); |
| 1891 | size_t compressed_len = |
| 1892 | frame_len - buffered_spdy_framer_->GetSynStreamMinimumSize(); |
| 1893 | |
| 1894 | if (payload_len) { |
| 1895 | // Make sure we avoid early decimal truncation. |
| 1896 | int compression_pct = 100 - (100 * compressed_len) / payload_len; |
| 1897 | UMA_HISTOGRAM_PERCENTAGE("Net.SpdySynStreamCompressionPercentage", |
| 1898 | compression_pct); |
| 1899 | } |
| 1900 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1901 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1902 | int SpdySession::OnInitialResponseHeadersReceived( |
| 1903 | const SpdyHeaderBlock& response_headers, |
| 1904 | base::Time response_time, |
| 1905 | base::TimeTicks recv_first_byte_time, |
| 1906 | SpdyStream* stream) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1907 | CHECK(in_io_loop_); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 1908 | SpdyStreamId stream_id = stream->stream_id(); |
| 1909 | // May invalidate |stream|. |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1910 | int rv = stream->OnInitialResponseHeadersReceived( |
| 1911 | response_headers, response_time, recv_first_byte_time); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1912 | if (rv < 0) { |
| 1913 | DCHECK_NE(rv, ERR_IO_PENDING); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1914 | DCHECK(active_streams_.find(stream_id) == active_streams_.end()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1915 | } |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1916 | return rv; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | void SpdySession::OnSynStream(SpdyStreamId stream_id, |
| 1920 | SpdyStreamId associated_stream_id, |
| 1921 | SpdyPriority priority, |
| 1922 | uint8 credential_slot, |
| 1923 | bool fin, |
| 1924 | bool unidirectional, |
| 1925 | const SpdyHeaderBlock& headers) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1926 | CHECK(in_io_loop_); |
| 1927 | |
| 1928 | if (availability_state_ == STATE_CLOSED) |
| 1929 | return; |
| 1930 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1931 | base::Time response_time = base::Time::Now(); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1932 | base::TimeTicks recv_first_byte_time = time_func_(); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1933 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1934 | if (net_log_.IsLoggingAllEvents()) { |
| 1935 | net_log_.AddEvent( |
| 1936 | NetLog::TYPE_SPDY_SESSION_PUSHED_SYN_STREAM, |
| 1937 | base::Bind(&NetLogSpdySynCallback, |
| 1938 | &headers, fin, unidirectional, |
| 1939 | stream_id, associated_stream_id)); |
| 1940 | } |
| 1941 | |
| 1942 | // Server-initiated streams should have even sequence numbers. |
| 1943 | if ((stream_id & 0x1) != 0) { |
| 1944 | LOG(WARNING) << "Received invalid OnSyn stream id " << stream_id; |
| 1945 | return; |
| 1946 | } |
| 1947 | |
| 1948 | if (IsStreamActive(stream_id)) { |
| 1949 | LOG(WARNING) << "Received OnSyn for active stream " << stream_id; |
| 1950 | return; |
| 1951 | } |
| 1952 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 1953 | RequestPriority request_priority = |
| 1954 | ConvertSpdyPriorityToRequestPriority(priority, GetProtocolVersion()); |
| 1955 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1956 | if (availability_state_ == STATE_GOING_AWAY) { |
| 1957 | // TODO(akalin): This behavior isn't in the SPDY spec, although it |
| 1958 | // probably should be. |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 1959 | EnqueueResetStreamFrame(stream_id, request_priority, |
| 1960 | RST_STREAM_REFUSED_STREAM, |
| 1961 | "OnSyn received when going away"); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 1962 | return; |
| 1963 | } |
| 1964 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1965 | if (associated_stream_id == 0) { |
| 1966 | std::string description = base::StringPrintf( |
| 1967 | "Received invalid OnSyn associated stream id %d for stream %d", |
| 1968 | associated_stream_id, stream_id); |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 1969 | EnqueueResetStreamFrame(stream_id, request_priority, |
| 1970 | RST_STREAM_REFUSED_STREAM, description); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1971 | return; |
| 1972 | } |
| 1973 | |
| 1974 | streams_pushed_count_++; |
| 1975 | |
| 1976 | // TODO(mbelshe): DCHECK that this is a GET method? |
| 1977 | |
| 1978 | // Verify that the response had a URL for us. |
| 1979 | GURL gurl = GetUrlFromHeaderBlock(headers, GetProtocolVersion(), true); |
| 1980 | if (!gurl.is_valid()) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 1981 | EnqueueResetStreamFrame( |
| 1982 | stream_id, request_priority, RST_STREAM_PROTOCOL_ERROR, |
| 1983 | "Pushed stream url was invalid: " + gurl.spec()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1984 | return; |
| 1985 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1986 | |
| 1987 | // Verify we have a valid stream association. |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 1988 | ActiveStreamMap::iterator associated_it = |
| 1989 | active_streams_.find(associated_stream_id); |
| 1990 | if (associated_it == active_streams_.end()) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 1991 | EnqueueResetStreamFrame( |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1992 | stream_id, request_priority, RST_STREAM_INVALID_STREAM, |
| 1993 | base::StringPrintf( |
| 1994 | "Received OnSyn with inactive associated stream %d", |
| 1995 | associated_stream_id)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1996 | return; |
| 1997 | } |
| 1998 | |
| 1999 | // Check that the SYN advertises the same origin as its associated stream. |
| 2000 | // Bypass this check if and only if this session is with a SPDY proxy that |
| 2001 | // is trusted explicitly via the --trusted-spdy-proxy switch. |
| 2002 | if (trusted_spdy_proxy_.Equals(host_port_pair())) { |
| 2003 | // Disallow pushing of HTTPS content. |
| 2004 | if (gurl.SchemeIs("https")) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 2005 | EnqueueResetStreamFrame( |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2006 | stream_id, request_priority, RST_STREAM_REFUSED_STREAM, |
| 2007 | base::StringPrintf( |
| 2008 | "Rejected push of Cross Origin HTTPS content %d", |
| 2009 | associated_stream_id)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2010 | } |
| 2011 | } else { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2012 | GURL associated_url(associated_it->second.stream->GetUrlFromHeaders()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2013 | if (associated_url.GetOrigin() != gurl.GetOrigin()) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 2014 | EnqueueResetStreamFrame( |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2015 | stream_id, request_priority, RST_STREAM_REFUSED_STREAM, |
| 2016 | base::StringPrintf( |
| 2017 | "Rejected Cross Origin Push Stream %d", |
| 2018 | associated_stream_id)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2019 | return; |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | // There should not be an existing pushed stream with the same path. |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2024 | PushedStreamMap::iterator pushed_it = |
| 2025 | unclaimed_pushed_streams_.lower_bound(gurl); |
| 2026 | if (pushed_it != unclaimed_pushed_streams_.end() && |
| 2027 | pushed_it->first == gurl) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 2028 | EnqueueResetStreamFrame( |
| 2029 | stream_id, request_priority, RST_STREAM_PROTOCOL_ERROR, |
| 2030 | "Received duplicate pushed stream with url: " + |
| 2031 | gurl.spec()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2032 | return; |
| 2033 | } |
| 2034 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2035 | scoped_ptr<SpdyStream> stream( |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2036 | new SpdyStream(SPDY_PUSH_STREAM, GetWeakPtr(), gurl, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 2037 | request_priority, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2038 | stream_initial_send_window_size_, |
| 2039 | stream_initial_recv_window_size_, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 2040 | net_log_)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2041 | stream->set_stream_id(stream_id); |
| 2042 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2043 | DeleteExpiredPushedStreams(); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2044 | PushedStreamMap::iterator inserted_pushed_it = |
| 2045 | unclaimed_pushed_streams_.insert( |
| 2046 | pushed_it, |
| 2047 | std::make_pair(gurl, PushedStreamInfo(stream_id, time_func_()))); |
| 2048 | DCHECK(inserted_pushed_it != pushed_it); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2049 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2050 | InsertActivatedStream(stream.Pass()); |
| 2051 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2052 | ActiveStreamMap::iterator active_it = active_streams_.find(stream_id); |
| 2053 | if (active_it == active_streams_.end()) { |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2054 | NOTREACHED(); |
| 2055 | return; |
| 2056 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2057 | |
| 2058 | // Parse the headers. |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2059 | if (OnInitialResponseHeadersReceived( |
| 2060 | headers, response_time, |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2061 | recv_first_byte_time, active_it->second.stream) != OK) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2062 | return; |
| 2063 | |
| 2064 | base::StatsCounter push_requests("spdy.pushed_streams"); |
| 2065 | push_requests.Increment(); |
| 2066 | } |
| 2067 | |
| 2068 | void SpdySession::DeleteExpiredPushedStreams() { |
| 2069 | if (unclaimed_pushed_streams_.empty()) |
| 2070 | return; |
| 2071 | |
| 2072 | // Check that adequate time has elapsed since the last sweep. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2073 | if (time_func_() < next_unclaimed_push_stream_sweep_time_) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2074 | return; |
| 2075 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2076 | // Gather old streams to delete. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2077 | base::TimeTicks minimum_freshness = time_func_() - |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2078 | base::TimeDelta::FromSeconds(kMinPushedStreamLifetimeSeconds); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2079 | std::vector<SpdyStreamId> streams_to_close; |
| 2080 | for (PushedStreamMap::iterator it = unclaimed_pushed_streams_.begin(); |
| 2081 | it != unclaimed_pushed_streams_.end(); ++it) { |
| 2082 | if (minimum_freshness > it->second.creation_time) |
| 2083 | streams_to_close.push_back(it->second.stream_id); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2084 | } |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2085 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2086 | for (std::vector<SpdyStreamId>::const_iterator to_close_it = |
| 2087 | streams_to_close.begin(); |
| 2088 | to_close_it != streams_to_close.end(); ++to_close_it) { |
| 2089 | ActiveStreamMap::iterator active_it = active_streams_.find(*to_close_it); |
| 2090 | if (active_it == active_streams_.end()) |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2091 | continue; |
| 2092 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2093 | LogAbandonedActiveStream(active_it, ERR_INVALID_SPDY_STREAM); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2094 | // CloseActiveStreamIterator() will remove the stream from |
| 2095 | // |unclaimed_pushed_streams_|. |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2096 | CloseActiveStreamIterator(active_it, ERR_INVALID_SPDY_STREAM); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2097 | } |
| 2098 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2099 | next_unclaimed_push_stream_sweep_time_ = time_func_() + |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2100 | base::TimeDelta::FromSeconds(kMinPushedStreamLifetimeSeconds); |
| 2101 | } |
| 2102 | |
| 2103 | void SpdySession::OnSynReply(SpdyStreamId stream_id, |
| 2104 | bool fin, |
| 2105 | const SpdyHeaderBlock& headers) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2106 | CHECK(in_io_loop_); |
| 2107 | |
| 2108 | if (availability_state_ == STATE_CLOSED) |
| 2109 | return; |
| 2110 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2111 | base::Time response_time = base::Time::Now(); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2112 | base::TimeTicks recv_first_byte_time = time_func_(); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2113 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2114 | if (net_log().IsLoggingAllEvents()) { |
| 2115 | net_log().AddEvent( |
| 2116 | NetLog::TYPE_SPDY_SESSION_SYN_REPLY, |
| 2117 | base::Bind(&NetLogSpdySynCallback, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2118 | &headers, fin, false, // not unidirectional |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2119 | stream_id, 0)); |
| 2120 | } |
| 2121 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2122 | ActiveStreamMap::iterator it = active_streams_.find(stream_id); |
| 2123 | if (it == active_streams_.end()) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2124 | // NOTE: it may just be that the stream was cancelled. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2125 | return; |
| 2126 | } |
| 2127 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2128 | SpdyStream* stream = it->second.stream; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2129 | CHECK_EQ(stream->stream_id(), stream_id); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2130 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2131 | if (!it->second.waiting_for_syn_reply) { |
| 2132 | const std::string& error = |
| 2133 | "Received duplicate SYN_REPLY for stream."; |
| 2134 | stream->LogStreamError(ERR_SPDY_PROTOCOL_ERROR, error); |
| 2135 | ResetStreamIterator(it, RST_STREAM_STREAM_IN_USE, error); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2136 | return; |
| 2137 | } |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2138 | it->second.waiting_for_syn_reply = false; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2139 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2140 | ignore_result(OnInitialResponseHeadersReceived( |
| 2141 | headers, response_time, recv_first_byte_time, stream)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | void SpdySession::OnHeaders(SpdyStreamId stream_id, |
| 2145 | bool fin, |
| 2146 | const SpdyHeaderBlock& headers) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2147 | CHECK(in_io_loop_); |
| 2148 | |
| 2149 | if (availability_state_ == STATE_CLOSED) |
| 2150 | return; |
| 2151 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2152 | if (net_log().IsLoggingAllEvents()) { |
| 2153 | net_log().AddEvent( |
| 2154 | NetLog::TYPE_SPDY_SESSION_RECV_HEADERS, |
| 2155 | base::Bind(&NetLogSpdySynCallback, |
| 2156 | &headers, fin, /*unidirectional=*/false, |
| 2157 | stream_id, 0)); |
| 2158 | } |
| 2159 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2160 | ActiveStreamMap::iterator it = active_streams_.find(stream_id); |
| 2161 | if (it == active_streams_.end()) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2162 | // NOTE: it may just be that the stream was cancelled. |
| 2163 | LOG(WARNING) << "Received HEADERS for invalid stream " << stream_id; |
| 2164 | return; |
| 2165 | } |
| 2166 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2167 | SpdyStream* stream = it->second.stream; |
| 2168 | CHECK_EQ(stream->stream_id(), stream_id); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2169 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2170 | int rv = stream->OnAdditionalResponseHeadersReceived(headers); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2171 | if (rv < 0) { |
| 2172 | DCHECK_NE(rv, ERR_IO_PENDING); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2173 | DCHECK(active_streams_.find(stream_id) == active_streams_.end()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2174 | } |
| 2175 | } |
| 2176 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2177 | void SpdySession::OnRstStream(SpdyStreamId stream_id, |
| 2178 | SpdyRstStreamStatus status) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2179 | CHECK(in_io_loop_); |
| 2180 | |
| 2181 | if (availability_state_ == STATE_CLOSED) |
| 2182 | return; |
| 2183 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2184 | std::string description; |
| 2185 | net_log().AddEvent( |
| 2186 | NetLog::TYPE_SPDY_SESSION_RST_STREAM, |
| 2187 | base::Bind(&NetLogSpdyRstCallback, |
| 2188 | stream_id, status, &description)); |
| 2189 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2190 | ActiveStreamMap::iterator it = active_streams_.find(stream_id); |
| 2191 | if (it == active_streams_.end()) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2192 | // NOTE: it may just be that the stream was cancelled. |
| 2193 | LOG(WARNING) << "Received RST for invalid stream" << stream_id; |
| 2194 | return; |
| 2195 | } |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2196 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2197 | CHECK_EQ(it->second.stream->stream_id(), stream_id); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2198 | |
| 2199 | if (status == 0) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2200 | it->second.stream->OnDataReceived(scoped_ptr<SpdyBuffer>()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2201 | } else if (status == RST_STREAM_REFUSED_STREAM) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2202 | CloseActiveStreamIterator(it, ERR_SPDY_SERVER_REFUSED_STREAM); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2203 | } else { |
| 2204 | RecordProtocolErrorHistogram( |
| 2205 | PROTOCOL_ERROR_RST_STREAM_FOR_NON_ACTIVE_STREAM); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2206 | it->second.stream->LogStreamError( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2207 | ERR_SPDY_PROTOCOL_ERROR, |
| 2208 | base::StringPrintf("SPDY stream closed with status: %d", status)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2209 | // TODO(mbelshe): Map from Spdy-protocol errors to something sensical. |
| 2210 | // For now, it doesn't matter much - it is a protocol error. |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2211 | CloseActiveStreamIterator(it, ERR_SPDY_PROTOCOL_ERROR); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2212 | } |
| 2213 | } |
| 2214 | |
| 2215 | void SpdySession::OnGoAway(SpdyStreamId last_accepted_stream_id, |
| 2216 | SpdyGoAwayStatus status) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2217 | CHECK(in_io_loop_); |
| 2218 | |
| 2219 | if (availability_state_ == STATE_CLOSED) |
| 2220 | return; |
| 2221 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2222 | net_log_.AddEvent(NetLog::TYPE_SPDY_SESSION_GOAWAY, |
| 2223 | base::Bind(&NetLogSpdyGoAwayCallback, |
| 2224 | last_accepted_stream_id, |
| 2225 | active_streams_.size(), |
| 2226 | unclaimed_pushed_streams_.size(), |
| 2227 | status)); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2228 | if (availability_state_ < STATE_GOING_AWAY) { |
| 2229 | availability_state_ = STATE_GOING_AWAY; |
| 2230 | // |pool_| will be NULL when |InitializeWithSocket()| is in the |
| 2231 | // call stack. |
| 2232 | if (pool_) |
| 2233 | pool_->MakeSessionUnavailable(GetWeakPtr()); |
| 2234 | } |
| 2235 | StartGoingAway(last_accepted_stream_id, ERR_ABORTED); |
| 2236 | // This is to handle the case when we already don't have any active |
| 2237 | // streams (i.e., StartGoingAway() did nothing). Otherwise, we have |
| 2238 | // active streams and so the last one being closed will finish the |
| 2239 | // going away process (see DeleteStream()). |
| 2240 | MaybeFinishGoingAway(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | void SpdySession::OnPing(uint32 unique_id) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2244 | CHECK(in_io_loop_); |
| 2245 | |
| 2246 | if (availability_state_ == STATE_CLOSED) |
| 2247 | return; |
| 2248 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2249 | net_log_.AddEvent( |
| 2250 | NetLog::TYPE_SPDY_SESSION_PING, |
| 2251 | base::Bind(&NetLogSpdyPingCallback, unique_id, "received")); |
| 2252 | |
| 2253 | // Send response to a PING from server. |
| 2254 | if (unique_id % 2 == 0) { |
| 2255 | WritePingFrame(unique_id); |
| 2256 | return; |
| 2257 | } |
| 2258 | |
| 2259 | --pings_in_flight_; |
| 2260 | if (pings_in_flight_ < 0) { |
| 2261 | RecordProtocolErrorHistogram(PROTOCOL_ERROR_UNEXPECTED_PING); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2262 | CloseSessionResult result = |
| 2263 | DoCloseSession(ERR_SPDY_PROTOCOL_ERROR, "pings_in_flight_ is < 0."); |
| 2264 | DCHECK_EQ(result, SESSION_CLOSED_BUT_NOT_REMOVED); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2265 | pings_in_flight_ = 0; |
| 2266 | return; |
| 2267 | } |
| 2268 | |
| 2269 | if (pings_in_flight_ > 0) |
| 2270 | return; |
| 2271 | |
| 2272 | // We will record RTT in histogram when there are no more client sent |
| 2273 | // pings_in_flight_. |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2274 | RecordPingRTTHistogram(time_func_() - last_ping_sent_time_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | void SpdySession::OnWindowUpdate(SpdyStreamId stream_id, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2278 | uint32 delta_window_size) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2279 | CHECK(in_io_loop_); |
| 2280 | |
| 2281 | if (availability_state_ == STATE_CLOSED) |
| 2282 | return; |
| 2283 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2284 | DCHECK_LE(delta_window_size, static_cast<uint32>(kint32max)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2285 | net_log_.AddEvent( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2286 | NetLog::TYPE_SPDY_SESSION_RECEIVED_WINDOW_UPDATE_FRAME, |
| 2287 | base::Bind(&NetLogSpdyWindowUpdateFrameCallback, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2288 | stream_id, delta_window_size)); |
| 2289 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2290 | if (stream_id == kSessionFlowControlStreamId) { |
| 2291 | // WINDOW_UPDATE for the session. |
| 2292 | if (flow_control_state_ < FLOW_CONTROL_STREAM_AND_SESSION) { |
| 2293 | LOG(WARNING) << "Received WINDOW_UPDATE for session when " |
| 2294 | << "session flow control is not turned on"; |
| 2295 | // TODO(akalin): Record an error and close the session. |
| 2296 | return; |
| 2297 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2298 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2299 | if (delta_window_size < 1u) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2300 | RecordProtocolErrorHistogram(PROTOCOL_ERROR_INVALID_WINDOW_UPDATE_SIZE); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2301 | CloseSessionResult result = DoCloseSession( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2302 | ERR_SPDY_PROTOCOL_ERROR, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2303 | "Received WINDOW_UPDATE with an invalid delta_window_size " + |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2304 | base::UintToString(delta_window_size)); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2305 | DCHECK_EQ(result, SESSION_CLOSED_BUT_NOT_REMOVED); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2306 | return; |
| 2307 | } |
| 2308 | |
| 2309 | IncreaseSendWindowSize(static_cast<int32>(delta_window_size)); |
| 2310 | } else { |
| 2311 | // WINDOW_UPDATE for a stream. |
| 2312 | if (flow_control_state_ < FLOW_CONTROL_STREAM) { |
| 2313 | // TODO(akalin): Record an error and close the session. |
| 2314 | LOG(WARNING) << "Received WINDOW_UPDATE for stream " << stream_id |
| 2315 | << " when flow control is not turned on"; |
| 2316 | return; |
| 2317 | } |
| 2318 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2319 | ActiveStreamMap::iterator it = active_streams_.find(stream_id); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2320 | |
| 2321 | if (it == active_streams_.end()) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2322 | // NOTE: it may just be that the stream was cancelled. |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2323 | LOG(WARNING) << "Received WINDOW_UPDATE for invalid stream " << stream_id; |
| 2324 | return; |
| 2325 | } |
| 2326 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2327 | SpdyStream* stream = it->second.stream; |
| 2328 | CHECK_EQ(stream->stream_id(), stream_id); |
| 2329 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2330 | if (delta_window_size < 1u) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2331 | ResetStreamIterator(it, |
| 2332 | RST_STREAM_FLOW_CONTROL_ERROR, |
| 2333 | base::StringPrintf( |
| 2334 | "Received WINDOW_UPDATE with an invalid " |
| 2335 | "delta_window_size %ud", delta_window_size)); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2336 | return; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2337 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2338 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2339 | CHECK_EQ(it->second.stream->stream_id(), stream_id); |
| 2340 | it->second.stream->IncreaseSendWindowSize( |
| 2341 | static_cast<int32>(delta_window_size)); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2342 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 2345 | void SpdySession::OnPushPromise(SpdyStreamId stream_id, |
| 2346 | SpdyStreamId promised_stream_id) { |
| 2347 | // TODO(akalin): Handle PUSH_PROMISE frames. |
| 2348 | } |
| 2349 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2350 | void SpdySession::SendStreamWindowUpdate(SpdyStreamId stream_id, |
| 2351 | uint32 delta_window_size) { |
| 2352 | CHECK_GE(flow_control_state_, FLOW_CONTROL_STREAM); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2353 | ActiveStreamMap::const_iterator it = active_streams_.find(stream_id); |
| 2354 | CHECK(it != active_streams_.end()); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2355 | CHECK_EQ(it->second.stream->stream_id(), stream_id); |
| 2356 | SendWindowUpdateFrame( |
| 2357 | stream_id, delta_window_size, it->second.stream->priority()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2358 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2359 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 2360 | void SpdySession::SendInitialData() { |
| 2361 | DCHECK(enable_sending_initial_data_); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2362 | DCHECK_NE(availability_state_, STATE_CLOSED); |
| 2363 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 2364 | if (send_connection_header_prefix_) { |
| 2365 | DCHECK_EQ(protocol_, kProtoHTTP2Draft04); |
| 2366 | scoped_ptr<SpdyFrame> connection_header_prefix_frame( |
| 2367 | new SpdyFrame(const_cast<char*>(kHttp2ConnectionHeaderPrefix), |
| 2368 | kHttp2ConnectionHeaderPrefixSize, |
| 2369 | false /* take_ownership */)); |
| 2370 | // Count the prefix as part of the subsequent SETTINGS frame. |
| 2371 | EnqueueSessionWrite(HIGHEST, SETTINGS, |
| 2372 | connection_header_prefix_frame.Pass()); |
| 2373 | } |
| 2374 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2375 | // First, notify the server about the settings they should use when |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2376 | // communicating with us. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 2377 | SettingsMap settings_map; |
| 2378 | // Create a new settings frame notifying the server of our |
| 2379 | // max concurrent streams and initial window size. |
| 2380 | settings_map[SETTINGS_MAX_CONCURRENT_STREAMS] = |
| 2381 | SettingsFlagsAndValue(SETTINGS_FLAG_NONE, kMaxConcurrentPushedStreams); |
| 2382 | if (flow_control_state_ >= FLOW_CONTROL_STREAM && |
| 2383 | stream_initial_recv_window_size_ != kSpdyStreamInitialWindowSize) { |
| 2384 | settings_map[SETTINGS_INITIAL_WINDOW_SIZE] = |
| 2385 | SettingsFlagsAndValue(SETTINGS_FLAG_NONE, |
| 2386 | stream_initial_recv_window_size_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2387 | } |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 2388 | SendSettings(settings_map); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2389 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2390 | // Next, notify the server about our initial recv window size. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 2391 | if (flow_control_state_ == FLOW_CONTROL_STREAM_AND_SESSION) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2392 | // Bump up the receive window size to the real initial value. This |
| 2393 | // has to go here since the WINDOW_UPDATE frame sent by |
| 2394 | // IncreaseRecvWindowSize() call uses |buffered_spdy_framer_|. |
| 2395 | DCHECK_GT(kDefaultInitialRecvWindowSize, session_recv_window_size_); |
| 2396 | // This condition implies that |kDefaultInitialRecvWindowSize| - |
| 2397 | // |session_recv_window_size_| doesn't overflow. |
| 2398 | DCHECK_GT(session_recv_window_size_, 0); |
| 2399 | IncreaseRecvWindowSize( |
| 2400 | kDefaultInitialRecvWindowSize - session_recv_window_size_); |
| 2401 | } |
| 2402 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 2403 | // Finally, notify the server about the settings they have |
| 2404 | // previously told us to use when communicating with them (after |
| 2405 | // applying them). |
| 2406 | const SettingsMap& server_settings_map = |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2407 | http_server_properties_->GetSpdySettings(host_port_pair()); |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 2408 | if (server_settings_map.empty()) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2409 | return; |
| 2410 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 2411 | SettingsMap::const_iterator it = |
| 2412 | server_settings_map.find(SETTINGS_CURRENT_CWND); |
| 2413 | uint32 cwnd = (it != server_settings_map.end()) ? it->second.second : 0; |
| 2414 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdySettingsCwndSent", cwnd, 1, 200, 100); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2415 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 2416 | for (SettingsMap::const_iterator it = server_settings_map.begin(); |
| 2417 | it != server_settings_map.end(); ++it) { |
| 2418 | const SpdySettingsIds new_id = it->first; |
| 2419 | const uint32 new_val = it->second.second; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2420 | HandleSetting(new_id, new_val); |
| 2421 | } |
| 2422 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 2423 | SendSettings(server_settings_map); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
| 2426 | |
| 2427 | void SpdySession::SendSettings(const SettingsMap& settings) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2428 | DCHECK_NE(availability_state_, STATE_CLOSED); |
| 2429 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2430 | net_log_.AddEvent( |
| 2431 | NetLog::TYPE_SPDY_SESSION_SEND_SETTINGS, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2432 | base::Bind(&NetLogSpdySendSettingsCallback, &settings)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2433 | |
| 2434 | // Create the SETTINGS frame and send it. |
| 2435 | DCHECK(buffered_spdy_framer_.get()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2436 | scoped_ptr<SpdyFrame> settings_frame( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2437 | buffered_spdy_framer_->CreateSettings(settings)); |
| 2438 | sent_settings_ = true; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2439 | EnqueueSessionWrite(HIGHEST, SETTINGS, settings_frame.Pass()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | void SpdySession::HandleSetting(uint32 id, uint32 value) { |
| 2443 | switch (id) { |
| 2444 | case SETTINGS_MAX_CONCURRENT_STREAMS: |
| 2445 | max_concurrent_streams_ = std::min(static_cast<size_t>(value), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2446 | kMaxConcurrentStreamLimit); |
| 2447 | ProcessPendingStreamRequests(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2448 | break; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2449 | case SETTINGS_INITIAL_WINDOW_SIZE: { |
| 2450 | if (flow_control_state_ < FLOW_CONTROL_STREAM) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2451 | net_log().AddEvent( |
| 2452 | NetLog::TYPE_SPDY_SESSION_INITIAL_WINDOW_SIZE_NO_FLOW_CONTROL); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2453 | return; |
| 2454 | } |
| 2455 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2456 | if (value > static_cast<uint32>(kint32max)) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2457 | net_log().AddEvent( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2458 | NetLog::TYPE_SPDY_SESSION_INITIAL_WINDOW_SIZE_OUT_OF_RANGE, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2459 | NetLog::IntegerCallback("initial_window_size", value)); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2460 | return; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2461 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2462 | |
| 2463 | // SETTINGS_INITIAL_WINDOW_SIZE updates initial_send_window_size_ only. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2464 | int32 delta_window_size = |
| 2465 | static_cast<int32>(value) - stream_initial_send_window_size_; |
| 2466 | stream_initial_send_window_size_ = static_cast<int32>(value); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2467 | UpdateStreamsSendWindowSize(delta_window_size); |
| 2468 | net_log().AddEvent( |
| 2469 | NetLog::TYPE_SPDY_SESSION_UPDATE_STREAMS_SEND_WINDOW_SIZE, |
| 2470 | NetLog::IntegerCallback("delta_window_size", delta_window_size)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2471 | break; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2472 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2473 | } |
| 2474 | } |
| 2475 | |
| 2476 | void SpdySession::UpdateStreamsSendWindowSize(int32 delta_window_size) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2477 | DCHECK_GE(flow_control_state_, FLOW_CONTROL_STREAM); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2478 | for (ActiveStreamMap::iterator it = active_streams_.begin(); |
| 2479 | it != active_streams_.end(); ++it) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2480 | it->second.stream->AdjustSendWindowSize(delta_window_size); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2481 | } |
| 2482 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2483 | for (CreatedStreamSet::const_iterator it = created_streams_.begin(); |
| 2484 | it != created_streams_.end(); it++) { |
| 2485 | (*it)->AdjustSendWindowSize(delta_window_size); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2486 | } |
| 2487 | } |
| 2488 | |
| 2489 | void SpdySession::SendPrefacePingIfNoneInFlight() { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2490 | if (pings_in_flight_ || !enable_ping_based_connection_checking_) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2491 | return; |
| 2492 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2493 | base::TimeTicks now = time_func_(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2494 | // If there is no activity in the session, then send a preface-PING. |
| 2495 | if ((now - last_activity_time_) > connection_at_risk_of_loss_time_) |
| 2496 | SendPrefacePing(); |
| 2497 | } |
| 2498 | |
| 2499 | void SpdySession::SendPrefacePing() { |
| 2500 | WritePingFrame(next_ping_id_); |
| 2501 | } |
| 2502 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2503 | void SpdySession::SendWindowUpdateFrame(SpdyStreamId stream_id, |
| 2504 | uint32 delta_window_size, |
| 2505 | RequestPriority priority) { |
| 2506 | CHECK_GE(flow_control_state_, FLOW_CONTROL_STREAM); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2507 | ActiveStreamMap::const_iterator it = active_streams_.find(stream_id); |
| 2508 | if (it != active_streams_.end()) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2509 | CHECK_EQ(it->second.stream->stream_id(), stream_id); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2510 | } else { |
| 2511 | CHECK_EQ(flow_control_state_, FLOW_CONTROL_STREAM_AND_SESSION); |
| 2512 | CHECK_EQ(stream_id, kSessionFlowControlStreamId); |
| 2513 | } |
| 2514 | |
| 2515 | net_log_.AddEvent( |
| 2516 | NetLog::TYPE_SPDY_SESSION_SENT_WINDOW_UPDATE_FRAME, |
| 2517 | base::Bind(&NetLogSpdyWindowUpdateFrameCallback, |
| 2518 | stream_id, delta_window_size)); |
| 2519 | |
| 2520 | DCHECK(buffered_spdy_framer_.get()); |
| 2521 | scoped_ptr<SpdyFrame> window_update_frame( |
| 2522 | buffered_spdy_framer_->CreateWindowUpdate(stream_id, delta_window_size)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2523 | EnqueueSessionWrite(priority, WINDOW_UPDATE, window_update_frame.Pass()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2526 | void SpdySession::WritePingFrame(uint32 unique_id) { |
| 2527 | DCHECK(buffered_spdy_framer_.get()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2528 | scoped_ptr<SpdyFrame> ping_frame( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2529 | buffered_spdy_framer_->CreatePingFrame(unique_id)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2530 | EnqueueSessionWrite(HIGHEST, PING, ping_frame.Pass()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2531 | |
| 2532 | if (net_log().IsLoggingAllEvents()) { |
| 2533 | net_log().AddEvent( |
| 2534 | NetLog::TYPE_SPDY_SESSION_PING, |
| 2535 | base::Bind(&NetLogSpdyPingCallback, unique_id, "sent")); |
| 2536 | } |
| 2537 | if (unique_id % 2 != 0) { |
| 2538 | next_ping_id_ += 2; |
| 2539 | ++pings_in_flight_; |
| 2540 | PlanToCheckPingStatus(); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2541 | last_ping_sent_time_ = time_func_(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | void SpdySession::PlanToCheckPingStatus() { |
| 2546 | if (check_ping_status_pending_) |
| 2547 | return; |
| 2548 | |
| 2549 | check_ping_status_pending_ = true; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 2550 | base::MessageLoop::current()->PostDelayedTask( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2551 | FROM_HERE, |
| 2552 | base::Bind(&SpdySession::CheckPingStatus, weak_factory_.GetWeakPtr(), |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2553 | time_func_()), hung_interval_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | void SpdySession::CheckPingStatus(base::TimeTicks last_check_time) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2557 | CHECK(!in_io_loop_); |
| 2558 | DCHECK_NE(availability_state_, STATE_CLOSED); |
| 2559 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2560 | // Check if we got a response back for all PINGs we had sent. |
| 2561 | if (pings_in_flight_ == 0) { |
| 2562 | check_ping_status_pending_ = false; |
| 2563 | return; |
| 2564 | } |
| 2565 | |
| 2566 | DCHECK(check_ping_status_pending_); |
| 2567 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2568 | base::TimeTicks now = time_func_(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2569 | base::TimeDelta delay = hung_interval_ - (now - last_activity_time_); |
| 2570 | |
| 2571 | if (delay.InMilliseconds() < 0 || last_activity_time_ < last_check_time) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2572 | // Track all failed PING messages in a separate bucket. |
| 2573 | const base::TimeDelta kFailedPing = |
| 2574 | base::TimeDelta::FromInternalValue(INT_MAX); |
| 2575 | RecordPingRTTHistogram(kFailedPing); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2576 | CloseSessionResult result = |
| 2577 | DoCloseSession(ERR_SPDY_PING_FAILED, "Failed ping."); |
| 2578 | DCHECK_EQ(result, SESSION_CLOSED_AND_REMOVED); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2579 | return; |
| 2580 | } |
| 2581 | |
| 2582 | // Check the status of connection after a delay. |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 2583 | base::MessageLoop::current()->PostDelayedTask( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2584 | FROM_HERE, |
| 2585 | base::Bind(&SpdySession::CheckPingStatus, weak_factory_.GetWeakPtr(), |
| 2586 | now), |
| 2587 | delay); |
| 2588 | } |
| 2589 | |
| 2590 | void SpdySession::RecordPingRTTHistogram(base::TimeDelta duration) { |
| 2591 | UMA_HISTOGRAM_TIMES("Net.SpdyPing.RTT", duration); |
| 2592 | } |
| 2593 | |
| 2594 | void SpdySession::RecordProtocolErrorHistogram( |
| 2595 | SpdyProtocolErrorDetails details) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2596 | UMA_HISTOGRAM_ENUMERATION("Net.SpdySessionErrorDetails2", details, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2597 | NUM_SPDY_PROTOCOL_ERROR_DETAILS); |
| 2598 | if (EndsWith(host_port_pair().host(), "google.com", false)) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2599 | UMA_HISTOGRAM_ENUMERATION("Net.SpdySessionErrorDetails_Google2", details, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2600 | NUM_SPDY_PROTOCOL_ERROR_DETAILS); |
| 2601 | } |
| 2602 | } |
| 2603 | |
| 2604 | void SpdySession::RecordHistograms() { |
| 2605 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdyStreamsPerSession", |
| 2606 | streams_initiated_count_, |
| 2607 | 0, 300, 50); |
| 2608 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdyStreamsPushedPerSession", |
| 2609 | streams_pushed_count_, |
| 2610 | 0, 300, 50); |
| 2611 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdyStreamsPushedAndClaimedPerSession", |
| 2612 | streams_pushed_and_claimed_count_, |
| 2613 | 0, 300, 50); |
| 2614 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdyStreamsAbandonedPerSession", |
| 2615 | streams_abandoned_count_, |
| 2616 | 0, 300, 50); |
| 2617 | UMA_HISTOGRAM_ENUMERATION("Net.SpdySettingsSent", |
| 2618 | sent_settings_ ? 1 : 0, 2); |
| 2619 | UMA_HISTOGRAM_ENUMERATION("Net.SpdySettingsReceived", |
| 2620 | received_settings_ ? 1 : 0, 2); |
| 2621 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdyStreamStallsPerSession", |
| 2622 | stalled_streams_, |
| 2623 | 0, 300, 50); |
| 2624 | UMA_HISTOGRAM_ENUMERATION("Net.SpdySessionsWithStalls", |
| 2625 | stalled_streams_ > 0 ? 1 : 0, 2); |
| 2626 | |
| 2627 | if (received_settings_) { |
| 2628 | // Enumerate the saved settings, and set histograms for it. |
| 2629 | const SettingsMap& settings_map = |
| 2630 | http_server_properties_->GetSpdySettings(host_port_pair()); |
| 2631 | |
| 2632 | SettingsMap::const_iterator it; |
| 2633 | for (it = settings_map.begin(); it != settings_map.end(); ++it) { |
| 2634 | const SpdySettingsIds id = it->first; |
| 2635 | const uint32 val = it->second.second; |
| 2636 | switch (id) { |
| 2637 | case SETTINGS_CURRENT_CWND: |
| 2638 | // Record several different histograms to see if cwnd converges |
| 2639 | // for larger volumes of data being sent. |
| 2640 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdySettingsCwnd", |
| 2641 | val, 1, 200, 100); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2642 | if (total_bytes_received_ > 10 * 1024) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2643 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdySettingsCwnd10K", |
| 2644 | val, 1, 200, 100); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2645 | if (total_bytes_received_ > 25 * 1024) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2646 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdySettingsCwnd25K", |
| 2647 | val, 1, 200, 100); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2648 | if (total_bytes_received_ > 50 * 1024) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2649 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdySettingsCwnd50K", |
| 2650 | val, 1, 200, 100); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2651 | if (total_bytes_received_ > 100 * 1024) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2652 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdySettingsCwnd100K", |
| 2653 | val, 1, 200, 100); |
| 2654 | } |
| 2655 | } |
| 2656 | } |
| 2657 | } |
| 2658 | break; |
| 2659 | case SETTINGS_ROUND_TRIP_TIME: |
| 2660 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdySettingsRTT", |
| 2661 | val, 1, 1200, 100); |
| 2662 | break; |
| 2663 | case SETTINGS_DOWNLOAD_RETRANS_RATE: |
| 2664 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdySettingsRetransRate", |
| 2665 | val, 1, 100, 50); |
| 2666 | break; |
| 2667 | default: |
| 2668 | break; |
| 2669 | } |
| 2670 | } |
| 2671 | } |
| 2672 | } |
| 2673 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2674 | void SpdySession::CompleteStreamRequest(SpdyStreamRequest* pending_request) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 2675 | CHECK(pending_request); |
| 2676 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2677 | PendingStreamRequestCompletionSet::iterator it = |
| 2678 | pending_stream_request_completions_.find(pending_request); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2679 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2680 | // Abort if the request has already been cancelled. |
| 2681 | if (it == pending_stream_request_completions_.end()) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2682 | return; |
| 2683 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2684 | base::WeakPtr<SpdyStream> stream; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2685 | int rv = CreateStream(*pending_request, &stream); |
| 2686 | pending_stream_request_completions_.erase(it); |
| 2687 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2688 | if (rv == OK) { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 2689 | DCHECK(stream.get()); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2690 | pending_request->OnRequestCompleteSuccess(&stream); |
| 2691 | } else { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 2692 | DCHECK(!stream.get()); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2693 | pending_request->OnRequestCompleteFailure(rv); |
| 2694 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
| 2697 | SSLClientSocket* SpdySession::GetSSLClientSocket() const { |
| 2698 | if (!is_secure_) |
| 2699 | return NULL; |
| 2700 | SSLClientSocket* ssl_socket = |
| 2701 | reinterpret_cast<SSLClientSocket*>(connection_->socket()); |
| 2702 | DCHECK(ssl_socket); |
| 2703 | return ssl_socket; |
| 2704 | } |
| 2705 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2706 | void SpdySession::OnWriteBufferConsumed( |
| 2707 | size_t frame_payload_size, |
| 2708 | size_t consume_size, |
| 2709 | SpdyBuffer::ConsumeSource consume_source) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2710 | // We can be called with |in_io_loop_| set if a write SpdyBuffer is |
| 2711 | // deleted (e.g., a stream is closed due to incoming data). |
| 2712 | |
| 2713 | if (availability_state_ == STATE_CLOSED) |
| 2714 | return; |
| 2715 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2716 | DCHECK_EQ(flow_control_state_, FLOW_CONTROL_STREAM_AND_SESSION); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2717 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2718 | if (consume_source == SpdyBuffer::DISCARD) { |
| 2719 | // If we're discarding a frame or part of it, increase the send |
| 2720 | // window by the number of discarded bytes. (Although if we're |
| 2721 | // discarding part of a frame, it's probably because of a write |
| 2722 | // error and we'll be tearing down the session soon.) |
| 2723 | size_t remaining_payload_bytes = std::min(consume_size, frame_payload_size); |
| 2724 | DCHECK_GT(remaining_payload_bytes, 0u); |
| 2725 | IncreaseSendWindowSize(static_cast<int32>(remaining_payload_bytes)); |
| 2726 | } |
| 2727 | // For consumed bytes, the send window is increased when we receive |
| 2728 | // a WINDOW_UPDATE frame. |
| 2729 | } |
| 2730 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2731 | void SpdySession::IncreaseSendWindowSize(int32 delta_window_size) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2732 | // We can be called with |in_io_loop_| set if a SpdyBuffer is |
| 2733 | // deleted (e.g., a stream is closed due to incoming data). |
| 2734 | |
| 2735 | DCHECK_NE(availability_state_, STATE_CLOSED); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2736 | DCHECK_EQ(flow_control_state_, FLOW_CONTROL_STREAM_AND_SESSION); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2737 | DCHECK_GE(delta_window_size, 1); |
| 2738 | |
| 2739 | // Check for overflow. |
| 2740 | int32 max_delta_window_size = kint32max - session_send_window_size_; |
| 2741 | if (delta_window_size > max_delta_window_size) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2742 | RecordProtocolErrorHistogram(PROTOCOL_ERROR_INVALID_WINDOW_UPDATE_SIZE); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2743 | CloseSessionResult result = DoCloseSession( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2744 | ERR_SPDY_PROTOCOL_ERROR, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2745 | "Received WINDOW_UPDATE [delta: " + |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2746 | base::IntToString(delta_window_size) + |
| 2747 | "] for session overflows session_send_window_size_ [current: " + |
| 2748 | base::IntToString(session_send_window_size_) + "]"); |
| 2749 | DCHECK_NE(result, SESSION_ALREADY_CLOSED); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2750 | return; |
| 2751 | } |
| 2752 | |
| 2753 | session_send_window_size_ += delta_window_size; |
| 2754 | |
| 2755 | net_log_.AddEvent( |
| 2756 | NetLog::TYPE_SPDY_SESSION_UPDATE_SEND_WINDOW, |
| 2757 | base::Bind(&NetLogSpdySessionWindowUpdateCallback, |
| 2758 | delta_window_size, session_send_window_size_)); |
| 2759 | |
| 2760 | DCHECK(!IsSendStalled()); |
| 2761 | ResumeSendStalledStreams(); |
| 2762 | } |
| 2763 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2764 | void SpdySession::DecreaseSendWindowSize(int32 delta_window_size) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2765 | DCHECK_NE(availability_state_, STATE_CLOSED); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2766 | DCHECK_EQ(flow_control_state_, FLOW_CONTROL_STREAM_AND_SESSION); |
| 2767 | |
| 2768 | // We only call this method when sending a frame. Therefore, |
| 2769 | // |delta_window_size| should be within the valid frame size range. |
| 2770 | DCHECK_GE(delta_window_size, 1); |
| 2771 | DCHECK_LE(delta_window_size, kMaxSpdyFrameChunkSize); |
| 2772 | |
| 2773 | // |send_window_size_| should have been at least |delta_window_size| for |
| 2774 | // this call to happen. |
| 2775 | DCHECK_GE(session_send_window_size_, delta_window_size); |
| 2776 | |
| 2777 | session_send_window_size_ -= delta_window_size; |
| 2778 | |
| 2779 | net_log_.AddEvent( |
| 2780 | NetLog::TYPE_SPDY_SESSION_UPDATE_SEND_WINDOW, |
| 2781 | base::Bind(&NetLogSpdySessionWindowUpdateCallback, |
| 2782 | -delta_window_size, session_send_window_size_)); |
| 2783 | } |
| 2784 | |
| 2785 | void SpdySession::OnReadBufferConsumed( |
| 2786 | size_t consume_size, |
| 2787 | SpdyBuffer::ConsumeSource consume_source) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2788 | // We can be called with |in_io_loop_| set if a read SpdyBuffer is |
| 2789 | // deleted (e.g., discarded by a SpdyReadQueue). |
| 2790 | |
| 2791 | if (availability_state_ == STATE_CLOSED) |
| 2792 | return; |
| 2793 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2794 | DCHECK_EQ(flow_control_state_, FLOW_CONTROL_STREAM_AND_SESSION); |
| 2795 | DCHECK_GE(consume_size, 1u); |
| 2796 | DCHECK_LE(consume_size, static_cast<size_t>(kint32max)); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2797 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2798 | IncreaseRecvWindowSize(static_cast<int32>(consume_size)); |
| 2799 | } |
| 2800 | |
| 2801 | void SpdySession::IncreaseRecvWindowSize(int32 delta_window_size) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2802 | DCHECK_NE(availability_state_, STATE_CLOSED); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2803 | DCHECK_EQ(flow_control_state_, FLOW_CONTROL_STREAM_AND_SESSION); |
| 2804 | DCHECK_GE(session_unacked_recv_window_bytes_, 0); |
| 2805 | DCHECK_GE(session_recv_window_size_, session_unacked_recv_window_bytes_); |
| 2806 | DCHECK_GE(delta_window_size, 1); |
| 2807 | // Check for overflow. |
| 2808 | DCHECK_LE(delta_window_size, kint32max - session_recv_window_size_); |
| 2809 | |
| 2810 | session_recv_window_size_ += delta_window_size; |
| 2811 | net_log_.AddEvent( |
| 2812 | NetLog::TYPE_SPDY_STREAM_UPDATE_RECV_WINDOW, |
| 2813 | base::Bind(&NetLogSpdySessionWindowUpdateCallback, |
| 2814 | delta_window_size, session_recv_window_size_)); |
| 2815 | |
| 2816 | session_unacked_recv_window_bytes_ += delta_window_size; |
| 2817 | if (session_unacked_recv_window_bytes_ > kSpdySessionInitialWindowSize / 2) { |
| 2818 | SendWindowUpdateFrame(kSessionFlowControlStreamId, |
| 2819 | session_unacked_recv_window_bytes_, |
| 2820 | HIGHEST); |
| 2821 | session_unacked_recv_window_bytes_ = 0; |
| 2822 | } |
| 2823 | } |
| 2824 | |
| 2825 | void SpdySession::DecreaseRecvWindowSize(int32 delta_window_size) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2826 | CHECK(in_io_loop_); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2827 | DCHECK_EQ(flow_control_state_, FLOW_CONTROL_STREAM_AND_SESSION); |
| 2828 | DCHECK_GE(delta_window_size, 1); |
| 2829 | |
| 2830 | // Since we never decrease the initial receive window size, |
| 2831 | // |delta_window_size| should never cause |recv_window_size_| to go |
| 2832 | // negative. If we do, the receive window isn't being respected. |
| 2833 | if (delta_window_size > session_recv_window_size_) { |
| 2834 | RecordProtocolErrorHistogram(PROTOCOL_ERROR_RECEIVE_WINDOW_VIOLATION); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2835 | CloseSessionResult result = DoCloseSession( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2836 | ERR_SPDY_PROTOCOL_ERROR, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2837 | "delta_window_size is " + base::IntToString(delta_window_size) + |
| 2838 | " in DecreaseRecvWindowSize, which is larger than the receive " + |
| 2839 | "window size of " + base::IntToString(session_recv_window_size_)); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2840 | DCHECK_EQ(result, SESSION_CLOSED_BUT_NOT_REMOVED); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 2841 | return; |
| 2842 | } |
| 2843 | |
| 2844 | session_recv_window_size_ -= delta_window_size; |
| 2845 | net_log_.AddEvent( |
| 2846 | NetLog::TYPE_SPDY_SESSION_UPDATE_RECV_WINDOW, |
| 2847 | base::Bind(&NetLogSpdySessionWindowUpdateCallback, |
| 2848 | -delta_window_size, session_recv_window_size_)); |
| 2849 | } |
| 2850 | |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 2851 | void SpdySession::QueueSendStalledStream(const SpdyStream& stream) { |
| 2852 | DCHECK(stream.send_stalled_by_flow_control()); |
| 2853 | stream_send_unstall_queue_[stream.priority()].push_back(stream.stream_id()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
| 2856 | namespace { |
| 2857 | |
| 2858 | // Helper function to return the total size of an array of objects |
| 2859 | // with .size() member functions. |
| 2860 | template <typename T, size_t N> size_t GetTotalSize(const T (&arr)[N]) { |
| 2861 | size_t total_size = 0; |
| 2862 | for (size_t i = 0; i < N; ++i) { |
| 2863 | total_size += arr[i].size(); |
| 2864 | } |
| 2865 | return total_size; |
| 2866 | } |
| 2867 | |
| 2868 | } // namespace |
| 2869 | |
| 2870 | void SpdySession::ResumeSendStalledStreams() { |
| 2871 | DCHECK_EQ(flow_control_state_, FLOW_CONTROL_STREAM_AND_SESSION); |
| 2872 | |
| 2873 | // We don't have to worry about new streams being queued, since |
| 2874 | // doing so would cause IsSendStalled() to return true. But we do |
| 2875 | // have to worry about streams being closed, as well as ourselves |
| 2876 | // being closed. |
| 2877 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 2878 | while (availability_state_ != STATE_CLOSED && !IsSendStalled()) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2879 | size_t old_size = 0; |
| 2880 | if (DCHECK_IS_ON()) |
| 2881 | old_size = GetTotalSize(stream_send_unstall_queue_); |
| 2882 | |
| 2883 | SpdyStreamId stream_id = PopStreamToPossiblyResume(); |
| 2884 | if (stream_id == 0) |
| 2885 | break; |
| 2886 | ActiveStreamMap::const_iterator it = active_streams_.find(stream_id); |
| 2887 | // The stream may actually still be send-stalled after this (due |
| 2888 | // to its own send window) but that's okay -- it'll then be |
| 2889 | // resumed once its send window increases. |
| 2890 | if (it != active_streams_.end()) |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 2891 | it->second.stream->PossiblyResumeIfSendStalled(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 2892 | |
| 2893 | // The size should decrease unless we got send-stalled again. |
| 2894 | if (!IsSendStalled()) |
| 2895 | DCHECK_LT(GetTotalSize(stream_send_unstall_queue_), old_size); |
| 2896 | } |
| 2897 | } |
| 2898 | |
| 2899 | SpdyStreamId SpdySession::PopStreamToPossiblyResume() { |
| 2900 | for (int i = NUM_PRIORITIES - 1; i >= 0; --i) { |
| 2901 | std::deque<SpdyStreamId>* queue = &stream_send_unstall_queue_[i]; |
| 2902 | if (!queue->empty()) { |
| 2903 | SpdyStreamId stream_id = queue->front(); |
| 2904 | queue->pop_front(); |
| 2905 | return stream_id; |
| 2906 | } |
| 2907 | } |
| 2908 | return 0; |
| 2909 | } |
| 2910 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 2911 | } // namespace net |