blob: 957b90ec3aa0b395bc5e67cc01a413598c014e64 [file] [log] [blame]
[email protected]c8b16742010-02-05 20:49:531// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]aea80602009-09-18 00:55:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]dab9c7d2010-02-06 21:44:325#include "net/spdy/spdy_session.h"
[email protected]aea80602009-09-18 00:55:086
7#include "base/basictypes.h"
[email protected]33c477b2010-05-13 19:21:078#include "base/linked_ptr.h"
[email protected]aea80602009-09-18 00:55:089#include "base/logging.h"
10#include "base/message_loop.h"
[email protected]aea80602009-09-18 00:55:0811#include "base/stats_counters.h"
[email protected]a677f2b2009-11-22 00:43:0012#include "base/stl_util-inl.h"
[email protected]528c56d2010-07-30 19:28:4413#include "base/string_number_conversions.h"
[email protected]aea80602009-09-18 00:55:0814#include "base/string_util.h"
[email protected]f1d81922010-07-31 17:47:0915#include "base/utf_string_conversions.h"
[email protected]3f662f12010-03-25 19:56:1216#include "base/time.h"
[email protected]33c477b2010-05-13 19:21:0717#include "base/values.h"
[email protected]5e2e6c77d12009-12-24 21:57:1618#include "net/base/connection_type_histograms.h"
[email protected]aea80602009-09-18 00:55:0819#include "net/base/load_flags.h"
[email protected]9e743cd2010-03-16 07:03:5320#include "net/base/net_log.h"
[email protected]33751562009-10-29 02:17:1121#include "net/base/net_util.h"
[email protected]aea80602009-09-18 00:55:0822#include "net/http/http_network_session.h"
[email protected]1f14a912009-12-21 20:32:4423#include "net/socket/client_socket.h"
[email protected]18c53ae2009-10-01 18:18:5224#include "net/socket/client_socket_factory.h"
25#include "net/socket/ssl_client_socket.h"
[email protected]dab9c7d2010-02-06 21:44:3226#include "net/spdy/spdy_frame_builder.h"
27#include "net/spdy/spdy_protocol.h"
[email protected]74188f22010-04-09 20:18:5028#include "net/spdy/spdy_settings_storage.h"
[email protected]dab9c7d2010-02-06 21:44:3229#include "net/spdy/spdy_stream.h"
[email protected]aea80602009-09-18 00:55:0830
31namespace net {
32
[email protected]a677f2b2009-11-22 00:43:0033namespace {
34
[email protected]9fc38b32010-01-11 21:34:4635#ifdef WIN32
36// We use an artificially small buffer size on windows because the async IO
37// system will artifiially delay IO completions when we use large buffers.
38const int kReadBufferSize = 2 * 1024;
39#else
40const int kReadBufferSize = 8 * 1024;
41#endif
[email protected]a677f2b2009-11-22 00:43:0042
[email protected]1f14a912009-12-21 20:32:4443void AdjustSocketBufferSizes(ClientSocket* socket) {
44 // Adjust socket buffer sizes.
[email protected]955fc2e72010-02-08 20:37:3045 // SPDY uses one socket, and we want a really big buffer.
[email protected]1f14a912009-12-21 20:32:4446 // This greatly helps on links with packet loss - we can even
47 // outperform Vista's dynamic window sizing algorithm.
48 // TODO(mbelshe): more study.
49 const int kSocketBufferSize = 512 * 1024;
50 socket->SetReceiveBufferSize(kSocketBufferSize);
51 socket->SetSendBufferSize(kSocketBufferSize);
52}
53
[email protected]6cd3bd202010-08-30 05:23:0654class NetLogSpdySessionParameter : public NetLog::EventParameters {
55 public:
56 explicit NetLogSpdySessionParameter(const HostPortProxyPair& host_pair)
57 : host_pair_(host_pair) {}
58 virtual Value* ToValue() const {
59 DictionaryValue* dict = new DictionaryValue();
60 dict->Set("host", new StringValue(host_pair_.first.ToString()));
61 dict->Set("proxy", new StringValue(host_pair_.second.ToPacString()));
62 return dict;
63 }
64 private:
65 const HostPortProxyPair host_pair_;
66 DISALLOW_COPY_AND_ASSIGN(NetLogSpdySessionParameter);
67};
68
[email protected]33c477b2010-05-13 19:21:0769class NetLogSpdySynParameter : public NetLog::EventParameters {
70 public:
71 NetLogSpdySynParameter(const linked_ptr<spdy::SpdyHeaderBlock>& headers,
72 spdy::SpdyControlFlags flags,
73 spdy::SpdyStreamId id)
74 : headers_(headers), flags_(flags), id_(id) {}
75
76 Value* ToValue() const {
77 DictionaryValue* dict = new DictionaryValue();
[email protected]815d63d92010-08-04 18:14:1578 ListValue* headers_list = new ListValue();
[email protected]33c477b2010-05-13 19:21:0779 for (spdy::SpdyHeaderBlock::const_iterator it = headers_->begin();
80 it != headers_->end(); ++it) {
[email protected]815d63d92010-08-04 18:14:1581 headers_list->Append(new StringValue(StringPrintf("%s: %s",
82 it->first.c_str(),
83 it->second.c_str())));
[email protected]33c477b2010-05-13 19:21:0784 }
[email protected]ccaff652010-07-31 06:28:2085 dict->SetInteger("flags", flags_);
[email protected]815d63d92010-08-04 18:14:1586 dict->Set("headers", headers_list);
[email protected]ccaff652010-07-31 06:28:2087 dict->SetInteger("id", id_);
[email protected]33c477b2010-05-13 19:21:0788 return dict;
89 }
90
91 private:
92 ~NetLogSpdySynParameter() {}
93
94 const linked_ptr<spdy::SpdyHeaderBlock> headers_;
95 spdy::SpdyControlFlags flags_;
96 spdy::SpdyStreamId id_;
97
98 DISALLOW_COPY_AND_ASSIGN(NetLogSpdySynParameter);
99};
100
[email protected]44297002010-05-16 00:59:10101class NetLogSpdySettingsParameter : public NetLog::EventParameters {
102 public:
103 explicit NetLogSpdySettingsParameter(const spdy::SpdySettings& settings)
104 : settings_(settings) {}
105
106 Value* ToValue() const {
107 DictionaryValue* dict = new DictionaryValue();
108 ListValue* settings = new ListValue();
109 for (spdy::SpdySettings::const_iterator it = settings_.begin();
110 it != settings_.end(); ++it) {
111 settings->Append(new StringValue(
112 StringPrintf("[%u:%u]", it->first.id(), it->second)));
113 }
[email protected]ccaff652010-07-31 06:28:20114 dict->Set("settings", settings);
[email protected]44297002010-05-16 00:59:10115 return dict;
116 }
117
118 private:
119 ~NetLogSpdySettingsParameter() {}
120
121 const spdy::SpdySettings settings_;
122
123 DISALLOW_COPY_AND_ASSIGN(NetLogSpdySettingsParameter);
124};
125
[email protected]a677f2b2009-11-22 00:43:00126} // namespace
127
[email protected]aea80602009-09-18 00:55:08128// static
[email protected]955fc2e72010-02-08 20:37:30129bool SpdySession::use_ssl_ = true;
[email protected]aea80602009-09-18 00:55:08130
[email protected]7349c6b12010-07-22 02:29:16131// static
[email protected]c25b15c12010-07-27 19:55:33132bool SpdySession::use_flow_control_ = false;
[email protected]7349c6b12010-07-22 02:29:16133
[email protected]b261d0e2010-08-02 19:13:24134SpdySession::SpdySession(const HostPortProxyPair& host_port_proxy_pair,
[email protected]635909f2010-05-12 18:19:36135 HttpNetworkSession* session,
[email protected]44297002010-05-16 00:59:10136 NetLog* net_log)
[email protected]aea80602009-09-18 00:55:08137 : ALLOW_THIS_IN_INITIALIZER_LIST(
[email protected]955fc2e72010-02-08 20:37:30138 connect_callback_(this, &SpdySession::OnTCPConnect)),
[email protected]18c53ae2009-10-01 18:18:52139 ALLOW_THIS_IN_INITIALIZER_LIST(
[email protected]955fc2e72010-02-08 20:37:30140 ssl_connect_callback_(this, &SpdySession::OnSSLConnect)),
[email protected]aea80602009-09-18 00:55:08141 ALLOW_THIS_IN_INITIALIZER_LIST(
[email protected]955fc2e72010-02-08 20:37:30142 read_callback_(this, &SpdySession::OnReadComplete)),
[email protected]aea80602009-09-18 00:55:08143 ALLOW_THIS_IN_INITIALIZER_LIST(
[email protected]955fc2e72010-02-08 20:37:30144 write_callback_(this, &SpdySession::OnWriteComplete)),
[email protected]b261d0e2010-08-02 19:13:24145 host_port_proxy_pair_(host_port_proxy_pair),
[email protected]aea80602009-09-18 00:55:08146 session_(session),
[email protected]1f14a912009-12-21 20:32:44147 connection_(new ClientSocketHandle),
[email protected]230cadd2009-09-22 16:33:59148 read_buffer_(new IOBuffer(kReadBufferSize)),
[email protected]aea80602009-09-18 00:55:08149 read_pending_(false),
[email protected]ba051e312009-10-07 22:12:33150 stream_hi_water_mark_(1), // Always start at 1 for the first stream id.
[email protected]dd11b932009-11-30 19:39:48151 write_pending_(false),
[email protected]60253bd2009-12-01 01:16:39152 delayed_write_pending_(false),
[email protected]dcc6bbb2009-12-09 19:09:01153 is_secure_(false),
[email protected]bdbda462010-06-28 17:30:37154 certificate_error_code_(OK),
[email protected]60253bd2009-12-01 01:16:39155 error_(OK),
[email protected]9b010802009-12-27 22:55:30156 state_(IDLE),
[email protected]2bd93022010-07-17 00:58:44157 max_concurrent_streams_(kDefaultMaxConcurrentStreams),
[email protected]9b010802009-12-27 22:55:30158 streams_initiated_count_(0),
159 streams_pushed_count_(0),
160 streams_pushed_and_claimed_count_(0),
[email protected]4b4762a2010-04-23 16:04:14161 streams_abandoned_count_(0),
[email protected]58cebf8f2010-07-31 19:20:16162 frames_received_(0),
[email protected]0aebc002010-05-21 06:50:19163 sent_settings_(false),
164 received_settings_(false),
[email protected]635909f2010-05-12 18:19:36165 in_session_pool_(true),
[email protected]7349c6b12010-07-22 02:29:16166 initial_send_window_size_(spdy::kInitialWindowSize),
[email protected]450c5022010-08-26 02:38:28167 initial_recv_window_size_(spdy::kInitialWindowSize),
[email protected]44297002010-05-16 00:59:10168 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SPDY_SESSION)) {
169 net_log_.BeginEvent(
170 NetLog::TYPE_SPDY_SESSION,
[email protected]6cd3bd202010-08-30 05:23:06171 new NetLogSpdySessionParameter(host_port_proxy_pair_));
[email protected]44297002010-05-16 00:59:10172
[email protected]ba051e312009-10-07 22:12:33173 // TODO(mbelshe): consider randomization of the stream_hi_water_mark.
[email protected]aea80602009-09-18 00:55:08174
[email protected]955fc2e72010-02-08 20:37:30175 spdy_framer_.set_visitor(this);
[email protected]18c53ae2009-10-01 18:18:52176
177 session_->ssl_config_service()->GetSSLConfig(&ssl_config_);
[email protected]74188f22010-04-09 20:18:50178
179 SendSettings();
[email protected]aea80602009-09-18 00:55:08180}
181
[email protected]955fc2e72010-02-08 20:37:30182SpdySession::~SpdySession() {
[email protected]b278eb72010-07-09 20:17:00183 state_ = CLOSED;
184
[email protected]93300672009-10-24 13:22:51185 // Cleanup all the streams.
186 CloseAllStreams(net::ERR_ABORTED);
187
[email protected]1f14a912009-12-21 20:32:44188 if (connection_->is_initialized()) {
[email protected]955fc2e72010-02-08 20:37:30189 // With Spdy we can't recycle sockets.
[email protected]1f14a912009-12-21 20:32:44190 connection_->socket()->Disconnect();
[email protected]aea80602009-09-18 00:55:08191 }
[email protected]d1eda932009-11-04 01:03:10192
[email protected]19ec8a72010-08-23 03:38:23193 // Streams should all be gone now.
194 DCHECK_EQ(0u, num_active_streams());
195 DCHECK_EQ(0u, num_unclaimed_pushed_streams());
196
[email protected]0aebc002010-05-21 06:50:19197 RecordHistograms();
[email protected]44297002010-05-16 00:59:10198
199 net_log_.EndEvent(NetLog::TYPE_SPDY_SESSION, NULL);
[email protected]aea80602009-09-18 00:55:08200}
201
[email protected]9e9e842e2010-07-23 23:09:15202net::Error SpdySession::InitializeWithSocket(
[email protected]bdbda462010-06-28 17:30:37203 ClientSocketHandle* connection,
[email protected]9e9e842e2010-07-23 23:09:15204 bool is_secure,
[email protected]bdbda462010-06-28 17:30:37205 int certificate_error_code) {
[email protected]955fc2e72010-02-08 20:37:30206 static StatsCounter spdy_sessions("spdy.sessions");
207 spdy_sessions.Increment();
[email protected]1f14a912009-12-21 20:32:44208
209 AdjustSocketBufferSizes(connection->socket());
210
211 state_ = CONNECTED;
212 connection_.reset(connection);
[email protected]9e9e842e2010-07-23 23:09:15213 is_secure_ = is_secure;
[email protected]bdbda462010-06-28 17:30:37214 certificate_error_code_ = certificate_error_code;
[email protected]1f14a912009-12-21 20:32:44215
216 // This is a newly initialized session that no client should have a handle to
217 // yet, so there's no need to start writing data as in OnTCPConnect(), but we
218 // should start reading data.
[email protected]26ef6582010-06-24 02:30:47219 net::Error error = ReadSocket();
220 if (error == ERR_IO_PENDING)
221 return OK;
222 return error;
[email protected]1f14a912009-12-21 20:32:44223}
224
[email protected]df4b4ef2010-07-12 18:25:21225net::Error SpdySession::Connect(
226 const std::string& group_name,
227 const scoped_refptr<TCPSocketParams>& destination,
228 RequestPriority priority) {
[email protected]c9c6f5c2010-07-31 01:30:03229 DCHECK(priority >= net::HIGHEST && priority < net::NUM_PRIORITIES);
[email protected]aea80602009-09-18 00:55:08230
231 // If the connect process is started, let the caller continue.
[email protected]60253bd2009-12-01 01:16:39232 if (state_ > IDLE)
[email protected]aea80602009-09-18 00:55:08233 return net::OK;
234
[email protected]60253bd2009-12-01 01:16:39235 state_ = CONNECTING;
[email protected]aea80602009-09-18 00:55:08236
[email protected]955fc2e72010-02-08 20:37:30237 static StatsCounter spdy_sessions("spdy.sessions");
238 spdy_sessions.Increment();
[email protected]aea80602009-09-18 00:55:08239
[email protected]7fc5b09a2010-02-27 00:07:38240 int rv = connection_->Init(group_name, destination, priority,
241 &connect_callback_, session_->tcp_socket_pool(),
[email protected]635909f2010-05-12 18:19:36242 net_log_);
[email protected]56e7c63d2010-07-12 18:42:17243 DCHECK(rv <= 0);
[email protected]aea80602009-09-18 00:55:08244
245 // If the connect is pending, we still return ok. The APIs enqueue
246 // work until after the connect completes asynchronously later.
247 if (rv == net::ERR_IO_PENDING)
248 return net::OK;
[email protected]651b77c2010-03-10 19:29:42249 OnTCPConnect(rv);
[email protected]aea80602009-09-18 00:55:08250 return static_cast<net::Error>(rv);
251}
252
[email protected]bdbda462010-06-28 17:30:37253int SpdySession::GetPushStream(
[email protected]9be804c82010-06-24 17:59:46254 const GURL& url,
[email protected]bdbda462010-06-28 17:30:37255 scoped_refptr<SpdyStream>* stream,
[email protected]33c477b2010-05-13 19:21:07256 const BoundNetLog& stream_net_log) {
[email protected]26ef6582010-06-24 02:30:47257 CHECK_NE(state_, CLOSED);
[email protected]bdbda462010-06-28 17:30:37258
259 *stream = NULL;
260
261 // Don't allow access to secure push streams over an unauthenticated, but
262 // encrypted SSL socket.
263 if (is_secure_ && certificate_error_code_ != OK &&
264 (url.SchemeIs("https") || url.SchemeIs("wss"))) {
265 LOG(DFATAL) << "Tried to get pushed spdy stream for secure content over an "
266 << "unauthenticated session.";
267 return certificate_error_code_;
268 }
269
[email protected]a677f2b2009-11-22 00:43:00270 const std::string& path = url.PathForRequest();
[email protected]aea80602009-09-18 00:55:08271
[email protected]bdbda462010-06-28 17:30:37272 *stream = GetActivePushStream(path);
273 if (stream->get()) {
[email protected]9be804c82010-06-24 17:59:46274 DCHECK(streams_pushed_and_claimed_count_ < streams_pushed_count_);
275 streams_pushed_and_claimed_count_++;
[email protected]bdbda462010-06-28 17:30:37276 return OK;
[email protected]aea80602009-09-18 00:55:08277 }
[email protected]e3ebba0f2010-08-05 17:59:58278 return NULL;
[email protected]9be804c82010-06-24 17:59:46279}
280
[email protected]bdbda462010-06-28 17:30:37281int SpdySession::CreateStream(
[email protected]9be804c82010-06-24 17:59:46282 const GURL& url,
283 RequestPriority priority,
[email protected]bdbda462010-06-28 17:30:37284 scoped_refptr<SpdyStream>* spdy_stream,
[email protected]2bd93022010-07-17 00:58:44285 const BoundNetLog& stream_net_log,
[email protected]971746e2010-07-21 03:02:23286 CompletionCallback* callback) {
[email protected]2bd93022010-07-17 00:58:44287 if (!max_concurrent_streams_ ||
288 active_streams_.size() < max_concurrent_streams_) {
289 return CreateStreamImpl(url, priority, spdy_stream, stream_net_log);
290 }
291
292 create_stream_queues_[priority].push(
293 PendingCreateStream(url, priority, spdy_stream,
[email protected]971746e2010-07-21 03:02:23294 stream_net_log, callback));
[email protected]2bd93022010-07-17 00:58:44295 return ERR_IO_PENDING;
296}
297
298void SpdySession::ProcessPendingCreateStreams() {
299 while (!max_concurrent_streams_ ||
300 active_streams_.size() < max_concurrent_streams_) {
301 bool no_pending_create_streams = true;
302 for (int i = 0;i < NUM_PRIORITIES;++i) {
303 if (!create_stream_queues_[i].empty()) {
304 PendingCreateStream& pending_create = create_stream_queues_[i].front();
305 no_pending_create_streams = false;
306 int error = CreateStreamImpl(*pending_create.url,
307 pending_create.priority,
308 pending_create.spdy_stream,
309 *pending_create.stream_net_log);
310 pending_create.callback->Run(error);
311 create_stream_queues_[i].pop();
312 break;
313 }
314 }
315 if (no_pending_create_streams)
316 return; // there were no streams in any queue
317 }
318}
319
320void SpdySession::CancelPendingCreateStreams(
[email protected]971746e2010-07-21 03:02:23321 const scoped_refptr<SpdyStream>* spdy_stream) {
[email protected]2bd93022010-07-17 00:58:44322 for (int i = 0;i < NUM_PRIORITIES;++i) {
323 PendingCreateStreamQueue tmp;
324 // Make a copy removing this trans
325 while (!create_stream_queues_[i].empty()) {
326 PendingCreateStream& pending_create = create_stream_queues_[i].front();
[email protected]971746e2010-07-21 03:02:23327 if (pending_create.spdy_stream != spdy_stream)
[email protected]2bd93022010-07-17 00:58:44328 tmp.push(pending_create);
329 create_stream_queues_[i].pop();
330 }
331 // Now copy it back
332 while (!tmp.empty()) {
333 create_stream_queues_[i].push(tmp.front());
334 tmp.pop();
335 }
336 }
337}
338
339int SpdySession::CreateStreamImpl(
340 const GURL& url,
341 RequestPriority priority,
342 scoped_refptr<SpdyStream>* spdy_stream,
[email protected]9be804c82010-06-24 17:59:46343 const BoundNetLog& stream_net_log) {
[email protected]bdbda462010-06-28 17:30:37344 // Make sure that we don't try to send https/wss over an unauthenticated, but
345 // encrypted SSL socket.
346 if (is_secure_ && certificate_error_code_ != OK &&
347 (url.SchemeIs("https") || url.SchemeIs("wss"))) {
348 LOG(DFATAL) << "Tried to create spdy stream for secure content over an "
349 << "unauthenticated session.";
350 return certificate_error_code_;
351 }
352
[email protected]9be804c82010-06-24 17:59:46353 const std::string& path = url.PathForRequest();
[email protected]18c53ae2009-10-01 18:18:52354
[email protected]955fc2e72010-02-08 20:37:30355 const spdy::SpdyStreamId stream_id = GetNewStreamId();
[email protected]aea80602009-09-18 00:55:08356
[email protected]bdbda462010-06-28 17:30:37357 *spdy_stream = new SpdyStream(this, stream_id, false);
358 const scoped_refptr<SpdyStream>& stream = *spdy_stream;
[email protected]9be804c82010-06-24 17:59:46359
360 stream->set_priority(priority);
[email protected]a677f2b2009-11-22 00:43:00361 stream->set_path(path);
[email protected]33c477b2010-05-13 19:21:07362 stream->set_net_log(stream_net_log);
[email protected]7349c6b12010-07-22 02:29:16363 stream->set_send_window_size(initial_send_window_size_);
[email protected]450c5022010-08-26 02:38:28364 stream->set_recv_window_size(initial_recv_window_size_);
[email protected]a677f2b2009-11-22 00:43:00365 ActivateStream(stream);
366
[email protected]9b010802009-12-27 22:55:30367 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdyPriorityCount",
[email protected]9be804c82010-06-24 17:59:46368 static_cast<int>(priority), 0, 10, 11);
[email protected]9b010802009-12-27 22:55:30369
[email protected]955fc2e72010-02-08 20:37:30370 LOG(INFO) << "SpdyStream: Creating stream " << stream_id << " for " << url;
[email protected]aea80602009-09-18 00:55:08371 // TODO(mbelshe): Optimize memory allocations
[email protected]c9c6f5c2010-07-31 01:30:03372 DCHECK(priority >= net::HIGHEST && priority < net::NUM_PRIORITIES);
[email protected]aea80602009-09-18 00:55:08373
[email protected]9be804c82010-06-24 17:59:46374 DCHECK_EQ(active_streams_[stream_id].get(), stream.get());
[email protected]bdbda462010-06-28 17:30:37375 return OK;
[email protected]9be804c82010-06-24 17:59:46376}
[email protected]aea80602009-09-18 00:55:08377
[email protected]9be804c82010-06-24 17:59:46378int SpdySession::WriteSynStream(
379 spdy::SpdyStreamId stream_id,
380 RequestPriority priority,
381 spdy::SpdyControlFlags flags,
382 const linked_ptr<spdy::SpdyHeaderBlock>& headers) {
383 // Find our stream
384 if (!IsStreamActive(stream_id))
385 return ERR_INVALID_SPDY_STREAM;
386 const scoped_refptr<SpdyStream>& stream = active_streams_[stream_id];
387 CHECK_EQ(stream->stream_id(), stream_id);
[email protected]72552f02009-10-28 15:25:01388
[email protected]955fc2e72010-02-08 20:37:30389 scoped_ptr<spdy::SpdySynStreamControlFrame> syn_frame(
[email protected]c9c6f5c2010-07-31 01:30:03390 spdy_framer_.CreateSynStream(
391 stream_id, 0,
392 ConvertRequestPriorityToSpdyPriority(priority),
393 flags, false, headers.get()));
[email protected]9be804c82010-06-24 17:59:46394 QueueFrame(syn_frame.get(), priority, stream);
[email protected]aea80602009-09-18 00:55:08395
[email protected]955fc2e72010-02-08 20:37:30396 static StatsCounter spdy_requests("spdy.requests");
397 spdy_requests.Increment();
[email protected]9b010802009-12-27 22:55:30398 streams_initiated_count_++;
[email protected]aea80602009-09-18 00:55:08399
[email protected]9be804c82010-06-24 17:59:46400 const BoundNetLog& log = stream->net_log();
[email protected]095c7cf2010-08-31 21:07:33401 if (log.IsLoggingAll()) {
[email protected]9be804c82010-06-24 17:59:46402 log.AddEvent(
[email protected]33c477b2010-05-13 19:21:07403 NetLog::TYPE_SPDY_STREAM_SYN_STREAM,
404 new NetLogSpdySynParameter(headers, flags, stream_id));
405 }
[email protected]aea80602009-09-18 00:55:08406
[email protected]9be804c82010-06-24 17:59:46407 return ERR_IO_PENDING;
[email protected]aea80602009-09-18 00:55:08408}
409
[email protected]955fc2e72010-02-08 20:37:30410int SpdySession::WriteStreamData(spdy::SpdyStreamId stream_id,
[email protected]4f386422010-07-20 04:19:49411 net::IOBuffer* data, int len,
412 spdy::SpdyDataFlags flags) {
[email protected]967dd5412009-11-21 23:33:30413 LOG(INFO) << "Writing Stream Data for stream " << stream_id << " (" << len
414 << " bytes)";
[email protected]ff57bb82009-11-12 06:52:14415
416 // Find our stream
[email protected]56e7c63d2010-07-12 18:42:17417 DCHECK(IsStreamActive(stream_id));
418 scoped_refptr<SpdyStream> stream = active_streams_[stream_id];
[email protected]9c6527c2010-07-12 18:16:14419 CHECK_EQ(stream->stream_id(), stream_id);
[email protected]56e7c63d2010-07-12 18:42:17420 if (!stream)
421 return ERR_INVALID_SPDY_STREAM;
[email protected]ff57bb82009-11-12 06:52:14422
[email protected]955fc2e72010-02-08 20:37:30423 if (len > kMaxSpdyFrameChunkSize) {
424 len = kMaxSpdyFrameChunkSize;
[email protected]310240592010-08-05 21:04:19425 flags = static_cast<spdy::SpdyDataFlags>(flags & ~spdy::DATA_FLAG_FIN);
[email protected]ff57bb82009-11-12 06:52:14426 }
427
[email protected]7349c6b12010-07-22 02:29:16428 // Obey send window size of the stream if flow control is enabled.
429 if (use_flow_control_) {
[email protected]6abf5372010-08-17 15:44:25430 if (stream->send_window_size() <= 0) {
431 stream->set_stalled_by_flow_control(true);
[email protected]7349c6b12010-07-22 02:29:16432 return ERR_IO_PENDING;
[email protected]6abf5372010-08-17 15:44:25433 }
[email protected]310240592010-08-05 21:04:19434 int new_len = std::min(len, stream->send_window_size());
435 if (new_len < len) {
436 len = new_len;
437 flags = static_cast<spdy::SpdyDataFlags>(flags & ~spdy::DATA_FLAG_FIN);
438 }
[email protected]7349c6b12010-07-22 02:29:16439 stream->DecreaseSendWindowSize(len);
440 }
441
[email protected]ff57bb82009-11-12 06:52:14442 // TODO(mbelshe): reduce memory copies here.
[email protected]955fc2e72010-02-08 20:37:30443 scoped_ptr<spdy::SpdyDataFrame> frame(
444 spdy_framer_.CreateDataFrame(stream_id, data->data(), len, flags));
[email protected]74188f22010-04-09 20:18:50445 QueueFrame(frame.get(), stream->priority(), stream);
[email protected]ff57bb82009-11-12 06:52:14446 return ERR_IO_PENDING;
447}
448
[email protected]56e7c63d2010-07-12 18:42:17449void SpdySession::CloseStream(spdy::SpdyStreamId stream_id, int status) {
450 LOG(INFO) << "Closing stream " << stream_id << " with status " << status;
451 // TODO(mbelshe): We should send a RST_STREAM control frame here
452 // so that the server can cancel a large send.
[email protected]92683b52009-12-21 21:01:12453
[email protected]56e7c63d2010-07-12 18:42:17454 DeleteStream(stream_id, status);
[email protected]aea80602009-09-18 00:55:08455}
456
[email protected]5af3c572010-07-20 14:16:27457void SpdySession::ResetStream(
458 spdy::SpdyStreamId stream_id, spdy::SpdyStatusCodes status) {
[email protected]5af3c572010-07-20 14:16:27459 LOG(INFO) << "Sending a RST_STREAM frame for stream " << stream_id
460 << " with status " << status;
461
462 scoped_ptr<spdy::SpdyRstStreamControlFrame> rst_frame(
463 spdy_framer_.CreateRstStream(stream_id, status));
[email protected]e3ebba0f2010-08-05 17:59:58464
465 // Default to lowest priority unless we know otherwise.
466 int priority = 3;
467 if(IsStreamActive(stream_id)) {
468 scoped_refptr<SpdyStream> stream = active_streams_[stream_id];
469 priority = stream->priority();
470 }
471 QueueFrame(rst_frame.get(), priority, NULL);
[email protected]5af3c572010-07-20 14:16:27472
473 DeleteStream(stream_id, ERR_SPDY_PROTOCOL_ERROR);
474}
475
[email protected]955fc2e72010-02-08 20:37:30476bool SpdySession::IsStreamActive(spdy::SpdyStreamId stream_id) const {
[email protected]a677f2b2009-11-22 00:43:00477 return ContainsKey(active_streams_, stream_id);
[email protected]aea80602009-09-18 00:55:08478}
479
[email protected]955fc2e72010-02-08 20:37:30480LoadState SpdySession::GetLoadState() const {
[email protected]9be4f02c2010-01-07 18:30:09481 // NOTE: The application only queries the LoadState via the
[email protected]955fc2e72010-02-08 20:37:30482 // SpdyNetworkTransaction, and details are only needed when
[email protected]9be4f02c2010-01-07 18:30:09483 // we're in the process of connecting.
484
485 // If we're connecting, defer to the connection to give us the actual
486 // LoadState.
487 if (state_ == CONNECTING)
488 return connection_->GetLoadState();
489
490 // Just report that we're idle since the session could be doing
491 // many things concurrently.
492 return LOAD_STATE_IDLE;
[email protected]aea80602009-09-18 00:55:08493}
494
[email protected]955fc2e72010-02-08 20:37:30495void SpdySession::OnTCPConnect(int result) {
496 LOG(INFO) << "Spdy socket connected (result=" << result << ")";
[email protected]aea80602009-09-18 00:55:08497
[email protected]5e2e6c77d12009-12-24 21:57:16498 // We shouldn't be coming through this path if we didn't just open a fresh
499 // socket (or have an error trying to do so).
500 DCHECK(!connection_->socket() || !connection_->is_reused());
501
[email protected]aea80602009-09-18 00:55:08502 if (result != net::OK) {
[email protected]353f6162009-12-10 18:04:12503 DCHECK_LT(result, 0);
[email protected]a01ea222010-08-19 16:50:53504 CloseSessionOnError(static_cast<net::Error>(result), true);
[email protected]aea80602009-09-18 00:55:08505 return;
[email protected]616925a2010-03-02 19:02:38506 } else {
507 UpdateConnectionTypeHistograms(CONNECTION_SPDY);
[email protected]aea80602009-09-18 00:55:08508 }
509
[email protected]1f14a912009-12-21 20:32:44510 AdjustSocketBufferSizes(connection_->socket());
[email protected]aea80602009-09-18 00:55:08511
[email protected]affe8fe2009-10-14 20:06:05512 if (use_ssl_) {
[email protected]18c53ae2009-10-01 18:18:52513 // Add a SSL socket on top of our existing transport socket.
[email protected]1f14a912009-12-21 20:32:44514 ClientSocket* socket = connection_->release_socket();
[email protected]18c53ae2009-10-01 18:18:52515 // TODO(mbelshe): Fix the hostname. This is BROKEN without having
516 // a real hostname.
517 socket = session_->socket_factory()->CreateSSLClientSocket(
518 socket, "" /* request_->url.HostNoBrackets() */ , ssl_config_);
[email protected]1f14a912009-12-21 20:32:44519 connection_->set_socket(socket);
[email protected]dcc6bbb2009-12-09 19:09:01520 is_secure_ = true;
[email protected]a2006ece2010-04-23 16:44:02521 int status = connection_->socket()->Connect(&ssl_connect_callback_);
[email protected]843c5072009-12-04 22:09:47522 if (status != ERR_IO_PENDING)
523 OnSSLConnect(status);
[email protected]18c53ae2009-10-01 18:18:52524 } else {
[email protected]60253bd2009-12-01 01:16:39525 DCHECK_EQ(state_, CONNECTING);
526 state_ = CONNECTED;
[email protected]18c53ae2009-10-01 18:18:52527
528 // Make sure we get any pending data sent.
529 WriteSocketLater();
530 // Start reading
531 ReadSocket();
532 }
533}
534
[email protected]955fc2e72010-02-08 20:37:30535void SpdySession::OnSSLConnect(int result) {
[email protected]18c53ae2009-10-01 18:18:52536 // TODO(mbelshe): We need to replicate the functionality of
537 // HttpNetworkTransaction::DoSSLConnectComplete here, where it calls
538 // HandleCertificateError() and such.
539 if (IsCertificateError(result))
540 result = OK; // TODO(mbelshe): pretend we're happy anyway.
541
[email protected]93300672009-10-24 13:22:51542 if (result == OK) {
[email protected]60253bd2009-12-01 01:16:39543 DCHECK_EQ(state_, CONNECTING);
544 state_ = CONNECTED;
[email protected]18c53ae2009-10-01 18:18:52545
[email protected]93300672009-10-24 13:22:51546 // After we've connected, send any data to the server, and then issue
547 // our read.
548 WriteSocketLater();
549 ReadSocket();
550 } else {
[email protected]353f6162009-12-10 18:04:12551 DCHECK_LT(result, 0); // It should be an error, not a byte count.
[email protected]a01ea222010-08-19 16:50:53552 CloseSessionOnError(static_cast<net::Error>(result), true);
[email protected]93300672009-10-24 13:22:51553 }
[email protected]aea80602009-09-18 00:55:08554}
555
[email protected]955fc2e72010-02-08 20:37:30556void SpdySession::OnReadComplete(int bytes_read) {
[email protected]aea80602009-09-18 00:55:08557 // Parse a frame. For now this code requires that the frame fit into our
558 // buffer (32KB).
559 // TODO(mbelshe): support arbitrarily large frames!
560
[email protected]955fc2e72010-02-08 20:37:30561 LOG(INFO) << "Spdy socket read: " << bytes_read << " bytes";
[email protected]aea80602009-09-18 00:55:08562
563 read_pending_ = false;
564
565 if (bytes_read <= 0) {
566 // Session is tearing down.
[email protected]353f6162009-12-10 18:04:12567 net::Error error = static_cast<net::Error>(bytes_read);
[email protected]bce9f682010-03-30 16:36:02568 if (bytes_read == 0) {
569 LOG(INFO) << "Spdy socket closed by server[" <<
570 host_port_pair().ToString() << "].";
[email protected]353f6162009-12-10 18:04:12571 error = ERR_CONNECTION_CLOSED;
[email protected]bce9f682010-03-30 16:36:02572 }
[email protected]a01ea222010-08-19 16:50:53573 CloseSessionOnError(error, true);
[email protected]aea80602009-09-18 00:55:08574 return;
575 }
576
[email protected]955fc2e72010-02-08 20:37:30577 // The SpdyFramer will use callbacks onto |this| as it parses frames.
[email protected]94d78132010-01-22 00:53:00578 // When errors occur, those callbacks can lead to teardown of all references
579 // to |this|, so maintain a reference to self during this call for safe
580 // cleanup.
[email protected]955fc2e72010-02-08 20:37:30581 scoped_refptr<SpdySession> self(this);
[email protected]94d78132010-01-22 00:53:00582
[email protected]aea80602009-09-18 00:55:08583 char *data = read_buffer_->data();
584 while (bytes_read &&
[email protected]955fc2e72010-02-08 20:37:30585 spdy_framer_.error_code() == spdy::SpdyFramer::SPDY_NO_ERROR) {
586 uint32 bytes_processed = spdy_framer_.ProcessInput(data, bytes_read);
[email protected]aea80602009-09-18 00:55:08587 bytes_read -= bytes_processed;
588 data += bytes_processed;
[email protected]955fc2e72010-02-08 20:37:30589 if (spdy_framer_.state() == spdy::SpdyFramer::SPDY_DONE)
590 spdy_framer_.Reset();
[email protected]aea80602009-09-18 00:55:08591 }
[email protected]57f123222010-01-19 22:00:28592
[email protected]94d78132010-01-22 00:53:00593 if (state_ != CLOSED)
594 ReadSocket();
[email protected]aea80602009-09-18 00:55:08595}
596
[email protected]955fc2e72010-02-08 20:37:30597void SpdySession::OnWriteComplete(int result) {
[email protected]aea80602009-09-18 00:55:08598 DCHECK(write_pending_);
[email protected]bf2491a92009-11-29 16:39:48599 DCHECK(in_flight_write_.size());
[email protected]bf2491a92009-11-29 16:39:48600
[email protected]aea80602009-09-18 00:55:08601 write_pending_ = false;
602
[email protected]74188f22010-04-09 20:18:50603 scoped_refptr<SpdyStream> stream = in_flight_write_.stream();
604
605 LOG(INFO) << "Spdy write complete (result=" << result << ")"
[email protected]8549f5e2010-04-16 22:39:09606 << (stream ? std::string(" for stream ") +
[email protected]528c56d2010-07-30 19:28:44607 base::IntToString(stream->stream_id()) : "");
[email protected]aea80602009-09-18 00:55:08608
[email protected]93300672009-10-24 13:22:51609 if (result >= 0) {
[email protected]bf2491a92009-11-29 16:39:48610 // It should not be possible to have written more bytes than our
611 // in_flight_write_.
612 DCHECK_LE(result, in_flight_write_.buffer()->BytesRemaining());
[email protected]eebc0b42009-11-04 00:17:13613
[email protected]bf2491a92009-11-29 16:39:48614 in_flight_write_.buffer()->DidConsume(result);
[email protected]ff57bb82009-11-12 06:52:14615
[email protected]bf2491a92009-11-29 16:39:48616 // We only notify the stream when we've fully written the pending frame.
617 if (!in_flight_write_.buffer()->BytesRemaining()) {
[email protected]74188f22010-04-09 20:18:50618 if (stream) {
619 // Report the number of bytes written to the caller, but exclude the
620 // frame size overhead. NOTE: if this frame was compressed the
621 // reported bytes written is the compressed size, not the original
622 // size.
[email protected]56e7c63d2010-07-12 18:42:17623 if (result > 0) {
624 result = in_flight_write_.buffer()->size();
625 DCHECK_GT(result, static_cast<int>(spdy::SpdyFrame::size()));
626 result -= static_cast<int>(spdy::SpdyFrame::size());
627 }
[email protected]bf2491a92009-11-29 16:39:48628
[email protected]74188f22010-04-09 20:18:50629 // It is possible that the stream was cancelled while we were writing
630 // to the socket.
631 if (!stream->cancelled())
632 stream->OnWriteComplete(result);
[email protected]bf2491a92009-11-29 16:39:48633 }
[email protected]92683b52009-12-21 21:01:12634
[email protected]bf2491a92009-11-29 16:39:48635 // Cleanup the write which just completed.
636 in_flight_write_.release();
[email protected]ff57bb82009-11-12 06:52:14637 }
[email protected]aea80602009-09-18 00:55:08638
[email protected]93300672009-10-24 13:22:51639 // Write more data. We're already in a continuation, so we can
640 // go ahead and write it immediately (without going back to the
641 // message loop).
642 WriteSocketLater();
643 } else {
[email protected]6572f122009-11-30 18:23:13644 in_flight_write_.release();
[email protected]dd11b932009-11-30 19:39:48645
[email protected]bf2491a92009-11-29 16:39:48646 // The stream is now errored. Close it down.
[email protected]a01ea222010-08-19 16:50:53647 CloseSessionOnError(static_cast<net::Error>(result), true);
[email protected]93300672009-10-24 13:22:51648 }
[email protected]aea80602009-09-18 00:55:08649}
650
[email protected]26ef6582010-06-24 02:30:47651net::Error SpdySession::ReadSocket() {
[email protected]aea80602009-09-18 00:55:08652 if (read_pending_)
[email protected]26ef6582010-06-24 02:30:47653 return OK;
[email protected]aea80602009-09-18 00:55:08654
[email protected]94d78132010-01-22 00:53:00655 if (state_ == CLOSED) {
656 NOTREACHED();
[email protected]26ef6582010-06-24 02:30:47657 return ERR_UNEXPECTED;
[email protected]94d78132010-01-22 00:53:00658 }
659
660 CHECK(connection_.get());
661 CHECK(connection_->socket());
[email protected]1f14a912009-12-21 20:32:44662 int bytes_read = connection_->socket()->Read(read_buffer_.get(),
[email protected]94d78132010-01-22 00:53:00663 kReadBufferSize,
664 &read_callback_);
[email protected]aea80602009-09-18 00:55:08665 switch (bytes_read) {
666 case 0:
667 // Socket is closed!
[email protected]a01ea222010-08-19 16:50:53668 CloseSessionOnError(ERR_CONNECTION_CLOSED, true);
[email protected]26ef6582010-06-24 02:30:47669 return ERR_CONNECTION_CLOSED;
[email protected]aea80602009-09-18 00:55:08670 case net::ERR_IO_PENDING:
671 // Waiting for data. Nothing to do now.
672 read_pending_ = true;
[email protected]26ef6582010-06-24 02:30:47673 return ERR_IO_PENDING;
[email protected]aea80602009-09-18 00:55:08674 default:
675 // Data was read, process it.
[email protected]57f123222010-01-19 22:00:28676 // Schedule the work through the message loop to avoid recursive
677 // callbacks.
678 read_pending_ = true;
679 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]955fc2e72010-02-08 20:37:30680 this, &SpdySession::OnReadComplete, bytes_read));
[email protected]aea80602009-09-18 00:55:08681 break;
682 }
[email protected]26ef6582010-06-24 02:30:47683 return OK;
[email protected]aea80602009-09-18 00:55:08684}
685
[email protected]955fc2e72010-02-08 20:37:30686void SpdySession::WriteSocketLater() {
[email protected]aea80602009-09-18 00:55:08687 if (delayed_write_pending_)
688 return;
689
[email protected]74188f22010-04-09 20:18:50690 if (state_ < CONNECTED)
691 return;
692
[email protected]aea80602009-09-18 00:55:08693 delayed_write_pending_ = true;
694 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]955fc2e72010-02-08 20:37:30695 this, &SpdySession::WriteSocket));
[email protected]aea80602009-09-18 00:55:08696}
697
[email protected]955fc2e72010-02-08 20:37:30698void SpdySession::WriteSocket() {
[email protected]aea80602009-09-18 00:55:08699 // This function should only be called via WriteSocketLater.
700 DCHECK(delayed_write_pending_);
701 delayed_write_pending_ = false;
702
703 // If the socket isn't connected yet, just wait; we'll get called
[email protected]6371bf42009-12-04 05:13:12704 // again when the socket connection completes. If the socket is
705 // closed, just return.
706 if (state_ < CONNECTED || state_ == CLOSED)
[email protected]aea80602009-09-18 00:55:08707 return;
708
709 if (write_pending_) // Another write is in progress still.
710 return;
711
[email protected]eebc0b42009-11-04 00:17:13712 // Loop sending frames until we've sent everything or until the write
713 // returns error (or ERR_IO_PENDING).
[email protected]8549f5e2010-04-16 22:39:09714 while (in_flight_write_.buffer() || !queue_.empty()) {
[email protected]bf2491a92009-11-29 16:39:48715 if (!in_flight_write_.buffer()) {
[email protected]955fc2e72010-02-08 20:37:30716 // Grab the next SpdyFrame to send.
717 SpdyIOBuffer next_buffer = queue_.top();
[email protected]bf2491a92009-11-29 16:39:48718 queue_.pop();
[email protected]aea80602009-09-18 00:55:08719
[email protected]bf2491a92009-11-29 16:39:48720 // We've deferred compression until just before we write it to the socket,
721 // which is now. At this time, we don't compress our data frames.
[email protected]955fc2e72010-02-08 20:37:30722 spdy::SpdyFrame uncompressed_frame(next_buffer.buffer()->data(), false);
[email protected]bf2491a92009-11-29 16:39:48723 size_t size;
[email protected]f15254092010-05-21 04:05:40724 if (spdy_framer_.IsCompressible(uncompressed_frame)) {
[email protected]955fc2e72010-02-08 20:37:30725 scoped_ptr<spdy::SpdyFrame> compressed_frame(
[email protected]f15254092010-05-21 04:05:40726 spdy_framer_.CompressFrame(uncompressed_frame));
[email protected]68b74502010-04-16 21:04:55727 if (!compressed_frame.get()) {
728 LOG(ERROR) << "SPDY Compression failure";
[email protected]a01ea222010-08-19 16:50:53729 CloseSessionOnError(net::ERR_SPDY_PROTOCOL_ERROR, true);
[email protected]68b74502010-04-16 21:04:55730 return;
731 }
732
[email protected]955fc2e72010-02-08 20:37:30733 size = compressed_frame->length() + spdy::SpdyFrame::size();
[email protected]aea80602009-09-18 00:55:08734
[email protected]8549f5e2010-04-16 22:39:09735 DCHECK_GT(size, 0u);
[email protected]eebc0b42009-11-04 00:17:13736
[email protected]bf2491a92009-11-29 16:39:48737 // TODO(mbelshe): We have too much copying of data here.
738 IOBufferWithSize* buffer = new IOBufferWithSize(size);
739 memcpy(buffer->data(), compressed_frame->data(), size);
[email protected]eebc0b42009-11-04 00:17:13740
[email protected]bf2491a92009-11-29 16:39:48741 // Attempt to send the frame.
[email protected]955fc2e72010-02-08 20:37:30742 in_flight_write_ = SpdyIOBuffer(buffer, size, 0, next_buffer.stream());
[email protected]bf2491a92009-11-29 16:39:48743 } else {
[email protected]955fc2e72010-02-08 20:37:30744 size = uncompressed_frame.length() + spdy::SpdyFrame::size();
[email protected]bf2491a92009-11-29 16:39:48745 in_flight_write_ = next_buffer;
746 }
[email protected]2931238b2009-11-19 01:19:51747 } else {
[email protected]bf2491a92009-11-29 16:39:48748 DCHECK(in_flight_write_.buffer()->BytesRemaining());
[email protected]2931238b2009-11-19 01:19:51749 }
750
[email protected]93300672009-10-24 13:22:51751 write_pending_ = true;
[email protected]1f14a912009-12-21 20:32:44752 int rv = connection_->socket()->Write(in_flight_write_.buffer(),
[email protected]bf2491a92009-11-29 16:39:48753 in_flight_write_.buffer()->BytesRemaining(), &write_callback_);
[email protected]93300672009-10-24 13:22:51754 if (rv == net::ERR_IO_PENDING)
[email protected]aea80602009-09-18 00:55:08755 break;
[email protected]eebc0b42009-11-04 00:17:13756
757 // We sent the frame successfully.
[email protected]93300672009-10-24 13:22:51758 OnWriteComplete(rv);
[email protected]eebc0b42009-11-04 00:17:13759
760 // TODO(mbelshe): Test this error case. Maybe we should mark the socket
761 // as in an error state.
762 if (rv < 0)
763 break;
[email protected]aea80602009-09-18 00:55:08764 }
765}
766
[email protected]48599ca2010-06-15 23:10:36767void SpdySession::CloseAllStreams(net::Error status) {
[email protected]bce9f682010-03-30 16:36:02768 LOG(INFO) << "Closing all SPDY Streams for " << host_port_pair().ToString();
[email protected]aea80602009-09-18 00:55:08769
[email protected]955fc2e72010-02-08 20:37:30770 static StatsCounter abandoned_streams("spdy.abandoned_streams");
771 static StatsCounter abandoned_push_streams("spdy.abandoned_push_streams");
[email protected]aea80602009-09-18 00:55:08772
[email protected]48599ca2010-06-15 23:10:36773 if (!active_streams_.empty())
[email protected]aea80602009-09-18 00:55:08774 abandoned_streams.Add(active_streams_.size());
[email protected]e3ebba0f2010-08-05 17:59:58775 if (!unclaimed_pushed_streams_.empty()) {
776 streams_abandoned_count_ += unclaimed_pushed_streams_.size();
777 abandoned_push_streams.Add(unclaimed_pushed_streams_.size());
[email protected]19ec8a72010-08-23 03:38:23778 unclaimed_pushed_streams_.clear();
[email protected]aea80602009-09-18 00:55:08779 }
[email protected]ab8949a2010-03-29 21:16:54780
[email protected]2bd93022010-07-17 00:58:44781 for (int i = 0;i < NUM_PRIORITIES;++i) {
782 while (!create_stream_queues_[i].empty()) {
783 PendingCreateStream& pending_create = create_stream_queues_[i].front();
784 pending_create.callback->Run(ERR_ABORTED);
785 create_stream_queues_[i].pop();
786 }
787 }
788
[email protected]48599ca2010-06-15 23:10:36789 while (!active_streams_.empty()) {
790 ActiveStreamMap::iterator it = active_streams_.begin();
791 const scoped_refptr<SpdyStream>& stream = it->second;
792 DCHECK(stream);
793 LOG(ERROR) << "ABANDONED (stream_id=" << stream->stream_id()
794 << "): " << stream->path();
[email protected]56e7c63d2010-07-12 18:42:17795 DeleteStream(stream->stream_id(), status);
[email protected]48599ca2010-06-15 23:10:36796 }
797
[email protected]ab8949a2010-03-29 21:16:54798 // We also need to drain the queue.
799 while (queue_.size())
800 queue_.pop();
[email protected]aea80602009-09-18 00:55:08801}
802
[email protected]955fc2e72010-02-08 20:37:30803int SpdySession::GetNewStreamId() {
[email protected]aea80602009-09-18 00:55:08804 int id = stream_hi_water_mark_;
805 stream_hi_water_mark_ += 2;
806 if (stream_hi_water_mark_ > 0x7fff)
807 stream_hi_water_mark_ = 1;
808 return id;
809}
810
[email protected]74188f22010-04-09 20:18:50811void SpdySession::QueueFrame(spdy::SpdyFrame* frame,
812 spdy::SpdyPriority priority,
813 SpdyStream* stream) {
814 int length = spdy::SpdyFrame::size() + frame->length();
815 IOBuffer* buffer = new IOBuffer(length);
816 memcpy(buffer->data(), frame->data(), length);
817 queue_.push(SpdyIOBuffer(buffer, length, priority, stream));
818
819 WriteSocketLater();
820}
821
[email protected]a01ea222010-08-19 16:50:53822void SpdySession::CloseSessionOnError(net::Error err, bool remove_from_pool) {
[email protected]69d717bd2010-04-21 18:43:21823 // Closing all streams can have a side-effect of dropping the last reference
824 // to |this|. Hold a reference through this function.
825 scoped_refptr<SpdySession> self(this);
826
[email protected]353f6162009-12-10 18:04:12827 DCHECK_LT(err, OK);
[email protected]bce9f682010-03-30 16:36:02828 LOG(INFO) << "spdy::CloseSessionOnError(" << err << ") for " <<
829 host_port_pair().ToString();
[email protected]60253bd2009-12-01 01:16:39830
831 // Don't close twice. This can occur because we can have both
832 // a read and a write outstanding, and each can complete with
833 // an error.
834 if (state_ != CLOSED) {
835 state_ = CLOSED;
836 error_ = err;
[email protected]a01ea222010-08-19 16:50:53837 if (remove_from_pool)
838 RemoveFromPool();
[email protected]b278eb72010-07-09 20:17:00839 CloseAllStreams(err);
[email protected]60253bd2009-12-01 01:16:39840 }
841}
842
[email protected]955fc2e72010-02-08 20:37:30843void SpdySession::ActivateStream(SpdyStream* stream) {
844 const spdy::SpdyStreamId id = stream->stream_id();
[email protected]aea80602009-09-18 00:55:08845 DCHECK(!IsStreamActive(id));
846
[email protected]aea80602009-09-18 00:55:08847 active_streams_[id] = stream;
[email protected]aea80602009-09-18 00:55:08848}
849
[email protected]56e7c63d2010-07-12 18:42:17850void SpdySession::DeleteStream(spdy::SpdyStreamId id, int status) {
[email protected]bdebd1b2010-08-09 20:18:08851 DLOG(INFO) << "Removing SpdyStream " << id << " from active stream list.";
[email protected]19ec8a72010-08-23 03:38:23852
853 // For push streams, if they are being deleted normally, we leave
854 // the stream in the unclaimed_pushed_streams_ list. However, if
855 // the stream is errored out, clean it up entirely.
856 if (status != OK) {
857 PushedStreamMap::iterator it;
858 for (it = unclaimed_pushed_streams_.begin();
859 it != unclaimed_pushed_streams_.end(); ++it) {
860 scoped_refptr<SpdyStream> curr = it->second;
861 if (id == curr->stream_id()) {
862 unclaimed_pushed_streams_.erase(it);
863 break;
864 }
[email protected]aea80602009-09-18 00:55:08865 }
866 }
867
[email protected]2ab5be42010-06-16 11:11:20868 // The stream might have been deleted.
[email protected]48599ca2010-06-15 23:10:36869 ActiveStreamMap::iterator it2 = active_streams_.find(id);
[email protected]2ab5be42010-06-16 11:11:20870 if (it2 == active_streams_.end())
871 return;
[email protected]48599ca2010-06-15 23:10:36872
873 // If this is an active stream, call the callback.
[email protected]45ab62b62010-06-18 02:11:40874 const scoped_refptr<SpdyStream> stream(it2->second);
875 active_streams_.erase(it2);
[email protected]56e7c63d2010-07-12 18:42:17876 if (stream)
[email protected]48599ca2010-06-15 23:10:36877 stream->OnClose(status);
[email protected]2bd93022010-07-17 00:58:44878 ProcessPendingCreateStreams();
[email protected]aea80602009-09-18 00:55:08879}
880
[email protected]4b4762a2010-04-23 16:04:14881void SpdySession::RemoveFromPool() {
882 if (in_session_pool_) {
883 session_->spdy_session_pool()->Remove(this);
884 in_session_pool_ = false;
885 }
886}
887
[email protected]9be804c82010-06-24 17:59:46888scoped_refptr<SpdyStream> SpdySession::GetActivePushStream(
[email protected]65d56aa2010-06-14 04:13:40889 const std::string& path) {
[email protected]955fc2e72010-02-08 20:37:30890 static StatsCounter used_push_streams("spdy.claimed_push_streams");
[email protected]aea80602009-09-18 00:55:08891
892 LOG(INFO) << "Looking for push stream: " << path;
893
[email protected]e3ebba0f2010-08-05 17:59:58894 PushedStreamMap::iterator it = unclaimed_pushed_streams_.find(path);
895 if (it != unclaimed_pushed_streams_.end()) {
896 LOG(INFO) << "Push stream: " << path << " found.";
897 net_log_.AddEvent(NetLog::TYPE_SPDY_STREAM_ADOPTED_PUSH_STREAM, NULL);
898 scoped_refptr<SpdyStream> stream = it->second;
899 unclaimed_pushed_streams_.erase(it);
900 used_push_streams.Increment();
901 return stream;
[email protected]aea80602009-09-18 00:55:08902 }
[email protected]e3ebba0f2010-08-05 17:59:58903 else {
904 LOG(INFO) << "Push stream: " << path << " not found.";
905 return NULL;
906 }
[email protected]aea80602009-09-18 00:55:08907}
908
[email protected]9be804c82010-06-24 17:59:46909bool SpdySession::GetSSLInfo(SSLInfo* ssl_info, bool* was_npn_negotiated) {
[email protected]dcc6bbb2009-12-09 19:09:01910 if (is_secure_) {
911 SSLClientSocket* ssl_socket =
[email protected]1f14a912009-12-21 20:32:44912 reinterpret_cast<SSLClientSocket*>(connection_->socket());
[email protected]dcc6bbb2009-12-09 19:09:01913 ssl_socket->GetSSLInfo(ssl_info);
[email protected]d7c9f422010-08-27 22:54:53914 *was_npn_negotiated = ssl_socket->was_npn_negotiated();
[email protected]9be804c82010-06-24 17:59:46915 return true;
[email protected]dcc6bbb2009-12-09 19:09:01916 }
[email protected]9be804c82010-06-24 17:59:46917 return false;
[email protected]dcc6bbb2009-12-09 19:09:01918}
919
[email protected]8e6441ca2010-08-19 05:56:38920bool SpdySession::GetSSLCertRequestInfo(
921 SSLCertRequestInfo* cert_request_info) {
922 if (is_secure_) {
923 SSLClientSocket* ssl_socket =
924 reinterpret_cast<SSLClientSocket*>(connection_->socket());
925 ssl_socket->GetSSLCertRequestInfo(cert_request_info);
926 return true;
927 }
928 return false;
929}
930
[email protected]955fc2e72010-02-08 20:37:30931void SpdySession::OnError(spdy::SpdyFramer* framer) {
932 LOG(ERROR) << "SpdySession error: " << framer->error_code();
[email protected]a01ea222010-08-19 16:50:53933 CloseSessionOnError(net::ERR_SPDY_PROTOCOL_ERROR, true);
[email protected]aea80602009-09-18 00:55:08934}
935
[email protected]955fc2e72010-02-08 20:37:30936void SpdySession::OnStreamFrameData(spdy::SpdyStreamId stream_id,
[email protected]aea80602009-09-18 00:55:08937 const char* data,
[email protected]aeac1e42009-10-10 00:26:01938 size_t len) {
[email protected]955fc2e72010-02-08 20:37:30939 LOG(INFO) << "Spdy data for stream " << stream_id << ", " << len << " bytes";
[email protected]65d56aa2010-06-14 04:13:40940
[email protected]72efdea2010-06-07 16:02:32941 if (!IsStreamActive(stream_id)) {
[email protected]92683b52009-12-21 21:01:12942 // NOTE: it may just be that the stream was cancelled.
[email protected]affe8fe2009-10-14 20:06:05943 LOG(WARNING) << "Received data frame for invalid stream " << stream_id;
[email protected]aea80602009-09-18 00:55:08944 return;
945 }
[email protected]93300672009-10-24 13:22:51946
[email protected]955fc2e72010-02-08 20:37:30947 scoped_refptr<SpdyStream> stream = active_streams_[stream_id];
[email protected]48599ca2010-06-15 23:10:36948 stream->OnDataReceived(data, len);
[email protected]aea80602009-09-18 00:55:08949}
950
[email protected]3f662f12010-03-25 19:56:12951bool SpdySession::Respond(const spdy::SpdyHeaderBlock& headers,
952 const scoped_refptr<SpdyStream> stream) {
[email protected]a5566f9702010-05-05 22:25:43953 int rv = OK;
954
[email protected]9be804c82010-06-24 17:59:46955 rv = stream->OnResponseReceived(headers);
[email protected]a5566f9702010-05-05 22:25:43956 if (rv < 0) {
957 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]3f662f12010-03-25 19:56:12958 const spdy::SpdyStreamId stream_id = stream->stream_id();
[email protected]56e7c63d2010-07-12 18:42:17959 DeleteStream(stream_id, rv);
[email protected]3f662f12010-03-25 19:56:12960 return false;
961 }
962 return true;
963}
964
[email protected]651b77c2010-03-10 19:29:42965void SpdySession::OnSyn(const spdy::SpdySynStreamControlFrame& frame,
[email protected]33c477b2010-05-13 19:21:07966 const linked_ptr<spdy::SpdyHeaderBlock>& headers) {
[email protected]651b77c2010-03-10 19:29:42967 spdy::SpdyStreamId stream_id = frame.stream_id();
[email protected]e3ebba0f2010-08-05 17:59:58968 spdy::SpdyStreamId associated_stream_id = frame.associated_stream_id();
969 LOG(INFO) << "Spdy SynStream for stream " << stream_id
970 << " with associated stream " << associated_stream_id;
[email protected]93300672009-10-24 13:22:51971 // Server-initiated streams should have even sequence numbers.
[email protected]aea80602009-09-18 00:55:08972 if ((stream_id & 0x1) != 0) {
[email protected]18c53ae2009-10-01 18:18:52973 LOG(ERROR) << "Received invalid OnSyn stream id " << stream_id;
[email protected]aea80602009-09-18 00:55:08974 return;
975 }
976
977 if (IsStreamActive(stream_id)) {
[email protected]18c53ae2009-10-01 18:18:52978 LOG(ERROR) << "Received OnSyn for active stream " << stream_id;
[email protected]aea80602009-09-18 00:55:08979 return;
980 }
981
[email protected]e3ebba0f2010-08-05 17:59:58982 if (associated_stream_id == 0) {
983 LOG(ERROR) << "Received invalid OnSyn associated stream id "
984 << associated_stream_id
985 << " for stream " << stream_id;
986 ResetStream(stream_id, spdy::INVALID_STREAM);
987 return;
988 }
989
[email protected]9b010802009-12-27 22:55:30990 streams_pushed_count_++;
[email protected]a677f2b2009-11-22 00:43:00991
[email protected]18c53ae2009-10-01 18:18:52992 // TODO(mbelshe): DCHECK that this is a GET method?
993
[email protected]33c477b2010-05-13 19:21:07994 const std::string& path = ContainsKey(*headers, "path") ?
995 headers->find("path")->second : "";
[email protected]a677f2b2009-11-22 00:43:00996
[email protected]aea80602009-09-18 00:55:08997 // Verify that the response had a URL for us.
[email protected]a677f2b2009-11-22 00:43:00998 if (path.empty()) {
[email protected]e3ebba0f2010-08-05 17:59:58999 ResetStream(stream_id, spdy::PROTOCOL_ERROR);
[email protected]aea80602009-09-18 00:55:081000 LOG(WARNING) << "Pushed stream did not contain a path.";
[email protected]aea80602009-09-18 00:55:081001 return;
1002 }
[email protected]18c53ae2009-10-01 18:18:521003
[email protected]e3ebba0f2010-08-05 17:59:581004 if (!IsStreamActive(associated_stream_id)) {
1005 LOG(ERROR) << "Received OnSyn with inactive associated stream "
1006 << associated_stream_id;
1007 ResetStream(stream_id, spdy::INVALID_ASSOCIATED_STREAM);
1008 return;
1009 }
1010
[email protected]9be804c82010-06-24 17:59:461011 scoped_refptr<SpdyStream> stream;
[email protected]a677f2b2009-11-22 00:43:001012
[email protected]e3ebba0f2010-08-05 17:59:581013 stream = new SpdyStream(this, stream_id, true);
1014
[email protected]095c7cf2010-08-31 21:07:331015 if (net_log_.IsLoggingAll()) {
[email protected]e3ebba0f2010-08-05 17:59:581016 net_log_.AddEvent(
1017 NetLog::TYPE_SPDY_SESSION_PUSHED_SYN_STREAM,
1018 new NetLogSpdySynParameter(
1019 headers, static_cast<spdy::SpdyControlFlags>(frame.flags()),
1020 stream_id));
[email protected]18c53ae2009-10-01 18:18:521021 }
[email protected]aea80602009-09-18 00:55:081022
[email protected]e3ebba0f2010-08-05 17:59:581023 // TODO(erikchen): Actually do something with the associated id.
[email protected]c8b16742010-02-05 20:49:531024
[email protected]e3ebba0f2010-08-05 17:59:581025 stream->set_path(path);
1026
1027 // There should not be an existing pushed stream with the same path.
1028 PushedStreamMap::iterator it = unclaimed_pushed_streams_.find(path);
1029 if (it != unclaimed_pushed_streams_.end()) {
1030 LOG(ERROR) << "Received duplicate pushed stream with path: " << path;
1031 ResetStream(stream_id, spdy::PROTOCOL_ERROR);
[email protected]a677f2b2009-11-22 00:43:001032 }
[email protected]e3ebba0f2010-08-05 17:59:581033 unclaimed_pushed_streams_[path] = stream;
[email protected]c8b16742010-02-05 20:49:531034
[email protected]a677f2b2009-11-22 00:43:001035 ActivateStream(stream);
[email protected]a5c493b92010-08-06 23:04:291036 stream->set_response_received();
[email protected]a677f2b2009-11-22 00:43:001037
[email protected]e3ebba0f2010-08-05 17:59:581038 // Parse the headers.
[email protected]33c477b2010-05-13 19:21:071039 if (!Respond(*headers, stream))
[email protected]dd11b932009-11-30 19:39:481040 return;
[email protected]a677f2b2009-11-22 00:43:001041
[email protected]aea80602009-09-18 00:55:081042 LOG(INFO) << "Got pushed stream for " << stream->path();
1043
[email protected]955fc2e72010-02-08 20:37:301044 static StatsCounter push_requests("spdy.pushed_streams");
[email protected]aea80602009-09-18 00:55:081045 push_requests.Increment();
1046}
1047
[email protected]651b77c2010-03-10 19:29:421048void SpdySession::OnSynReply(const spdy::SpdySynReplyControlFrame& frame,
[email protected]33c477b2010-05-13 19:21:071049 const linked_ptr<spdy::SpdyHeaderBlock>& headers) {
[email protected]651b77c2010-03-10 19:29:421050 spdy::SpdyStreamId stream_id = frame.stream_id();
[email protected]9ccdad22010-03-04 19:19:211051 LOG(INFO) << "Spdy SynReply for stream " << stream_id;
1052
[email protected]aea80602009-09-18 00:55:081053 bool valid_stream = IsStreamActive(stream_id);
1054 if (!valid_stream) {
[email protected]92683b52009-12-21 21:01:121055 // NOTE: it may just be that the stream was cancelled.
[email protected]affe8fe2009-10-14 20:06:051056 LOG(WARNING) << "Received SYN_REPLY for invalid stream " << stream_id;
[email protected]aea80602009-09-18 00:55:081057 return;
1058 }
1059
[email protected]d30022352010-06-24 19:17:581060 scoped_refptr<SpdyStream> stream = active_streams_[stream_id];
1061 CHECK_EQ(stream->stream_id(), stream_id);
1062 CHECK(!stream->cancelled());
1063
[email protected]a5c493b92010-08-06 23:04:291064 if (stream->response_received()) {
[email protected]d30022352010-06-24 19:17:581065 LOG(WARNING) << "Received duplicate SYN_REPLY for stream " << stream_id;
1066 CloseStream(stream->stream_id(), ERR_SPDY_PROTOCOL_ERROR);
1067 return;
1068 }
[email protected]a5c493b92010-08-06 23:04:291069 stream->set_response_received();
[email protected]d30022352010-06-24 19:17:581070
[email protected]33c477b2010-05-13 19:21:071071 const BoundNetLog& log = stream->net_log();
[email protected]095c7cf2010-08-31 21:07:331072 if (log.IsLoggingAll()) {
[email protected]33c477b2010-05-13 19:21:071073 log.AddEvent(
1074 NetLog::TYPE_SPDY_STREAM_SYN_REPLY,
1075 new NetLogSpdySynParameter(
1076 headers, static_cast<spdy::SpdyControlFlags>(frame.flags()),
1077 stream_id));
1078 }
1079
1080 Respond(*headers, stream);
[email protected]aea80602009-09-18 00:55:081081}
1082
[email protected]955fc2e72010-02-08 20:37:301083void SpdySession::OnControl(const spdy::SpdyControlFrame* frame) {
[email protected]33c477b2010-05-13 19:21:071084 const linked_ptr<spdy::SpdyHeaderBlock> headers(new spdy::SpdyHeaderBlock);
[email protected]aea80602009-09-18 00:55:081085 uint32 type = frame->type();
[email protected]955fc2e72010-02-08 20:37:301086 if (type == spdy::SYN_STREAM || type == spdy::SYN_REPLY) {
[email protected]33c477b2010-05-13 19:21:071087 if (!spdy_framer_.ParseHeaderBlock(frame, headers.get())) {
[email protected]6c6ea172010-07-27 20:04:031088 LOG(WARNING) << "Could not parse Spdy Control Frame Header.";
1089 int stream_id = 0;
1090 if (type == spdy::SYN_STREAM)
1091 stream_id = (reinterpret_cast<const spdy::SpdySynStreamControlFrame*>
1092 (frame))->stream_id();
1093 if (type == spdy::SYN_REPLY)
1094 stream_id = (reinterpret_cast<const spdy::SpdySynReplyControlFrame*>
1095 (frame))->stream_id();
1096 if(IsStreamActive(stream_id))
1097 ResetStream(stream_id, spdy::PROTOCOL_ERROR);
[email protected]aea80602009-09-18 00:55:081098 return;
1099 }
1100 }
1101
[email protected]58cebf8f2010-07-31 19:20:161102 frames_received_++;
1103
[email protected]aea80602009-09-18 00:55:081104 switch (type) {
[email protected]74188f22010-04-09 20:18:501105 case spdy::GOAWAY:
1106 OnGoAway(*reinterpret_cast<const spdy::SpdyGoAwayControlFrame*>(frame));
1107 break;
1108 case spdy::SETTINGS:
1109 OnSettings(
1110 *reinterpret_cast<const spdy::SpdySettingsControlFrame*>(frame));
1111 break;
1112 case spdy::RST_STREAM:
[email protected]e3ebba0f2010-08-05 17:59:581113 OnRst(*reinterpret_cast<const spdy::SpdyRstStreamControlFrame*>(frame));
[email protected]74188f22010-04-09 20:18:501114 break;
[email protected]955fc2e72010-02-08 20:37:301115 case spdy::SYN_STREAM:
[email protected]651b77c2010-03-10 19:29:421116 OnSyn(*reinterpret_cast<const spdy::SpdySynStreamControlFrame*>(frame),
1117 headers);
[email protected]aea80602009-09-18 00:55:081118 break;
[email protected]955fc2e72010-02-08 20:37:301119 case spdy::SYN_REPLY:
[email protected]aea80602009-09-18 00:55:081120 OnSynReply(
[email protected]651b77c2010-03-10 19:29:421121 *reinterpret_cast<const spdy::SpdySynReplyControlFrame*>(frame),
1122 headers);
[email protected]aea80602009-09-18 00:55:081123 break;
[email protected]5af3c572010-07-20 14:16:271124 case spdy::WINDOW_UPDATE:
1125 OnWindowUpdate(
1126 *reinterpret_cast<const spdy::SpdyWindowUpdateControlFrame*>(frame));
1127 break;
[email protected]aea80602009-09-18 00:55:081128 default:
1129 DCHECK(false); // Error!
1130 }
1131}
1132
[email protected]e3ebba0f2010-08-05 17:59:581133void SpdySession::OnRst(const spdy::SpdyRstStreamControlFrame& frame) {
[email protected]651b77c2010-03-10 19:29:421134 spdy::SpdyStreamId stream_id = frame.stream_id();
[email protected]a5c493b92010-08-06 23:04:291135 LOG(INFO) << "Spdy RST for stream " << stream_id;
[email protected]9ccdad22010-03-04 19:19:211136
[email protected]aea80602009-09-18 00:55:081137 bool valid_stream = IsStreamActive(stream_id);
1138 if (!valid_stream) {
[email protected]92683b52009-12-21 21:01:121139 // NOTE: it may just be that the stream was cancelled.
[email protected]a5c493b92010-08-06 23:04:291140 LOG(WARNING) << "Received RST for invalid stream" << stream_id;
[email protected]aea80602009-09-18 00:55:081141 return;
1142 }
[email protected]955fc2e72010-02-08 20:37:301143 scoped_refptr<SpdyStream> stream = active_streams_[stream_id];
[email protected]b1f031dd2010-03-02 23:19:331144 CHECK_EQ(stream->stream_id(), stream_id);
[email protected]92683b52009-12-21 21:01:121145 CHECK(!stream->cancelled());
[email protected]44297002010-05-16 00:59:101146
1147 const BoundNetLog& log = stream->net_log();
1148 log.AddEvent(
1149 NetLog::TYPE_SPDY_STREAM_RST_STREAM,
1150 new NetLogIntegerParameter("status", frame.status()));
1151
[email protected]651b77c2010-03-10 19:29:421152 if (frame.status() == 0) {
[email protected]a677f2b2009-11-22 00:43:001153 stream->OnDataReceived(NULL, 0);
[email protected]aea80602009-09-18 00:55:081154 } else {
[email protected]651b77c2010-03-10 19:29:421155 LOG(ERROR) << "Spdy stream closed: " << frame.status();
[email protected]955fc2e72010-02-08 20:37:301156 // TODO(mbelshe): Map from Spdy-protocol errors to something sensical.
[email protected]93300672009-10-24 13:22:511157 // For now, it doesn't matter much - it is a protocol error.
[email protected]56e7c63d2010-07-12 18:42:171158 DeleteStream(stream_id, ERR_SPDY_PROTOCOL_ERROR);
[email protected]aea80602009-09-18 00:55:081159 }
[email protected]aea80602009-09-18 00:55:081160}
1161
[email protected]651b77c2010-03-10 19:29:421162void SpdySession::OnGoAway(const spdy::SpdyGoAwayControlFrame& frame) {
[email protected]bce9f682010-03-30 16:36:021163 LOG(INFO) << "Spdy GOAWAY for session[" << this << "] for " <<
1164 host_port_pair().ToString();
[email protected]c9336d22010-08-10 00:04:181165 if(!active_streams_.empty() || !unclaimed_pushed_streams_.empty()) {
1166 LOG(ERROR) << "Spdy GOAWAY received with " << active_streams_.size()
1167 << " streams still active.";
1168 LOG(ERROR) << "Spdy GOAWAY received with "
1169 << unclaimed_pushed_streams_.size()
1170 << " unclaimed push streams.";
1171 net_log_.AddEvent(
1172 NetLog::TYPE_SPDY_SESSION_GOAWAY,
1173 new NetLogIntegerParameter(
1174 "number of streams still active: ",
1175 active_streams_.size()));
1176 net_log_.AddEvent(
1177 NetLog::TYPE_SPDY_SESSION_GOAWAY,
1178 new NetLogIntegerParameter(
1179 "number of unclaimed push streams: ",
1180 unclaimed_pushed_streams_.size()));
1181 }
[email protected]44297002010-05-16 00:59:101182 net_log_.AddEvent(
1183 NetLog::TYPE_SPDY_SESSION_GOAWAY,
1184 new NetLogIntegerParameter(
1185 "last_accepted_stream_id",
1186 frame.last_accepted_stream_id()));
1187
[email protected]4b4762a2010-04-23 16:04:141188 RemoveFromPool();
[email protected]c9336d22010-08-10 00:04:181189 CloseAllStreams(net::ERR_ABORTED);
[email protected]651b77c2010-03-10 19:29:421190
1191 // TODO(willchan): Cancel any streams that are past the GoAway frame's
1192 // |last_accepted_stream_id|.
1193
1194 // Don't bother killing any streams that are still reading. They'll either
1195 // complete successfully or get an ERR_CONNECTION_CLOSED when the socket is
1196 // closed.
1197}
1198
[email protected]74188f22010-04-09 20:18:501199void SpdySession::OnSettings(const spdy::SpdySettingsControlFrame& frame) {
1200 spdy::SpdySettings settings;
1201 if (spdy_framer_.ParseSettings(&frame, &settings)) {
[email protected]2bd93022010-07-17 00:58:441202 HandleSettings(settings);
[email protected]74188f22010-04-09 20:18:501203 SpdySettingsStorage* settings_storage = session_->mutable_spdy_settings();
[email protected]b261d0e2010-08-02 19:13:241204 settings_storage->Set(host_port_pair(), settings);
[email protected]74188f22010-04-09 20:18:501205 }
[email protected]44297002010-05-16 00:59:101206
[email protected]0aebc002010-05-21 06:50:191207 received_settings_ = true;
1208
[email protected]44297002010-05-16 00:59:101209 net_log_.AddEvent(
1210 NetLog::TYPE_SPDY_SESSION_RECV_SETTINGS,
1211 new NetLogSpdySettingsParameter(settings));
[email protected]74188f22010-04-09 20:18:501212}
1213
[email protected]5af3c572010-07-20 14:16:271214void SpdySession::OnWindowUpdate(
1215 const spdy::SpdyWindowUpdateControlFrame& frame) {
1216 spdy::SpdyStreamId stream_id = frame.stream_id();
1217 LOG(INFO) << "Spdy WINDOW_UPDATE for stream " << stream_id;
1218
1219 if (!IsStreamActive(stream_id)) {
1220 LOG(WARNING) << "Received WINDOW_UPDATE for invalid stream " << stream_id;
1221 return;
1222 }
1223
1224 int delta_window_size = static_cast<int>(frame.delta_window_size());
1225 if (delta_window_size < 1) {
1226 LOG(WARNING) << "Received WINDOW_UPDATE with an invalid delta_window_size "
1227 << delta_window_size;
1228 ResetStream(stream_id, spdy::FLOW_CONTROL_ERROR);
1229 return;
1230 }
1231
1232 scoped_refptr<SpdyStream> stream = active_streams_[stream_id];
1233 CHECK_EQ(stream->stream_id(), stream_id);
1234 CHECK(!stream->cancelled());
1235
[email protected]7349c6b12010-07-22 02:29:161236 if (use_flow_control_)
1237 stream->IncreaseSendWindowSize(delta_window_size);
[email protected]5af3c572010-07-20 14:16:271238}
1239
[email protected]450c5022010-08-26 02:38:281240void SpdySession::SendWindowUpdate(spdy::SpdyStreamId stream_id,
1241 int delta_window_size) {
1242 DCHECK(IsStreamActive(stream_id));
1243 scoped_refptr<SpdyStream> stream = active_streams_[stream_id];
1244 CHECK_EQ(stream->stream_id(), stream_id);
1245
1246 LOG(INFO) << "Sending a WINDOW_UPDATE frame for stream " << stream_id
1247 << " with delta window size " << delta_window_size;
1248
1249 scoped_ptr<spdy::SpdyWindowUpdateControlFrame> window_update_frame(
1250 spdy_framer_.CreateWindowUpdate(stream_id, delta_window_size));
1251 QueueFrame(window_update_frame.get(), stream->priority(), stream);
1252}
1253
[email protected]74188f22010-04-09 20:18:501254void SpdySession::SendSettings() {
1255 const SpdySettingsStorage& settings_storage = session_->spdy_settings();
[email protected]b261d0e2010-08-02 19:13:241256 const spdy::SpdySettings& settings = settings_storage.Get(host_port_pair());
[email protected]74188f22010-04-09 20:18:501257 if (settings.empty())
1258 return;
[email protected]2bd93022010-07-17 00:58:441259 HandleSettings(settings);
[email protected]74188f22010-04-09 20:18:501260
[email protected]44297002010-05-16 00:59:101261 net_log_.AddEvent(
1262 NetLog::TYPE_SPDY_SESSION_SEND_SETTINGS,
1263 new NetLogSpdySettingsParameter(settings));
1264
[email protected]74188f22010-04-09 20:18:501265 // Create the SETTINGS frame and send it.
1266 scoped_ptr<spdy::SpdySettingsControlFrame> settings_frame(
1267 spdy_framer_.CreateSettings(settings));
[email protected]0aebc002010-05-21 06:50:191268 sent_settings_ = true;
[email protected]74188f22010-04-09 20:18:501269 QueueFrame(settings_frame.get(), 0, NULL);
1270}
1271
[email protected]2bd93022010-07-17 00:58:441272void SpdySession::HandleSettings(const spdy::SpdySettings& settings) {
1273 for (spdy::SpdySettings::const_iterator i = settings.begin(),
1274 end = settings.end(); i != end; ++i) {
1275 const uint32 id = i->first.id();
1276 const uint32 val = i->second;
1277 switch (id) {
1278 case spdy::SETTINGS_MAX_CONCURRENT_STREAMS:
1279 max_concurrent_streams_ = val;
1280 ProcessPendingCreateStreams();
1281 break;
1282 }
1283 }
1284}
1285
[email protected]0aebc002010-05-21 06:50:191286void SpdySession::RecordHistograms() {
1287 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdyStreamsPerSession",
1288 streams_initiated_count_,
1289 0, 300, 50);
1290 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdyStreamsPushedPerSession",
1291 streams_pushed_count_,
1292 0, 300, 50);
1293 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdyStreamsPushedAndClaimedPerSession",
1294 streams_pushed_and_claimed_count_,
1295 0, 300, 50);
1296 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdyStreamsAbandonedPerSession",
1297 streams_abandoned_count_,
1298 0, 300, 50);
1299 UMA_HISTOGRAM_ENUMERATION("Net.SpdySettingsSent",
1300 sent_settings_ ? 1 : 0, 2);
1301 UMA_HISTOGRAM_ENUMERATION("Net.SpdySettingsReceived",
1302 received_settings_ ? 1 : 0, 2);
1303
1304 if (received_settings_) {
1305 // Enumerate the saved settings, and set histograms for it.
1306 const SpdySettingsStorage& settings_storage = session_->spdy_settings();
[email protected]b261d0e2010-08-02 19:13:241307 const spdy::SpdySettings& settings = settings_storage.Get(host_port_pair());
[email protected]0aebc002010-05-21 06:50:191308
1309 spdy::SpdySettings::const_iterator it;
1310 for (it = settings.begin(); it != settings.end(); ++it) {
1311 const spdy::SpdySetting setting = *it;
1312 switch (setting.first.id()) {
1313 case spdy::SETTINGS_CURRENT_CWND:
1314 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdySettingsCwnd",
1315 setting.second,
1316 1, 200, 100);
1317 break;
1318 case spdy::SETTINGS_ROUND_TRIP_TIME:
1319 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdySettingsRTT",
1320 setting.second,
1321 1, 1200, 100);
1322 break;
1323 case spdy::SETTINGS_DOWNLOAD_RETRANS_RATE:
1324 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdySettingsRetransRate",
1325 setting.second,
1326 1, 100, 50);
1327 break;
1328 }
1329 }
1330 }
1331}
1332
[email protected]aea80602009-09-18 00:55:081333} // namespace net