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

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

Issue 2073293002: Revert of JobController 1: Remove cross reference between Request, Job, and Impl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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
« no previous file with comments | « net/http/http_stream_factory_impl.h ('k') | net/http/http_stream_factory_impl_job.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/metrics/histogram_macros.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"
17 #include "net/http/http_stream_factory_impl_request.h" 16 #include "net/http/http_stream_factory_impl_request.h"
18 #include "net/http/transport_security_state.h" 17 #include "net/http/transport_security_state.h"
19 #include "net/log/net_log.h" 18 #include "net/log/net_log.h"
20 #include "net/quic/quic_server_id.h" 19 #include "net/quic/quic_server_id.h"
21 #include "net/spdy/bidirectional_stream_spdy_impl.h" 20 #include "net/spdy/bidirectional_stream_spdy_impl.h"
22 #include "net/spdy/spdy_http_stream.h" 21 #include "net/spdy/spdy_http_stream.h"
23 #include "url/gurl.h" 22 #include "url/gurl.h"
24 23
25 namespace net { 24 namespace net {
26 25
27 namespace {
28 // Default JobFactory for creating HttpStreamFactoryImpl::Jobs.
29 class DefaultJobFactory : public HttpStreamFactoryImpl::JobFactory {
30 public:
31 DefaultJobFactory() {}
32
33 ~DefaultJobFactory() override {}
34
35 HttpStreamFactoryImpl::Job* CreateJob(
36 HttpStreamFactoryImpl::Job::Delegate* delegate,
37 HttpStreamFactoryImpl::JobType job_type,
38 HttpNetworkSession* session,
39 const HttpRequestInfo& request_info,
40 RequestPriority priority,
41 const SSLConfig& server_ssl_config,
42 const SSLConfig& proxy_ssl_config,
43 HostPortPair destination,
44 GURL origin_url,
45 NetLog* net_log) override {
46 return new HttpStreamFactoryImpl::Job(
47 delegate, job_type, session, request_info, priority, server_ssl_config,
48 proxy_ssl_config, destination, origin_url, net_log);
49 }
50
51 HttpStreamFactoryImpl::Job* CreateJob(
52 HttpStreamFactoryImpl::Job::Delegate* delegate,
53 HttpStreamFactoryImpl::JobType job_type,
54 HttpNetworkSession* session,
55 const HttpRequestInfo& request_info,
56 RequestPriority priority,
57 const SSLConfig& server_ssl_config,
58 const SSLConfig& proxy_ssl_config,
59 HostPortPair destination,
60 GURL origin_url,
61 AlternativeService alternative_service,
62 NetLog* net_log) override {
63 return new HttpStreamFactoryImpl::Job(
64 delegate, job_type, session, request_info, priority, server_ssl_config,
65 proxy_ssl_config, destination, origin_url, alternative_service,
66 net_log);
67 }
68 };
69 } // anonymous namespace
70
71 HttpStreamFactoryImpl::HttpStreamFactoryImpl(HttpNetworkSession* session, 26 HttpStreamFactoryImpl::HttpStreamFactoryImpl(HttpNetworkSession* session,
72 bool for_websockets) 27 bool for_websockets)
73 : session_(session), 28 : session_(session),
74 job_factory_(new DefaultJobFactory()),
75 for_websockets_(for_websockets) {} 29 for_websockets_(for_websockets) {}
76 30
77 HttpStreamFactoryImpl::~HttpStreamFactoryImpl() { 31 HttpStreamFactoryImpl::~HttpStreamFactoryImpl() {
78 DCHECK(request_map_.empty()); 32 DCHECK(request_map_.empty());
79 DCHECK(spdy_session_request_map_.empty()); 33 DCHECK(spdy_session_request_map_.empty());
34
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());
80 } 44 }
81 45
82 HttpStreamRequest* HttpStreamFactoryImpl::RequestStream( 46 HttpStreamRequest* HttpStreamFactoryImpl::RequestStream(
83 const HttpRequestInfo& request_info, 47 const HttpRequestInfo& request_info,
84 RequestPriority priority, 48 RequestPriority priority,
85 const SSLConfig& server_ssl_config, 49 const SSLConfig& server_ssl_config,
86 const SSLConfig& proxy_ssl_config, 50 const SSLConfig& proxy_ssl_config,
87 HttpStreamRequest::Delegate* delegate, 51 HttpStreamRequest::Delegate* delegate,
88 const BoundNetLog& net_log) { 52 const BoundNetLog& net_log) {
89 DCHECK(!for_websockets_); 53 DCHECK(!for_websockets_);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 HttpStreamRequest* HttpStreamFactoryImpl::RequestStreamInternal( 89 HttpStreamRequest* HttpStreamFactoryImpl::RequestStreamInternal(
126 const HttpRequestInfo& request_info, 90 const HttpRequestInfo& request_info,
127 RequestPriority priority, 91 RequestPriority priority,
128 const SSLConfig& server_ssl_config, 92 const SSLConfig& server_ssl_config,
129 const SSLConfig& proxy_ssl_config, 93 const SSLConfig& proxy_ssl_config,
130 HttpStreamRequest::Delegate* delegate, 94 HttpStreamRequest::Delegate* delegate,
131 WebSocketHandshakeStreamBase::CreateHelper* 95 WebSocketHandshakeStreamBase::CreateHelper*
132 websocket_handshake_stream_create_helper, 96 websocket_handshake_stream_create_helper,
133 HttpStreamRequest::StreamType stream_type, 97 HttpStreamRequest::StreamType stream_type,
134 const BoundNetLog& net_log) { 98 const BoundNetLog& net_log) {
135 JobController* job_controller = 99 Request* request = new Request(request_info.url, this, delegate,
136 new JobController(this, delegate, session_, job_factory_.get()); 100 websocket_handshake_stream_create_helper,
137 job_controller_set_.insert(base::WrapUnique(job_controller)); 101 net_log, stream_type);
102 HostPortPair destination(HostPortPair::FromURL(request_info.url));
103 GURL origin_url = ApplyHostMappingRules(request_info.url, &destination);
138 104
139 Request* request = job_controller->Start( 105 Job* job =
140 request_info, delegate, websocket_handshake_stream_create_helper, net_log, 106 new Job(this, session_, request_info, priority, server_ssl_config,
141 stream_type, priority, server_ssl_config, proxy_ssl_config); 107 proxy_ssl_config, destination, origin_url, net_log.net_log());
108 request->AttachJob(job);
142 109
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);
143 return request; 141 return request;
144 } 142 }
145 143
146 void HttpStreamFactoryImpl::PreconnectStreams( 144 void HttpStreamFactoryImpl::PreconnectStreams(
147 int num_streams, 145 int num_streams,
148 const HttpRequestInfo& request_info) { 146 const HttpRequestInfo& request_info) {
149 SSLConfig server_ssl_config; 147 SSLConfig server_ssl_config;
150 SSLConfig proxy_ssl_config; 148 SSLConfig proxy_ssl_config;
151 session_->GetSSLConfig(request_info, &server_ssl_config, &proxy_ssl_config); 149 session_->GetSSLConfig(request_info, &server_ssl_config, &proxy_ssl_config);
152 // All preconnects should perform EV certificate verification. 150 // All preconnects should perform EV certificate verification.
153 server_ssl_config.verify_ev_cert = true; 151 server_ssl_config.verify_ev_cert = true;
154 proxy_ssl_config.verify_ev_cert = true; 152 proxy_ssl_config.verify_ev_cert = true;
155 153
156 DCHECK(!for_websockets_); 154 DCHECK(!for_websockets_);
157 155 AlternativeService alternative_service = GetAlternativeServiceFor(
158 JobController* job_controller = 156 request_info, nullptr, HttpStreamRequest::HTTP_STREAM);
159 new JobController(this, nullptr, session_, job_factory_.get()); 157 HostPortPair destination(HostPortPair::FromURL(request_info.url));
160 job_controller_set_.insert(base::WrapUnique(job_controller)); 158 GURL origin_url = ApplyHostMappingRules(request_info.url, &destination);
161 job_controller->Preconnect(num_streams, request_info, server_ssl_config, 159 if (alternative_service.protocol != UNINITIALIZED_ALTERNATE_PROTOCOL) {
162 proxy_ssl_config); 160 if (session_->params().quic_disable_preconnect_if_0rtt &&
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);
163 } 178 }
164 179
165 const HostMappingRules* HttpStreamFactoryImpl::GetHostMappingRules() const { 180 const HostMappingRules* HttpStreamFactoryImpl::GetHostMappingRules() const {
166 return session_->params().host_mapping_rules; 181 return session_->params().host_mapping_rules;
167 } 182 }
168 183
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
169 void HttpStreamFactoryImpl::OnNewSpdySessionReady( 339 void HttpStreamFactoryImpl::OnNewSpdySessionReady(
170 const base::WeakPtr<SpdySession>& spdy_session, 340 const base::WeakPtr<SpdySession>& spdy_session,
171 bool direct, 341 bool direct,
172 const SSLConfig& used_ssl_config, 342 const SSLConfig& used_ssl_config,
173 const ProxyInfo& used_proxy_info, 343 const ProxyInfo& used_proxy_info,
174 bool was_npn_negotiated, 344 bool was_npn_negotiated,
175 NextProto protocol_negotiated, 345 NextProto protocol_negotiated,
176 bool using_spdy, 346 bool using_spdy,
177 const BoundNetLog& net_log) { 347 const BoundNetLog& net_log) {
178 while (true) { 348 while (true) {
(...skipping 11 matching lines...) Expand all
190 break; 360 break;
191 Request* request = *spdy_session_request_map_[spdy_session_key].begin(); 361 Request* request = *spdy_session_request_map_[spdy_session_key].begin();
192 request->Complete(was_npn_negotiated, protocol_negotiated, using_spdy); 362 request->Complete(was_npn_negotiated, protocol_negotiated, using_spdy);
193 if (for_websockets_) { 363 if (for_websockets_) {
194 // TODO(ricea): Restore this code path when WebSocket over SPDY 364 // TODO(ricea): Restore this code path when WebSocket over SPDY
195 // implementation is ready. 365 // implementation is ready.
196 NOTREACHED(); 366 NOTREACHED();
197 } else if (request->stream_type() == 367 } else if (request->stream_type() ==
198 HttpStreamRequest::BIDIRECTIONAL_STREAM) { 368 HttpStreamRequest::BIDIRECTIONAL_STREAM) {
199 request->OnBidirectionalStreamImplReady( 369 request->OnBidirectionalStreamImplReady(
200 used_ssl_config, used_proxy_info, 370 nullptr, used_ssl_config, used_proxy_info,
201 new BidirectionalStreamSpdyImpl(spdy_session)); 371 new BidirectionalStreamSpdyImpl(spdy_session));
202 } else { 372 } else {
203 bool use_relative_url = direct || request->url().SchemeIs("https"); 373 bool use_relative_url = direct || request->url().SchemeIs("https");
204 request->OnStreamReady( 374 request->OnStreamReady(
205 used_ssl_config, used_proxy_info, 375 nullptr, used_ssl_config, used_proxy_info,
206 new SpdyHttpStream(spdy_session, use_relative_url)); 376 new SpdyHttpStream(spdy_session, use_relative_url));
207 } 377 }
208 } 378 }
209 // TODO(mbelshe): Alert other valid requests. 379 // TODO(mbelshe): Alert other valid requests.
210 } 380 }
211 381
212 void HttpStreamFactoryImpl::OnJobControllerComplete(JobController* controller) { 382 void HttpStreamFactoryImpl::OnOrphanedJobComplete(const Job* job) {
213 for (auto it = job_controller_set_.begin(); it != job_controller_set_.end(); 383 orphaned_job_set_.erase(job);
214 ++it) { 384 delete job;
215 if (it->get() == controller) { 385 }
216 job_controller_set_.erase(it); 386
217 return; 387 void HttpStreamFactoryImpl::OnPreconnectsComplete(const Job* job) {
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;
218 } 399 }
219 } 400 }
220 NOTREACHED(); 401
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));
221 } 411 }
222 412
223 } // namespace net 413 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_stream_factory_impl.h ('k') | net/http/http_stream_factory_impl_job.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698