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 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"));
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|
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.
122 void HttpStreamFactoryImpl::PreconnectStreams(
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 // TODO(bnc): Make sure that callers connect to the specified host, and that
163 // certificate requirements are enforced. Then remove the following two
165 if (alternative_service
.host
!= origin
.host())
166 return kNoAlternativeService
;
167 if (http_server_properties
.IsAlternativeServiceBroken(alternative_service
)) {
168 HistogramAlternateProtocolUsage(ALTERNATE_PROTOCOL_USAGE_BROKEN
);
169 return kNoAlternativeService
;
171 if (!IsAlternateProtocolValid(alternative_service
.protocol
)) {
173 return kNoAlternativeService
;
176 // Some shared unix systems may have user home directories (like
177 // http://foo.com/~mike) which allow users to emit headers. This is a bad
178 // idea already, but with Alternate-Protocol, it provides the ability for a
179 // single user on a multi-user system to hijack the alternate protocol.
180 // These systems also enforce ports <1024 as restricted ports. So don't
181 // allow protocol upgrades to user-controllable ports.
182 const int kUnrestrictedPort
= 1024;
183 if (!session_
->params().enable_user_alternate_protocol_ports
&&
184 (alternative_service
.port
>= kUnrestrictedPort
&&
185 origin
.port() < kUnrestrictedPort
))
186 return kNoAlternativeService
;
188 origin
.set_port(alternative_service
.port
);
189 if (alternative_service
.protocol
>= NPN_SPDY_MINIMUM_VERSION
&&
190 alternative_service
.protocol
<= NPN_SPDY_MAXIMUM_VERSION
) {
191 if (!HttpStreamFactory::spdy_enabled())
192 return kNoAlternativeService
;
194 if (session_
->HasSpdyExclusion(origin
))
195 return kNoAlternativeService
;
197 return alternative_service
;
200 DCHECK_EQ(QUIC
, alternative_service
.protocol
);
201 if (!session_
->params().enable_quic
)
202 return kNoAlternativeService
;
204 if (session_
->quic_stream_factory()->IsQuicDisabled(origin
.port()))
205 return kNoAlternativeService
;
207 return alternative_service
;
210 void HttpStreamFactoryImpl::OrphanJob(Job
* job
, const Request
* request
) {
211 DCHECK(ContainsKey(request_map_
, job
));
212 DCHECK_EQ(request_map_
[job
], request
);
213 DCHECK(!ContainsKey(orphaned_job_set_
, job
));
215 request_map_
.erase(job
);
217 orphaned_job_set_
.insert(job
);
218 job
->Orphan(request
);
221 void HttpStreamFactoryImpl::OnNewSpdySessionReady(
222 const base::WeakPtr
<SpdySession
>& spdy_session
,
224 const SSLConfig
& used_ssl_config
,
225 const ProxyInfo
& used_proxy_info
,
226 bool was_npn_negotiated
,
227 NextProto protocol_negotiated
,
229 const BoundNetLog
& net_log
) {
233 const SpdySessionKey
& spdy_session_key
= spdy_session
->spdy_session_key();
234 // Each iteration may empty out the RequestSet for |spdy_session_key| in
235 // |spdy_session_request_map_|. So each time, check for RequestSet and use
238 // TODO(willchan): If it's important, switch RequestSet out for a FIFO
239 // queue (Order by priority first, then FIFO within same priority). Unclear
240 // that it matters here.
241 if (!ContainsKey(spdy_session_request_map_
, spdy_session_key
))
243 Request
* request
= *spdy_session_request_map_
[spdy_session_key
].begin();
244 request
->Complete(was_npn_negotiated
,
248 if (for_websockets_
) {
249 // TODO(ricea): Restore this code path when WebSocket over SPDY
250 // implementation is ready.
253 bool use_relative_url
= direct
|| request
->url().SchemeIs("https");
254 request
->OnStreamReady(
258 new SpdyHttpStream(spdy_session
, use_relative_url
));
261 // TODO(mbelshe): Alert other valid requests.
264 void HttpStreamFactoryImpl::OnOrphanedJobComplete(const Job
* job
) {
265 orphaned_job_set_
.erase(job
);
269 void HttpStreamFactoryImpl::OnPreconnectsComplete(const Job
* job
) {
270 preconnect_job_set_
.erase(job
);
272 OnPreconnectsCompleteInternal();