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"
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"
22 HttpStreamFactoryImpl::HttpStreamFactoryImpl(HttpNetworkSession
* 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());
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
,
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
,
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
,
90 websocket_handshake_stream_create_helper
,
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 const AlternativeServiceVector alternative_service_vector
=
97 GetAlternativeServicesFor(request_info
.url
);
98 if (!alternative_service_vector
.empty()) {
99 // TODO(bnc): Pass on multiple alternative services to Job.
100 const AlternativeService
& alternative_service
=
101 alternative_service_vector
[0];
102 // Never share connection with other jobs for FTP requests.
103 DCHECK(!request_info
.url
.SchemeIs("ftp"));
105 Job
* alternative_job
=
106 new Job(this, session_
, request_info
, priority
, server_ssl_config
,
107 proxy_ssl_config
, alternative_service
, net_log
.net_log());
108 request
->AttachJob(alternative_job
);
110 job
->WaitFor(alternative_job
);
111 // Make sure to wait until we call WaitFor(), before starting
112 // |alternative_job|, otherwise |alternative_job| will not notify |job|
114 alternative_job
->Start(request
);
117 // Even if |alternative_job| has already finished, it will not have notified
118 // the request yet, since we defer that to the next iteration of the
119 // MessageLoop, so starting |job| is always safe.
124 void HttpStreamFactoryImpl::PreconnectStreams(
126 const HttpRequestInfo
& request_info
,
127 const SSLConfig
& server_ssl_config
,
128 const SSLConfig
& proxy_ssl_config
) {
129 DCHECK(!for_websockets_
);
130 AlternativeService alternative_service
;
131 AlternativeServiceVector alternative_service_vector
=
132 GetAlternativeServicesFor(request_info
.url
);
133 if (!alternative_service_vector
.empty()) {
134 // TODO(bnc): Pass on multiple alternative services to Job.
135 alternative_service
= alternative_service_vector
[0];
138 // Due to how the socket pools handle priorities and idle sockets, only IDLE
139 // priority currently makes sense for preconnects. The priority for
140 // preconnects is currently ignored (see RequestSocketsForPool()), but could
141 // be used at some point for proxy resolution or something.
143 new Job(this, session_
, request_info
, IDLE
, server_ssl_config
,
144 proxy_ssl_config
, alternative_service
, session_
->net_log());
145 preconnect_job_set_
.insert(job
);
146 job
->Preconnect(num_streams
);
149 const HostMappingRules
* HttpStreamFactoryImpl::GetHostMappingRules() const {
150 return session_
->params().host_mapping_rules
;
153 AlternativeServiceVector
HttpStreamFactoryImpl::GetAlternativeServicesFor(
154 const GURL
& original_url
) {
155 if (original_url
.SchemeIs("ftp"))
156 return AlternativeServiceVector();
158 HostPortPair origin
= HostPortPair::FromURL(original_url
);
159 HttpServerProperties
& http_server_properties
=
160 *session_
->http_server_properties();
161 const AlternativeServiceVector alternative_service_vector
=
162 http_server_properties
.GetAlternativeServices(origin
);
163 if (alternative_service_vector
.empty())
164 return AlternativeServiceVector();
166 const bool enable_different_host
=
167 session_
->params().use_alternative_services
;
169 AlternativeServiceVector enabled_alternative_service_vector
;
170 for (const AlternativeService
& alternative_service
:
171 alternative_service_vector
) {
172 DCHECK(IsAlternateProtocolValid(alternative_service
.protocol
));
173 if (http_server_properties
.IsAlternativeServiceBroken(
174 alternative_service
)) {
175 HistogramAlternateProtocolUsage(ALTERNATE_PROTOCOL_USAGE_BROKEN
);
179 if (origin
.host() != alternative_service
.host
&& !enable_different_host
)
182 // Some shared unix systems may have user home directories (like
183 // http://foo.com/~mike) which allow users to emit headers. This is a bad
184 // idea already, but with Alternate-Protocol, it provides the ability for a
185 // single user on a multi-user system to hijack the alternate protocol.
186 // These systems also enforce ports <1024 as restricted ports. So don't
187 // allow protocol upgrades to user-controllable ports.
188 const int kUnrestrictedPort
= 1024;
189 if (!session_
->params().enable_user_alternate_protocol_ports
&&
190 (alternative_service
.port
>= kUnrestrictedPort
&&
191 origin
.port() < kUnrestrictedPort
))
194 origin
.set_port(alternative_service
.port
);
195 if (alternative_service
.protocol
>= NPN_SPDY_MINIMUM_VERSION
&&
196 alternative_service
.protocol
<= NPN_SPDY_MAXIMUM_VERSION
) {
197 if (!HttpStreamFactory::spdy_enabled())
200 if (session_
->HasSpdyExclusion(origin
))
203 enabled_alternative_service_vector
.push_back(alternative_service
);
207 DCHECK_EQ(QUIC
, alternative_service
.protocol
);
208 if (!session_
->params().enable_quic
)
211 if (session_
->quic_stream_factory()->IsQuicDisabled(origin
.port()))
214 if (!session_
->params().enable_insecure_quic
&&
215 !original_url
.SchemeIs("https")) {
219 enabled_alternative_service_vector
.push_back(alternative_service
);
221 return enabled_alternative_service_vector
;
224 void HttpStreamFactoryImpl::OrphanJob(Job
* job
, const Request
* request
) {
225 DCHECK(ContainsKey(request_map_
, job
));
226 DCHECK_EQ(request_map_
[job
], request
);
227 DCHECK(!ContainsKey(orphaned_job_set_
, job
));
229 request_map_
.erase(job
);
231 orphaned_job_set_
.insert(job
);
232 job
->Orphan(request
);
235 void HttpStreamFactoryImpl::OnNewSpdySessionReady(
236 const base::WeakPtr
<SpdySession
>& spdy_session
,
238 const SSLConfig
& used_ssl_config
,
239 const ProxyInfo
& used_proxy_info
,
240 bool was_npn_negotiated
,
241 NextProto protocol_negotiated
,
243 const BoundNetLog
& net_log
) {
247 const SpdySessionKey
& spdy_session_key
= spdy_session
->spdy_session_key();
248 // Each iteration may empty out the RequestSet for |spdy_session_key| in
249 // |spdy_session_request_map_|. So each time, check for RequestSet and use
252 // TODO(willchan): If it's important, switch RequestSet out for a FIFO
253 // queue (Order by priority first, then FIFO within same priority). Unclear
254 // that it matters here.
255 if (!ContainsKey(spdy_session_request_map_
, spdy_session_key
))
257 Request
* request
= *spdy_session_request_map_
[spdy_session_key
].begin();
258 request
->Complete(was_npn_negotiated
, protocol_negotiated
, using_spdy
);
259 if (for_websockets_
) {
260 // TODO(ricea): Restore this code path when WebSocket over SPDY
261 // implementation is ready.
264 bool use_relative_url
= direct
|| request
->url().SchemeIs("https");
265 request
->OnStreamReady(
269 new SpdyHttpStream(spdy_session
, use_relative_url
));
272 // TODO(mbelshe): Alert other valid requests.
275 void HttpStreamFactoryImpl::OnOrphanedJobComplete(const Job
* job
) {
276 orphaned_job_set_
.erase(job
);
280 void HttpStreamFactoryImpl::OnPreconnectsComplete(const Job
* job
) {
281 preconnect_job_set_
.erase(job
);
283 OnPreconnectsCompleteInternal();