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 "base/strings/string_number_conversions.h"
12 #include "net/base/net_util.h"
13 #include "net/http/http_network_session.h"
14 #include "net/http/http_server_properties.h"
15 #include "net/http/http_stream_factory_impl_job.h"
16 #include "net/http/http_stream_factory_impl_request.h"
17 #include "net/log/net_log.h"
18 #include "net/spdy/spdy_http_stream.h"
25 GURL
UpgradeUrlToHttps(const GURL
& original_url
, int port
) {
26 GURL::Replacements replacements
;
27 // new_port needs to be in scope here because GURL::Replacements references
28 // the memory contained by it directly.
29 const std::string new_port
= base::IntToString(port
);
30 replacements
.SetSchemeStr("https");
31 replacements
.SetPortStr(new_port
);
32 return original_url
.ReplaceComponents(replacements
);
37 HttpStreamFactoryImpl::HttpStreamFactoryImpl(HttpNetworkSession
* session
,
40 for_websockets_(for_websockets
) {}
42 HttpStreamFactoryImpl::~HttpStreamFactoryImpl() {
43 DCHECK(request_map_
.empty());
44 DCHECK(spdy_session_request_map_
.empty());
46 std::set
<const Job
*> tmp_job_set
;
47 tmp_job_set
.swap(orphaned_job_set_
);
48 STLDeleteContainerPointers(tmp_job_set
.begin(), tmp_job_set
.end());
49 DCHECK(orphaned_job_set_
.empty());
52 tmp_job_set
.swap(preconnect_job_set_
);
53 STLDeleteContainerPointers(tmp_job_set
.begin(), tmp_job_set
.end());
54 DCHECK(preconnect_job_set_
.empty());
57 HttpStreamRequest
* HttpStreamFactoryImpl::RequestStream(
58 const HttpRequestInfo
& request_info
,
59 RequestPriority priority
,
60 const SSLConfig
& server_ssl_config
,
61 const SSLConfig
& proxy_ssl_config
,
62 HttpStreamRequest::Delegate
* delegate
,
63 const BoundNetLog
& net_log
) {
64 DCHECK(!for_websockets_
);
65 return RequestStreamInternal(request_info
,
74 HttpStreamRequest
* HttpStreamFactoryImpl::RequestWebSocketHandshakeStream(
75 const HttpRequestInfo
& request_info
,
76 RequestPriority priority
,
77 const SSLConfig
& server_ssl_config
,
78 const SSLConfig
& proxy_ssl_config
,
79 HttpStreamRequest::Delegate
* delegate
,
80 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
,
81 const BoundNetLog
& net_log
) {
82 DCHECK(for_websockets_
);
83 DCHECK(create_helper
);
84 return RequestStreamInternal(request_info
,
93 HttpStreamRequest
* HttpStreamFactoryImpl::RequestStreamInternal(
94 const HttpRequestInfo
& request_info
,
95 RequestPriority priority
,
96 const SSLConfig
& server_ssl_config
,
97 const SSLConfig
& proxy_ssl_config
,
98 HttpStreamRequest::Delegate
* delegate
,
99 WebSocketHandshakeStreamBase::CreateHelper
*
100 websocket_handshake_stream_create_helper
,
101 const BoundNetLog
& net_log
) {
102 Request
* request
= new Request(request_info
.url
,
105 websocket_handshake_stream_create_helper
,
108 // TODO(bnc): Get rid of |alternate_url|.
110 AlternativeService alternative_service
=
111 GetAlternativeServiceRequestFor(request_info
.url
, &alternate_url
);
112 Job
* alternate_job
= NULL
;
113 if (alternative_service
.protocol
!= UNINITIALIZED_ALTERNATE_PROTOCOL
) {
114 // Never share connection with other jobs for FTP requests.
115 DCHECK(!request_info
.url
.SchemeIs("ftp"));
117 HttpRequestInfo alternate_request_info
= request_info
;
118 alternate_request_info
.url
= alternate_url
;
120 new Job(this, session_
, alternate_request_info
, priority
,
121 server_ssl_config
, proxy_ssl_config
, net_log
.net_log());
122 request
->AttachJob(alternate_job
);
123 alternate_job
->MarkAsAlternate(request_info
.url
, alternative_service
);
126 Job
* job
= new Job(this, session_
, request_info
, priority
,
127 server_ssl_config
, proxy_ssl_config
, net_log
.net_log());
128 request
->AttachJob(job
);
130 // Never share connection with other jobs for FTP requests.
131 DCHECK(!request_info
.url
.SchemeIs("ftp"));
133 job
->WaitFor(alternate_job
);
134 // Make sure to wait until we call WaitFor(), before starting
135 // |alternate_job|, otherwise |alternate_job| will not notify |job|
137 alternate_job
->Start(request
);
139 // Even if |alternate_job| has already finished, it won't have notified the
140 // request yet, since we defer that to the next iteration of the MessageLoop,
141 // so starting |job| is always safe.
146 void HttpStreamFactoryImpl::PreconnectStreams(
148 const HttpRequestInfo
& request_info
,
149 RequestPriority priority
,
150 const SSLConfig
& server_ssl_config
,
151 const SSLConfig
& proxy_ssl_config
) {
152 DCHECK(!for_websockets_
);
154 AlternativeService alternative_service
=
155 GetAlternativeServiceRequestFor(request_info
.url
, &alternate_url
);
157 if (alternative_service
.protocol
!= UNINITIALIZED_ALTERNATE_PROTOCOL
) {
158 HttpRequestInfo alternate_request_info
= request_info
;
159 alternate_request_info
.url
= alternate_url
;
160 job
= new Job(this, session_
, alternate_request_info
, priority
,
161 server_ssl_config
, proxy_ssl_config
, session_
->net_log());
162 job
->MarkAsAlternate(request_info
.url
, alternative_service
);
164 job
= new Job(this, session_
, request_info
, priority
,
165 server_ssl_config
, proxy_ssl_config
, session_
->net_log());
167 preconnect_job_set_
.insert(job
);
168 job
->Preconnect(num_streams
);
171 const HostMappingRules
* HttpStreamFactoryImpl::GetHostMappingRules() const {
172 return session_
->params().host_mapping_rules
;
175 AlternativeService
HttpStreamFactoryImpl::GetAlternativeServiceRequestFor(
176 const GURL
& original_url
,
177 GURL
* alternate_url
) {
178 const AlternativeService kNoAlternativeService
;
180 if (!session_
->params().use_alternate_protocols
)
181 return kNoAlternativeService
;
183 if (original_url
.SchemeIs("ftp"))
184 return kNoAlternativeService
;
186 HostPortPair origin
= HostPortPair::FromURL(original_url
);
187 HttpServerProperties
& http_server_properties
=
188 *session_
->http_server_properties();
189 const AlternativeService alternative_service
=
190 http_server_properties
.GetAlternativeService(origin
);
192 if (alternative_service
.protocol
== UNINITIALIZED_ALTERNATE_PROTOCOL
)
193 return kNoAlternativeService
;
194 // TODO(bnc): Make sure that callers connect to the specified host, and that
195 // certificate requirements are enforced. Then remove the following two
197 if (alternative_service
.host
!= origin
.host())
198 return kNoAlternativeService
;
199 if (http_server_properties
.IsAlternativeServiceBroken(alternative_service
)) {
200 HistogramAlternateProtocolUsage(ALTERNATE_PROTOCOL_USAGE_BROKEN
);
201 return kNoAlternativeService
;
203 if (!IsAlternateProtocolValid(alternative_service
.protocol
)) {
205 return kNoAlternativeService
;
208 // Some shared unix systems may have user home directories (like
209 // http://foo.com/~mike) which allow users to emit headers. This is a bad
210 // idea already, but with Alternate-Protocol, it provides the ability for a
211 // single user on a multi-user system to hijack the alternate protocol.
212 // These systems also enforce ports <1024 as restricted ports. So don't
213 // allow protocol upgrades to user-controllable ports.
214 const int kUnrestrictedPort
= 1024;
215 if (!session_
->params().enable_user_alternate_protocol_ports
&&
216 (alternative_service
.port
>= kUnrestrictedPort
&&
217 origin
.port() < kUnrestrictedPort
))
218 return kNoAlternativeService
;
220 origin
.set_port(alternative_service
.port
);
221 if (alternative_service
.protocol
>= NPN_SPDY_MINIMUM_VERSION
&&
222 alternative_service
.protocol
<= NPN_SPDY_MAXIMUM_VERSION
) {
223 if (!HttpStreamFactory::spdy_enabled())
224 return kNoAlternativeService
;
226 if (session_
->HasSpdyExclusion(origin
))
227 return kNoAlternativeService
;
229 *alternate_url
= UpgradeUrlToHttps(original_url
, alternative_service
.port
);
231 DCHECK_EQ(QUIC
, alternative_service
.protocol
);
232 if (!session_
->params().enable_quic
)
233 return kNoAlternativeService
;
235 if (session_
->quic_stream_factory()->IsQuicDisabled(origin
.port()))
236 return kNoAlternativeService
;
238 // TODO(rch): Figure out how to make QUIC iteract with PAC
239 // scripts. By not re-writing the URL, we will query the PAC script
240 // for the proxy to use to reach the original URL via TCP. But
241 // the alternate request will be going via UDP to a different port.
242 *alternate_url
= original_url
;
244 return alternative_service
;
247 void HttpStreamFactoryImpl::OrphanJob(Job
* job
, const Request
* request
) {
248 DCHECK(ContainsKey(request_map_
, job
));
249 DCHECK_EQ(request_map_
[job
], request
);
250 DCHECK(!ContainsKey(orphaned_job_set_
, job
));
252 request_map_
.erase(job
);
254 orphaned_job_set_
.insert(job
);
255 job
->Orphan(request
);
258 void HttpStreamFactoryImpl::OnNewSpdySessionReady(
259 const base::WeakPtr
<SpdySession
>& spdy_session
,
261 const SSLConfig
& used_ssl_config
,
262 const ProxyInfo
& used_proxy_info
,
263 bool was_npn_negotiated
,
264 NextProto protocol_negotiated
,
266 const BoundNetLog
& net_log
) {
270 const SpdySessionKey
& spdy_session_key
= spdy_session
->spdy_session_key();
271 // Each iteration may empty out the RequestSet for |spdy_session_key| in
272 // |spdy_session_request_map_|. So each time, check for RequestSet and use
275 // TODO(willchan): If it's important, switch RequestSet out for a FIFO
276 // queue (Order by priority first, then FIFO within same priority). Unclear
277 // that it matters here.
278 if (!ContainsKey(spdy_session_request_map_
, spdy_session_key
))
280 Request
* request
= *spdy_session_request_map_
[spdy_session_key
].begin();
281 request
->Complete(was_npn_negotiated
,
285 if (for_websockets_
) {
286 // TODO(ricea): Restore this code path when WebSocket over SPDY
287 // implementation is ready.
290 bool use_relative_url
= direct
|| request
->url().SchemeIs("https");
291 request
->OnStreamReady(
295 new SpdyHttpStream(spdy_session
, use_relative_url
));
298 // TODO(mbelshe): Alert other valid requests.
301 void HttpStreamFactoryImpl::OnOrphanedJobComplete(const Job
* job
) {
302 orphaned_job_set_
.erase(job
);
306 void HttpStreamFactoryImpl::OnPreconnectsComplete(const Job
* job
) {
307 preconnect_job_set_
.erase(job
);
309 OnPreconnectsCompleteInternal();