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

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: rebase 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/metrics/histogram_macros.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "net/http/http_network_session.h" 13 #include "net/http/http_network_session.h"
14 #include "net/http/http_server_properties.h" 14 #include "net/http/http_server_properties.h"
15 #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"
16 #include "net/http/http_stream_factory_impl_request.h" 17 #include "net/http/http_stream_factory_impl_request.h"
17 #include "net/http/transport_security_state.h" 18 #include "net/http/transport_security_state.h"
18 #include "net/log/net_log.h" 19 #include "net/log/net_log.h"
19 #include "net/quic/quic_server_id.h" 20 #include "net/quic/quic_server_id.h"
20 #include "net/spdy/bidirectional_stream_spdy_impl.h" 21 #include "net/spdy/bidirectional_stream_spdy_impl.h"
21 #include "net/spdy/spdy_http_stream.h" 22 #include "net/spdy/spdy_http_stream.h"
22 #include "url/gurl.h" 23 #include "url/gurl.h"
23 24
24 namespace net { 25 namespace net {
25 26
27 class DefaultJobFactory : public HttpStreamFactoryImpl::JobFactory {
Ryan Hamilton 2016/06/15 23:34:20 nit: can you wrap this in namespace {} nit: class
Zhongyi Shi 2016/06/16 00:02:31 Done.
28 public:
29 DefaultJobFactory() {}
30
31 ~DefaultJobFactory() override {}
32
33 HttpStreamFactoryImpl::Job* CreateJob(
34 HttpStreamFactoryImpl::Job::Delegate* delegate,
35 HttpStreamFactoryImpl::JobType job_type,
36 HttpNetworkSession* session,
37 const HttpRequestInfo& request_info,
38 RequestPriority priority,
39 const SSLConfig& server_ssl_config,
40 const SSLConfig& proxy_ssl_config,
41 HostPortPair destination,
42 GURL origin_url,
43 NetLog* net_log) override {
44 return new HttpStreamFactoryImpl::Job(
45 delegate, job_type, session, request_info, priority, server_ssl_config,
46 proxy_ssl_config, destination, origin_url, net_log);
47 }
48
49 HttpStreamFactoryImpl::Job* CreateJob(
50 HttpStreamFactoryImpl::Job::Delegate* delegate,
51 HttpStreamFactoryImpl::JobType job_type,
52 HttpNetworkSession* session,
53 const HttpRequestInfo& request_info,
54 RequestPriority priority,
55 const SSLConfig& server_ssl_config,
56 const SSLConfig& proxy_ssl_config,
57 HostPortPair destination,
58 GURL origin_url,
59 AlternativeService alternative_service,
60 NetLog* net_log) override {
61 return new HttpStreamFactoryImpl::Job(
62 delegate, job_type, session, request_info, priority, server_ssl_config,
63 proxy_ssl_config, destination, origin_url, alternative_service,
64 net_log);
65 }
66 };
67
26 HttpStreamFactoryImpl::HttpStreamFactoryImpl(HttpNetworkSession* session, 68 HttpStreamFactoryImpl::HttpStreamFactoryImpl(HttpNetworkSession* session,
27 bool for_websockets) 69 bool for_websockets)
28 : session_(session), 70 : session_(session),
71 job_factory_(new DefaultJobFactory()),
29 for_websockets_(for_websockets) {} 72 for_websockets_(for_websockets) {}
30 73
31 HttpStreamFactoryImpl::~HttpStreamFactoryImpl() { 74 HttpStreamFactoryImpl::~HttpStreamFactoryImpl() {
32 DCHECK(request_map_.empty()); 75 DCHECK(request_map_.empty());
33 DCHECK(spdy_session_request_map_.empty()); 76 DCHECK(spdy_session_request_map_.empty());
34 77 job_controller_set_.clear();
Ryan Hamilton 2016/06/15 23:34:20 nit: Is this required, or will the default destruc
Zhongyi Shi 2016/06/16 00:02:31 Done.
35 std::set<const Job*> tmp_job_set;
36 tmp_job_set.swap(orphaned_job_set_);
37 STLDeleteContainerPointers(tmp_job_set.begin(), tmp_job_set.end());
38 DCHECK(orphaned_job_set_.empty());
39
40 tmp_job_set.clear();
41 tmp_job_set.swap(preconnect_job_set_);
42 STLDeleteContainerPointers(tmp_job_set.begin(), tmp_job_set.end());
43 DCHECK(preconnect_job_set_.empty());
44 } 78 }
45 79
46 HttpStreamRequest* HttpStreamFactoryImpl::RequestStream( 80 HttpStreamRequest* HttpStreamFactoryImpl::RequestStream(
47 const HttpRequestInfo& request_info, 81 const HttpRequestInfo& request_info,
48 RequestPriority priority, 82 RequestPriority priority,
49 const SSLConfig& server_ssl_config, 83 const SSLConfig& server_ssl_config,
50 const SSLConfig& proxy_ssl_config, 84 const SSLConfig& proxy_ssl_config,
51 HttpStreamRequest::Delegate* delegate, 85 HttpStreamRequest::Delegate* delegate,
52 const BoundNetLog& net_log) { 86 const BoundNetLog& net_log) {
53 DCHECK(!for_websockets_); 87 DCHECK(!for_websockets_);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 HttpStreamRequest* HttpStreamFactoryImpl::RequestStreamInternal( 123 HttpStreamRequest* HttpStreamFactoryImpl::RequestStreamInternal(
90 const HttpRequestInfo& request_info, 124 const HttpRequestInfo& request_info,
91 RequestPriority priority, 125 RequestPriority priority,
92 const SSLConfig& server_ssl_config, 126 const SSLConfig& server_ssl_config,
93 const SSLConfig& proxy_ssl_config, 127 const SSLConfig& proxy_ssl_config,
94 HttpStreamRequest::Delegate* delegate, 128 HttpStreamRequest::Delegate* delegate,
95 WebSocketHandshakeStreamBase::CreateHelper* 129 WebSocketHandshakeStreamBase::CreateHelper*
96 websocket_handshake_stream_create_helper, 130 websocket_handshake_stream_create_helper,
97 HttpStreamRequest::StreamType stream_type, 131 HttpStreamRequest::StreamType stream_type,
98 const BoundNetLog& net_log) { 132 const BoundNetLog& net_log) {
99 Request* request = new Request(request_info.url, this, delegate, 133 JobController* job_controller =
100 websocket_handshake_stream_create_helper, 134 new JobController(this, delegate, session_, job_factory_.get());
101 net_log, stream_type); 135 job_controller_set_.insert(base::WrapUnique(job_controller));
102 HostPortPair destination(HostPortPair::FromURL(request_info.url));
103 GURL origin_url = ApplyHostMappingRules(request_info.url, &destination);
104 136
105 Job* job = 137 Request* request = job_controller->Start(
106 new Job(this, session_, request_info, priority, server_ssl_config, 138 request_info, delegate, websocket_handshake_stream_create_helper, net_log,
107 proxy_ssl_config, destination, origin_url, net_log.net_log()); 139 stream_type, priority, server_ssl_config, proxy_ssl_config);
108 request->AttachJob(job);
109 140
110 const AlternativeService alternative_service =
111 GetAlternativeServiceFor(request_info, delegate, stream_type);
112
113 if (alternative_service.protocol != UNINITIALIZED_ALTERNATE_PROTOCOL) {
114 // Never share connection with other jobs for FTP requests.
115 DVLOG(1) << "Selected alternative service (host: "
116 << alternative_service.host_port_pair().host()
117 << " port: " << alternative_service.host_port_pair().port() << ")";
118
119 DCHECK(!request_info.url.SchemeIs("ftp"));
120 HostPortPair alternative_destination(alternative_service.host_port_pair());
121 ignore_result(
122 ApplyHostMappingRules(request_info.url, &alternative_destination));
123
124 Job* alternative_job =
125 new Job(this, session_, request_info, priority, server_ssl_config,
126 proxy_ssl_config, alternative_destination, origin_url,
127 alternative_service, net_log.net_log());
128 request->AttachJob(alternative_job);
129
130 job->WaitFor(alternative_job);
131 // Make sure to wait until we call WaitFor(), before starting
132 // |alternative_job|, otherwise |alternative_job| will not notify |job|
133 // appropriately.
134 alternative_job->Start(request);
135 }
136
137 // Even if |alternative_job| has already finished, it will not have notified
138 // the request yet, since we defer that to the next iteration of the
139 // MessageLoop, so starting |job| is always safe.
140 job->Start(request);
141 return request; 141 return request;
142 } 142 }
143 143
144 void HttpStreamFactoryImpl::PreconnectStreams( 144 void HttpStreamFactoryImpl::PreconnectStreams(
145 int num_streams, 145 int num_streams,
146 const HttpRequestInfo& request_info) { 146 const HttpRequestInfo& request_info) {
147 SSLConfig server_ssl_config; 147 SSLConfig server_ssl_config;
148 SSLConfig proxy_ssl_config; 148 SSLConfig proxy_ssl_config;
149 session_->GetSSLConfig(request_info, &server_ssl_config, &proxy_ssl_config); 149 session_->GetSSLConfig(request_info, &server_ssl_config, &proxy_ssl_config);
150 // All preconnects should perform EV certificate verification. 150 // All preconnects should perform EV certificate verification.
151 server_ssl_config.verify_ev_cert = true; 151 server_ssl_config.verify_ev_cert = true;
152 proxy_ssl_config.verify_ev_cert = true; 152 proxy_ssl_config.verify_ev_cert = true;
153 153
154 DCHECK(!for_websockets_); 154 DCHECK(!for_websockets_);
155 AlternativeService alternative_service = GetAlternativeServiceFor( 155
156 request_info, nullptr, HttpStreamRequest::HTTP_STREAM); 156 JobController* job_controller =
157 HostPortPair destination(HostPortPair::FromURL(request_info.url)); 157 new JobController(this, nullptr, session_, job_factory_.get());
158 GURL origin_url = ApplyHostMappingRules(request_info.url, &destination); 158 job_controller_set_.insert(base::WrapUnique(job_controller));
159 if (alternative_service.protocol != UNINITIALIZED_ALTERNATE_PROTOCOL) { 159 job_controller->Preconnect(num_streams, request_info, server_ssl_config,
160 if (session_->params().quic_disable_preconnect_if_0rtt && 160 proxy_ssl_config);
161 alternative_service.protocol == QUIC &&
162 session_->quic_stream_factory()->ZeroRTTEnabledFor(QuicServerId(
163 alternative_service.host_port_pair(), request_info.privacy_mode))) {
164 return;
165 }
166 destination = alternative_service.host_port_pair();
167 ignore_result(ApplyHostMappingRules(request_info.url, &destination));
168 }
169 // Due to how the socket pools handle priorities and idle sockets, only IDLE
170 // priority currently makes sense for preconnects. The priority for
171 // preconnects is currently ignored (see RequestSocketsForPool()), but could
172 // be used at some point for proxy resolution or something.
173 Job* job = new Job(this, session_, request_info, IDLE, server_ssl_config,
174 proxy_ssl_config, destination, origin_url,
175 alternative_service, session_->net_log());
176 preconnect_job_set_.insert(job);
177 job->Preconnect(num_streams);
178 } 161 }
179 162
180 const HostMappingRules* HttpStreamFactoryImpl::GetHostMappingRules() const { 163 const HostMappingRules* HttpStreamFactoryImpl::GetHostMappingRules() const {
181 return session_->params().host_mapping_rules; 164 return session_->params().host_mapping_rules;
182 } 165 }
183 166
184 AlternativeService HttpStreamFactoryImpl::GetAlternativeServiceFor(
185 const HttpRequestInfo& request_info,
186 HttpStreamRequest::Delegate* delegate,
187 HttpStreamRequest::StreamType stream_type) {
188 AlternativeService alternative_service =
189 GetAlternativeServiceForInternal(request_info, delegate, stream_type);
190 AlternativeServiceType type;
191 if (alternative_service.protocol == UNINITIALIZED_ALTERNATE_PROTOCOL) {
192 type = NO_ALTERNATIVE_SERVICE;
193 } else if (alternative_service.protocol == QUIC) {
194 if (request_info.url.host() == alternative_service.host) {
195 type = QUIC_SAME_DESTINATION;
196 } else {
197 type = QUIC_DIFFERENT_DESTINATION;
198 }
199 } else {
200 if (request_info.url.host() == alternative_service.host) {
201 type = NOT_QUIC_SAME_DESTINATION;
202 } else {
203 type = NOT_QUIC_DIFFERENT_DESTINATION;
204 }
205 }
206 UMA_HISTOGRAM_ENUMERATION("Net.AlternativeServiceTypeForRequest", type,
207 MAX_ALTERNATIVE_SERVICE_TYPE);
208 return alternative_service;
209 }
210
211 AlternativeService HttpStreamFactoryImpl::GetAlternativeServiceForInternal(
212 const HttpRequestInfo& request_info,
213 HttpStreamRequest::Delegate* delegate,
214 HttpStreamRequest::StreamType stream_type) {
215 GURL original_url = request_info.url;
216
217 if (original_url.SchemeIs("ftp"))
218 return AlternativeService();
219
220 if (!session_->params().enable_alternative_service_for_insecure_origins &&
221 !original_url.SchemeIs("https"))
222 return AlternativeService();
223
224 url::SchemeHostPort origin(original_url);
225 HttpServerProperties& http_server_properties =
226 *session_->http_server_properties();
227 const AlternativeServiceVector alternative_service_vector =
228 http_server_properties.GetAlternativeServices(origin);
229 if (alternative_service_vector.empty())
230 return AlternativeService();
231
232 bool quic_advertised = false;
233 bool quic_all_broken = true;
234
235 const bool enable_different_host =
236 session_->params().enable_alternative_service_with_different_host;
237
238 // First Alt-Svc that is not marked as broken.
239 AlternativeService first_alternative_service;
240
241 for (const AlternativeService& alternative_service :
242 alternative_service_vector) {
243 DCHECK(IsAlternateProtocolValid(alternative_service.protocol));
244 if (!quic_advertised && alternative_service.protocol == QUIC)
245 quic_advertised = true;
246 if (http_server_properties.IsAlternativeServiceBroken(
247 alternative_service)) {
248 HistogramAlternateProtocolUsage(ALTERNATE_PROTOCOL_USAGE_BROKEN);
249 continue;
250 }
251
252 if (origin.host() != alternative_service.host && !enable_different_host)
253 continue;
254
255 // Some shared unix systems may have user home directories (like
256 // http://foo.com/~mike) which allow users to emit headers. This is a bad
257 // idea already, but with Alternate-Protocol, it provides the ability for a
258 // single user on a multi-user system to hijack the alternate protocol.
259 // These systems also enforce ports <1024 as restricted ports. So don't
260 // allow protocol upgrades to user-controllable ports.
261 const int kUnrestrictedPort = 1024;
262 if (!session_->params().enable_user_alternate_protocol_ports &&
263 (alternative_service.port >= kUnrestrictedPort &&
264 origin.port() < kUnrestrictedPort))
265 continue;
266
267 if (alternative_service.protocol >= NPN_SPDY_MINIMUM_VERSION &&
268 alternative_service.protocol <= NPN_SPDY_MAXIMUM_VERSION) {
269 if (!HttpStreamFactory::spdy_enabled())
270 continue;
271
272 // TODO(bnc): Re-enable when https://crbug.com/615413 is fixed.
273 if (origin.host() != alternative_service.host)
274 continue;
275
276 // Cache this entry if we don't have a non-broken Alt-Svc yet.
277 if (first_alternative_service.protocol ==
278 UNINITIALIZED_ALTERNATE_PROTOCOL)
279 first_alternative_service = alternative_service;
280 continue;
281 }
282
283 DCHECK_EQ(QUIC, alternative_service.protocol);
284 quic_all_broken = false;
285 if (!session_->params().enable_quic)
286 continue;
287
288 if (!IsQuicWhitelistedForHost(origin.host()))
289 continue;
290
291 if (stream_type == HttpStreamRequest::BIDIRECTIONAL_STREAM &&
292 session_->params().quic_disable_bidirectional_streams) {
293 continue;
294 }
295
296 if (session_->quic_stream_factory()->IsQuicDisabled(
297 alternative_service.port))
298 continue;
299
300 if (!original_url.SchemeIs("https"))
301 continue;
302
303 // Check whether there is an existing QUIC session to use for this origin.
304 HostPortPair mapped_origin(origin.host(), origin.port());
305 ignore_result(ApplyHostMappingRules(original_url, &mapped_origin));
306 QuicServerId server_id(mapped_origin, request_info.privacy_mode);
307
308 HostPortPair destination(alternative_service.host_port_pair());
309 ignore_result(ApplyHostMappingRules(original_url, &destination));
310
311 if (session_->quic_stream_factory()->CanUseExistingSession(server_id,
312 destination)) {
313 return alternative_service;
314 }
315
316 // Cache this entry if we don't have a non-broken Alt-Svc yet.
317 if (first_alternative_service.protocol == UNINITIALIZED_ALTERNATE_PROTOCOL)
318 first_alternative_service = alternative_service;
319 }
320
321 // Ask delegate to mark QUIC as broken for the origin.
322 if (quic_advertised && quic_all_broken && delegate != nullptr)
323 delegate->OnQuicBroken();
324
325 return first_alternative_service;
326 }
327
328 void HttpStreamFactoryImpl::OrphanJob(Job* job, const Request* request) {
329 DCHECK(ContainsKey(request_map_, job));
330 DCHECK_EQ(request_map_[job], request);
331 DCHECK(!ContainsKey(orphaned_job_set_, job));
332
333 request_map_.erase(job);
334
335 orphaned_job_set_.insert(job);
336 job->Orphan(request);
337 }
338
339 void HttpStreamFactoryImpl::OnNewSpdySessionReady( 167 void HttpStreamFactoryImpl::OnNewSpdySessionReady(
340 const base::WeakPtr<SpdySession>& spdy_session, 168 const base::WeakPtr<SpdySession>& spdy_session,
341 bool direct, 169 bool direct,
342 const SSLConfig& used_ssl_config, 170 const SSLConfig& used_ssl_config,
343 const ProxyInfo& used_proxy_info, 171 const ProxyInfo& used_proxy_info,
344 bool was_npn_negotiated, 172 bool was_npn_negotiated,
345 NextProto protocol_negotiated, 173 NextProto protocol_negotiated,
346 bool using_spdy, 174 bool using_spdy,
347 const BoundNetLog& net_log) { 175 const BoundNetLog& net_log) {
348 while (true) { 176 while (true) {
(...skipping 11 matching lines...) Expand all
360 break; 188 break;
361 Request* request = *spdy_session_request_map_[spdy_session_key].begin(); 189 Request* request = *spdy_session_request_map_[spdy_session_key].begin();
362 request->Complete(was_npn_negotiated, protocol_negotiated, using_spdy); 190 request->Complete(was_npn_negotiated, protocol_negotiated, using_spdy);
363 if (for_websockets_) { 191 if (for_websockets_) {
364 // TODO(ricea): Restore this code path when WebSocket over SPDY 192 // TODO(ricea): Restore this code path when WebSocket over SPDY
365 // implementation is ready. 193 // implementation is ready.
366 NOTREACHED(); 194 NOTREACHED();
367 } else if (request->stream_type() == 195 } else if (request->stream_type() ==
368 HttpStreamRequest::BIDIRECTIONAL_STREAM) { 196 HttpStreamRequest::BIDIRECTIONAL_STREAM) {
369 request->OnBidirectionalStreamImplReady( 197 request->OnBidirectionalStreamImplReady(
370 nullptr, used_ssl_config, used_proxy_info, 198 used_ssl_config, used_proxy_info,
371 new BidirectionalStreamSpdyImpl(spdy_session)); 199 new BidirectionalStreamSpdyImpl(spdy_session));
372 } else { 200 } else {
373 bool use_relative_url = direct || request->url().SchemeIs("https"); 201 bool use_relative_url = direct || request->url().SchemeIs("https");
374 request->OnStreamReady( 202 request->OnStreamReady(
375 nullptr, used_ssl_config, used_proxy_info, 203 used_ssl_config, used_proxy_info,
376 new SpdyHttpStream(spdy_session, use_relative_url)); 204 new SpdyHttpStream(spdy_session, use_relative_url));
377 } 205 }
378 } 206 }
379 // TODO(mbelshe): Alert other valid requests. 207 // TODO(mbelshe): Alert other valid requests.
380 } 208 }
381 209
382 void HttpStreamFactoryImpl::OnOrphanedJobComplete(const Job* job) { 210 void HttpStreamFactoryImpl::OnJobControllerComplete(JobController* controller) {
383 orphaned_job_set_.erase(job); 211 for (auto it = job_controller_set_.begin(); it != job_controller_set_.end();
384 delete job; 212 ++it) {
385 } 213 if (it->get() == controller) {
386 214 job_controller_set_.erase(it);
387 void HttpStreamFactoryImpl::OnPreconnectsComplete(const Job* job) { 215 return;
388 preconnect_job_set_.erase(job);
389 delete job;
390 OnPreconnectsCompleteInternal();
391 }
392
393 bool HttpStreamFactoryImpl::IsQuicWhitelistedForHost(const std::string& host) {
394 bool whitelist_needed = false;
395 for (QuicVersion version : session_->params().quic_supported_versions) {
396 if (version <= QUIC_VERSION_30) {
397 whitelist_needed = true;
398 break;
399 } 216 }
400 } 217 }
401 218 NOTREACHED();
402 // The QUIC whitelist is not needed in QUIC versions after 30.
403 if (!whitelist_needed)
404 return true;
405
406 if (session_->params().transport_security_state->IsGooglePinnedHost(host))
407 return true;
408
409 return ContainsKey(session_->params().quic_host_whitelist,
410 base::ToLowerASCII(host));
411 } 219 }
412 220
413 } // namespace net 221 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698