Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Side by Side Diff: net/http/http_stream_factory_impl.cc

Issue 1941083002: JobController 1: Adding a new class HttpStreamFactoryImpl::JobController (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add JobFactory interface in JobController, remove JobControllerPeer Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/http/http_stream_factory_impl.h" 5 #include "net/http/http_stream_factory_impl.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/ptr_util.h"
10 #include "base/stl_util.h" 11 #include "base/stl_util.h"
11 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
12 #include "net/http/http_network_session.h" 13 #include "net/http/http_network_session.h"
13 #include "net/http/http_server_properties.h" 14 #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_job.h"
16 #include "net/http/http_stream_factory_impl_job_controller.h"
17 #include "net/http/http_stream_factory_impl_job_factory_impl.h"
15 #include "net/http/http_stream_factory_impl_request.h" 18 #include "net/http/http_stream_factory_impl_request.h"
16 #include "net/http/transport_security_state.h" 19 #include "net/http/transport_security_state.h"
17 #include "net/log/net_log.h" 20 #include "net/log/net_log.h"
18 #include "net/quic/quic_server_id.h" 21 #include "net/quic/quic_server_id.h"
19 #include "net/spdy/bidirectional_stream_spdy_impl.h" 22 #include "net/spdy/bidirectional_stream_spdy_impl.h"
20 #include "net/spdy/spdy_http_stream.h" 23 #include "net/spdy/spdy_http_stream.h"
21 #include "url/gurl.h" 24 #include "url/gurl.h"
22 25
23 namespace net { 26 namespace net {
24 27
25 HttpStreamFactoryImpl::HttpStreamFactoryImpl(HttpNetworkSession* session, 28 HttpStreamFactoryImpl::HttpStreamFactoryImpl(HttpNetworkSession* session,
26 bool for_websockets) 29 bool for_websockets)
27 : session_(session), 30 : session_(session),
31 job_factory_(new JobFactoryImpl()),
Ryan Hamilton 2016/06/06 17:57:31 Doesn't this need to be a constructor argument so
Zhongyi Shi 2016/06/06 22:50:55 The HttpStreamFactoryImpl is the owner of factory.
28 for_websockets_(for_websockets) {} 32 for_websockets_(for_websockets) {}
29 33
30 HttpStreamFactoryImpl::~HttpStreamFactoryImpl() { 34 HttpStreamFactoryImpl::~HttpStreamFactoryImpl() {
31 DCHECK(request_map_.empty()); 35 DCHECK(request_map_.empty());
32 DCHECK(spdy_session_request_map_.empty()); 36 DCHECK(spdy_session_request_map_.empty());
33 37 job_controller_set_.clear();
34 std::set<const Job*> tmp_job_set;
35 tmp_job_set.swap(orphaned_job_set_);
36 STLDeleteContainerPointers(tmp_job_set.begin(), tmp_job_set.end());
37 DCHECK(orphaned_job_set_.empty());
38
39 tmp_job_set.clear();
40 tmp_job_set.swap(preconnect_job_set_);
41 STLDeleteContainerPointers(tmp_job_set.begin(), tmp_job_set.end());
42 DCHECK(preconnect_job_set_.empty());
43 } 38 }
44 39
45 HttpStreamRequest* HttpStreamFactoryImpl::RequestStream( 40 HttpStreamRequest* HttpStreamFactoryImpl::RequestStream(
46 const HttpRequestInfo& request_info, 41 const HttpRequestInfo& request_info,
47 RequestPriority priority, 42 RequestPriority priority,
48 const SSLConfig& server_ssl_config, 43 const SSLConfig& server_ssl_config,
49 const SSLConfig& proxy_ssl_config, 44 const SSLConfig& proxy_ssl_config,
50 HttpStreamRequest::Delegate* delegate, 45 HttpStreamRequest::Delegate* delegate,
51 const BoundNetLog& net_log) { 46 const BoundNetLog& net_log) {
52 DCHECK(!for_websockets_); 47 DCHECK(!for_websockets_);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 HttpStreamRequest* HttpStreamFactoryImpl::RequestStreamInternal( 83 HttpStreamRequest* HttpStreamFactoryImpl::RequestStreamInternal(
89 const HttpRequestInfo& request_info, 84 const HttpRequestInfo& request_info,
90 RequestPriority priority, 85 RequestPriority priority,
91 const SSLConfig& server_ssl_config, 86 const SSLConfig& server_ssl_config,
92 const SSLConfig& proxy_ssl_config, 87 const SSLConfig& proxy_ssl_config,
93 HttpStreamRequest::Delegate* delegate, 88 HttpStreamRequest::Delegate* delegate,
94 WebSocketHandshakeStreamBase::CreateHelper* 89 WebSocketHandshakeStreamBase::CreateHelper*
95 websocket_handshake_stream_create_helper, 90 websocket_handshake_stream_create_helper,
96 HttpStreamRequest::StreamType stream_type, 91 HttpStreamRequest::StreamType stream_type,
97 const BoundNetLog& net_log) { 92 const BoundNetLog& net_log) {
98 Request* request = new Request(request_info.url, this, delegate, 93 JobController* job_controller =
99 websocket_handshake_stream_create_helper, 94 new JobController(this, delegate, session_, job_factory_.get());
100 net_log, stream_type); 95 job_controller_set_.insert(base::WrapUnique(job_controller));
101 HostPortPair destination(HostPortPair::FromURL(request_info.url));
102 GURL origin_url = ApplyHostMappingRules(request_info.url, &destination);
103 96
104 Job* job = 97 Request* request = job_controller->Start(
105 new Job(this, session_, request_info, priority, server_ssl_config, 98 request_info, delegate, websocket_handshake_stream_create_helper, net_log,
106 proxy_ssl_config, destination, origin_url, net_log.net_log()); 99 stream_type, priority, server_ssl_config, proxy_ssl_config);
107 request->AttachJob(job);
108 100
109 const AlternativeService alternative_service =
110 GetAlternativeServiceFor(request_info, delegate, stream_type);
111
112 if (alternative_service.protocol != UNINITIALIZED_ALTERNATE_PROTOCOL) {
113 // Never share connection with other jobs for FTP requests.
114 DVLOG(1) << "Selected alternative service (host: "
115 << alternative_service.host_port_pair().host()
116 << " port: " << alternative_service.host_port_pair().port() << ")";
117
118 DCHECK(!request_info.url.SchemeIs("ftp"));
119 HostPortPair alternative_destination(alternative_service.host_port_pair());
120 ignore_result(
121 ApplyHostMappingRules(request_info.url, &alternative_destination));
122
123 Job* alternative_job =
124 new Job(this, session_, request_info, priority, server_ssl_config,
125 proxy_ssl_config, alternative_destination, origin_url,
126 alternative_service, net_log.net_log());
127 request->AttachJob(alternative_job);
128
129 job->WaitFor(alternative_job);
130 // Make sure to wait until we call WaitFor(), before starting
131 // |alternative_job|, otherwise |alternative_job| will not notify |job|
132 // appropriately.
133 alternative_job->Start(request);
134 }
135
136 // Even if |alternative_job| has already finished, it will not have notified
137 // the request yet, since we defer that to the next iteration of the
138 // MessageLoop, so starting |job| is always safe.
139 job->Start(request);
140 return request; 101 return request;
141 } 102 }
142 103
143 void HttpStreamFactoryImpl::PreconnectStreams( 104 void HttpStreamFactoryImpl::PreconnectStreams(
144 int num_streams, 105 int num_streams,
145 const HttpRequestInfo& request_info) { 106 const HttpRequestInfo& request_info) {
146 SSLConfig server_ssl_config; 107 SSLConfig server_ssl_config;
147 SSLConfig proxy_ssl_config; 108 SSLConfig proxy_ssl_config;
148 session_->GetSSLConfig(request_info, &server_ssl_config, &proxy_ssl_config); 109 session_->GetSSLConfig(request_info, &server_ssl_config, &proxy_ssl_config);
149 // All preconnects should perform EV certificate verification. 110 // All preconnects should perform EV certificate verification.
150 server_ssl_config.verify_ev_cert = true; 111 server_ssl_config.verify_ev_cert = true;
151 proxy_ssl_config.verify_ev_cert = true; 112 proxy_ssl_config.verify_ev_cert = true;
152 113
153 DCHECK(!for_websockets_); 114 DCHECK(!for_websockets_);
154 AlternativeService alternative_service = GetAlternativeServiceFor( 115
155 request_info, nullptr, HttpStreamRequest::HTTP_STREAM); 116 JobController* job_controller =
156 HostPortPair destination(HostPortPair::FromURL(request_info.url)); 117 new JobController(this, nullptr, session_, job_factory_.get());
157 GURL origin_url = ApplyHostMappingRules(request_info.url, &destination); 118 job_controller_set_.insert(base::WrapUnique(job_controller));
158 if (alternative_service.protocol != UNINITIALIZED_ALTERNATE_PROTOCOL) { 119 job_controller->Preconnect(num_streams, request_info, server_ssl_config,
159 if (session_->params().quic_disable_preconnect_if_0rtt && 120 proxy_ssl_config);
160 alternative_service.protocol == QUIC &&
161 session_->quic_stream_factory()->ZeroRTTEnabledFor(QuicServerId(
162 alternative_service.host_port_pair(), request_info.privacy_mode))) {
163 return;
164 }
165 destination = alternative_service.host_port_pair();
166 ignore_result(ApplyHostMappingRules(request_info.url, &destination));
167 }
168 // Due to how the socket pools handle priorities and idle sockets, only IDLE
169 // priority currently makes sense for preconnects. The priority for
170 // preconnects is currently ignored (see RequestSocketsForPool()), but could
171 // be used at some point for proxy resolution or something.
172 Job* job = new Job(this, session_, request_info, IDLE, server_ssl_config,
173 proxy_ssl_config, destination, origin_url,
174 alternative_service, session_->net_log());
175 preconnect_job_set_.insert(job);
176 job->Preconnect(num_streams);
177 } 121 }
178 122
179 const HostMappingRules* HttpStreamFactoryImpl::GetHostMappingRules() const { 123 const HostMappingRules* HttpStreamFactoryImpl::GetHostMappingRules() const {
180 return session_->params().host_mapping_rules; 124 return session_->params().host_mapping_rules;
181 } 125 }
182 126
183 AlternativeService HttpStreamFactoryImpl::GetAlternativeServiceFor(
184 const HttpRequestInfo& request_info,
185 HttpStreamRequest::Delegate* delegate,
186 HttpStreamRequest::StreamType stream_type) {
187 GURL original_url = request_info.url;
188
189 if (original_url.SchemeIs("ftp"))
190 return AlternativeService();
191
192 url::SchemeHostPort origin(original_url);
193 HttpServerProperties& http_server_properties =
194 *session_->http_server_properties();
195 const AlternativeServiceVector alternative_service_vector =
196 http_server_properties.GetAlternativeServices(origin);
197 if (alternative_service_vector.empty())
198 return AlternativeService();
199
200 bool quic_advertised = false;
201 bool quic_all_broken = true;
202
203 const bool enable_different_host =
204 session_->params().enable_alternative_service_with_different_host;
205
206 // First Alt-Svc that is not marked as broken.
207 AlternativeService first_alternative_service;
208
209 for (const AlternativeService& alternative_service :
210 alternative_service_vector) {
211 DCHECK(IsAlternateProtocolValid(alternative_service.protocol));
212 if (!quic_advertised && alternative_service.protocol == QUIC)
213 quic_advertised = true;
214 if (http_server_properties.IsAlternativeServiceBroken(
215 alternative_service)) {
216 HistogramAlternateProtocolUsage(ALTERNATE_PROTOCOL_USAGE_BROKEN);
217 continue;
218 }
219
220 if (origin.host() != alternative_service.host && !enable_different_host)
221 continue;
222
223 // Some shared unix systems may have user home directories (like
224 // http://foo.com/~mike) which allow users to emit headers. This is a bad
225 // idea already, but with Alternate-Protocol, it provides the ability for a
226 // single user on a multi-user system to hijack the alternate protocol.
227 // These systems also enforce ports <1024 as restricted ports. So don't
228 // allow protocol upgrades to user-controllable ports.
229 const int kUnrestrictedPort = 1024;
230 if (!session_->params().enable_user_alternate_protocol_ports &&
231 (alternative_service.port >= kUnrestrictedPort &&
232 origin.port() < kUnrestrictedPort))
233 continue;
234
235 if (alternative_service.protocol >= NPN_SPDY_MINIMUM_VERSION &&
236 alternative_service.protocol <= NPN_SPDY_MAXIMUM_VERSION) {
237 if (!HttpStreamFactory::spdy_enabled())
238 continue;
239
240 // Cache this entry if we don't have a non-broken Alt-Svc yet.
241 if (first_alternative_service.protocol ==
242 UNINITIALIZED_ALTERNATE_PROTOCOL)
243 first_alternative_service = alternative_service;
244 continue;
245 }
246
247 DCHECK_EQ(QUIC, alternative_service.protocol);
248 quic_all_broken = false;
249 if (!session_->params().enable_quic)
250 continue;
251
252 if (!IsQuicWhitelistedForHost(origin.host()))
253 continue;
254
255 if (stream_type == HttpStreamRequest::BIDIRECTIONAL_STREAM &&
256 session_->params().quic_disable_bidirectional_streams) {
257 continue;
258 }
259
260 if (session_->quic_stream_factory()->IsQuicDisabled(
261 alternative_service.port))
262 continue;
263
264 if (!original_url.SchemeIs("https"))
265 continue;
266
267 // Check whether there is an existing QUIC session to use for this origin.
268 HostPortPair destination(alternative_service.host_port_pair());
269 ignore_result(ApplyHostMappingRules(original_url, &destination));
270 QuicServerId server_id(destination, request_info.privacy_mode);
271
272 HostPortPair origin_copy(origin.host(), origin.port());
273 ignore_result(ApplyHostMappingRules(original_url, &origin_copy));
274
275 if (session_->quic_stream_factory()->CanUseExistingSession(
276 server_id, origin_copy.host())) {
277 return alternative_service;
278 }
279
280 // Cache this entry if we don't have a non-broken Alt-Svc yet.
281 if (first_alternative_service.protocol == UNINITIALIZED_ALTERNATE_PROTOCOL)
282 first_alternative_service = alternative_service;
283 }
284
285 // Ask delegate to mark QUIC as broken for the origin.
286 if (quic_advertised && quic_all_broken && delegate != nullptr)
287 delegate->OnQuicBroken();
288
289 return first_alternative_service;
290 }
291
292 void HttpStreamFactoryImpl::OrphanJob(Job* job, const Request* request) {
293 DCHECK(ContainsKey(request_map_, job));
294 DCHECK_EQ(request_map_[job], request);
295 DCHECK(!ContainsKey(orphaned_job_set_, job));
296
297 request_map_.erase(job);
298
299 orphaned_job_set_.insert(job);
300 job->Orphan(request);
301 }
302
303 void HttpStreamFactoryImpl::OnNewSpdySessionReady( 127 void HttpStreamFactoryImpl::OnNewSpdySessionReady(
304 const base::WeakPtr<SpdySession>& spdy_session, 128 const base::WeakPtr<SpdySession>& spdy_session,
305 bool direct, 129 bool direct,
306 const SSLConfig& used_ssl_config, 130 const SSLConfig& used_ssl_config,
307 const ProxyInfo& used_proxy_info, 131 const ProxyInfo& used_proxy_info,
308 bool was_npn_negotiated, 132 bool was_npn_negotiated,
309 NextProto protocol_negotiated, 133 NextProto protocol_negotiated,
310 bool using_spdy, 134 bool using_spdy,
311 const BoundNetLog& net_log) { 135 const BoundNetLog& net_log) {
312 while (true) { 136 while (true) {
(...skipping 11 matching lines...) Expand all
324 break; 148 break;
325 Request* request = *spdy_session_request_map_[spdy_session_key].begin(); 149 Request* request = *spdy_session_request_map_[spdy_session_key].begin();
326 request->Complete(was_npn_negotiated, protocol_negotiated, using_spdy); 150 request->Complete(was_npn_negotiated, protocol_negotiated, using_spdy);
327 if (for_websockets_) { 151 if (for_websockets_) {
328 // TODO(ricea): Restore this code path when WebSocket over SPDY 152 // TODO(ricea): Restore this code path when WebSocket over SPDY
329 // implementation is ready. 153 // implementation is ready.
330 NOTREACHED(); 154 NOTREACHED();
331 } else if (request->stream_type() == 155 } else if (request->stream_type() ==
332 HttpStreamRequest::BIDIRECTIONAL_STREAM) { 156 HttpStreamRequest::BIDIRECTIONAL_STREAM) {
333 request->OnBidirectionalStreamImplReady( 157 request->OnBidirectionalStreamImplReady(
334 nullptr, used_ssl_config, used_proxy_info, 158 used_ssl_config, used_proxy_info,
335 new BidirectionalStreamSpdyImpl(spdy_session)); 159 new BidirectionalStreamSpdyImpl(spdy_session));
336 } else { 160 } else {
337 bool use_relative_url = direct || request->url().SchemeIs("https"); 161 bool use_relative_url = direct || request->url().SchemeIs("https");
338 request->OnStreamReady( 162 request->OnStreamReady(
339 nullptr, used_ssl_config, used_proxy_info, 163 used_ssl_config, used_proxy_info,
340 new SpdyHttpStream(spdy_session, use_relative_url)); 164 new SpdyHttpStream(spdy_session, use_relative_url));
341 } 165 }
342 } 166 }
343 // TODO(mbelshe): Alert other valid requests. 167 // TODO(mbelshe): Alert other valid requests.
344 } 168 }
345 169
346 void HttpStreamFactoryImpl::OnOrphanedJobComplete(const Job* job) { 170 void HttpStreamFactoryImpl::OnJobControllerComplete(JobController* controller) {
347 orphaned_job_set_.erase(job); 171 for (auto it = job_controller_set_.begin(); it != job_controller_set_.end();
348 delete job; 172 ++it) {
349 } 173 if (it->get() == controller) {
350 174 job_controller_set_.erase(it);
351 void HttpStreamFactoryImpl::OnPreconnectsComplete(const Job* job) { 175 return;
352 preconnect_job_set_.erase(job);
353 delete job;
354 OnPreconnectsCompleteInternal();
355 }
356
357 bool HttpStreamFactoryImpl::IsQuicWhitelistedForHost(const std::string& host) {
358 bool whitelist_needed = false;
359 for (QuicVersion version : session_->params().quic_supported_versions) {
360 if (version <= QUIC_VERSION_30) {
361 whitelist_needed = true;
362 break;
363 } 176 }
364 } 177 }
365 178 NOTREACHED();
366 // The QUIC whitelist is not needed in QUIC versions after 30.
367 if (!whitelist_needed)
368 return true;
369
370 if (session_->params().transport_security_state->IsGooglePinnedHost(host))
371 return true;
372
373 return ContainsKey(session_->params().quic_host_whitelist,
374 base::ToLowerASCII(host));
375 } 179 }
376 180
377 } // namespace net 181 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698