1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "net/http/http_network_layer.h" 6 7 #include "base/logging.h" 8 #include "base/string_number_conversions.h" 9 #include "base/string_split.h" 10 #include "base/string_util.h" 11 #include "net/http/http_network_session.h" 12 #include "net/http/http_network_transaction.h" 13 #include "net/spdy/spdy_framer.h" 14 #include "net/spdy/spdy_session.h" 15 #include "net/spdy/spdy_session_pool.h" 16 17 namespace net { 18 19 //----------------------------------------------------------------------------- 20 HttpNetworkLayer::HttpNetworkLayer(HttpNetworkSession* session) 21 : session_(session), 22 suspended_(false) { 23 DCHECK(session_.get()); 24 } 25 26 HttpNetworkLayer::~HttpNetworkLayer() { 27 } 28 29 //----------------------------------------------------------------------------- 30 31 // static 32 HttpTransactionFactory* HttpNetworkLayer::CreateFactory( 33 HttpNetworkSession* session) { 34 DCHECK(session); 35 36 return new HttpNetworkLayer(session); 37 } 38 39 // static 40 void HttpNetworkLayer::EnableSpdy(const std::string& mode) { 41 static const char kOff[] = "off"; 42 static const char kSSL[] = "ssl"; 43 static const char kDisableSSL[] = "no-ssl"; 44 static const char kExclude[] = "exclude"; // Hosts to exclude 45 static const char kDisableCompression[] = "no-compress"; 46 static const char kDisableAltProtocols[] = "no-alt-protocols"; 47 static const char kEnableVersionOne[] = "v1"; 48 static const char kForceAltProtocols[] = "force-alt-protocols"; 49 static const char kSingleDomain[] = "single-domain"; 50 51 // If flow-control is enabled, received WINDOW_UPDATE and SETTINGS 52 // messages are processed and outstanding window size is actually obeyed 53 // when sending data frames, and WINDOW_UPDATE messages are generated 54 // when data is consumed. 55 static const char kEnableFlowControl[] = "flow-control"; 56 57 // We want an A/B experiment between SPDY enabled and SPDY disabled, 58 // but only for pages where SPDY *could have been* negotiated. To do 59 // this, we use NPN, but prevent it from negotiating SPDY. If the 60 // server negotiates HTTP, rather than SPDY, today that will only happen 61 // on servers that installed NPN (and could have done SPDY). But this is 62 // a bit of a hack, as this correlation between NPN and SPDY is not 63 // really guaranteed. 64 static const char kEnableNPN[] = "npn"; 65 static const char kEnableNpnHttpOnly[] = "npn-http"; 66 67 // Except for the first element, the order is irrelevant. First element 68 // specifies the fallback in case nothing matches 69 // (SSLClientSocket::kNextProtoNoOverlap). Otherwise, the SSL library 70 // will choose the first overlapping protocol in the server's list, since 71 // it presumedly has a better understanding of which protocol we should 72 // use, therefore the rest of the ordering here is not important. 73 static const char kNpnProtosFull[] = "\x08http/1.1\x06spdy/2"; 74 // This is a temporary hack to pretend we support version 1. 75 static const char kNpnProtosFullV1[] = "\x08http/1.1\x06spdy/1"; 76 // No spdy specified. 77 static const char kNpnProtosHttpOnly[] = "\x08http/1.1\x07http1.1"; 78 79 std::vector<std::string> spdy_options; 80 base::SplitString(mode, ',', &spdy_options); 81 82 bool use_alt_protocols = true; 83 84 for (std::vector<std::string>::iterator it = spdy_options.begin(); 85 it != spdy_options.end(); ++it) { 86 const std::string& element = *it; 87 std::vector<std::string> name_value; 88 base::SplitString(element, '=', &name_value); 89 const std::string& option = name_value[0]; 90 const std::string value = name_value.size() > 1 ? name_value[1] : ""; 91 92 if (option == kOff) { 93 HttpStreamFactory::set_spdy_enabled(false); 94 } else if (option == kDisableSSL) { 95 SpdySession::SetSSLMode(false); // Disable SSL 96 HttpStreamFactory::set_force_spdy_over_ssl(false); 97 HttpStreamFactory::set_force_spdy_always(true); 98 } else if (option == kSSL) { 99 HttpStreamFactory::set_force_spdy_over_ssl(true); 100 HttpStreamFactory::set_force_spdy_always(true); 101 } else if (option == kExclude) { 102 HttpStreamFactory::add_forced_spdy_exclusion(value); 103 } else if (option == kDisableCompression) { 104 spdy::SpdyFramer::set_enable_compression_default(false); 105 } else if (option == kEnableNPN) { 106 HttpStreamFactory::set_use_alternate_protocols(use_alt_protocols); 107 HttpStreamFactory::set_next_protos(kNpnProtosFull); 108 } else if (option == kEnableNpnHttpOnly) { 109 // Avoid alternate protocol in this case. Otherwise, browser will try SSL 110 // and then fallback to http. This introduces extra load. 111 HttpStreamFactory::set_use_alternate_protocols(false); 112 HttpStreamFactory::set_next_protos(kNpnProtosHttpOnly); 113 } else if (option == kEnableVersionOne) { 114 spdy::SpdyFramer::set_protocol_version(1); 115 HttpStreamFactory::set_next_protos(kNpnProtosFullV1); 116 } else if (option == kDisableAltProtocols) { 117 use_alt_protocols = false; 118 HttpStreamFactory::set_use_alternate_protocols(false); 119 } else if (option == kEnableFlowControl) { 120 SpdySession::set_flow_control(true); 121 } else if (option == kForceAltProtocols) { 122 HttpAlternateProtocols::PortProtocolPair pair; 123 pair.port = 443; 124 pair.protocol = HttpAlternateProtocols::NPN_SPDY_2; 125 HttpAlternateProtocols::ForceAlternateProtocol(pair); 126 } else if (option == kSingleDomain) { 127 SpdySessionPool::ForceSingleDomain(); 128 LOG(ERROR) << "FORCING SINGLE DOMAIN"; 129 } else if (option.empty() && it == spdy_options.begin()) { 130 continue; 131 } else { 132 LOG(DFATAL) << "Unrecognized spdy option: " << option; 133 } 134 } 135 } 136 137 //----------------------------------------------------------------------------- 138 139 int HttpNetworkLayer::CreateTransaction(scoped_ptr<HttpTransaction>* trans) { 140 if (suspended_) 141 return ERR_NETWORK_IO_SUSPENDED; 142 143 trans->reset(new HttpNetworkTransaction(GetSession())); 144 return OK; 145 } 146 147 HttpCache* HttpNetworkLayer::GetCache() { 148 return NULL; 149 } 150 151 HttpNetworkSession* HttpNetworkLayer::GetSession() { 152 return session_; 153 } 154 155 void HttpNetworkLayer::Suspend(bool suspend) { 156 suspended_ = suspend; 157 158 if (suspend && session_) 159 session_->CloseIdleConnections(); 160 } 161 162 } // namespace net 163