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_log.h"
13 #include "net/base/net_util.h"
14 #include "net/http/http_network_session.h"
15 #include "net/http/http_server_properties.h"
16 #include "net/http/http_stream_factory_impl_job.h"
17 #include "net/http/http_stream_factory_impl_request.h"
18 #include "net/spdy/spdy_http_stream.h"
25 GURL
UpgradeUrlToHttps(const GURL
& original_url
, int port
) {
26 GURL::Replacements replacements
;
27 // new_sheme and new_port need to be in scope here because GURL::Replacements
28 // references the memory contained by them directly.
29 const std::string new_scheme
= "https";
30 const std::string new_port
= base::IntToString(port
);
31 replacements
.SetSchemeStr(new_scheme
);
32 replacements
.SetPortStr(new_port
);
33 return original_url
.ReplaceComponents(replacements
);
38 HttpStreamFactoryImpl::HttpStreamFactoryImpl(HttpNetworkSession
* session
,
41 for_websockets_(for_websockets
) {}
43 HttpStreamFactoryImpl::~HttpStreamFactoryImpl() {
44 DCHECK(request_map_
.empty());
45 DCHECK(spdy_session_request_map_
.empty());
47 std::set
<const Job
*> tmp_job_set
;
48 tmp_job_set
.swap(orphaned_job_set_
);
49 STLDeleteContainerPointers(tmp_job_set
.begin(), tmp_job_set
.end());
50 DCHECK(orphaned_job_set_
.empty());
53 tmp_job_set
.swap(preconnect_job_set_
);
54 STLDeleteContainerPointers(tmp_job_set
.begin(), tmp_job_set
.end());
55 DCHECK(preconnect_job_set_
.empty());
58 HttpStreamRequest
* HttpStreamFactoryImpl::RequestStream(
59 const HttpRequestInfo
& request_info
,
60 RequestPriority priority
,
61 const SSLConfig
& server_ssl_config
,
62 const SSLConfig
& proxy_ssl_config
,
63 HttpStreamRequest::Delegate
* delegate
,
64 const BoundNetLog
& net_log
) {
65 DCHECK(!for_websockets_
);
66 return RequestStreamInternal(request_info
,
75 HttpStreamRequest
* HttpStreamFactoryImpl::RequestWebSocketHandshakeStream(
76 const HttpRequestInfo
& request_info
,
77 RequestPriority priority
,
78 const SSLConfig
& server_ssl_config
,
79 const SSLConfig
& proxy_ssl_config
,
80 HttpStreamRequest::Delegate
* delegate
,
81 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
,
82 const BoundNetLog
& net_log
) {
83 DCHECK(for_websockets_
);
84 DCHECK(create_helper
);
85 return RequestStreamInternal(request_info
,
94 HttpStreamRequest
* HttpStreamFactoryImpl::RequestStreamInternal(
95 const HttpRequestInfo
& request_info
,
96 RequestPriority priority
,
97 const SSLConfig
& server_ssl_config
,
98 const SSLConfig
& proxy_ssl_config
,
99 HttpStreamRequest::Delegate
* delegate
,
100 WebSocketHandshakeStreamBase::CreateHelper
*
101 websocket_handshake_stream_create_helper
,
102 const BoundNetLog
& net_log
) {
103 Request
* request
= new Request(request_info
.url
,
106 websocket_handshake_stream_create_helper
,
110 AlternateProtocolInfo alternate
=
111 GetAlternateProtocolRequestFor(request_info
.url
, &alternate_url
);
112 Job
* alternate_job
= NULL
;
113 if (alternate
.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
, alternate
);
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 AlternateProtocolInfo alternate
=
155 GetAlternateProtocolRequestFor(request_info
.url
, &alternate_url
);
157 if (alternate
.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
, alternate
);
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 AlternateProtocolInfo
HttpStreamFactoryImpl::GetAlternateProtocolRequestFor(
176 const GURL
& original_url
,
177 GURL
* alternate_url
) {
178 const AlternateProtocolInfo kNoAlternateProtocol
=
179 AlternateProtocolInfo(0, UNINITIALIZED_ALTERNATE_PROTOCOL
, 0);
181 if (!session_
->params().use_alternate_protocols
)
182 return kNoAlternateProtocol
;
184 if (original_url
.SchemeIs("ftp"))
185 return kNoAlternateProtocol
;
187 HostPortPair origin
= HostPortPair(original_url
.HostNoBrackets(),
188 original_url
.EffectiveIntPort());
190 HttpServerProperties
& http_server_properties
=
191 *session_
->http_server_properties();
192 if (!http_server_properties
.HasAlternateProtocol(origin
))
193 return kNoAlternateProtocol
;
195 AlternateProtocolInfo alternate
=
196 http_server_properties
.GetAlternateProtocol(origin
);
197 if (alternate
.protocol
== ALTERNATE_PROTOCOL_BROKEN
) {
198 HistogramAlternateProtocolUsage(
199 ALTERNATE_PROTOCOL_USAGE_BROKEN
,
200 http_server_properties
.GetAlternateProtocolExperiment());
201 return kNoAlternateProtocol
;
204 if (!IsAlternateProtocolValid(alternate
.protocol
)) {
206 return kNoAlternateProtocol
;
209 // Some shared unix systems may have user home directories (like
210 // http://foo.com/~mike) which allow users to emit headers. This is a bad
211 // idea already, but with Alternate-Protocol, it provides the ability for a
212 // single user on a multi-user system to hijack the alternate protocol.
213 // These systems also enforce ports <1024 as restricted ports. So don't
214 // allow protocol upgrades to user-controllable ports.
215 const int kUnrestrictedPort
= 1024;
216 if (!session_
->params().enable_user_alternate_protocol_ports
&&
217 (alternate
.port
>= kUnrestrictedPort
&&
218 origin
.port() < kUnrestrictedPort
))
219 return kNoAlternateProtocol
;
221 origin
.set_port(alternate
.port
);
222 if (alternate
.protocol
>= NPN_SPDY_MINIMUM_VERSION
&&
223 alternate
.protocol
<= NPN_SPDY_MAXIMUM_VERSION
) {
224 if (!HttpStreamFactory::spdy_enabled())
225 return kNoAlternateProtocol
;
227 if (session_
->HasSpdyExclusion(origin
))
228 return kNoAlternateProtocol
;
230 *alternate_url
= UpgradeUrlToHttps(original_url
, alternate
.port
);
232 DCHECK_EQ(QUIC
, alternate
.protocol
);
233 if (!session_
->params().enable_quic
)
234 return kNoAlternateProtocol
;
236 // TODO(rch): Figure out how to make QUIC iteract with PAC
237 // scripts. By not re-writing the URL, we will query the PAC script
238 // for the proxy to use to reach the original URL via TCP. But
239 // the alternate request will be going via UDP to a different port.
240 *alternate_url
= original_url
;
245 void HttpStreamFactoryImpl::OrphanJob(Job
* job
, const Request
* request
) {
246 DCHECK(ContainsKey(request_map_
, job
));
247 DCHECK_EQ(request_map_
[job
], request
);
248 DCHECK(!ContainsKey(orphaned_job_set_
, job
));
250 request_map_
.erase(job
);
252 orphaned_job_set_
.insert(job
);
253 job
->Orphan(request
);
256 void HttpStreamFactoryImpl::OnNewSpdySessionReady(
257 const base::WeakPtr
<SpdySession
>& spdy_session
,
259 const SSLConfig
& used_ssl_config
,
260 const ProxyInfo
& used_proxy_info
,
261 bool was_npn_negotiated
,
262 NextProto protocol_negotiated
,
264 const BoundNetLog
& net_log
) {
268 const SpdySessionKey
& spdy_session_key
= spdy_session
->spdy_session_key();
269 // Each iteration may empty out the RequestSet for |spdy_session_key| in
270 // |spdy_session_request_map_|. So each time, check for RequestSet and use
273 // TODO(willchan): If it's important, switch RequestSet out for a FIFO
274 // queue (Order by priority first, then FIFO within same priority). Unclear
275 // that it matters here.
276 if (!ContainsKey(spdy_session_request_map_
, spdy_session_key
))
278 Request
* request
= *spdy_session_request_map_
[spdy_session_key
].begin();
279 request
->Complete(was_npn_negotiated
,
283 if (for_websockets_
) {
284 // TODO(ricea): Restore this code path when WebSocket over SPDY
285 // implementation is ready.
288 bool use_relative_url
= direct
|| request
->url().SchemeIs("https");
289 request
->OnStreamReady(
293 new SpdyHttpStream(spdy_session
, use_relative_url
));
296 // TODO(mbelshe): Alert other valid requests.
299 void HttpStreamFactoryImpl::OnOrphanedJobComplete(const Job
* job
) {
300 orphaned_job_set_
.erase(job
);
304 void HttpStreamFactoryImpl::OnPreconnectsComplete(const Job
* job
) {
305 preconnect_job_set_
.erase(job
);
307 OnPreconnectsCompleteInternal();