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