blob: 2329627c1bd4d9477aa60dddc12321febceb6d12 [file] [log] [blame]
[email protected]fa55e192010-02-15 14:25:501// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#include "net/http/http_network_layer.h"
6
[email protected]65041fa2010-05-21 06:56:537#include "base/field_trial.h"
[email protected]0dfc81b2008-08-25 03:44:408#include "base/logging.h"
[email protected]85c0ed82009-12-15 23:14:149#include "base/string_util.h"
initial.commit586acc5fe2008-07-26 22:42:5210#include "net/http/http_network_session.h"
11#include "net/http/http_network_transaction.h"
[email protected]f7984fc62009-06-22 23:26:4412#include "net/socket/client_socket_factory.h"
[email protected]dab9c7d2010-02-06 21:44:3213#include "net/spdy/spdy_framer.h"
14#include "net/spdy/spdy_network_transaction.h"
15#include "net/spdy/spdy_session.h"
[email protected]61a86c42010-04-19 22:45:5316#include "net/spdy/spdy_session_pool.h"
initial.commit586acc5fe2008-07-26 22:42:5217
18namespace net {
19
20//-----------------------------------------------------------------------------
21
initial.commit586acc5fe2008-07-26 22:42:5222// static
23HttpTransactionFactory* HttpNetworkLayer::CreateFactory(
[email protected]d13c3272010-02-04 00:24:5124 NetworkChangeNotifier* network_change_notifier,
[email protected]8a00f00a2009-06-12 00:49:3825 HostResolver* host_resolver,
[email protected]db36938c2009-08-19 21:48:4226 ProxyService* proxy_service,
[email protected]fa55e192010-02-15 14:25:5027 SSLConfigService* ssl_config_service,
[email protected]06650c52010-06-03 00:49:1728 HttpAuthHandlerFactory* http_auth_handler_factory,
[email protected]ac039522010-06-15 16:39:4429 HttpNetworkDelegate* network_delegate,
[email protected]06650c52010-06-03 00:49:1730 NetLog* net_log) {
[email protected]63de95b2008-12-10 04:11:2731 DCHECK(proxy_service);
initial.commit586acc5fe2008-07-26 22:42:5232
[email protected]c6894572009-06-16 17:10:1033 return new HttpNetworkLayer(ClientSocketFactory::GetDefaultFactory(),
[email protected]d13c3272010-02-04 00:24:5134 network_change_notifier,
[email protected]fa55e192010-02-15 14:25:5035 host_resolver, proxy_service, ssl_config_service,
[email protected]06650c52010-06-03 00:49:1736 http_auth_handler_factory,
[email protected]ac039522010-06-15 16:39:4437 network_delegate,
[email protected]06650c52010-06-03 00:49:1738 net_log);
initial.commit586acc5fe2008-07-26 22:42:5239}
40
[email protected]e7f29642009-03-02 22:53:1841// static
42HttpTransactionFactory* HttpNetworkLayer::CreateFactory(
43 HttpNetworkSession* session) {
44 DCHECK(session);
45
46 return new HttpNetworkLayer(session);
47}
48
initial.commit586acc5fe2008-07-26 22:42:5249//-----------------------------------------------------------------------------
[email protected]955fc2e72010-02-08 20:37:3050bool HttpNetworkLayer::force_spdy_ = false;
initial.commit586acc5fe2008-07-26 22:42:5251
[email protected]d13c3272010-02-04 00:24:5152HttpNetworkLayer::HttpNetworkLayer(
53 ClientSocketFactory* socket_factory,
54 NetworkChangeNotifier* network_change_notifier,
55 HostResolver* host_resolver,
56 ProxyService* proxy_service,
[email protected]fa55e192010-02-15 14:25:5057 SSLConfigService* ssl_config_service,
[email protected]06650c52010-06-03 00:49:1758 HttpAuthHandlerFactory* http_auth_handler_factory,
[email protected]ac039522010-06-15 16:39:4459 HttpNetworkDelegate* network_delegate,
[email protected]06650c52010-06-03 00:49:1760 NetLog* net_log)
[email protected]c6894572009-06-16 17:10:1061 : socket_factory_(socket_factory),
[email protected]d13c3272010-02-04 00:24:5162 network_change_notifier_(network_change_notifier),
[email protected]c6894572009-06-16 17:10:1063 host_resolver_(host_resolver),
[email protected]8a00f00a2009-06-12 00:49:3864 proxy_service_(proxy_service),
[email protected]db36938c2009-08-19 21:48:4265 ssl_config_service_(ssl_config_service),
[email protected]8a00f00a2009-06-12 00:49:3866 session_(NULL),
[email protected]61a86c42010-04-19 22:45:5367 spdy_session_pool_(NULL),
[email protected]fa55e192010-02-15 14:25:5068 http_auth_handler_factory_(http_auth_handler_factory),
[email protected]ac039522010-06-15 16:39:4469 network_delegate_(network_delegate),
[email protected]06650c52010-06-03 00:49:1770 net_log_(net_log),
[email protected]8a00f00a2009-06-12 00:49:3871 suspended_(false) {
[email protected]63de95b2008-12-10 04:11:2772 DCHECK(proxy_service_);
[email protected]db36938c2009-08-19 21:48:4273 DCHECK(ssl_config_service_.get());
initial.commit586acc5fe2008-07-26 22:42:5274}
75
[email protected]e7f29642009-03-02 22:53:1876HttpNetworkLayer::HttpNetworkLayer(HttpNetworkSession* session)
[email protected]c6894572009-06-16 17:10:1077 : socket_factory_(ClientSocketFactory::GetDefaultFactory()),
[email protected]d13c3272010-02-04 00:24:5178 network_change_notifier_(NULL),
[email protected]db36938c2009-08-19 21:48:4279 ssl_config_service_(NULL),
[email protected]c6894572009-06-16 17:10:1080 session_(session),
[email protected]61a86c42010-04-19 22:45:5381 spdy_session_pool_(session->spdy_session_pool()),
[email protected]fa55e192010-02-15 14:25:5082 http_auth_handler_factory_(NULL),
[email protected]ac039522010-06-15 16:39:4483 network_delegate_(NULL),
[email protected]06650c52010-06-03 00:49:1784 net_log_(NULL),
[email protected]c6894572009-06-16 17:10:1085 suspended_(false) {
[email protected]e7f29642009-03-02 22:53:1886 DCHECK(session_.get());
87}
88
initial.commit586acc5fe2008-07-26 22:42:5289HttpNetworkLayer::~HttpNetworkLayer() {
90}
91
[email protected]1638d602009-09-24 03:49:1792int HttpNetworkLayer::CreateTransaction(scoped_ptr<HttpTransaction>* trans) {
initial.commit586acc5fe2008-07-26 22:42:5293 if (suspended_)
[email protected]1638d602009-09-24 03:49:1794 return ERR_NETWORK_IO_SUSPENDED;
initial.commit586acc5fe2008-07-26 22:42:5295
[email protected]955fc2e72010-02-08 20:37:3096 if (force_spdy_)
97 trans->reset(new SpdyNetworkTransaction(GetSession()));
[email protected]7adaccb2009-10-13 04:43:2198 else
99 trans->reset(new HttpNetworkTransaction(GetSession()));
[email protected]1638d602009-09-24 03:49:17100 return OK;
initial.commit586acc5fe2008-07-26 22:42:52101}
102
103HttpCache* HttpNetworkLayer::GetCache() {
104 return NULL;
105}
106
initial.commit586acc5fe2008-07-26 22:42:52107void HttpNetworkLayer::Suspend(bool suspend) {
108 suspended_ = suspend;
109
[email protected]a3b006eea2008-10-22 21:23:08110 if (suspend && session_)
[email protected]61a86c42010-04-19 22:45:53111 session_->tcp_socket_pool()->CloseIdleSockets();
initial.commit586acc5fe2008-07-26 22:42:52112}
113
[email protected]e7f29642009-03-02 22:53:18114HttpNetworkSession* HttpNetworkLayer::GetSession() {
115 if (!session_) {
116 DCHECK(proxy_service_);
[email protected]b846acd2010-06-07 18:13:10117 SpdySessionPool* spdy_pool = new SpdySessionPool(network_change_notifier_);
[email protected]d1eda932009-11-04 01:03:10118 session_ = new HttpNetworkSession(
[email protected]d13c3272010-02-04 00:24:51119 network_change_notifier_, host_resolver_, proxy_service_,
[email protected]a554a8262010-05-20 00:13:52120 socket_factory_, ssl_config_service_, spdy_pool,
[email protected]06650c52010-06-03 00:49:17121 http_auth_handler_factory_,
[email protected]ac039522010-06-15 16:39:44122 network_delegate_,
[email protected]06650c52010-06-03 00:49:17123 net_log_);
[email protected]80d6524d2009-08-18 03:58:09124 // These were just temps for lazy-initializing HttpNetworkSession.
[email protected]d13c3272010-02-04 00:24:51125 network_change_notifier_ = NULL;
[email protected]80d6524d2009-08-18 03:58:09126 host_resolver_ = NULL;
127 proxy_service_ = NULL;
[email protected]5695b8c2009-09-30 21:36:43128 socket_factory_ = NULL;
[email protected]fa55e192010-02-15 14:25:50129 http_auth_handler_factory_ = NULL;
[email protected]06650c52010-06-03 00:49:17130 net_log_ = NULL;
[email protected]ac039522010-06-15 16:39:44131 network_delegate_ = NULL;
[email protected]e7f29642009-03-02 22:53:18132 }
133 return session_;
134}
135
[email protected]7adaccb2009-10-13 04:43:21136// static
[email protected]955fc2e72010-02-08 20:37:30137void HttpNetworkLayer::EnableSpdy(const std::string& mode) {
[email protected]85c0ed82009-12-15 23:14:14138 static const char kDisableSSL[] = "no-ssl";
139 static const char kDisableCompression[] = "no-compress";
[email protected]65041fa2010-05-21 06:56:53140
141 // We want an A/B experiment between SPDY enabled and SPDY disabled,
142 // but only for pages where SPDY *could have been* negotiated. To do
143 // this, we use NPN, but prevent it from negotiating SPDY. If the
144 // server negotiates HTTP, rather than SPDY, today that will only happen
145 // on servers that installed NPN (and could have done SPDY). But this is
146 // a bit of a hack, as this correlation between NPN and SPDY is not
147 // really guaranteed.
[email protected]1f14a912009-12-21 20:32:44148 static const char kEnableNPN[] = "npn";
[email protected]65041fa2010-05-21 06:56:53149 static const char kEnableNpnHttpOnly[] = "npn-http";
150
151 // Except for the first element, the order is irrelevant. First element
152 // specifies the fallback in case nothing matches
153 // (SSLClientSocket::kNextProtoNoOverlap). Otherwise, the SSL library
154 // will choose the first overlapping protocol in the server's list, since
155 // it presumedly has a better understanding of which protocol we should
156 // use, therefore the rest of the ordering here is not important.
157 static const char kNpnProtosFull[] =
158 "\x08http/1.1\x07http1.1\x06spdy/1\x04spdy";
159 // No spdy specified.
160 static const char kNpnProtosHttpOnly[] = "\x08http/1.1\x07http1.1";
[email protected]650e2cae2009-10-21 23:52:07161
[email protected]955fc2e72010-02-08 20:37:30162 std::vector<std::string> spdy_options;
163 SplitString(mode, ',', &spdy_options);
[email protected]650e2cae2009-10-21 23:52:07164
[email protected]955fc2e72010-02-08 20:37:30165 // Force spdy mode (use SpdyNetworkTransaction for all http requests).
166 force_spdy_ = true;
[email protected]650e2cae2009-10-21 23:52:07167
[email protected]955fc2e72010-02-08 20:37:30168 for (std::vector<std::string>::iterator it = spdy_options.begin();
169 it != spdy_options.end(); ++it) {
[email protected]85c0ed82009-12-15 23:14:14170 const std::string& option = *it;
[email protected]85c0ed82009-12-15 23:14:14171 if (option == kDisableSSL) {
[email protected]65041fa2010-05-21 06:56:53172 SpdySession::SetSSLMode(false); // Disable SSL
[email protected]85c0ed82009-12-15 23:14:14173 } else if (option == kDisableCompression) {
[email protected]955fc2e72010-02-08 20:37:30174 spdy::SpdyFramer::set_enable_compression_default(false);
[email protected]1f14a912009-12-21 20:32:44175 } else if (option == kEnableNPN) {
[email protected]65041fa2010-05-21 06:56:53176 HttpNetworkTransaction::SetUseAlternateProtocols(true);
177 HttpNetworkTransaction::SetNextProtos(kNpnProtosFull);
178 force_spdy_ = false;
179 } else if (option == kEnableNpnHttpOnly) {
180 HttpNetworkTransaction::SetUseAlternateProtocols(true);
181 HttpNetworkTransaction::SetNextProtos(kNpnProtosHttpOnly);
[email protected]955fc2e72010-02-08 20:37:30182 force_spdy_ = false;
183 } else if (option.empty() && it == spdy_options.begin()) {
[email protected]85c0ed82009-12-15 23:14:14184 continue;
185 } else {
[email protected]955fc2e72010-02-08 20:37:30186 LOG(DFATAL) << "Unrecognized spdy option: " << option;
[email protected]85c0ed82009-12-15 23:14:14187 }
188 }
[email protected]7adaccb2009-10-13 04:43:21189}
190
initial.commit586acc5fe2008-07-26 22:42:52191} // namespace net