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::FromURL(original_url
);
189 HttpServerProperties
& http_server_properties
=
190 *session_
->http_server_properties();
191 if (!http_server_properties
.HasAlternateProtocol(origin
))
192 return kNoAlternateProtocol
;
194 AlternateProtocolInfo alternate
=
195 http_server_properties
.GetAlternateProtocol(origin
);
196 if (alternate
.is_broken
) {
197 HistogramAlternateProtocolUsage(ALTERNATE_PROTOCOL_USAGE_BROKEN
);
198 return kNoAlternateProtocol
;
201 if (!IsAlternateProtocolValid(alternate
.protocol
)) {
203 return kNoAlternateProtocol
;
206 // Some shared unix systems may have user home directories (like
207 // http://foo.com/~mike) which allow users to emit headers. This is a bad
208 // idea already, but with Alternate-Protocol, it provides the ability for a
209 // single user on a multi-user system to hijack the alternate protocol.
210 // These systems also enforce ports <1024 as restricted ports. So don't
211 // allow protocol upgrades to user-controllable ports.
212 const int kUnrestrictedPort
= 1024;
213 if (!session_
->params().enable_user_alternate_protocol_ports
&&
214 (alternate
.port
>= kUnrestrictedPort
&&
215 origin
.port() < kUnrestrictedPort
))
216 return kNoAlternateProtocol
;
218 origin
.set_port(alternate
.port
);
219 if (alternate
.protocol
>= NPN_SPDY_MINIMUM_VERSION
&&
220 alternate
.protocol
<= NPN_SPDY_MAXIMUM_VERSION
) {
221 if (!HttpStreamFactory::spdy_enabled())
222 return kNoAlternateProtocol
;
224 if (session_
->HasSpdyExclusion(origin
))
225 return kNoAlternateProtocol
;
227 *alternate_url
= UpgradeUrlToHttps(original_url
, alternate
.port
);
229 DCHECK_EQ(QUIC
, alternate
.protocol
);
230 if (!session_
->params().enable_quic
)
231 return kNoAlternateProtocol
;
233 // TODO(rch): Figure out how to make QUIC iteract with PAC
234 // scripts. By not re-writing the URL, we will query the PAC script
235 // for the proxy to use to reach the original URL via TCP. But
236 // the alternate request will be going via UDP to a different port.
237 *alternate_url
= original_url
;
242 void HttpStreamFactoryImpl::OrphanJob(Job
* job
, const Request
* request
) {
243 DCHECK(ContainsKey(request_map_
, job
));
244 DCHECK_EQ(request_map_
[job
], request
);
245 DCHECK(!ContainsKey(orphaned_job_set_
, job
));
247 request_map_
.erase(job
);
249 orphaned_job_set_
.insert(job
);
250 job
->Orphan(request
);
253 void HttpStreamFactoryImpl::OnNewSpdySessionReady(
254 const base::WeakPtr
<SpdySession
>& spdy_session
,
256 const SSLConfig
& used_ssl_config
,
257 const ProxyInfo
& used_proxy_info
,
258 bool was_npn_negotiated
,
259 NextProto protocol_negotiated
,
261 const BoundNetLog
& net_log
) {
265 const SpdySessionKey
& spdy_session_key
= spdy_session
->spdy_session_key();
266 // Each iteration may empty out the RequestSet for |spdy_session_key| in
267 // |spdy_session_request_map_|. So each time, check for RequestSet and use
270 // TODO(willchan): If it's important, switch RequestSet out for a FIFO
271 // queue (Order by priority first, then FIFO within same priority). Unclear
272 // that it matters here.
273 if (!ContainsKey(spdy_session_request_map_
, spdy_session_key
))
275 Request
* request
= *spdy_session_request_map_
[spdy_session_key
].begin();
276 request
->Complete(was_npn_negotiated
,
280 if (for_websockets_
) {
281 // TODO(ricea): Restore this code path when WebSocket over SPDY
282 // implementation is ready.
285 bool use_relative_url
= direct
|| request
->url().SchemeIs("https");
286 request
->OnStreamReady(
290 new SpdyHttpStream(spdy_session
, use_relative_url
));
293 // TODO(mbelshe): Alert other valid requests.
296 void HttpStreamFactoryImpl::OnOrphanedJobComplete(const Job
* job
) {
297 orphaned_job_set_
.erase(job
);
301 void HttpStreamFactoryImpl::OnPreconnectsComplete(const Job
* job
) {
302 preconnect_job_set_
.erase(job
);
304 OnPreconnectsCompleteInternal();