[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | #include "net/http/http_network_layer.h" |
| 6 | |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7 | #include "base/field_trial.h" |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 8 | #include "base/logging.h" |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 9 | #include "base/string_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 10 | #include "net/http/http_network_session.h" |
| 11 | #include "net/http/http_network_transaction.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 12 | #include "net/socket/client_socket_factory.h" |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 13 | #include "net/spdy/spdy_framer.h" |
| 14 | #include "net/spdy/spdy_network_transaction.h" |
| 15 | #include "net/spdy/spdy_session.h" |
[email protected] | 61a86c4 | 2010-04-19 22:45:53 | [diff] [blame] | 16 | #include "net/spdy/spdy_session_pool.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 17 | |
| 18 | namespace net { |
| 19 | |
| 20 | //----------------------------------------------------------------------------- |
| 21 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 22 | // static |
| 23 | HttpTransactionFactory* HttpNetworkLayer::CreateFactory( |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 24 | NetworkChangeNotifier* network_change_notifier, |
[email protected] | 8a00f00a | 2009-06-12 00:49:38 | [diff] [blame] | 25 | HostResolver* host_resolver, |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 26 | ProxyService* proxy_service, |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 27 | SSLConfigService* ssl_config_service, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 28 | HttpAuthHandlerFactory* http_auth_handler_factory, |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame^] | 29 | HttpNetworkDelegate* network_delegate, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 30 | NetLog* net_log) { |
[email protected] | 63de95b | 2008-12-10 04:11:27 | [diff] [blame] | 31 | DCHECK(proxy_service); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 32 | |
[email protected] | c689457 | 2009-06-16 17:10:10 | [diff] [blame] | 33 | return new HttpNetworkLayer(ClientSocketFactory::GetDefaultFactory(), |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 34 | network_change_notifier, |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 35 | host_resolver, proxy_service, ssl_config_service, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 36 | http_auth_handler_factory, |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame^] | 37 | network_delegate, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 38 | net_log); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 39 | } |
| 40 | |
[email protected] | e7f2964 | 2009-03-02 22:53:18 | [diff] [blame] | 41 | // static |
| 42 | HttpTransactionFactory* HttpNetworkLayer::CreateFactory( |
| 43 | HttpNetworkSession* session) { |
| 44 | DCHECK(session); |
| 45 | |
| 46 | return new HttpNetworkLayer(session); |
| 47 | } |
| 48 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 49 | //----------------------------------------------------------------------------- |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 50 | bool HttpNetworkLayer::force_spdy_ = false; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 51 | |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 52 | HttpNetworkLayer::HttpNetworkLayer( |
| 53 | ClientSocketFactory* socket_factory, |
| 54 | NetworkChangeNotifier* network_change_notifier, |
| 55 | HostResolver* host_resolver, |
| 56 | ProxyService* proxy_service, |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 57 | SSLConfigService* ssl_config_service, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 58 | HttpAuthHandlerFactory* http_auth_handler_factory, |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame^] | 59 | HttpNetworkDelegate* network_delegate, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 60 | NetLog* net_log) |
[email protected] | c689457 | 2009-06-16 17:10:10 | [diff] [blame] | 61 | : socket_factory_(socket_factory), |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 62 | network_change_notifier_(network_change_notifier), |
[email protected] | c689457 | 2009-06-16 17:10:10 | [diff] [blame] | 63 | host_resolver_(host_resolver), |
[email protected] | 8a00f00a | 2009-06-12 00:49:38 | [diff] [blame] | 64 | proxy_service_(proxy_service), |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 65 | ssl_config_service_(ssl_config_service), |
[email protected] | 8a00f00a | 2009-06-12 00:49:38 | [diff] [blame] | 66 | session_(NULL), |
[email protected] | 61a86c4 | 2010-04-19 22:45:53 | [diff] [blame] | 67 | spdy_session_pool_(NULL), |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 68 | http_auth_handler_factory_(http_auth_handler_factory), |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame^] | 69 | network_delegate_(network_delegate), |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 70 | net_log_(net_log), |
[email protected] | 8a00f00a | 2009-06-12 00:49:38 | [diff] [blame] | 71 | suspended_(false) { |
[email protected] | 63de95b | 2008-12-10 04:11:27 | [diff] [blame] | 72 | DCHECK(proxy_service_); |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 73 | DCHECK(ssl_config_service_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 74 | } |
| 75 | |
[email protected] | e7f2964 | 2009-03-02 22:53:18 | [diff] [blame] | 76 | HttpNetworkLayer::HttpNetworkLayer(HttpNetworkSession* session) |
[email protected] | c689457 | 2009-06-16 17:10:10 | [diff] [blame] | 77 | : socket_factory_(ClientSocketFactory::GetDefaultFactory()), |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 78 | network_change_notifier_(NULL), |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 79 | ssl_config_service_(NULL), |
[email protected] | c689457 | 2009-06-16 17:10:10 | [diff] [blame] | 80 | session_(session), |
[email protected] | 61a86c4 | 2010-04-19 22:45:53 | [diff] [blame] | 81 | spdy_session_pool_(session->spdy_session_pool()), |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 82 | http_auth_handler_factory_(NULL), |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame^] | 83 | network_delegate_(NULL), |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 84 | net_log_(NULL), |
[email protected] | c689457 | 2009-06-16 17:10:10 | [diff] [blame] | 85 | suspended_(false) { |
[email protected] | e7f2964 | 2009-03-02 22:53:18 | [diff] [blame] | 86 | DCHECK(session_.get()); |
| 87 | } |
| 88 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 89 | HttpNetworkLayer::~HttpNetworkLayer() { |
| 90 | } |
| 91 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 92 | int HttpNetworkLayer::CreateTransaction(scoped_ptr<HttpTransaction>* trans) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 93 | if (suspended_) |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 94 | return ERR_NETWORK_IO_SUSPENDED; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 95 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 96 | if (force_spdy_) |
| 97 | trans->reset(new SpdyNetworkTransaction(GetSession())); |
[email protected] | 7adaccb | 2009-10-13 04:43:21 | [diff] [blame] | 98 | else |
| 99 | trans->reset(new HttpNetworkTransaction(GetSession())); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 100 | return OK; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | HttpCache* HttpNetworkLayer::GetCache() { |
| 104 | return NULL; |
| 105 | } |
| 106 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 107 | void HttpNetworkLayer::Suspend(bool suspend) { |
| 108 | suspended_ = suspend; |
| 109 | |
[email protected] | a3b006eea | 2008-10-22 21:23:08 | [diff] [blame] | 110 | if (suspend && session_) |
[email protected] | 61a86c4 | 2010-04-19 22:45:53 | [diff] [blame] | 111 | session_->tcp_socket_pool()->CloseIdleSockets(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 112 | } |
| 113 | |
[email protected] | e7f2964 | 2009-03-02 22:53:18 | [diff] [blame] | 114 | HttpNetworkSession* HttpNetworkLayer::GetSession() { |
| 115 | if (!session_) { |
| 116 | DCHECK(proxy_service_); |
[email protected] | b846acd | 2010-06-07 18:13:10 | [diff] [blame] | 117 | SpdySessionPool* spdy_pool = new SpdySessionPool(network_change_notifier_); |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 118 | session_ = new HttpNetworkSession( |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 119 | network_change_notifier_, host_resolver_, proxy_service_, |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 120 | socket_factory_, ssl_config_service_, spdy_pool, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 121 | http_auth_handler_factory_, |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame^] | 122 | network_delegate_, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 123 | net_log_); |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 124 | // These were just temps for lazy-initializing HttpNetworkSession. |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 125 | network_change_notifier_ = NULL; |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 126 | host_resolver_ = NULL; |
| 127 | proxy_service_ = NULL; |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 128 | socket_factory_ = NULL; |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 129 | http_auth_handler_factory_ = NULL; |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 130 | net_log_ = NULL; |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame^] | 131 | network_delegate_ = NULL; |
[email protected] | e7f2964 | 2009-03-02 22:53:18 | [diff] [blame] | 132 | } |
| 133 | return session_; |
| 134 | } |
| 135 | |
[email protected] | 7adaccb | 2009-10-13 04:43:21 | [diff] [blame] | 136 | // static |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 137 | void HttpNetworkLayer::EnableSpdy(const std::string& mode) { |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 138 | static const char kDisableSSL[] = "no-ssl"; |
| 139 | static const char kDisableCompression[] = "no-compress"; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 140 | |
| 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] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 148 | static const char kEnableNPN[] = "npn"; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 149 | 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] | 650e2cae | 2009-10-21 23:52:07 | [diff] [blame] | 161 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 162 | std::vector<std::string> spdy_options; |
| 163 | SplitString(mode, ',', &spdy_options); |
[email protected] | 650e2cae | 2009-10-21 23:52:07 | [diff] [blame] | 164 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 165 | // Force spdy mode (use SpdyNetworkTransaction for all http requests). |
| 166 | force_spdy_ = true; |
[email protected] | 650e2cae | 2009-10-21 23:52:07 | [diff] [blame] | 167 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 168 | for (std::vector<std::string>::iterator it = spdy_options.begin(); |
| 169 | it != spdy_options.end(); ++it) { |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 170 | const std::string& option = *it; |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 171 | if (option == kDisableSSL) { |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 172 | SpdySession::SetSSLMode(false); // Disable SSL |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 173 | } else if (option == kDisableCompression) { |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 174 | spdy::SpdyFramer::set_enable_compression_default(false); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 175 | } else if (option == kEnableNPN) { |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 176 | 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] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 182 | force_spdy_ = false; |
| 183 | } else if (option.empty() && it == spdy_options.begin()) { |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 184 | continue; |
| 185 | } else { |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 186 | LOG(DFATAL) << "Unrecognized spdy option: " << option; |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 187 | } |
| 188 | } |
[email protected] | 7adaccb | 2009-10-13 04:43:21 | [diff] [blame] | 189 | } |
| 190 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 191 | } // namespace net |