Commit de6251d0 authored by mbelshe@chromium.org's avatar mbelshe@chromium.org

Add option to simulate alternate protocol always being present.

It's a bit hardcoded (port 443, NPN/SPDY2), but it is just for
testing purposes.

To use this mode, run chrome with:
   chrome.exe --use-spdy=npn,force-alt-protocols

BUG=none
TEST=http_alternate_protocols_unittest.cc

Review URL: http://codereview.chromium.org/3195015

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@57022 0039d316-1c4b-4281-b951-d872f2087c98
parent a88fe62c
...@@ -15,12 +15,17 @@ const char* const HttpAlternateProtocols::kProtocolStrings[] = { ...@@ -15,12 +15,17 @@ const char* const HttpAlternateProtocols::kProtocolStrings[] = {
"npn-spdy/2", "npn-spdy/2",
}; };
// static
HttpAlternateProtocols::PortProtocolPair*
HttpAlternateProtocols::forced_alternate_protocol_ = NULL;
HttpAlternateProtocols::HttpAlternateProtocols() {} HttpAlternateProtocols::HttpAlternateProtocols() {}
HttpAlternateProtocols::~HttpAlternateProtocols() {} HttpAlternateProtocols::~HttpAlternateProtocols() {}
bool HttpAlternateProtocols::HasAlternateProtocolFor( bool HttpAlternateProtocols::HasAlternateProtocolFor(
const HostPortPair& http_host_port_pair) const { const HostPortPair& http_host_port_pair) const {
return ContainsKey(protocol_map_, http_host_port_pair); return ContainsKey(protocol_map_, http_host_port_pair) ||
forced_alternate_protocol_;
} }
bool HttpAlternateProtocols::HasAlternateProtocolFor( bool HttpAlternateProtocols::HasAlternateProtocolFor(
...@@ -32,8 +37,16 @@ bool HttpAlternateProtocols::HasAlternateProtocolFor( ...@@ -32,8 +37,16 @@ bool HttpAlternateProtocols::HasAlternateProtocolFor(
HttpAlternateProtocols::PortProtocolPair HttpAlternateProtocols::PortProtocolPair
HttpAlternateProtocols::GetAlternateProtocolFor( HttpAlternateProtocols::GetAlternateProtocolFor(
const HostPortPair& http_host_port_pair) const { const HostPortPair& http_host_port_pair) const {
DCHECK(ContainsKey(protocol_map_, http_host_port_pair)); DCHECK(HasAlternateProtocolFor(http_host_port_pair));
return protocol_map_.find(http_host_port_pair)->second;
// First check the map.
ProtocolMap::const_iterator it = protocol_map_.find(http_host_port_pair);
if (it != protocol_map_.end())
return it->second;
// We must be forcing an alternate.
DCHECK(forced_alternate_protocol_);
return *forced_alternate_protocol_;
} }
HttpAlternateProtocols::PortProtocolPair HttpAlternateProtocols::PortProtocolPair
...@@ -83,4 +96,19 @@ void HttpAlternateProtocols::MarkBrokenAlternateProtocolFor( ...@@ -83,4 +96,19 @@ void HttpAlternateProtocols::MarkBrokenAlternateProtocolFor(
protocol_map_[http_host_port_pair].protocol = BROKEN; protocol_map_[http_host_port_pair].protocol = BROKEN;
} }
// static
void HttpAlternateProtocols::ForceAlternateProtocol(
const PortProtocolPair& pair) {
// Note: we're going to leak this.
if (forced_alternate_protocol_)
delete forced_alternate_protocol_;
forced_alternate_protocol_ = new PortProtocolPair(pair);
}
// static
void HttpAlternateProtocols::DisableForcedAlternateProtocol() {
delete forced_alternate_protocol_;
forced_alternate_protocol_ = NULL;
}
} // namespace net } // namespace net
...@@ -63,11 +63,21 @@ class HttpAlternateProtocols { ...@@ -63,11 +63,21 @@ class HttpAlternateProtocols {
// attempts to set the alternate protocol for |http_host_port_pair| will fail. // attempts to set the alternate protocol for |http_host_port_pair| will fail.
void MarkBrokenAlternateProtocolFor(const HostPortPair& http_host_port_pair); void MarkBrokenAlternateProtocolFor(const HostPortPair& http_host_port_pair);
// Debugging to simulate presence of an AlternateProtocol.
// If we don't have an alternate protocol in the map for any given host/port
// pair, force this ProtocolPortPair.
static void ForceAlternateProtocol(const PortProtocolPair& pair);
static void DisableForcedAlternateProtocol();
private: private:
typedef std::map<HostPortPair, PortProtocolPair> ProtocolMap; typedef std::map<HostPortPair, PortProtocolPair> ProtocolMap;
ProtocolMap protocol_map_; ProtocolMap protocol_map_;
// The forced alternate protocol. If not-null, there is a protocol being
// forced.
static PortProtocolPair* forced_alternate_protocol_;
DISALLOW_COPY_AND_ASSIGN(HttpAlternateProtocols); DISALLOW_COPY_AND_ASSIGN(HttpAlternateProtocols);
}; };
......
...@@ -44,5 +44,42 @@ TEST(HttpAlternateProtocols, SetBroken) { ...@@ -44,5 +44,42 @@ TEST(HttpAlternateProtocols, SetBroken) {
<< "Second attempt should be ignored."; << "Second attempt should be ignored.";
} }
TEST(HttpAlternateProtocols, Forced) {
// Test forced alternate protocols.
HttpAlternateProtocols::PortProtocolPair default_protocol;
default_protocol.port = 1234;
default_protocol.protocol = HttpAlternateProtocols::NPN_SPDY_2;
HttpAlternateProtocols::ForceAlternateProtocol(default_protocol);
HttpAlternateProtocols alternate_protocols;
// Verify the forced protocol.
HostPortPair test_host_port_pair("foo", 80);
EXPECT_TRUE(
alternate_protocols.HasAlternateProtocolFor(test_host_port_pair));
HttpAlternateProtocols::PortProtocolPair alternate =
alternate_protocols.GetAlternateProtocolFor(test_host_port_pair);
EXPECT_EQ(default_protocol.port, alternate.port);
EXPECT_EQ(default_protocol.protocol, alternate.protocol);
// Verify the real protocol overrides the forced protocol.
alternate_protocols.SetAlternateProtocolFor(
test_host_port_pair, 443, HttpAlternateProtocols::NPN_SPDY_1);
ASSERT_TRUE(alternate_protocols.HasAlternateProtocolFor(test_host_port_pair));
alternate = alternate_protocols.GetAlternateProtocolFor(test_host_port_pair);
EXPECT_EQ(443, alternate.port);
EXPECT_EQ(HttpAlternateProtocols::NPN_SPDY_1, alternate.protocol);
// Turn off the static, forced alternate protocol so that tests don't
// have this state.
HttpAlternateProtocols::DisableForcedAlternateProtocol();
// Verify the forced protocol is off.
HostPortPair test_host_port_pair2("bar", 80);
EXPECT_FALSE(
alternate_protocols.HasAlternateProtocolFor(test_host_port_pair2));
}
} // namespace } // namespace
} // namespace net } // namespace net
...@@ -148,6 +148,7 @@ void HttpNetworkLayer::EnableSpdy(const std::string& mode) { ...@@ -148,6 +148,7 @@ void HttpNetworkLayer::EnableSpdy(const std::string& mode) {
static const char kDisableCompression[] = "no-compress"; static const char kDisableCompression[] = "no-compress";
static const char kDisableAltProtocols[] = "no-alt-protocols"; static const char kDisableAltProtocols[] = "no-alt-protocols";
static const char kEnableVersionOne[] = "v1"; static const char kEnableVersionOne[] = "v1";
static const char kForceAltProtocols[] = "force-alt-protocols";
// If flow-control is enabled, received WINDOW_UPDATE and SETTINGS // If flow-control is enabled, received WINDOW_UPDATE and SETTINGS
// messages are processed and outstanding window size is actually obeyed // messages are processed and outstanding window size is actually obeyed
...@@ -210,6 +211,11 @@ void HttpNetworkLayer::EnableSpdy(const std::string& mode) { ...@@ -210,6 +211,11 @@ void HttpNetworkLayer::EnableSpdy(const std::string& mode) {
HttpStreamFactory::set_use_alternate_protocols(false); HttpStreamFactory::set_use_alternate_protocols(false);
} else if (option == kEnableFlowControl) { } else if (option == kEnableFlowControl) {
SpdySession::SetFlowControl(true); SpdySession::SetFlowControl(true);
} else if (option == kForceAltProtocols) {
HttpAlternateProtocols::PortProtocolPair pair;
pair.port = 443;
pair.protocol = HttpAlternateProtocols::NPN_SPDY_2;
HttpAlternateProtocols::ForceAlternateProtocol(pair);
} else if (option.empty() && it == spdy_options.begin()) { } else if (option.empty() && it == spdy_options.begin()) {
continue; continue;
} else { } else {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment