diff options
author | toyoshim@chromium.org <toyoshim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-16 10:34:50 +0000 |
---|---|---|
committer | toyoshim@chromium.org <toyoshim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-16 10:34:50 +0000 |
commit | 80b8161e79ed4ec189592203b072002188044e08 (patch) | |
tree | bd56b0acb044d01f97e5368ef5e4608bff8196aa /net/socket_stream | |
parent | d3ef999453b4a280c9e2a3de6ab37d1d65d2ff1f (diff) | |
download | chromium_src-80b8161e79ed4ec189592203b072002188044e08.zip chromium_src-80b8161e79ed4ec189592203b072002188044e08.tar.gz chromium_src-80b8161e79ed4ec189592203b072002188044e08.tar.bz2 |
WebSocket over SPDY: protocol switch
This change enable to switch protocol from WebSocket to SPDY.
SpdyWebsocketStream class is not included, then SPDY connection
always fail now.
BUG=42320
TEST=net_unittests --gtest_filter=WebSocket\*
Review URL: http://codereview.chromium.org/7062043
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@89318 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/socket_stream')
-rw-r--r-- | net/socket_stream/socket_stream.cc | 26 | ||||
-rw-r--r-- | net/socket_stream/socket_stream.h | 2 | ||||
-rw-r--r-- | net/socket_stream/socket_stream_unittest.cc | 105 |
3 files changed, 107 insertions, 26 deletions
diff --git a/net/socket_stream/socket_stream.cc b/net/socket_stream/socket_stream.cc index a36603f..5915ba4 100644 --- a/net/socket_stream/socket_stream.cc +++ b/net/socket_stream/socket_stream.cc @@ -257,9 +257,10 @@ void SocketStream::CopyAddrInfo(struct addrinfo* head) { void SocketStream::DoClose() { closing_ = true; - // If next_state_ is STATE_TCP_CONNECT, it's waiting other socket establishing - // connection. If next_state_ is STATE_AUTH_REQUIRED, it's waiting for - // restarting. In these states, we'll close the SocketStream now. + // If next_state_ is STATE_TCP_CONNECT, it's waiting other socket + // establishing connection. If next_state_ is STATE_AUTH_REQUIRED, it's + // waiting for restarting. In these states, we'll close the SocketStream + // now. if (next_state_ == STATE_TCP_CONNECT || next_state_ == STATE_AUTH_REQUIRED) { DoLoop(ERR_ABORTED); return; @@ -470,6 +471,14 @@ int SocketStream::DoResolveProxy() { return ERR_INVALID_ARGUMENT; } + // TODO(toyoshim): Check server advertisement of SPDY through the HTTP + // Alternate-Protocol header, then switch to SPDY if SPDY is available. + // Usually we already have a session to the SPDY server because JavaScript + // running WebSocket itself would be served by SPDY. But, in some situation + // (E.g. Used by Chrome Extensions or used for cross origin connection), this + // connection might be the first one. At that time, we should check + // Alternate-Protocol header here for ws:// or TLS NPN extension for wss:// . + return proxy_service()->ResolveProxy( proxy_url_, &proxy_info_, &io_callback_, &pac_request_, net_log_); } @@ -541,6 +550,9 @@ int SocketStream::DoResolveHostComplete(int result) { result = delegate_->OnStartOpenConnection(this, &io_callback_); if (result == ERR_IO_PENDING) metrics_->OnWaitConnection(); + else if (result == ERR_PROTOCOL_SWITCHED) + // TODO(toyoshim): Add metrics events for protocol switch. + next_state_ = STATE_CLOSE; } else { next_state_ = STATE_CLOSE; } @@ -548,6 +560,10 @@ int SocketStream::DoResolveHostComplete(int result) { return result; } +const ProxyServer& SocketStream::proxy_server() const { + return proxy_info_.proxy_server(); +} + int SocketStream::DoTcpConnect(int result) { if (result != OK) { next_state_ = STATE_CLOSE; @@ -853,6 +869,10 @@ int SocketStream::DoSSLConnectComplete(int result) { } } + // TODO(toyoshim): Upgrade to SPDY through TLS NPN extension if possible. + // If we use HTTPS and this is the first connection to the SPDY server, + // we should take care of TLS NPN extension here. + if (result == OK) result = DidEstablishConnection(); else diff --git a/net/socket_stream/socket_stream.h b/net/socket_stream/socket_stream.h index 0b1bc10..4fb9ea3 100644 --- a/net/socket_stream/socket_stream.h +++ b/net/socket_stream/socket_stream.h @@ -159,6 +159,8 @@ class NET_API SocketStream : public base::RefCountedThreadSafe<SocketStream> { // back. virtual void DetachDelegate(); + const ProxyServer& proxy_server() const; + // Sets an alternative HostResolver. For testing purposes only. void SetHostResolver(HostResolver* host_resolver); diff --git a/net/socket_stream/socket_stream_unittest.cc b/net/socket_stream/socket_stream_unittest.cc index c1e2d7d..5cd0207 100644 --- a/net/socket_stream/socket_stream_unittest.cc +++ b/net/socket_stream/socket_stream_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -20,33 +20,39 @@ struct SocketStreamEvent { enum EventType { - EVENT_CONNECTED, EVENT_SENT_DATA, EVENT_RECEIVED_DATA, EVENT_CLOSE, - EVENT_AUTH_REQUIRED, + EVENT_START_OPEN_CONNECTION, EVENT_CONNECTED, EVENT_SENT_DATA, + EVENT_RECEIVED_DATA, EVENT_CLOSE, EVENT_AUTH_REQUIRED, }; - SocketStreamEvent(EventType type, net::SocketStream* socket_stream, - int num, const std::string& str, - net::AuthChallengeInfo* auth_challenge_info) + SocketStreamEvent(EventType type, + net::SocketStream* socket_stream, + int num, + const std::string& str, + net::AuthChallengeInfo* auth_challenge_info, + net::CompletionCallback* callback) : event_type(type), socket(socket_stream), number(num), data(str), - auth_info(auth_challenge_info) {} + auth_info(auth_challenge_info), result(net::OK) {} EventType event_type; net::SocketStream* socket; int number; std::string data; scoped_refptr<net::AuthChallengeInfo> auth_info; + int result; }; class SocketStreamEventRecorder : public net::SocketStream::Delegate { public: explicit SocketStreamEventRecorder(net::CompletionCallback* callback) - : on_connected_(NULL), + : on_start_open_connection_(NULL), + on_connected_(NULL), on_sent_data_(NULL), on_received_data_(NULL), on_close_(NULL), on_auth_required_(NULL), callback_(callback) {} virtual ~SocketStreamEventRecorder() { + delete on_start_open_connection_; delete on_connected_; delete on_sent_data_; delete on_received_data_; @@ -54,6 +60,10 @@ class SocketStreamEventRecorder : public net::SocketStream::Delegate { delete on_auth_required_; } + void SetOnStartOpenConnection( + Callback1<SocketStreamEvent*>::Type* callback) { + on_start_open_connection_ = callback; + } void SetOnConnected(Callback1<SocketStreamEvent*>::Type* callback) { on_connected_ = callback; } @@ -70,12 +80,21 @@ class SocketStreamEventRecorder : public net::SocketStream::Delegate { on_auth_required_ = callback; } + virtual int OnStartOpenConnection(net::SocketStream* socket, + net::CompletionCallback* callback) { + events_.push_back( + SocketStreamEvent(SocketStreamEvent::EVENT_START_OPEN_CONNECTION, + socket, 0, std::string(), NULL, callback)); + if (on_start_open_connection_) + on_start_open_connection_->Run(&events_.back()); + return events_.back().result; + } virtual void OnConnected(net::SocketStream* socket, int num_pending_send_allowed) { events_.push_back( SocketStreamEvent(SocketStreamEvent::EVENT_CONNECTED, socket, num_pending_send_allowed, std::string(), - NULL)); + NULL, NULL)); if (on_connected_) on_connected_->Run(&events_.back()); } @@ -83,7 +102,7 @@ class SocketStreamEventRecorder : public net::SocketStream::Delegate { int amount_sent) { events_.push_back( SocketStreamEvent(SocketStreamEvent::EVENT_SENT_DATA, - socket, amount_sent, std::string(), NULL)); + socket, amount_sent, std::string(), NULL, NULL)); if (on_sent_data_) on_sent_data_->Run(&events_.back()); } @@ -91,14 +110,14 @@ class SocketStreamEventRecorder : public net::SocketStream::Delegate { const char* data, int len) { events_.push_back( SocketStreamEvent(SocketStreamEvent::EVENT_RECEIVED_DATA, - socket, len, std::string(data, len), NULL)); + socket, len, std::string(data, len), NULL, NULL)); if (on_received_data_) on_received_data_->Run(&events_.back()); } virtual void OnClose(net::SocketStream* socket) { events_.push_back( SocketStreamEvent(SocketStreamEvent::EVENT_CLOSE, - socket, 0, std::string(), NULL)); + socket, 0, std::string(), NULL, NULL)); if (on_close_) on_close_->Run(&events_.back()); if (callback_) @@ -108,7 +127,7 @@ class SocketStreamEventRecorder : public net::SocketStream::Delegate { net::AuthChallengeInfo* auth_info) { events_.push_back( SocketStreamEvent(SocketStreamEvent::EVENT_AUTH_REQUIRED, - socket, 0, std::string(), auth_info)); + socket, 0, std::string(), auth_info, NULL)); if (on_auth_required_) on_auth_required_->Run(&events_.back()); } @@ -133,6 +152,7 @@ class SocketStreamEventRecorder : public net::SocketStream::Delegate { private: std::vector<SocketStreamEvent> events_; + Callback1<SocketStreamEvent*>::Type* on_start_open_connection_; Callback1<SocketStreamEvent*>::Type* on_connected_; Callback1<SocketStreamEvent*>::Type* on_sent_data_; Callback1<SocketStreamEvent*>::Type* on_received_data_; @@ -193,6 +213,10 @@ class SocketStreamTest : public PlatformTest { event->socket->Close(); } + virtual void DoSwitchToSpdyTest(SocketStreamEvent* event) { + event->result = net::ERR_PROTOCOL_SWITCHED; + } + static const char* kWebSocketHandshakeRequest; static const char* kWebSocketHandshakeResponse; @@ -275,14 +299,16 @@ TEST_F(SocketStreamTest, CloseFlushPendingWrite) { callback.WaitForResult(); const std::vector<SocketStreamEvent>& events = delegate->GetSeenEvents(); - EXPECT_EQ(6U, events.size()); + ASSERT_EQ(7U, events.size()); - EXPECT_EQ(SocketStreamEvent::EVENT_CONNECTED, events[0].event_type); - EXPECT_EQ(SocketStreamEvent::EVENT_SENT_DATA, events[1].event_type); - EXPECT_EQ(SocketStreamEvent::EVENT_RECEIVED_DATA, events[2].event_type); - EXPECT_EQ(SocketStreamEvent::EVENT_SENT_DATA, events[3].event_type); + EXPECT_EQ(SocketStreamEvent::EVENT_START_OPEN_CONNECTION, + events[0].event_type); + EXPECT_EQ(SocketStreamEvent::EVENT_CONNECTED, events[1].event_type); + EXPECT_EQ(SocketStreamEvent::EVENT_SENT_DATA, events[2].event_type); + EXPECT_EQ(SocketStreamEvent::EVENT_RECEIVED_DATA, events[3].event_type); EXPECT_EQ(SocketStreamEvent::EVENT_SENT_DATA, events[4].event_type); - EXPECT_EQ(SocketStreamEvent::EVENT_CLOSE, events[5].event_type); + EXPECT_EQ(SocketStreamEvent::EVENT_SENT_DATA, events[5].event_type); + EXPECT_EQ(SocketStreamEvent::EVENT_CLOSE, events[6].event_type); } TEST_F(SocketStreamTest, BasicAuthProxy) { @@ -344,13 +370,46 @@ TEST_F(SocketStreamTest, BasicAuthProxy) { callback.WaitForResult(); const std::vector<SocketStreamEvent>& events = delegate->GetSeenEvents(); - EXPECT_EQ(3U, events.size()); + ASSERT_EQ(4U, events.size()); - EXPECT_EQ(SocketStreamEvent::EVENT_AUTH_REQUIRED, events[0].event_type); - EXPECT_EQ(SocketStreamEvent::EVENT_CONNECTED, events[1].event_type); - EXPECT_EQ(SocketStreamEvent::EVENT_CLOSE, events[2].event_type); + EXPECT_EQ(SocketStreamEvent::EVENT_START_OPEN_CONNECTION, + events[0].event_type); + EXPECT_EQ(SocketStreamEvent::EVENT_AUTH_REQUIRED, events[1].event_type); + EXPECT_EQ(SocketStreamEvent::EVENT_CONNECTED, events[2].event_type); + EXPECT_EQ(SocketStreamEvent::EVENT_CLOSE, events[3].event_type); // TODO(eroman): Add back NetLogTest here... } +TEST_F(SocketStreamTest, SwitchToSpdy) { + TestCompletionCallback callback; + + scoped_ptr<SocketStreamEventRecorder> delegate( + new SocketStreamEventRecorder(&callback)); + SocketStreamTest* test = this; + delegate->SetOnStartOpenConnection(NewCallback( + test, &SocketStreamTest::DoSwitchToSpdyTest)); + + MockHostResolver host_resolver; + + scoped_refptr<SocketStream> socket_stream( + new SocketStream(GURL("ws://example.com/demo"), delegate.get())); + + socket_stream->set_context(new TestURLRequestContext()); + socket_stream->SetHostResolver(&host_resolver); + + socket_stream->Connect(); + + int result = callback.WaitForResult(); + + const std::vector<SocketStreamEvent>& events = delegate->GetSeenEvents(); + ASSERT_EQ(2U, events.size()); + + EXPECT_EQ(SocketStreamEvent::EVENT_START_OPEN_CONNECTION, + events[0].event_type); + EXPECT_EQ(net::ERR_PROTOCOL_SWITCHED, events[0].result); + EXPECT_EQ(SocketStreamEvent::EVENT_CLOSE, events[1].event_type); + EXPECT_EQ(net::OK, result); +} + } // namespace net |