license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | #include "net/http/http_network_layer.h" |
| 6 | |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 7 | #include "base/logging.h" |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 8 | #include "base/string_util.h" |
[email protected] | 650e2cae | 2009-10-21 23:52:07 | [diff] [blame] | 9 | #include "net/flip/flip_framer.h" |
[email protected] | 7adaccb | 2009-10-13 04:43:21 | [diff] [blame] | 10 | #include "net/flip/flip_network_transaction.h" |
[email protected] | 650e2cae | 2009-10-21 23:52:07 | [diff] [blame] | 11 | #include "net/flip/flip_session.h" |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 12 | #include "net/flip/flip_session_pool.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 13 | #include "net/http/http_network_session.h" |
| 14 | #include "net/http/http_network_transaction.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 15 | #include "net/socket/client_socket_factory.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 16 | |
| 17 | namespace net { |
| 18 | |
| 19 | //----------------------------------------------------------------------------- |
| 20 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 21 | // static |
| 22 | HttpTransactionFactory* HttpNetworkLayer::CreateFactory( |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame^] | 23 | NetworkChangeNotifier* network_change_notifier, |
[email protected] | 8a00f00a | 2009-06-12 00:49:38 | [diff] [blame] | 24 | HostResolver* host_resolver, |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 25 | ProxyService* proxy_service, |
| 26 | SSLConfigService* ssl_config_service) { |
[email protected] | 63de95b | 2008-12-10 04:11:27 | [diff] [blame] | 27 | DCHECK(proxy_service); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 28 | |
[email protected] | c689457 | 2009-06-16 17:10:10 | [diff] [blame] | 29 | return new HttpNetworkLayer(ClientSocketFactory::GetDefaultFactory(), |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame^] | 30 | network_change_notifier, |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 31 | host_resolver, proxy_service, ssl_config_service); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 32 | } |
| 33 | |
[email protected] | e7f2964 | 2009-03-02 22:53:18 | [diff] [blame] | 34 | // static |
| 35 | HttpTransactionFactory* HttpNetworkLayer::CreateFactory( |
| 36 | HttpNetworkSession* session) { |
| 37 | DCHECK(session); |
| 38 | |
| 39 | return new HttpNetworkLayer(session); |
| 40 | } |
| 41 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 42 | //----------------------------------------------------------------------------- |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 43 | bool HttpNetworkLayer::force_flip_ = false; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 44 | |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame^] | 45 | HttpNetworkLayer::HttpNetworkLayer( |
| 46 | ClientSocketFactory* socket_factory, |
| 47 | NetworkChangeNotifier* network_change_notifier, |
| 48 | HostResolver* host_resolver, |
| 49 | ProxyService* proxy_service, |
| 50 | SSLConfigService* ssl_config_service) |
[email protected] | c689457 | 2009-06-16 17:10:10 | [diff] [blame] | 51 | : socket_factory_(socket_factory), |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame^] | 52 | network_change_notifier_(network_change_notifier), |
[email protected] | c689457 | 2009-06-16 17:10:10 | [diff] [blame] | 53 | host_resolver_(host_resolver), |
[email protected] | 8a00f00a | 2009-06-12 00:49:38 | [diff] [blame] | 54 | proxy_service_(proxy_service), |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 55 | ssl_config_service_(ssl_config_service), |
[email protected] | 8a00f00a | 2009-06-12 00:49:38 | [diff] [blame] | 56 | session_(NULL), |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 57 | flip_session_pool_(NULL), |
[email protected] | 8a00f00a | 2009-06-12 00:49:38 | [diff] [blame] | 58 | suspended_(false) { |
[email protected] | 63de95b | 2008-12-10 04:11:27 | [diff] [blame] | 59 | DCHECK(proxy_service_); |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 60 | DCHECK(ssl_config_service_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 61 | } |
| 62 | |
[email protected] | e7f2964 | 2009-03-02 22:53:18 | [diff] [blame] | 63 | HttpNetworkLayer::HttpNetworkLayer(HttpNetworkSession* session) |
[email protected] | c689457 | 2009-06-16 17:10:10 | [diff] [blame] | 64 | : socket_factory_(ClientSocketFactory::GetDefaultFactory()), |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame^] | 65 | network_change_notifier_(NULL), |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 66 | ssl_config_service_(NULL), |
[email protected] | c689457 | 2009-06-16 17:10:10 | [diff] [blame] | 67 | session_(session), |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 68 | flip_session_pool_(session->flip_session_pool()), |
[email protected] | c689457 | 2009-06-16 17:10:10 | [diff] [blame] | 69 | suspended_(false) { |
[email protected] | e7f2964 | 2009-03-02 22:53:18 | [diff] [blame] | 70 | DCHECK(session_.get()); |
| 71 | } |
| 72 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 73 | HttpNetworkLayer::~HttpNetworkLayer() { |
| 74 | } |
| 75 | |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 76 | int HttpNetworkLayer::CreateTransaction(scoped_ptr<HttpTransaction>* trans) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 77 | if (suspended_) |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 78 | return ERR_NETWORK_IO_SUSPENDED; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 79 | |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 80 | if (force_flip_) |
[email protected] | 7adaccb | 2009-10-13 04:43:21 | [diff] [blame] | 81 | trans->reset(new FlipNetworkTransaction(GetSession())); |
| 82 | else |
| 83 | trans->reset(new HttpNetworkTransaction(GetSession())); |
[email protected] | 1638d60 | 2009-09-24 03:49:17 | [diff] [blame] | 84 | return OK; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | HttpCache* HttpNetworkLayer::GetCache() { |
| 88 | return NULL; |
| 89 | } |
| 90 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 91 | void HttpNetworkLayer::Suspend(bool suspend) { |
| 92 | suspended_ = suspend; |
| 93 | |
[email protected] | a3b006eea | 2008-10-22 21:23:08 | [diff] [blame] | 94 | if (suspend && session_) |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 95 | session_->tcp_socket_pool()->CloseIdleSockets(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | e7f2964 | 2009-03-02 22:53:18 | [diff] [blame] | 98 | HttpNetworkSession* HttpNetworkLayer::GetSession() { |
| 99 | if (!session_) { |
| 100 | DCHECK(proxy_service_); |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 101 | FlipSessionPool* flip_pool = new FlipSessionPool; |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 102 | session_ = new HttpNetworkSession( |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame^] | 103 | network_change_notifier_, host_resolver_, proxy_service_, |
| 104 | socket_factory_, ssl_config_service_, flip_pool); |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 105 | // These were just temps for lazy-initializing HttpNetworkSession. |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame^] | 106 | network_change_notifier_ = NULL; |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 107 | host_resolver_ = NULL; |
| 108 | proxy_service_ = NULL; |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 109 | socket_factory_ = NULL; |
[email protected] | e7f2964 | 2009-03-02 22:53:18 | [diff] [blame] | 110 | } |
| 111 | return session_; |
| 112 | } |
| 113 | |
[email protected] | 7adaccb | 2009-10-13 04:43:21 | [diff] [blame] | 114 | // static |
[email protected] | 650e2cae | 2009-10-21 23:52:07 | [diff] [blame] | 115 | void HttpNetworkLayer::EnableFlip(const std::string& mode) { |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 116 | static const char kDisableSSL[] = "no-ssl"; |
| 117 | static const char kDisableCompression[] = "no-compress"; |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 118 | static const char kEnableNPN[] = "npn"; |
[email protected] | 650e2cae | 2009-10-21 23:52:07 | [diff] [blame] | 119 | |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 120 | std::vector<std::string> flip_options; |
| 121 | SplitString(mode, ',', &flip_options); |
[email protected] | 650e2cae | 2009-10-21 23:52:07 | [diff] [blame] | 122 | |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 123 | // Force flip mode (use FlipNetworkTransaction for all http requests). |
| 124 | force_flip_ = true; |
[email protected] | 650e2cae | 2009-10-21 23:52:07 | [diff] [blame] | 125 | |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 126 | for (std::vector<std::string>::iterator it = flip_options.begin(); |
| 127 | it != flip_options.end(); ++it) { |
| 128 | const std::string& option = *it; |
| 129 | |
| 130 | // Disable SSL |
| 131 | if (option == kDisableSSL) { |
| 132 | FlipSession::SetSSLMode(false); |
| 133 | } else if (option == kDisableCompression) { |
| 134 | flip::FlipFramer::set_enable_compression_default(false); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 135 | } else if (option == kEnableNPN) { |
| 136 | HttpNetworkTransaction::SetNextProtos("\007http1.1\004spdy"); |
| 137 | force_flip_ = false; |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 138 | } else if (option.empty() && it == flip_options.begin()) { |
| 139 | continue; |
| 140 | } else { |
| 141 | LOG(DFATAL) << "Unrecognized flip option: " << option; |
| 142 | } |
| 143 | } |
[email protected] | 7adaccb | 2009-10-13 04:43:21 | [diff] [blame] | 144 | } |
| 145 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 146 | } // namespace net |