Make Ctrl + T from apps focus the new tab's omnibox on Linux and ChromeOS.
[chromium-blink-merge.git] / net / http / http_stream_factory_impl.cc
blob1867d6d38b341491923307d9afa62ab95faef108
1 // Copyright (c) 2012 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.
5 #include "net/http/http_stream_factory_impl.h"
7 #include <string>
9 #include "base/logging.h"
10 #include "base/stl_util.h"
11 #include "net/base/net_util.h"
12 #include "net/http/http_network_session.h"
13 #include "net/http/http_server_properties.h"
14 #include "net/http/http_stream_factory_impl_job.h"
15 #include "net/http/http_stream_factory_impl_request.h"
16 #include "net/log/net_log.h"
17 #include "net/spdy/spdy_http_stream.h"
18 #include "url/gurl.h"
20 namespace net {
22 HttpStreamFactoryImpl::HttpStreamFactoryImpl(HttpNetworkSession* session,
23 bool for_websockets)
24 : session_(session),
25 for_websockets_(for_websockets) {}
27 HttpStreamFactoryImpl::~HttpStreamFactoryImpl() {
28 DCHECK(request_map_.empty());
29 DCHECK(spdy_session_request_map_.empty());
31 std::set<const Job*> tmp_job_set;
32 tmp_job_set.swap(orphaned_job_set_);
33 STLDeleteContainerPointers(tmp_job_set.begin(), tmp_job_set.end());
34 DCHECK(orphaned_job_set_.empty());
36 tmp_job_set.clear();
37 tmp_job_set.swap(preconnect_job_set_);
38 STLDeleteContainerPointers(tmp_job_set.begin(), tmp_job_set.end());
39 DCHECK(preconnect_job_set_.empty());
42 HttpStreamRequest* HttpStreamFactoryImpl::RequestStream(
43 const HttpRequestInfo& request_info,
44 RequestPriority priority,
45 const SSLConfig& server_ssl_config,
46 const SSLConfig& proxy_ssl_config,
47 HttpStreamRequest::Delegate* delegate,
48 const BoundNetLog& net_log) {
49 DCHECK(!for_websockets_);
50 return RequestStreamInternal(request_info,
51 priority,
52 server_ssl_config,
53 proxy_ssl_config,
54 delegate,
55 NULL,
56 net_log);
59 HttpStreamRequest* HttpStreamFactoryImpl::RequestWebSocketHandshakeStream(
60 const HttpRequestInfo& request_info,
61 RequestPriority priority,
62 const SSLConfig& server_ssl_config,
63 const SSLConfig& proxy_ssl_config,
64 HttpStreamRequest::Delegate* delegate,
65 WebSocketHandshakeStreamBase::CreateHelper* create_helper,
66 const BoundNetLog& net_log) {
67 DCHECK(for_websockets_);
68 DCHECK(create_helper);
69 return RequestStreamInternal(request_info,
70 priority,
71 server_ssl_config,
72 proxy_ssl_config,
73 delegate,
74 create_helper,
75 net_log);
78 HttpStreamRequest* HttpStreamFactoryImpl::RequestStreamInternal(
79 const HttpRequestInfo& request_info,
80 RequestPriority priority,
81 const SSLConfig& server_ssl_config,
82 const SSLConfig& proxy_ssl_config,
83 HttpStreamRequest::Delegate* delegate,
84 WebSocketHandshakeStreamBase::CreateHelper*
85 websocket_handshake_stream_create_helper,
86 const BoundNetLog& net_log) {
87 Request* request = new Request(request_info.url,
88 this,
89 delegate,
90 websocket_handshake_stream_create_helper,
91 net_log);
92 Job* job = new Job(this, session_, request_info, priority, server_ssl_config,
93 proxy_ssl_config, net_log.net_log());
94 request->AttachJob(job);
96 AlternativeService alternative_service =
97 GetAlternativeServiceFor(request_info.url);
98 if (alternative_service.protocol != UNINITIALIZED_ALTERNATE_PROTOCOL) {
99 // Never share connection with other jobs for FTP requests.
100 DCHECK(!request_info.url.SchemeIs("ftp"));
102 Job* alternate_job =
103 new Job(this, session_, request_info, priority, server_ssl_config,
104 proxy_ssl_config, net_log.net_log());
105 request->AttachJob(alternate_job);
106 alternate_job->MarkAsAlternate(alternative_service);
108 job->WaitFor(alternate_job);
109 // Make sure to wait until we call WaitFor(), before starting
110 // |alternate_job|, otherwise |alternate_job| will not notify |job|
111 // appropriately.
112 alternate_job->Start(request);
115 // Even if |alternate_job| has already finished, it won't have notified the
116 // request yet, since we defer that to the next iteration of the MessageLoop,
117 // so starting |job| is always safe.
118 job->Start(request);
119 return request;
122 void HttpStreamFactoryImpl::PreconnectStreams(
123 int num_streams,
124 const HttpRequestInfo& request_info,
125 RequestPriority priority,
126 const SSLConfig& server_ssl_config,
127 const SSLConfig& proxy_ssl_config) {
128 DCHECK(!for_websockets_);
129 AlternativeService alternative_service =
130 GetAlternativeServiceFor(request_info.url);
131 Job* job = new Job(this, session_, request_info, priority, server_ssl_config,
132 proxy_ssl_config, session_->net_log());
133 if (alternative_service.protocol != UNINITIALIZED_ALTERNATE_PROTOCOL) {
134 job->MarkAsAlternate(alternative_service);
136 preconnect_job_set_.insert(job);
137 job->Preconnect(num_streams);
140 const HostMappingRules* HttpStreamFactoryImpl::GetHostMappingRules() const {
141 return session_->params().host_mapping_rules;
144 AlternativeService HttpStreamFactoryImpl::GetAlternativeServiceFor(
145 const GURL& original_url) {
146 const AlternativeService kNoAlternativeService;
148 if (!session_->params().use_alternate_protocols)
149 return kNoAlternativeService;
151 if (original_url.SchemeIs("ftp"))
152 return kNoAlternativeService;
154 HostPortPair origin = HostPortPair::FromURL(original_url);
155 HttpServerProperties& http_server_properties =
156 *session_->http_server_properties();
157 const AlternativeService alternative_service =
158 http_server_properties.GetAlternativeService(origin);
160 if (alternative_service.protocol == UNINITIALIZED_ALTERNATE_PROTOCOL)
161 return kNoAlternativeService;
162 if (http_server_properties.IsAlternativeServiceBroken(alternative_service)) {
163 HistogramAlternateProtocolUsage(ALTERNATE_PROTOCOL_USAGE_BROKEN);
164 return kNoAlternativeService;
166 if (!IsAlternateProtocolValid(alternative_service.protocol)) {
167 NOTREACHED();
168 return kNoAlternativeService;
171 // Some shared unix systems may have user home directories (like
172 // http://foo.com/~mike) which allow users to emit headers. This is a bad
173 // idea already, but with Alternate-Protocol, it provides the ability for a
174 // single user on a multi-user system to hijack the alternate protocol.
175 // These systems also enforce ports <1024 as restricted ports. So don't
176 // allow protocol upgrades to user-controllable ports.
177 const int kUnrestrictedPort = 1024;
178 if (!session_->params().enable_user_alternate_protocol_ports &&
179 (alternative_service.port >= kUnrestrictedPort &&
180 origin.port() < kUnrestrictedPort))
181 return kNoAlternativeService;
183 origin.set_port(alternative_service.port);
184 if (alternative_service.protocol >= NPN_SPDY_MINIMUM_VERSION &&
185 alternative_service.protocol <= NPN_SPDY_MAXIMUM_VERSION) {
186 if (!HttpStreamFactory::spdy_enabled())
187 return kNoAlternativeService;
189 if (session_->HasSpdyExclusion(origin))
190 return kNoAlternativeService;
192 return alternative_service;
195 DCHECK_EQ(QUIC, alternative_service.protocol);
196 // TODO(bnc): Make sure that certificate requirements are enforced when using
197 // QUIC, then remove the following two lines.
198 if (alternative_service.host != origin.host())
199 return kNoAlternativeService;
200 if (!session_->params().enable_quic)
201 return kNoAlternativeService;
203 if (session_->quic_stream_factory()->IsQuicDisabled(origin.port()))
204 return kNoAlternativeService;
206 return alternative_service;
209 void HttpStreamFactoryImpl::OrphanJob(Job* job, const Request* request) {
210 DCHECK(ContainsKey(request_map_, job));
211 DCHECK_EQ(request_map_[job], request);
212 DCHECK(!ContainsKey(orphaned_job_set_, job));
214 request_map_.erase(job);
216 orphaned_job_set_.insert(job);
217 job->Orphan(request);
220 void HttpStreamFactoryImpl::OnNewSpdySessionReady(
221 const base::WeakPtr<SpdySession>& spdy_session,
222 bool direct,
223 const SSLConfig& used_ssl_config,
224 const ProxyInfo& used_proxy_info,
225 bool was_npn_negotiated,
226 NextProto protocol_negotiated,
227 bool using_spdy,
228 const BoundNetLog& net_log) {
229 while (true) {
230 if (!spdy_session)
231 break;
232 const SpdySessionKey& spdy_session_key = spdy_session->spdy_session_key();
233 // Each iteration may empty out the RequestSet for |spdy_session_key| in
234 // |spdy_session_request_map_|. So each time, check for RequestSet and use
235 // the first one.
237 // TODO(willchan): If it's important, switch RequestSet out for a FIFO
238 // queue (Order by priority first, then FIFO within same priority). Unclear
239 // that it matters here.
240 if (!ContainsKey(spdy_session_request_map_, spdy_session_key))
241 break;
242 Request* request = *spdy_session_request_map_[spdy_session_key].begin();
243 request->Complete(was_npn_negotiated,
244 protocol_negotiated,
245 using_spdy,
246 net_log);
247 if (for_websockets_) {
248 // TODO(ricea): Restore this code path when WebSocket over SPDY
249 // implementation is ready.
250 NOTREACHED();
251 } else {
252 bool use_relative_url = direct || request->url().SchemeIs("https");
253 request->OnStreamReady(
254 NULL,
255 used_ssl_config,
256 used_proxy_info,
257 new SpdyHttpStream(spdy_session, use_relative_url));
260 // TODO(mbelshe): Alert other valid requests.
263 void HttpStreamFactoryImpl::OnOrphanedJobComplete(const Job* job) {
264 orphaned_job_set_.erase(job);
265 delete job;
268 void HttpStreamFactoryImpl::OnPreconnectsComplete(const Job* job) {
269 preconnect_job_set_.erase(job);
270 delete job;
271 OnPreconnectsCompleteInternal();
274 } // namespace net