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 if (http_server_properties
.IsAlternativeServiceBroken(alternative_service
)) {
163 HistogramAlternateProtocolUsage(ALTERNATE_PROTOCOL_USAGE_BROKEN
);
164 return kNoAlternativeService
;
166 if (!IsAlternateProtocolValid(alternative_service
.protocol
)) {
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
,
223 const SSLConfig
& used_ssl_config
,
224 const ProxyInfo
& used_proxy_info
,
225 bool was_npn_negotiated
,
226 NextProto protocol_negotiated
,
228 const BoundNetLog
& net_log
) {
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
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
))
242 Request
* request
= *spdy_session_request_map_
[spdy_session_key
].begin();
243 request
->Complete(was_npn_negotiated
,
247 if (for_websockets_
) {
248 // TODO(ricea): Restore this code path when WebSocket over SPDY
249 // implementation is ready.
252 bool use_relative_url
= direct
|| request
->url().SchemeIs("https");
253 request
->OnStreamReady(
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
);
268 void HttpStreamFactoryImpl::OnPreconnectsComplete(const Job
* job
) {
269 preconnect_job_set_
.erase(job
);
271 OnPreconnectsCompleteInternal();