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 (!session_
->params().use_alternate_protocols
)
156 return AlternativeServiceVector();
158 if (original_url
.SchemeIs("ftp"))
159 return AlternativeServiceVector();
161 HostPortPair origin
= HostPortPair::FromURL(original_url
);
162 HttpServerProperties
& http_server_properties
=
163 *session_
->http_server_properties();
164 const AlternativeServiceVector alternative_service_vector
=
165 http_server_properties
.GetAlternativeServices(origin
);
166 if (alternative_service_vector
.empty())
167 return AlternativeServiceVector();
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 // Some shared unix systems may have user home directories (like
180 // http://foo.com/~mike) which allow users to emit headers. This is a bad
181 // idea already, but with Alternate-Protocol, it provides the ability for a
182 // single user on a multi-user system to hijack the alternate protocol.
183 // These systems also enforce ports <1024 as restricted ports. So don't
184 // allow protocol upgrades to user-controllable ports.
185 const int kUnrestrictedPort
= 1024;
186 if (!session_
->params().enable_user_alternate_protocol_ports
&&
187 (alternative_service
.port
>= kUnrestrictedPort
&&
188 origin
.port() < kUnrestrictedPort
))
191 origin
.set_port(alternative_service
.port
);
192 if (alternative_service
.protocol
>= NPN_SPDY_MINIMUM_VERSION
&&
193 alternative_service
.protocol
<= NPN_SPDY_MAXIMUM_VERSION
) {
194 if (!HttpStreamFactory::spdy_enabled())
197 if (session_
->HasSpdyExclusion(origin
))
200 enabled_alternative_service_vector
.push_back(alternative_service
);
204 DCHECK_EQ(QUIC
, alternative_service
.protocol
);
205 if (!session_
->params().enable_quic
)
208 if (session_
->quic_stream_factory()->IsQuicDisabled(origin
.port()))
211 if (!session_
->params().enable_insecure_quic
&&
212 !original_url
.SchemeIs("https")) {
216 enabled_alternative_service_vector
.push_back(alternative_service
);
218 return enabled_alternative_service_vector
;
221 void HttpStreamFactoryImpl::OrphanJob(Job
* job
, const Request
* request
) {
222 DCHECK(ContainsKey(request_map_
, job
));
223 DCHECK_EQ(request_map_
[job
], request
);
224 DCHECK(!ContainsKey(orphaned_job_set_
, job
));
226 request_map_
.erase(job
);
228 orphaned_job_set_
.insert(job
);
229 job
->Orphan(request
);
232 void HttpStreamFactoryImpl::OnNewSpdySessionReady(
233 const base::WeakPtr
<SpdySession
>& spdy_session
,
235 const SSLConfig
& used_ssl_config
,
236 const ProxyInfo
& used_proxy_info
,
237 bool was_npn_negotiated
,
238 NextProto protocol_negotiated
,
240 const BoundNetLog
& net_log
) {
244 const SpdySessionKey
& spdy_session_key
= spdy_session
->spdy_session_key();
245 // Each iteration may empty out the RequestSet for |spdy_session_key| in
246 // |spdy_session_request_map_|. So each time, check for RequestSet and use
249 // TODO(willchan): If it's important, switch RequestSet out for a FIFO
250 // queue (Order by priority first, then FIFO within same priority). Unclear
251 // that it matters here.
252 if (!ContainsKey(spdy_session_request_map_
, spdy_session_key
))
254 Request
* request
= *spdy_session_request_map_
[spdy_session_key
].begin();
255 request
->Complete(was_npn_negotiated
, protocol_negotiated
, using_spdy
);
256 if (for_websockets_
) {
257 // TODO(ricea): Restore this code path when WebSocket over SPDY
258 // implementation is ready.
261 bool use_relative_url
= direct
|| request
->url().SchemeIs("https");
262 request
->OnStreamReady(
266 new SpdyHttpStream(spdy_session
, use_relative_url
));
269 // TODO(mbelshe): Alert other valid requests.
272 void HttpStreamFactoryImpl::OnOrphanedJobComplete(const Job
* job
) {
273 orphaned_job_set_
.erase(job
);
277 void HttpStreamFactoryImpl::OnPreconnectsComplete(const Job
* job
) {
278 preconnect_job_set_
.erase(job
);
280 OnPreconnectsCompleteInternal();