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.
4
5 #include "net/http/http_network_layer.h"
6
7 #include "base/logging.h"
8 #include "base/string_util.h"
9 #include "net/flip/flip_framer.h"
10 #include "net/flip/flip_network_transaction.h"
11 #include "net/flip/flip_session.h"
12 #include "net/flip/flip_session_pool.h"
13 #include "net/http/http_network_session.h"
14 #include "net/http/http_network_transaction.h"
15 #include "net/socket/client_socket_factory.h"
16
17 namespace net {
18
19 //-----------------------------------------------------------------------------
20
21 // static
CreateFactory(NetworkChangeNotifier * network_change_notifier,HostResolver * host_resolver,ProxyService * proxy_service,SSLConfigService * ssl_config_service)22 HttpTransactionFactory* HttpNetworkLayer::CreateFactory(
23 NetworkChangeNotifier* network_change_notifier,
24 HostResolver* host_resolver,
25 ProxyService* proxy_service,
26 SSLConfigService* ssl_config_service) {
27 DCHECK(proxy_service);
28
29 return new HttpNetworkLayer(ClientSocketFactory::GetDefaultFactory(),
30 network_change_notifier,
31 host_resolver, proxy_service, ssl_config_service);
32 }
33
34 // static
CreateFactory(HttpNetworkSession * session)35 HttpTransactionFactory* HttpNetworkLayer::CreateFactory(
36 HttpNetworkSession* session) {
37 DCHECK(session);
38
39 return new HttpNetworkLayer(session);
40 }
41
42 //-----------------------------------------------------------------------------
43 bool HttpNetworkLayer::force_flip_ = false;
44
HttpNetworkLayer(ClientSocketFactory * socket_factory,NetworkChangeNotifier * network_change_notifier,HostResolver * host_resolver,ProxyService * proxy_service,SSLConfigService * ssl_config_service)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)
51 : socket_factory_(socket_factory),
52 network_change_notifier_(network_change_notifier),
53 host_resolver_(host_resolver),
54 proxy_service_(proxy_service),
55 ssl_config_service_(ssl_config_service),
56 session_(NULL),
57 flip_session_pool_(NULL),
58 suspended_(false) {
59 DCHECK(proxy_service_);
60 DCHECK(ssl_config_service_.get());
61 }
62
HttpNetworkLayer(HttpNetworkSession * session)63 HttpNetworkLayer::HttpNetworkLayer(HttpNetworkSession* session)
64 : socket_factory_(ClientSocketFactory::GetDefaultFactory()),
65 network_change_notifier_(NULL),
66 ssl_config_service_(NULL),
67 session_(session),
68 flip_session_pool_(session->flip_session_pool()),
69 suspended_(false) {
70 DCHECK(session_.get());
71 }
72
~HttpNetworkLayer()73 HttpNetworkLayer::~HttpNetworkLayer() {
74 }
75
CreateTransaction(scoped_ptr<HttpTransaction> * trans)76 int HttpNetworkLayer::CreateTransaction(scoped_ptr<HttpTransaction>* trans) {
77 if (suspended_)
78 return ERR_NETWORK_IO_SUSPENDED;
79
80 if (force_flip_)
81 trans->reset(new FlipNetworkTransaction(GetSession()));
82 else
83 trans->reset(new HttpNetworkTransaction(GetSession()));
84 return OK;
85 }
86
GetCache()87 HttpCache* HttpNetworkLayer::GetCache() {
88 return NULL;
89 }
90
Suspend(bool suspend)91 void HttpNetworkLayer::Suspend(bool suspend) {
92 suspended_ = suspend;
93
94 if (suspend && session_)
95 session_->tcp_socket_pool()->CloseIdleSockets();
96 }
97
GetSession()98 HttpNetworkSession* HttpNetworkLayer::GetSession() {
99 if (!session_) {
100 DCHECK(proxy_service_);
101 FlipSessionPool* flip_pool = new FlipSessionPool;
102 session_ = new HttpNetworkSession(
103 network_change_notifier_, host_resolver_, proxy_service_,
104 socket_factory_, ssl_config_service_, flip_pool);
105 // These were just temps for lazy-initializing HttpNetworkSession.
106 network_change_notifier_ = NULL;
107 host_resolver_ = NULL;
108 proxy_service_ = NULL;
109 socket_factory_ = NULL;
110 }
111 return session_;
112 }
113
114 // static
EnableFlip(const std::string & mode)115 void HttpNetworkLayer::EnableFlip(const std::string& mode) {
116 static const char kDisableSSL[] = "no-ssl";
117 static const char kDisableCompression[] = "no-compress";
118 static const char kEnableNPN[] = "npn";
119
120 std::vector<std::string> flip_options;
121 SplitString(mode, ',', &flip_options);
122
123 // Force flip mode (use FlipNetworkTransaction for all http requests).
124 force_flip_ = true;
125
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);
135 } else if (option == kEnableNPN) {
136 HttpNetworkTransaction::SetNextProtos("\007http1.1\004spdy");
137 force_flip_ = false;
138 } else if (option.empty() && it == flip_options.begin()) {
139 continue;
140 } else {
141 LOG(DFATAL) << "Unrecognized flip option: " << option;
142 }
143 }
144 }
145
146 } // namespace net
147