[email protected] | c8b1674 | 2010-02-05 20:49:53 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 5 | #include "net/spdy/spdy_session.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 6 | |
| 7 | #include "base/basictypes.h" |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 8 | #include "base/linked_ptr.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 9 | #include "base/logging.h" |
| 10 | #include "base/message_loop.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 11 | #include "base/stats_counters.h" |
[email protected] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 12 | #include "base/stl_util-inl.h" |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 13 | #include "base/string_number_conversions.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 14 | #include "base/string_util.h" |
[email protected] | f1d8192 | 2010-07-31 17:47:09 | [diff] [blame] | 15 | #include "base/utf_string_conversions.h" |
[email protected] | 3f662f1 | 2010-03-25 19:56:12 | [diff] [blame] | 16 | #include "base/time.h" |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 17 | #include "base/values.h" |
[email protected] | 5e2e6c77d1 | 2009-12-24 21:57:16 | [diff] [blame] | 18 | #include "net/base/connection_type_histograms.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 19 | #include "net/base/load_flags.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 20 | #include "net/base/net_log.h" |
[email protected] | 3375156 | 2009-10-29 02:17:11 | [diff] [blame] | 21 | #include "net/base/net_util.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 22 | #include "net/http/http_network_session.h" |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 23 | #include "net/socket/client_socket.h" |
[email protected] | 18c53ae | 2009-10-01 18:18:52 | [diff] [blame] | 24 | #include "net/socket/client_socket_factory.h" |
| 25 | #include "net/socket/ssl_client_socket.h" |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 26 | #include "net/spdy/spdy_frame_builder.h" |
| 27 | #include "net/spdy/spdy_protocol.h" |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 28 | #include "net/spdy/spdy_settings_storage.h" |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 29 | #include "net/spdy/spdy_stream.h" |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 30 | |
| 31 | namespace net { |
| 32 | |
[email protected] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 33 | namespace { |
| 34 | |
[email protected] | 9fc38b3 | 2010-01-11 21:34:46 | [diff] [blame] | 35 | #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. |
| 38 | const int kReadBufferSize = 2 * 1024; |
| 39 | #else |
| 40 | const int kReadBufferSize = 8 * 1024; |
| 41 | #endif |
[email protected] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 42 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 43 | void AdjustSocketBufferSizes(ClientSocket* socket) { |
| 44 | // Adjust socket buffer sizes. |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 45 | // SPDY uses one socket, and we want a really big buffer. |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 46 | // 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] | 6cd3bd20 | 2010-08-30 05:23:06 | [diff] [blame] | 54 | class 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] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 69 | class 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] | 815d63d9 | 2010-08-04 18:14:15 | [diff] [blame] | 78 | ListValue* headers_list = new ListValue(); |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 79 | for (spdy::SpdyHeaderBlock::const_iterator it = headers_->begin(); |
| 80 | it != headers_->end(); ++it) { |
[email protected] | 815d63d9 | 2010-08-04 18:14:15 | [diff] [blame] | 81 | headers_list->Append(new StringValue(StringPrintf("%s: %s", |
| 82 | it->first.c_str(), |
| 83 | it->second.c_str()))); |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 84 | } |
[email protected] | ccaff65 | 2010-07-31 06:28:20 | [diff] [blame] | 85 | dict->SetInteger("flags", flags_); |
[email protected] | 815d63d9 | 2010-08-04 18:14:15 | [diff] [blame] | 86 | dict->Set("headers", headers_list); |
[email protected] | ccaff65 | 2010-07-31 06:28:20 | [diff] [blame] | 87 | dict->SetInteger("id", id_); |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 88 | 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] | 4429700 | 2010-05-16 00:59:10 | [diff] [blame] | 101 | class 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] | ccaff65 | 2010-07-31 06:28:20 | [diff] [blame] | 114 | dict->Set("settings", settings); |
[email protected] | 4429700 | 2010-05-16 00:59:10 | [diff] [blame] | 115 | 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] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 126 | } // namespace |
| 127 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 128 | // static |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 129 | bool SpdySession::use_ssl_ = true; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 130 | |
[email protected] | 7349c6b1 | 2010-07-22 02:29:16 | [diff] [blame] | 131 | // static |
[email protected] | c25b15c1 | 2010-07-27 19:55:33 | [diff] [blame] | 132 | bool SpdySession::use_flow_control_ = false; |
[email protected] | 7349c6b1 | 2010-07-22 02:29:16 | [diff] [blame] | 133 | |
[email protected] | b261d0e | 2010-08-02 19:13:24 | [diff] [blame] | 134 | SpdySession::SpdySession(const HostPortProxyPair& host_port_proxy_pair, |
[email protected] | 635909f | 2010-05-12 18:19:36 | [diff] [blame] | 135 | HttpNetworkSession* session, |
[email protected] | 4429700 | 2010-05-16 00:59:10 | [diff] [blame] | 136 | NetLog* net_log) |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 137 | : ALLOW_THIS_IN_INITIALIZER_LIST( |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 138 | connect_callback_(this, &SpdySession::OnTCPConnect)), |
[email protected] | 18c53ae | 2009-10-01 18:18:52 | [diff] [blame] | 139 | ALLOW_THIS_IN_INITIALIZER_LIST( |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 140 | ssl_connect_callback_(this, &SpdySession::OnSSLConnect)), |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 141 | ALLOW_THIS_IN_INITIALIZER_LIST( |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 142 | read_callback_(this, &SpdySession::OnReadComplete)), |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 143 | ALLOW_THIS_IN_INITIALIZER_LIST( |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 144 | write_callback_(this, &SpdySession::OnWriteComplete)), |
[email protected] | b261d0e | 2010-08-02 19:13:24 | [diff] [blame] | 145 | host_port_proxy_pair_(host_port_proxy_pair), |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 146 | session_(session), |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 147 | connection_(new ClientSocketHandle), |
[email protected] | 230cadd | 2009-09-22 16:33:59 | [diff] [blame] | 148 | read_buffer_(new IOBuffer(kReadBufferSize)), |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 149 | read_pending_(false), |
[email protected] | ba051e31 | 2009-10-07 22:12:33 | [diff] [blame] | 150 | stream_hi_water_mark_(1), // Always start at 1 for the first stream id. |
[email protected] | dd11b93 | 2009-11-30 19:39:48 | [diff] [blame] | 151 | write_pending_(false), |
[email protected] | 60253bd | 2009-12-01 01:16:39 | [diff] [blame] | 152 | delayed_write_pending_(false), |
[email protected] | dcc6bbb | 2009-12-09 19:09:01 | [diff] [blame] | 153 | is_secure_(false), |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 154 | certificate_error_code_(OK), |
[email protected] | 60253bd | 2009-12-01 01:16:39 | [diff] [blame] | 155 | error_(OK), |
[email protected] | 9b01080 | 2009-12-27 22:55:30 | [diff] [blame] | 156 | state_(IDLE), |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 157 | max_concurrent_streams_(kDefaultMaxConcurrentStreams), |
[email protected] | 9b01080 | 2009-12-27 22:55:30 | [diff] [blame] | 158 | streams_initiated_count_(0), |
| 159 | streams_pushed_count_(0), |
| 160 | streams_pushed_and_claimed_count_(0), |
[email protected] | 4b4762a | 2010-04-23 16:04:14 | [diff] [blame] | 161 | streams_abandoned_count_(0), |
[email protected] | 58cebf8f | 2010-07-31 19:20:16 | [diff] [blame] | 162 | frames_received_(0), |
[email protected] | 0aebc00 | 2010-05-21 06:50:19 | [diff] [blame] | 163 | sent_settings_(false), |
| 164 | received_settings_(false), |
[email protected] | 635909f | 2010-05-12 18:19:36 | [diff] [blame] | 165 | in_session_pool_(true), |
[email protected] | 7349c6b1 | 2010-07-22 02:29:16 | [diff] [blame] | 166 | initial_send_window_size_(spdy::kInitialWindowSize), |
[email protected] | 450c502 | 2010-08-26 02:38:28 | [diff] [blame] | 167 | initial_recv_window_size_(spdy::kInitialWindowSize), |
[email protected] | 4429700 | 2010-05-16 00:59:10 | [diff] [blame] | 168 | net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SPDY_SESSION)) { |
| 169 | net_log_.BeginEvent( |
| 170 | NetLog::TYPE_SPDY_SESSION, |
[email protected] | 6cd3bd20 | 2010-08-30 05:23:06 | [diff] [blame] | 171 | new NetLogSpdySessionParameter(host_port_proxy_pair_)); |
[email protected] | 4429700 | 2010-05-16 00:59:10 | [diff] [blame] | 172 | |
[email protected] | ba051e31 | 2009-10-07 22:12:33 | [diff] [blame] | 173 | // TODO(mbelshe): consider randomization of the stream_hi_water_mark. |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 174 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 175 | spdy_framer_.set_visitor(this); |
[email protected] | 18c53ae | 2009-10-01 18:18:52 | [diff] [blame] | 176 | |
| 177 | session_->ssl_config_service()->GetSSLConfig(&ssl_config_); |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 178 | |
| 179 | SendSettings(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 180 | } |
| 181 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 182 | SpdySession::~SpdySession() { |
[email protected] | b278eb7 | 2010-07-09 20:17:00 | [diff] [blame] | 183 | state_ = CLOSED; |
| 184 | |
[email protected] | 9330067 | 2009-10-24 13:22:51 | [diff] [blame] | 185 | // Cleanup all the streams. |
| 186 | CloseAllStreams(net::ERR_ABORTED); |
| 187 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 188 | if (connection_->is_initialized()) { |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 189 | // With Spdy we can't recycle sockets. |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 190 | connection_->socket()->Disconnect(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 191 | } |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 192 | |
[email protected] | 19ec8a7 | 2010-08-23 03:38:23 | [diff] [blame] | 193 | // Streams should all be gone now. |
| 194 | DCHECK_EQ(0u, num_active_streams()); |
| 195 | DCHECK_EQ(0u, num_unclaimed_pushed_streams()); |
| 196 | |
[email protected] | 0aebc00 | 2010-05-21 06:50:19 | [diff] [blame] | 197 | RecordHistograms(); |
[email protected] | 4429700 | 2010-05-16 00:59:10 | [diff] [blame] | 198 | |
| 199 | net_log_.EndEvent(NetLog::TYPE_SPDY_SESSION, NULL); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 200 | } |
| 201 | |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 202 | net::Error SpdySession::InitializeWithSocket( |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 203 | ClientSocketHandle* connection, |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 204 | bool is_secure, |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 205 | int certificate_error_code) { |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 206 | static StatsCounter spdy_sessions("spdy.sessions"); |
| 207 | spdy_sessions.Increment(); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 208 | |
| 209 | AdjustSocketBufferSizes(connection->socket()); |
| 210 | |
| 211 | state_ = CONNECTED; |
| 212 | connection_.reset(connection); |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 213 | is_secure_ = is_secure; |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 214 | certificate_error_code_ = certificate_error_code; |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 215 | |
| 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] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 219 | net::Error error = ReadSocket(); |
| 220 | if (error == ERR_IO_PENDING) |
| 221 | return OK; |
| 222 | return error; |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 223 | } |
| 224 | |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 225 | net::Error SpdySession::Connect( |
| 226 | const std::string& group_name, |
| 227 | const scoped_refptr<TCPSocketParams>& destination, |
| 228 | RequestPriority priority) { |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 229 | DCHECK(priority >= net::HIGHEST && priority < net::NUM_PRIORITIES); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 230 | |
| 231 | // If the connect process is started, let the caller continue. |
[email protected] | 60253bd | 2009-12-01 01:16:39 | [diff] [blame] | 232 | if (state_ > IDLE) |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 233 | return net::OK; |
| 234 | |
[email protected] | 60253bd | 2009-12-01 01:16:39 | [diff] [blame] | 235 | state_ = CONNECTING; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 236 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 237 | static StatsCounter spdy_sessions("spdy.sessions"); |
| 238 | spdy_sessions.Increment(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 239 | |
[email protected] | 7fc5b09a | 2010-02-27 00:07:38 | [diff] [blame] | 240 | int rv = connection_->Init(group_name, destination, priority, |
| 241 | &connect_callback_, session_->tcp_socket_pool(), |
[email protected] | 635909f | 2010-05-12 18:19:36 | [diff] [blame] | 242 | net_log_); |
[email protected] | 56e7c63d | 2010-07-12 18:42:17 | [diff] [blame] | 243 | DCHECK(rv <= 0); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 244 | |
| 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] | 651b77c | 2010-03-10 19:29:42 | [diff] [blame] | 249 | OnTCPConnect(rv); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 250 | return static_cast<net::Error>(rv); |
| 251 | } |
| 252 | |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 253 | int SpdySession::GetPushStream( |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 254 | const GURL& url, |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 255 | scoped_refptr<SpdyStream>* stream, |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 256 | const BoundNetLog& stream_net_log) { |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 257 | CHECK_NE(state_, CLOSED); |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 258 | |
| 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] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 270 | const std::string& path = url.PathForRequest(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 271 | |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 272 | *stream = GetActivePushStream(path); |
| 273 | if (stream->get()) { |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 274 | DCHECK(streams_pushed_and_claimed_count_ < streams_pushed_count_); |
| 275 | streams_pushed_and_claimed_count_++; |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 276 | return OK; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 277 | } |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 278 | return NULL; |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 281 | int SpdySession::CreateStream( |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 282 | const GURL& url, |
| 283 | RequestPriority priority, |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 284 | scoped_refptr<SpdyStream>* spdy_stream, |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 285 | const BoundNetLog& stream_net_log, |
[email protected] | 971746e | 2010-07-21 03:02:23 | [diff] [blame] | 286 | CompletionCallback* callback) { |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 287 | 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] | 971746e | 2010-07-21 03:02:23 | [diff] [blame] | 294 | stream_net_log, callback)); |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 295 | return ERR_IO_PENDING; |
| 296 | } |
| 297 | |
| 298 | void 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 | |
| 320 | void SpdySession::CancelPendingCreateStreams( |
[email protected] | 971746e | 2010-07-21 03:02:23 | [diff] [blame] | 321 | const scoped_refptr<SpdyStream>* spdy_stream) { |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 322 | 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] | 971746e | 2010-07-21 03:02:23 | [diff] [blame] | 327 | if (pending_create.spdy_stream != spdy_stream) |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 328 | 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 | |
| 339 | int SpdySession::CreateStreamImpl( |
| 340 | const GURL& url, |
| 341 | RequestPriority priority, |
| 342 | scoped_refptr<SpdyStream>* spdy_stream, |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 343 | const BoundNetLog& stream_net_log) { |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 344 | // 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] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 353 | const std::string& path = url.PathForRequest(); |
[email protected] | 18c53ae | 2009-10-01 18:18:52 | [diff] [blame] | 354 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 355 | const spdy::SpdyStreamId stream_id = GetNewStreamId(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 356 | |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 357 | *spdy_stream = new SpdyStream(this, stream_id, false); |
| 358 | const scoped_refptr<SpdyStream>& stream = *spdy_stream; |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 359 | |
| 360 | stream->set_priority(priority); |
[email protected] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 361 | stream->set_path(path); |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 362 | stream->set_net_log(stream_net_log); |
[email protected] | 7349c6b1 | 2010-07-22 02:29:16 | [diff] [blame] | 363 | stream->set_send_window_size(initial_send_window_size_); |
[email protected] | 450c502 | 2010-08-26 02:38:28 | [diff] [blame] | 364 | stream->set_recv_window_size(initial_recv_window_size_); |
[email protected] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 365 | ActivateStream(stream); |
| 366 | |
[email protected] | 9b01080 | 2009-12-27 22:55:30 | [diff] [blame] | 367 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.SpdyPriorityCount", |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 368 | static_cast<int>(priority), 0, 10, 11); |
[email protected] | 9b01080 | 2009-12-27 22:55:30 | [diff] [blame] | 369 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 370 | LOG(INFO) << "SpdyStream: Creating stream " << stream_id << " for " << url; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 371 | // TODO(mbelshe): Optimize memory allocations |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 372 | DCHECK(priority >= net::HIGHEST && priority < net::NUM_PRIORITIES); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 373 | |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 374 | DCHECK_EQ(active_streams_[stream_id].get(), stream.get()); |
[email protected] | bdbda46 | 2010-06-28 17:30:37 | [diff] [blame] | 375 | return OK; |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 376 | } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 377 | |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 378 | int 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] | 72552f0 | 2009-10-28 15:25:01 | [diff] [blame] | 388 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 389 | scoped_ptr<spdy::SpdySynStreamControlFrame> syn_frame( |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 390 | spdy_framer_.CreateSynStream( |
| 391 | stream_id, 0, |
| 392 | ConvertRequestPriorityToSpdyPriority(priority), |
| 393 | flags, false, headers.get())); |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 394 | QueueFrame(syn_frame.get(), priority, stream); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 395 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 396 | static StatsCounter spdy_requests("spdy.requests"); |
| 397 | spdy_requests.Increment(); |
[email protected] | 9b01080 | 2009-12-27 22:55:30 | [diff] [blame] | 398 | streams_initiated_count_++; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 399 | |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 400 | const BoundNetLog& log = stream->net_log(); |
[email protected] | 095c7cf | 2010-08-31 21:07:33 | [diff] [blame^] | 401 | if (log.IsLoggingAll()) { |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 402 | log.AddEvent( |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 403 | NetLog::TYPE_SPDY_STREAM_SYN_STREAM, |
| 404 | new NetLogSpdySynParameter(headers, flags, stream_id)); |
| 405 | } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 406 | |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 407 | return ERR_IO_PENDING; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 408 | } |
| 409 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 410 | int SpdySession::WriteStreamData(spdy::SpdyStreamId stream_id, |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 411 | net::IOBuffer* data, int len, |
| 412 | spdy::SpdyDataFlags flags) { |
[email protected] | 967dd541 | 2009-11-21 23:33:30 | [diff] [blame] | 413 | LOG(INFO) << "Writing Stream Data for stream " << stream_id << " (" << len |
| 414 | << " bytes)"; |
[email protected] | ff57bb8 | 2009-11-12 06:52:14 | [diff] [blame] | 415 | |
| 416 | // Find our stream |
[email protected] | 56e7c63d | 2010-07-12 18:42:17 | [diff] [blame] | 417 | DCHECK(IsStreamActive(stream_id)); |
| 418 | scoped_refptr<SpdyStream> stream = active_streams_[stream_id]; |
[email protected] | 9c6527c | 2010-07-12 18:16:14 | [diff] [blame] | 419 | CHECK_EQ(stream->stream_id(), stream_id); |
[email protected] | 56e7c63d | 2010-07-12 18:42:17 | [diff] [blame] | 420 | if (!stream) |
| 421 | return ERR_INVALID_SPDY_STREAM; |
[email protected] | ff57bb8 | 2009-11-12 06:52:14 | [diff] [blame] | 422 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 423 | if (len > kMaxSpdyFrameChunkSize) { |
| 424 | len = kMaxSpdyFrameChunkSize; |
[email protected] | 31024059 | 2010-08-05 21:04:19 | [diff] [blame] | 425 | flags = static_cast<spdy::SpdyDataFlags>(flags & ~spdy::DATA_FLAG_FIN); |
[email protected] | ff57bb8 | 2009-11-12 06:52:14 | [diff] [blame] | 426 | } |
| 427 | |
[email protected] | 7349c6b1 | 2010-07-22 02:29:16 | [diff] [blame] | 428 | // Obey send window size of the stream if flow control is enabled. |
| 429 | if (use_flow_control_) { |
[email protected] | 6abf537 | 2010-08-17 15:44:25 | [diff] [blame] | 430 | if (stream->send_window_size() <= 0) { |
| 431 | stream->set_stalled_by_flow_control(true); |
[email protected] | 7349c6b1 | 2010-07-22 02:29:16 | [diff] [blame] | 432 | return ERR_IO_PENDING; |
[email protected] | 6abf537 | 2010-08-17 15:44:25 | [diff] [blame] | 433 | } |
[email protected] | 31024059 | 2010-08-05 21:04:19 | [diff] [blame] | 434 | 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] | 7349c6b1 | 2010-07-22 02:29:16 | [diff] [blame] | 439 | stream->DecreaseSendWindowSize(len); |
| 440 | } |
| 441 | |
[email protected] | ff57bb8 | 2009-11-12 06:52:14 | [diff] [blame] | 442 | // TODO(mbelshe): reduce memory copies here. |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 443 | scoped_ptr<spdy::SpdyDataFrame> frame( |
| 444 | spdy_framer_.CreateDataFrame(stream_id, data->data(), len, flags)); |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 445 | QueueFrame(frame.get(), stream->priority(), stream); |
[email protected] | ff57bb8 | 2009-11-12 06:52:14 | [diff] [blame] | 446 | return ERR_IO_PENDING; |
| 447 | } |
| 448 | |
[email protected] | 56e7c63d | 2010-07-12 18:42:17 | [diff] [blame] | 449 | void 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] | 92683b5 | 2009-12-21 21:01:12 | [diff] [blame] | 453 | |
[email protected] | 56e7c63d | 2010-07-12 18:42:17 | [diff] [blame] | 454 | DeleteStream(stream_id, status); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 455 | } |
| 456 | |
[email protected] | 5af3c57 | 2010-07-20 14:16:27 | [diff] [blame] | 457 | void SpdySession::ResetStream( |
| 458 | spdy::SpdyStreamId stream_id, spdy::SpdyStatusCodes status) { |
[email protected] | 5af3c57 | 2010-07-20 14:16:27 | [diff] [blame] | 459 | 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] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 464 | |
| 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] | 5af3c57 | 2010-07-20 14:16:27 | [diff] [blame] | 472 | |
| 473 | DeleteStream(stream_id, ERR_SPDY_PROTOCOL_ERROR); |
| 474 | } |
| 475 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 476 | bool SpdySession::IsStreamActive(spdy::SpdyStreamId stream_id) const { |
[email protected] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 477 | return ContainsKey(active_streams_, stream_id); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 478 | } |
| 479 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 480 | LoadState SpdySession::GetLoadState() const { |
[email protected] | 9be4f02c | 2010-01-07 18:30:09 | [diff] [blame] | 481 | // NOTE: The application only queries the LoadState via the |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 482 | // SpdyNetworkTransaction, and details are only needed when |
[email protected] | 9be4f02c | 2010-01-07 18:30:09 | [diff] [blame] | 483 | // 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] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 493 | } |
| 494 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 495 | void SpdySession::OnTCPConnect(int result) { |
| 496 | LOG(INFO) << "Spdy socket connected (result=" << result << ")"; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 497 | |
[email protected] | 5e2e6c77d1 | 2009-12-24 21:57:16 | [diff] [blame] | 498 | // 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] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 502 | if (result != net::OK) { |
[email protected] | 353f616 | 2009-12-10 18:04:12 | [diff] [blame] | 503 | DCHECK_LT(result, 0); |
[email protected] | a01ea22 | 2010-08-19 16:50:53 | [diff] [blame] | 504 | CloseSessionOnError(static_cast<net::Error>(result), true); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 505 | return; |
[email protected] | 616925a | 2010-03-02 19:02:38 | [diff] [blame] | 506 | } else { |
| 507 | UpdateConnectionTypeHistograms(CONNECTION_SPDY); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 508 | } |
| 509 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 510 | AdjustSocketBufferSizes(connection_->socket()); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 511 | |
[email protected] | affe8fe | 2009-10-14 20:06:05 | [diff] [blame] | 512 | if (use_ssl_) { |
[email protected] | 18c53ae | 2009-10-01 18:18:52 | [diff] [blame] | 513 | // Add a SSL socket on top of our existing transport socket. |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 514 | ClientSocket* socket = connection_->release_socket(); |
[email protected] | 18c53ae | 2009-10-01 18:18:52 | [diff] [blame] | 515 | // 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] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 519 | connection_->set_socket(socket); |
[email protected] | dcc6bbb | 2009-12-09 19:09:01 | [diff] [blame] | 520 | is_secure_ = true; |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 521 | int status = connection_->socket()->Connect(&ssl_connect_callback_); |
[email protected] | 843c507 | 2009-12-04 22:09:47 | [diff] [blame] | 522 | if (status != ERR_IO_PENDING) |
| 523 | OnSSLConnect(status); |
[email protected] | 18c53ae | 2009-10-01 18:18:52 | [diff] [blame] | 524 | } else { |
[email protected] | 60253bd | 2009-12-01 01:16:39 | [diff] [blame] | 525 | DCHECK_EQ(state_, CONNECTING); |
| 526 | state_ = CONNECTED; |
[email protected] | 18c53ae | 2009-10-01 18:18:52 | [diff] [blame] | 527 | |
| 528 | // Make sure we get any pending data sent. |
| 529 | WriteSocketLater(); |
| 530 | // Start reading |
| 531 | ReadSocket(); |
| 532 | } |
| 533 | } |
| 534 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 535 | void SpdySession::OnSSLConnect(int result) { |
[email protected] | 18c53ae | 2009-10-01 18:18:52 | [diff] [blame] | 536 | // 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] | 9330067 | 2009-10-24 13:22:51 | [diff] [blame] | 542 | if (result == OK) { |
[email protected] | 60253bd | 2009-12-01 01:16:39 | [diff] [blame] | 543 | DCHECK_EQ(state_, CONNECTING); |
| 544 | state_ = CONNECTED; |
[email protected] | 18c53ae | 2009-10-01 18:18:52 | [diff] [blame] | 545 | |
[email protected] | 9330067 | 2009-10-24 13:22:51 | [diff] [blame] | 546 | // After we've connected, send any data to the server, and then issue |
| 547 | // our read. |
| 548 | WriteSocketLater(); |
| 549 | ReadSocket(); |
| 550 | } else { |
[email protected] | 353f616 | 2009-12-10 18:04:12 | [diff] [blame] | 551 | DCHECK_LT(result, 0); // It should be an error, not a byte count. |
[email protected] | a01ea22 | 2010-08-19 16:50:53 | [diff] [blame] | 552 | CloseSessionOnError(static_cast<net::Error>(result), true); |
[email protected] | 9330067 | 2009-10-24 13:22:51 | [diff] [blame] | 553 | } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 554 | } |
| 555 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 556 | void SpdySession::OnReadComplete(int bytes_read) { |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 557 | // 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] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 561 | LOG(INFO) << "Spdy socket read: " << bytes_read << " bytes"; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 562 | |
| 563 | read_pending_ = false; |
| 564 | |
| 565 | if (bytes_read <= 0) { |
| 566 | // Session is tearing down. |
[email protected] | 353f616 | 2009-12-10 18:04:12 | [diff] [blame] | 567 | net::Error error = static_cast<net::Error>(bytes_read); |
[email protected] | bce9f68 | 2010-03-30 16:36:02 | [diff] [blame] | 568 | if (bytes_read == 0) { |
| 569 | LOG(INFO) << "Spdy socket closed by server[" << |
| 570 | host_port_pair().ToString() << "]."; |
[email protected] | 353f616 | 2009-12-10 18:04:12 | [diff] [blame] | 571 | error = ERR_CONNECTION_CLOSED; |
[email protected] | bce9f68 | 2010-03-30 16:36:02 | [diff] [blame] | 572 | } |
[email protected] | a01ea22 | 2010-08-19 16:50:53 | [diff] [blame] | 573 | CloseSessionOnError(error, true); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 574 | return; |
| 575 | } |
| 576 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 577 | // The SpdyFramer will use callbacks onto |this| as it parses frames. |
[email protected] | 94d7813 | 2010-01-22 00:53:00 | [diff] [blame] | 578 | // 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] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 581 | scoped_refptr<SpdySession> self(this); |
[email protected] | 94d7813 | 2010-01-22 00:53:00 | [diff] [blame] | 582 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 583 | char *data = read_buffer_->data(); |
| 584 | while (bytes_read && |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 585 | spdy_framer_.error_code() == spdy::SpdyFramer::SPDY_NO_ERROR) { |
| 586 | uint32 bytes_processed = spdy_framer_.ProcessInput(data, bytes_read); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 587 | bytes_read -= bytes_processed; |
| 588 | data += bytes_processed; |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 589 | if (spdy_framer_.state() == spdy::SpdyFramer::SPDY_DONE) |
| 590 | spdy_framer_.Reset(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 591 | } |
[email protected] | 57f12322 | 2010-01-19 22:00:28 | [diff] [blame] | 592 | |
[email protected] | 94d7813 | 2010-01-22 00:53:00 | [diff] [blame] | 593 | if (state_ != CLOSED) |
| 594 | ReadSocket(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 595 | } |
| 596 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 597 | void SpdySession::OnWriteComplete(int result) { |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 598 | DCHECK(write_pending_); |
[email protected] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 599 | DCHECK(in_flight_write_.size()); |
[email protected] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 600 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 601 | write_pending_ = false; |
| 602 | |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 603 | scoped_refptr<SpdyStream> stream = in_flight_write_.stream(); |
| 604 | |
| 605 | LOG(INFO) << "Spdy write complete (result=" << result << ")" |
[email protected] | 8549f5e | 2010-04-16 22:39:09 | [diff] [blame] | 606 | << (stream ? std::string(" for stream ") + |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 607 | base::IntToString(stream->stream_id()) : ""); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 608 | |
[email protected] | 9330067 | 2009-10-24 13:22:51 | [diff] [blame] | 609 | if (result >= 0) { |
[email protected] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 610 | // 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] | eebc0b4 | 2009-11-04 00:17:13 | [diff] [blame] | 613 | |
[email protected] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 614 | in_flight_write_.buffer()->DidConsume(result); |
[email protected] | ff57bb8 | 2009-11-12 06:52:14 | [diff] [blame] | 615 | |
[email protected] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 616 | // We only notify the stream when we've fully written the pending frame. |
| 617 | if (!in_flight_write_.buffer()->BytesRemaining()) { |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 618 | 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] | 56e7c63d | 2010-07-12 18:42:17 | [diff] [blame] | 623 | 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] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 628 | |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 629 | // 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] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 633 | } |
[email protected] | 92683b5 | 2009-12-21 21:01:12 | [diff] [blame] | 634 | |
[email protected] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 635 | // Cleanup the write which just completed. |
| 636 | in_flight_write_.release(); |
[email protected] | ff57bb8 | 2009-11-12 06:52:14 | [diff] [blame] | 637 | } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 638 | |
[email protected] | 9330067 | 2009-10-24 13:22:51 | [diff] [blame] | 639 | // 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] | 6572f12 | 2009-11-30 18:23:13 | [diff] [blame] | 644 | in_flight_write_.release(); |
[email protected] | dd11b93 | 2009-11-30 19:39:48 | [diff] [blame] | 645 | |
[email protected] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 646 | // The stream is now errored. Close it down. |
[email protected] | a01ea22 | 2010-08-19 16:50:53 | [diff] [blame] | 647 | CloseSessionOnError(static_cast<net::Error>(result), true); |
[email protected] | 9330067 | 2009-10-24 13:22:51 | [diff] [blame] | 648 | } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 649 | } |
| 650 | |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 651 | net::Error SpdySession::ReadSocket() { |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 652 | if (read_pending_) |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 653 | return OK; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 654 | |
[email protected] | 94d7813 | 2010-01-22 00:53:00 | [diff] [blame] | 655 | if (state_ == CLOSED) { |
| 656 | NOTREACHED(); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 657 | return ERR_UNEXPECTED; |
[email protected] | 94d7813 | 2010-01-22 00:53:00 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | CHECK(connection_.get()); |
| 661 | CHECK(connection_->socket()); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 662 | int bytes_read = connection_->socket()->Read(read_buffer_.get(), |
[email protected] | 94d7813 | 2010-01-22 00:53:00 | [diff] [blame] | 663 | kReadBufferSize, |
| 664 | &read_callback_); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 665 | switch (bytes_read) { |
| 666 | case 0: |
| 667 | // Socket is closed! |
[email protected] | a01ea22 | 2010-08-19 16:50:53 | [diff] [blame] | 668 | CloseSessionOnError(ERR_CONNECTION_CLOSED, true); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 669 | return ERR_CONNECTION_CLOSED; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 670 | case net::ERR_IO_PENDING: |
| 671 | // Waiting for data. Nothing to do now. |
| 672 | read_pending_ = true; |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 673 | return ERR_IO_PENDING; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 674 | default: |
| 675 | // Data was read, process it. |
[email protected] | 57f12322 | 2010-01-19 22:00:28 | [diff] [blame] | 676 | // 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] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 680 | this, &SpdySession::OnReadComplete, bytes_read)); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 681 | break; |
| 682 | } |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 683 | return OK; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 684 | } |
| 685 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 686 | void SpdySession::WriteSocketLater() { |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 687 | if (delayed_write_pending_) |
| 688 | return; |
| 689 | |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 690 | if (state_ < CONNECTED) |
| 691 | return; |
| 692 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 693 | delayed_write_pending_ = true; |
| 694 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 695 | this, &SpdySession::WriteSocket)); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 696 | } |
| 697 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 698 | void SpdySession::WriteSocket() { |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 699 | // 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] | 6371bf4 | 2009-12-04 05:13:12 | [diff] [blame] | 704 | // again when the socket connection completes. If the socket is |
| 705 | // closed, just return. |
| 706 | if (state_ < CONNECTED || state_ == CLOSED) |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 707 | return; |
| 708 | |
| 709 | if (write_pending_) // Another write is in progress still. |
| 710 | return; |
| 711 | |
[email protected] | eebc0b4 | 2009-11-04 00:17:13 | [diff] [blame] | 712 | // Loop sending frames until we've sent everything or until the write |
| 713 | // returns error (or ERR_IO_PENDING). |
[email protected] | 8549f5e | 2010-04-16 22:39:09 | [diff] [blame] | 714 | while (in_flight_write_.buffer() || !queue_.empty()) { |
[email protected] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 715 | if (!in_flight_write_.buffer()) { |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 716 | // Grab the next SpdyFrame to send. |
| 717 | SpdyIOBuffer next_buffer = queue_.top(); |
[email protected] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 718 | queue_.pop(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 719 | |
[email protected] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 720 | // 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] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 722 | spdy::SpdyFrame uncompressed_frame(next_buffer.buffer()->data(), false); |
[email protected] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 723 | size_t size; |
[email protected] | f1525409 | 2010-05-21 04:05:40 | [diff] [blame] | 724 | if (spdy_framer_.IsCompressible(uncompressed_frame)) { |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 725 | scoped_ptr<spdy::SpdyFrame> compressed_frame( |
[email protected] | f1525409 | 2010-05-21 04:05:40 | [diff] [blame] | 726 | spdy_framer_.CompressFrame(uncompressed_frame)); |
[email protected] | 68b7450 | 2010-04-16 21:04:55 | [diff] [blame] | 727 | if (!compressed_frame.get()) { |
| 728 | LOG(ERROR) << "SPDY Compression failure"; |
[email protected] | a01ea22 | 2010-08-19 16:50:53 | [diff] [blame] | 729 | CloseSessionOnError(net::ERR_SPDY_PROTOCOL_ERROR, true); |
[email protected] | 68b7450 | 2010-04-16 21:04:55 | [diff] [blame] | 730 | return; |
| 731 | } |
| 732 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 733 | size = compressed_frame->length() + spdy::SpdyFrame::size(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 734 | |
[email protected] | 8549f5e | 2010-04-16 22:39:09 | [diff] [blame] | 735 | DCHECK_GT(size, 0u); |
[email protected] | eebc0b4 | 2009-11-04 00:17:13 | [diff] [blame] | 736 | |
[email protected] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 737 | // 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] | eebc0b4 | 2009-11-04 00:17:13 | [diff] [blame] | 740 | |
[email protected] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 741 | // Attempt to send the frame. |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 742 | in_flight_write_ = SpdyIOBuffer(buffer, size, 0, next_buffer.stream()); |
[email protected] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 743 | } else { |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 744 | size = uncompressed_frame.length() + spdy::SpdyFrame::size(); |
[email protected] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 745 | in_flight_write_ = next_buffer; |
| 746 | } |
[email protected] | 2931238b | 2009-11-19 01:19:51 | [diff] [blame] | 747 | } else { |
[email protected] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 748 | DCHECK(in_flight_write_.buffer()->BytesRemaining()); |
[email protected] | 2931238b | 2009-11-19 01:19:51 | [diff] [blame] | 749 | } |
| 750 | |
[email protected] | 9330067 | 2009-10-24 13:22:51 | [diff] [blame] | 751 | write_pending_ = true; |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 752 | int rv = connection_->socket()->Write(in_flight_write_.buffer(), |
[email protected] | bf2491a9 | 2009-11-29 16:39:48 | [diff] [blame] | 753 | in_flight_write_.buffer()->BytesRemaining(), &write_callback_); |
[email protected] | 9330067 | 2009-10-24 13:22:51 | [diff] [blame] | 754 | if (rv == net::ERR_IO_PENDING) |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 755 | break; |
[email protected] | eebc0b4 | 2009-11-04 00:17:13 | [diff] [blame] | 756 | |
| 757 | // We sent the frame successfully. |
[email protected] | 9330067 | 2009-10-24 13:22:51 | [diff] [blame] | 758 | OnWriteComplete(rv); |
[email protected] | eebc0b4 | 2009-11-04 00:17:13 | [diff] [blame] | 759 | |
| 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] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
[email protected] | 48599ca | 2010-06-15 23:10:36 | [diff] [blame] | 767 | void SpdySession::CloseAllStreams(net::Error status) { |
[email protected] | bce9f68 | 2010-03-30 16:36:02 | [diff] [blame] | 768 | LOG(INFO) << "Closing all SPDY Streams for " << host_port_pair().ToString(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 769 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 770 | static StatsCounter abandoned_streams("spdy.abandoned_streams"); |
| 771 | static StatsCounter abandoned_push_streams("spdy.abandoned_push_streams"); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 772 | |
[email protected] | 48599ca | 2010-06-15 23:10:36 | [diff] [blame] | 773 | if (!active_streams_.empty()) |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 774 | abandoned_streams.Add(active_streams_.size()); |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 775 | if (!unclaimed_pushed_streams_.empty()) { |
| 776 | streams_abandoned_count_ += unclaimed_pushed_streams_.size(); |
| 777 | abandoned_push_streams.Add(unclaimed_pushed_streams_.size()); |
[email protected] | 19ec8a7 | 2010-08-23 03:38:23 | [diff] [blame] | 778 | unclaimed_pushed_streams_.clear(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 779 | } |
[email protected] | ab8949a | 2010-03-29 21:16:54 | [diff] [blame] | 780 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 781 | 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] | 48599ca | 2010-06-15 23:10:36 | [diff] [blame] | 789 | 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] | 56e7c63d | 2010-07-12 18:42:17 | [diff] [blame] | 795 | DeleteStream(stream->stream_id(), status); |
[email protected] | 48599ca | 2010-06-15 23:10:36 | [diff] [blame] | 796 | } |
| 797 | |
[email protected] | ab8949a | 2010-03-29 21:16:54 | [diff] [blame] | 798 | // We also need to drain the queue. |
| 799 | while (queue_.size()) |
| 800 | queue_.pop(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 801 | } |
| 802 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 803 | int SpdySession::GetNewStreamId() { |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 804 | 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] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 811 | void 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] | a01ea22 | 2010-08-19 16:50:53 | [diff] [blame] | 822 | void SpdySession::CloseSessionOnError(net::Error err, bool remove_from_pool) { |
[email protected] | 69d717bd | 2010-04-21 18:43:21 | [diff] [blame] | 823 | // 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] | 353f616 | 2009-12-10 18:04:12 | [diff] [blame] | 827 | DCHECK_LT(err, OK); |
[email protected] | bce9f68 | 2010-03-30 16:36:02 | [diff] [blame] | 828 | LOG(INFO) << "spdy::CloseSessionOnError(" << err << ") for " << |
| 829 | host_port_pair().ToString(); |
[email protected] | 60253bd | 2009-12-01 01:16:39 | [diff] [blame] | 830 | |
| 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] | a01ea22 | 2010-08-19 16:50:53 | [diff] [blame] | 837 | if (remove_from_pool) |
| 838 | RemoveFromPool(); |
[email protected] | b278eb7 | 2010-07-09 20:17:00 | [diff] [blame] | 839 | CloseAllStreams(err); |
[email protected] | 60253bd | 2009-12-01 01:16:39 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 843 | void SpdySession::ActivateStream(SpdyStream* stream) { |
| 844 | const spdy::SpdyStreamId id = stream->stream_id(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 845 | DCHECK(!IsStreamActive(id)); |
| 846 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 847 | active_streams_[id] = stream; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 848 | } |
| 849 | |
[email protected] | 56e7c63d | 2010-07-12 18:42:17 | [diff] [blame] | 850 | void SpdySession::DeleteStream(spdy::SpdyStreamId id, int status) { |
[email protected] | bdebd1b | 2010-08-09 20:18:08 | [diff] [blame] | 851 | DLOG(INFO) << "Removing SpdyStream " << id << " from active stream list."; |
[email protected] | 19ec8a7 | 2010-08-23 03:38:23 | [diff] [blame] | 852 | |
| 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] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | |
[email protected] | 2ab5be4 | 2010-06-16 11:11:20 | [diff] [blame] | 868 | // The stream might have been deleted. |
[email protected] | 48599ca | 2010-06-15 23:10:36 | [diff] [blame] | 869 | ActiveStreamMap::iterator it2 = active_streams_.find(id); |
[email protected] | 2ab5be4 | 2010-06-16 11:11:20 | [diff] [blame] | 870 | if (it2 == active_streams_.end()) |
| 871 | return; |
[email protected] | 48599ca | 2010-06-15 23:10:36 | [diff] [blame] | 872 | |
| 873 | // If this is an active stream, call the callback. |
[email protected] | 45ab62b6 | 2010-06-18 02:11:40 | [diff] [blame] | 874 | const scoped_refptr<SpdyStream> stream(it2->second); |
| 875 | active_streams_.erase(it2); |
[email protected] | 56e7c63d | 2010-07-12 18:42:17 | [diff] [blame] | 876 | if (stream) |
[email protected] | 48599ca | 2010-06-15 23:10:36 | [diff] [blame] | 877 | stream->OnClose(status); |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 878 | ProcessPendingCreateStreams(); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 879 | } |
| 880 | |
[email protected] | 4b4762a | 2010-04-23 16:04:14 | [diff] [blame] | 881 | void SpdySession::RemoveFromPool() { |
| 882 | if (in_session_pool_) { |
| 883 | session_->spdy_session_pool()->Remove(this); |
| 884 | in_session_pool_ = false; |
| 885 | } |
| 886 | } |
| 887 | |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 888 | scoped_refptr<SpdyStream> SpdySession::GetActivePushStream( |
[email protected] | 65d56aa | 2010-06-14 04:13:40 | [diff] [blame] | 889 | const std::string& path) { |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 890 | static StatsCounter used_push_streams("spdy.claimed_push_streams"); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 891 | |
| 892 | LOG(INFO) << "Looking for push stream: " << path; |
| 893 | |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 894 | 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] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 902 | } |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 903 | else { |
| 904 | LOG(INFO) << "Push stream: " << path << " not found."; |
| 905 | return NULL; |
| 906 | } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 907 | } |
| 908 | |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 909 | bool SpdySession::GetSSLInfo(SSLInfo* ssl_info, bool* was_npn_negotiated) { |
[email protected] | dcc6bbb | 2009-12-09 19:09:01 | [diff] [blame] | 910 | if (is_secure_) { |
| 911 | SSLClientSocket* ssl_socket = |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 912 | reinterpret_cast<SSLClientSocket*>(connection_->socket()); |
[email protected] | dcc6bbb | 2009-12-09 19:09:01 | [diff] [blame] | 913 | ssl_socket->GetSSLInfo(ssl_info); |
[email protected] | d7c9f42 | 2010-08-27 22:54:53 | [diff] [blame] | 914 | *was_npn_negotiated = ssl_socket->was_npn_negotiated(); |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 915 | return true; |
[email protected] | dcc6bbb | 2009-12-09 19:09:01 | [diff] [blame] | 916 | } |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 917 | return false; |
[email protected] | dcc6bbb | 2009-12-09 19:09:01 | [diff] [blame] | 918 | } |
| 919 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 920 | bool 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] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 931 | void SpdySession::OnError(spdy::SpdyFramer* framer) { |
| 932 | LOG(ERROR) << "SpdySession error: " << framer->error_code(); |
[email protected] | a01ea22 | 2010-08-19 16:50:53 | [diff] [blame] | 933 | CloseSessionOnError(net::ERR_SPDY_PROTOCOL_ERROR, true); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 934 | } |
| 935 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 936 | void SpdySession::OnStreamFrameData(spdy::SpdyStreamId stream_id, |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 937 | const char* data, |
[email protected] | aeac1e4 | 2009-10-10 00:26:01 | [diff] [blame] | 938 | size_t len) { |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 939 | LOG(INFO) << "Spdy data for stream " << stream_id << ", " << len << " bytes"; |
[email protected] | 65d56aa | 2010-06-14 04:13:40 | [diff] [blame] | 940 | |
[email protected] | 72efdea | 2010-06-07 16:02:32 | [diff] [blame] | 941 | if (!IsStreamActive(stream_id)) { |
[email protected] | 92683b5 | 2009-12-21 21:01:12 | [diff] [blame] | 942 | // NOTE: it may just be that the stream was cancelled. |
[email protected] | affe8fe | 2009-10-14 20:06:05 | [diff] [blame] | 943 | LOG(WARNING) << "Received data frame for invalid stream " << stream_id; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 944 | return; |
| 945 | } |
[email protected] | 9330067 | 2009-10-24 13:22:51 | [diff] [blame] | 946 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 947 | scoped_refptr<SpdyStream> stream = active_streams_[stream_id]; |
[email protected] | 48599ca | 2010-06-15 23:10:36 | [diff] [blame] | 948 | stream->OnDataReceived(data, len); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 949 | } |
| 950 | |
[email protected] | 3f662f1 | 2010-03-25 19:56:12 | [diff] [blame] | 951 | bool SpdySession::Respond(const spdy::SpdyHeaderBlock& headers, |
| 952 | const scoped_refptr<SpdyStream> stream) { |
[email protected] | a5566f970 | 2010-05-05 22:25:43 | [diff] [blame] | 953 | int rv = OK; |
| 954 | |
[email protected] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 955 | rv = stream->OnResponseReceived(headers); |
[email protected] | a5566f970 | 2010-05-05 22:25:43 | [diff] [blame] | 956 | if (rv < 0) { |
| 957 | DCHECK_NE(rv, ERR_IO_PENDING); |
[email protected] | 3f662f1 | 2010-03-25 19:56:12 | [diff] [blame] | 958 | const spdy::SpdyStreamId stream_id = stream->stream_id(); |
[email protected] | 56e7c63d | 2010-07-12 18:42:17 | [diff] [blame] | 959 | DeleteStream(stream_id, rv); |
[email protected] | 3f662f1 | 2010-03-25 19:56:12 | [diff] [blame] | 960 | return false; |
| 961 | } |
| 962 | return true; |
| 963 | } |
| 964 | |
[email protected] | 651b77c | 2010-03-10 19:29:42 | [diff] [blame] | 965 | void SpdySession::OnSyn(const spdy::SpdySynStreamControlFrame& frame, |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 966 | const linked_ptr<spdy::SpdyHeaderBlock>& headers) { |
[email protected] | 651b77c | 2010-03-10 19:29:42 | [diff] [blame] | 967 | spdy::SpdyStreamId stream_id = frame.stream_id(); |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 968 | 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] | 9330067 | 2009-10-24 13:22:51 | [diff] [blame] | 971 | // Server-initiated streams should have even sequence numbers. |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 972 | if ((stream_id & 0x1) != 0) { |
[email protected] | 18c53ae | 2009-10-01 18:18:52 | [diff] [blame] | 973 | LOG(ERROR) << "Received invalid OnSyn stream id " << stream_id; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 974 | return; |
| 975 | } |
| 976 | |
| 977 | if (IsStreamActive(stream_id)) { |
[email protected] | 18c53ae | 2009-10-01 18:18:52 | [diff] [blame] | 978 | LOG(ERROR) << "Received OnSyn for active stream " << stream_id; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 979 | return; |
| 980 | } |
| 981 | |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 982 | 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] | 9b01080 | 2009-12-27 22:55:30 | [diff] [blame] | 990 | streams_pushed_count_++; |
[email protected] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 991 | |
[email protected] | 18c53ae | 2009-10-01 18:18:52 | [diff] [blame] | 992 | // TODO(mbelshe): DCHECK that this is a GET method? |
| 993 | |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 994 | const std::string& path = ContainsKey(*headers, "path") ? |
| 995 | headers->find("path")->second : ""; |
[email protected] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 996 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 997 | // Verify that the response had a URL for us. |
[email protected] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 998 | if (path.empty()) { |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 999 | ResetStream(stream_id, spdy::PROTOCOL_ERROR); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1000 | LOG(WARNING) << "Pushed stream did not contain a path."; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1001 | return; |
| 1002 | } |
[email protected] | 18c53ae | 2009-10-01 18:18:52 | [diff] [blame] | 1003 | |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 1004 | 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] | 9be804c8 | 2010-06-24 17:59:46 | [diff] [blame] | 1011 | scoped_refptr<SpdyStream> stream; |
[email protected] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 1012 | |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 1013 | stream = new SpdyStream(this, stream_id, true); |
| 1014 | |
[email protected] | 095c7cf | 2010-08-31 21:07:33 | [diff] [blame^] | 1015 | if (net_log_.IsLoggingAll()) { |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 1016 | 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] | 18c53ae | 2009-10-01 18:18:52 | [diff] [blame] | 1021 | } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1022 | |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 1023 | // TODO(erikchen): Actually do something with the associated id. |
[email protected] | c8b1674 | 2010-02-05 20:49:53 | [diff] [blame] | 1024 | |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 1025 | 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] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 1032 | } |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 1033 | unclaimed_pushed_streams_[path] = stream; |
[email protected] | c8b1674 | 2010-02-05 20:49:53 | [diff] [blame] | 1034 | |
[email protected] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 1035 | ActivateStream(stream); |
[email protected] | a5c493b9 | 2010-08-06 23:04:29 | [diff] [blame] | 1036 | stream->set_response_received(); |
[email protected] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 1037 | |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 1038 | // Parse the headers. |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 1039 | if (!Respond(*headers, stream)) |
[email protected] | dd11b93 | 2009-11-30 19:39:48 | [diff] [blame] | 1040 | return; |
[email protected] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 1041 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1042 | LOG(INFO) << "Got pushed stream for " << stream->path(); |
| 1043 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 1044 | static StatsCounter push_requests("spdy.pushed_streams"); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1045 | push_requests.Increment(); |
| 1046 | } |
| 1047 | |
[email protected] | 651b77c | 2010-03-10 19:29:42 | [diff] [blame] | 1048 | void SpdySession::OnSynReply(const spdy::SpdySynReplyControlFrame& frame, |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 1049 | const linked_ptr<spdy::SpdyHeaderBlock>& headers) { |
[email protected] | 651b77c | 2010-03-10 19:29:42 | [diff] [blame] | 1050 | spdy::SpdyStreamId stream_id = frame.stream_id(); |
[email protected] | 9ccdad2 | 2010-03-04 19:19:21 | [diff] [blame] | 1051 | LOG(INFO) << "Spdy SynReply for stream " << stream_id; |
| 1052 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1053 | bool valid_stream = IsStreamActive(stream_id); |
| 1054 | if (!valid_stream) { |
[email protected] | 92683b5 | 2009-12-21 21:01:12 | [diff] [blame] | 1055 | // NOTE: it may just be that the stream was cancelled. |
[email protected] | affe8fe | 2009-10-14 20:06:05 | [diff] [blame] | 1056 | LOG(WARNING) << "Received SYN_REPLY for invalid stream " << stream_id; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1057 | return; |
| 1058 | } |
| 1059 | |
[email protected] | d3002235 | 2010-06-24 19:17:58 | [diff] [blame] | 1060 | scoped_refptr<SpdyStream> stream = active_streams_[stream_id]; |
| 1061 | CHECK_EQ(stream->stream_id(), stream_id); |
| 1062 | CHECK(!stream->cancelled()); |
| 1063 | |
[email protected] | a5c493b9 | 2010-08-06 23:04:29 | [diff] [blame] | 1064 | if (stream->response_received()) { |
[email protected] | d3002235 | 2010-06-24 19:17:58 | [diff] [blame] | 1065 | LOG(WARNING) << "Received duplicate SYN_REPLY for stream " << stream_id; |
| 1066 | CloseStream(stream->stream_id(), ERR_SPDY_PROTOCOL_ERROR); |
| 1067 | return; |
| 1068 | } |
[email protected] | a5c493b9 | 2010-08-06 23:04:29 | [diff] [blame] | 1069 | stream->set_response_received(); |
[email protected] | d3002235 | 2010-06-24 19:17:58 | [diff] [blame] | 1070 | |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 1071 | const BoundNetLog& log = stream->net_log(); |
[email protected] | 095c7cf | 2010-08-31 21:07:33 | [diff] [blame^] | 1072 | if (log.IsLoggingAll()) { |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 1073 | 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] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1081 | } |
| 1082 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 1083 | void SpdySession::OnControl(const spdy::SpdyControlFrame* frame) { |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 1084 | const linked_ptr<spdy::SpdyHeaderBlock> headers(new spdy::SpdyHeaderBlock); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1085 | uint32 type = frame->type(); |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 1086 | if (type == spdy::SYN_STREAM || type == spdy::SYN_REPLY) { |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 1087 | if (!spdy_framer_.ParseHeaderBlock(frame, headers.get())) { |
[email protected] | 6c6ea17 | 2010-07-27 20:04:03 | [diff] [blame] | 1088 | 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] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1098 | return; |
| 1099 | } |
| 1100 | } |
| 1101 | |
[email protected] | 58cebf8f | 2010-07-31 19:20:16 | [diff] [blame] | 1102 | frames_received_++; |
| 1103 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1104 | switch (type) { |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 1105 | 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] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 1113 | OnRst(*reinterpret_cast<const spdy::SpdyRstStreamControlFrame*>(frame)); |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 1114 | break; |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 1115 | case spdy::SYN_STREAM: |
[email protected] | 651b77c | 2010-03-10 19:29:42 | [diff] [blame] | 1116 | OnSyn(*reinterpret_cast<const spdy::SpdySynStreamControlFrame*>(frame), |
| 1117 | headers); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1118 | break; |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 1119 | case spdy::SYN_REPLY: |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1120 | OnSynReply( |
[email protected] | 651b77c | 2010-03-10 19:29:42 | [diff] [blame] | 1121 | *reinterpret_cast<const spdy::SpdySynReplyControlFrame*>(frame), |
| 1122 | headers); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1123 | break; |
[email protected] | 5af3c57 | 2010-07-20 14:16:27 | [diff] [blame] | 1124 | case spdy::WINDOW_UPDATE: |
| 1125 | OnWindowUpdate( |
| 1126 | *reinterpret_cast<const spdy::SpdyWindowUpdateControlFrame*>(frame)); |
| 1127 | break; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1128 | default: |
| 1129 | DCHECK(false); // Error! |
| 1130 | } |
| 1131 | } |
| 1132 | |
[email protected] | e3ebba0f | 2010-08-05 17:59:58 | [diff] [blame] | 1133 | void SpdySession::OnRst(const spdy::SpdyRstStreamControlFrame& frame) { |
[email protected] | 651b77c | 2010-03-10 19:29:42 | [diff] [blame] | 1134 | spdy::SpdyStreamId stream_id = frame.stream_id(); |
[email protected] | a5c493b9 | 2010-08-06 23:04:29 | [diff] [blame] | 1135 | LOG(INFO) << "Spdy RST for stream " << stream_id; |
[email protected] | 9ccdad2 | 2010-03-04 19:19:21 | [diff] [blame] | 1136 | |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1137 | bool valid_stream = IsStreamActive(stream_id); |
| 1138 | if (!valid_stream) { |
[email protected] | 92683b5 | 2009-12-21 21:01:12 | [diff] [blame] | 1139 | // NOTE: it may just be that the stream was cancelled. |
[email protected] | a5c493b9 | 2010-08-06 23:04:29 | [diff] [blame] | 1140 | LOG(WARNING) << "Received RST for invalid stream" << stream_id; |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1141 | return; |
| 1142 | } |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 1143 | scoped_refptr<SpdyStream> stream = active_streams_[stream_id]; |
[email protected] | b1f031dd | 2010-03-02 23:19:33 | [diff] [blame] | 1144 | CHECK_EQ(stream->stream_id(), stream_id); |
[email protected] | 92683b5 | 2009-12-21 21:01:12 | [diff] [blame] | 1145 | CHECK(!stream->cancelled()); |
[email protected] | 4429700 | 2010-05-16 00:59:10 | [diff] [blame] | 1146 | |
| 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] | 651b77c | 2010-03-10 19:29:42 | [diff] [blame] | 1152 | if (frame.status() == 0) { |
[email protected] | a677f2b | 2009-11-22 00:43:00 | [diff] [blame] | 1153 | stream->OnDataReceived(NULL, 0); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1154 | } else { |
[email protected] | 651b77c | 2010-03-10 19:29:42 | [diff] [blame] | 1155 | LOG(ERROR) << "Spdy stream closed: " << frame.status(); |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 1156 | // TODO(mbelshe): Map from Spdy-protocol errors to something sensical. |
[email protected] | 9330067 | 2009-10-24 13:22:51 | [diff] [blame] | 1157 | // For now, it doesn't matter much - it is a protocol error. |
[email protected] | 56e7c63d | 2010-07-12 18:42:17 | [diff] [blame] | 1158 | DeleteStream(stream_id, ERR_SPDY_PROTOCOL_ERROR); |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1159 | } |
[email protected] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1160 | } |
| 1161 | |
[email protected] | 651b77c | 2010-03-10 19:29:42 | [diff] [blame] | 1162 | void SpdySession::OnGoAway(const spdy::SpdyGoAwayControlFrame& frame) { |
[email protected] | bce9f68 | 2010-03-30 16:36:02 | [diff] [blame] | 1163 | LOG(INFO) << "Spdy GOAWAY for session[" << this << "] for " << |
| 1164 | host_port_pair().ToString(); |
[email protected] | c9336d2 | 2010-08-10 00:04:18 | [diff] [blame] | 1165 | 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] | 4429700 | 2010-05-16 00:59:10 | [diff] [blame] | 1182 | 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] | 4b4762a | 2010-04-23 16:04:14 | [diff] [blame] | 1188 | RemoveFromPool(); |
[email protected] | c9336d2 | 2010-08-10 00:04:18 | [diff] [blame] | 1189 | CloseAllStreams(net::ERR_ABORTED); |
[email protected] | 651b77c | 2010-03-10 19:29:42 | [diff] [blame] | 1190 | |
| 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] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 1199 | void SpdySession::OnSettings(const spdy::SpdySettingsControlFrame& frame) { |
| 1200 | spdy::SpdySettings settings; |
| 1201 | if (spdy_framer_.ParseSettings(&frame, &settings)) { |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 1202 | HandleSettings(settings); |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 1203 | SpdySettingsStorage* settings_storage = session_->mutable_spdy_settings(); |
[email protected] | b261d0e | 2010-08-02 19:13:24 | [diff] [blame] | 1204 | settings_storage->Set(host_port_pair(), settings); |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 1205 | } |
[email protected] | 4429700 | 2010-05-16 00:59:10 | [diff] [blame] | 1206 | |
[email protected] | 0aebc00 | 2010-05-21 06:50:19 | [diff] [blame] | 1207 | received_settings_ = true; |
| 1208 | |
[email protected] | 4429700 | 2010-05-16 00:59:10 | [diff] [blame] | 1209 | net_log_.AddEvent( |
| 1210 | NetLog::TYPE_SPDY_SESSION_RECV_SETTINGS, |
| 1211 | new NetLogSpdySettingsParameter(settings)); |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 1212 | } |
| 1213 | |
[email protected] | 5af3c57 | 2010-07-20 14:16:27 | [diff] [blame] | 1214 | void 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] | 7349c6b1 | 2010-07-22 02:29:16 | [diff] [blame] | 1236 | if (use_flow_control_) |
| 1237 | stream->IncreaseSendWindowSize(delta_window_size); |
[email protected] | 5af3c57 | 2010-07-20 14:16:27 | [diff] [blame] | 1238 | } |
| 1239 | |
[email protected] | 450c502 | 2010-08-26 02:38:28 | [diff] [blame] | 1240 | void 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] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 1254 | void SpdySession::SendSettings() { |
| 1255 | const SpdySettingsStorage& settings_storage = session_->spdy_settings(); |
[email protected] | b261d0e | 2010-08-02 19:13:24 | [diff] [blame] | 1256 | const spdy::SpdySettings& settings = settings_storage.Get(host_port_pair()); |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 1257 | if (settings.empty()) |
| 1258 | return; |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 1259 | HandleSettings(settings); |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 1260 | |
[email protected] | 4429700 | 2010-05-16 00:59:10 | [diff] [blame] | 1261 | net_log_.AddEvent( |
| 1262 | NetLog::TYPE_SPDY_SESSION_SEND_SETTINGS, |
| 1263 | new NetLogSpdySettingsParameter(settings)); |
| 1264 | |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 1265 | // Create the SETTINGS frame and send it. |
| 1266 | scoped_ptr<spdy::SpdySettingsControlFrame> settings_frame( |
| 1267 | spdy_framer_.CreateSettings(settings)); |
[email protected] | 0aebc00 | 2010-05-21 06:50:19 | [diff] [blame] | 1268 | sent_settings_ = true; |
[email protected] | 74188f2 | 2010-04-09 20:18:50 | [diff] [blame] | 1269 | QueueFrame(settings_frame.get(), 0, NULL); |
| 1270 | } |
| 1271 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 1272 | void 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] | 0aebc00 | 2010-05-21 06:50:19 | [diff] [blame] | 1286 | void 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] | b261d0e | 2010-08-02 19:13:24 | [diff] [blame] | 1307 | const spdy::SpdySettings& settings = settings_storage.Get(host_port_pair()); |
[email protected] | 0aebc00 | 2010-05-21 06:50:19 | [diff] [blame] | 1308 | |
| 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] | aea8060 | 2009-09-18 00:55:08 | [diff] [blame] | 1333 | } // namespace net |