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_request.h"
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "base/stl_util.h"
10 #include "net/http/http_stream_factory_impl_job.h"
11 #include "net/spdy/spdy_http_stream.h"
12 #include "net/spdy/spdy_session.h"
16 HttpStreamFactoryImpl::Request::Request(
18 HttpStreamFactoryImpl
* factory
,
19 HttpStreamRequest::Delegate
* delegate
,
20 WebSocketHandshakeStreamBase::CreateHelper
*
21 websocket_handshake_stream_create_helper
,
22 const BoundNetLog
& net_log
)
25 websocket_handshake_stream_create_helper_(
26 websocket_handshake_stream_create_helper
),
30 was_npn_negotiated_(false),
31 protocol_negotiated_(kProtoUnknown
),
36 net_log_
.BeginEvent(NetLog::TYPE_HTTP_STREAM_REQUEST
);
39 HttpStreamFactoryImpl::Request::~Request() {
41 DCHECK(jobs_
.empty());
43 DCHECK(!jobs_
.empty());
45 net_log_
.EndEvent(NetLog::TYPE_HTTP_STREAM_REQUEST
);
47 for (std::set
<Job
*>::iterator it
= jobs_
.begin(); it
!= jobs_
.end(); ++it
)
48 factory_
->request_map_
.erase(*it
);
50 RemoveRequestFromSpdySessionRequestMap();
51 RemoveRequestFromHttpPipeliningRequestMap();
53 STLDeleteElements(&jobs_
);
56 void HttpStreamFactoryImpl::Request::SetSpdySessionKey(
57 const SpdySessionKey
& spdy_session_key
) {
58 DCHECK(!spdy_session_key_
.get());
59 spdy_session_key_
.reset(new SpdySessionKey(spdy_session_key
));
60 RequestSet
& request_set
=
61 factory_
->spdy_session_request_map_
[spdy_session_key
];
62 DCHECK(!ContainsKey(request_set
, this));
63 request_set
.insert(this);
66 bool HttpStreamFactoryImpl::Request::SetHttpPipeliningKey(
67 const HttpPipelinedHost::Key
& http_pipelining_key
) {
68 CHECK(!http_pipelining_key_
.get());
69 http_pipelining_key_
.reset(new HttpPipelinedHost::Key(http_pipelining_key
));
70 bool was_new_key
= !ContainsKey(factory_
->http_pipelining_request_map_
,
72 RequestVector
& request_vector
=
73 factory_
->http_pipelining_request_map_
[http_pipelining_key
];
74 request_vector
.push_back(this);
78 void HttpStreamFactoryImpl::Request::AttachJob(Job
* job
) {
81 factory_
->request_map_
[job
] = this;
84 void HttpStreamFactoryImpl::Request::Complete(
85 bool was_npn_negotiated
,
86 NextProto protocol_negotiated
,
88 const BoundNetLog
& job_net_log
) {
91 was_npn_negotiated_
= was_npn_negotiated
;
92 protocol_negotiated_
= protocol_negotiated
;
93 using_spdy_
= using_spdy
;
95 NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_JOB
,
96 job_net_log
.source().ToEventParametersCallback());
98 NetLog::TYPE_HTTP_STREAM_JOB_BOUND_TO_REQUEST
,
99 net_log_
.source().ToEventParametersCallback());
102 void HttpStreamFactoryImpl::Request::OnStreamReady(
104 const SSLConfig
& used_ssl_config
,
105 const ProxyInfo
& used_proxy_info
,
106 HttpStreamBase
* stream
) {
107 DCHECK(!factory_
->for_websockets_
);
112 delegate_
->OnStreamReady(used_ssl_config
, used_proxy_info
, stream
);
115 void HttpStreamFactoryImpl::Request::OnWebSocketHandshakeStreamReady(
117 const SSLConfig
& used_ssl_config
,
118 const ProxyInfo
& used_proxy_info
,
119 WebSocketHandshakeStreamBase
* stream
) {
120 DCHECK(factory_
->for_websockets_
);
125 delegate_
->OnWebSocketHandshakeStreamReady(
126 used_ssl_config
, used_proxy_info
, stream
);
129 void HttpStreamFactoryImpl::Request::OnStreamFailed(
132 const SSLConfig
& used_ssl_config
) {
133 DCHECK_NE(OK
, status
);
134 // |job| should only be NULL if we're being canceled by a late bound
135 // HttpPipelinedConnection (one that was not created by a job in our |jobs_|
138 DCHECK(!bound_job_
.get());
139 DCHECK(!jobs_
.empty());
140 // NOTE(willchan): We do *NOT* call OrphanJobs() here. The reason is because
141 // we *WANT* to cancel the unnecessary Jobs from other requests if another
142 // Job completes first.
143 } else if (!bound_job_
.get()) {
144 // Hey, we've got other jobs! Maybe one of them will succeed, let's just
145 // ignore this failure.
146 if (jobs_
.size() > 1) {
148 factory_
->request_map_
.erase(job
);
152 bound_job_
.reset(job
);
154 DCHECK(jobs_
.empty());
155 factory_
->request_map_
.erase(job
);
158 DCHECK(jobs_
.empty());
160 delegate_
->OnStreamFailed(status
, used_ssl_config
);
163 void HttpStreamFactoryImpl::Request::OnCertificateError(
166 const SSLConfig
& used_ssl_config
,
167 const SSLInfo
& ssl_info
) {
168 DCHECK_NE(OK
, status
);
169 if (!bound_job_
.get())
170 OrphanJobsExcept(job
);
172 DCHECK(jobs_
.empty());
173 delegate_
->OnCertificateError(status
, used_ssl_config
, ssl_info
);
176 void HttpStreamFactoryImpl::Request::OnNeedsProxyAuth(
178 const HttpResponseInfo
& proxy_response
,
179 const SSLConfig
& used_ssl_config
,
180 const ProxyInfo
& used_proxy_info
,
181 HttpAuthController
* auth_controller
) {
182 if (!bound_job_
.get())
183 OrphanJobsExcept(job
);
185 DCHECK(jobs_
.empty());
186 delegate_
->OnNeedsProxyAuth(
187 proxy_response
, used_ssl_config
, used_proxy_info
, auth_controller
);
190 void HttpStreamFactoryImpl::Request::OnNeedsClientAuth(
192 const SSLConfig
& used_ssl_config
,
193 SSLCertRequestInfo
* cert_info
) {
194 if (!bound_job_
.get())
195 OrphanJobsExcept(job
);
197 DCHECK(jobs_
.empty());
198 delegate_
->OnNeedsClientAuth(used_ssl_config
, cert_info
);
201 void HttpStreamFactoryImpl::Request::OnHttpsProxyTunnelResponse(
203 const HttpResponseInfo
& response_info
,
204 const SSLConfig
& used_ssl_config
,
205 const ProxyInfo
& used_proxy_info
,
206 HttpStreamBase
* stream
) {
207 if (!bound_job_
.get())
208 OrphanJobsExcept(job
);
210 DCHECK(jobs_
.empty());
211 delegate_
->OnHttpsProxyTunnelResponse(
212 response_info
, used_ssl_config
, used_proxy_info
, stream
);
215 int HttpStreamFactoryImpl::Request::RestartTunnelWithProxyAuth(
216 const AuthCredentials
& credentials
) {
217 DCHECK(bound_job_
.get());
218 return bound_job_
->RestartTunnelWithProxyAuth(credentials
);
221 void HttpStreamFactoryImpl::Request::SetPriority(RequestPriority priority
) {
222 for (std::set
<HttpStreamFactoryImpl::Job
*>::const_iterator it
= jobs_
.begin();
223 it
!= jobs_
.end(); ++it
) {
224 (*it
)->SetPriority(priority
);
227 bound_job_
->SetPriority(priority
);
230 LoadState
HttpStreamFactoryImpl::Request::GetLoadState() const {
231 if (bound_job_
.get())
232 return bound_job_
->GetLoadState();
233 DCHECK(!jobs_
.empty());
235 // Just pick the first one.
236 return (*jobs_
.begin())->GetLoadState();
239 bool HttpStreamFactoryImpl::Request::was_npn_negotiated() const {
241 return was_npn_negotiated_
;
244 NextProto
HttpStreamFactoryImpl::Request::protocol_negotiated()
247 return protocol_negotiated_
;
250 bool HttpStreamFactoryImpl::Request::using_spdy() const {
256 HttpStreamFactoryImpl::Request::RemoveRequestFromSpdySessionRequestMap() {
257 if (spdy_session_key_
.get()) {
258 SpdySessionRequestMap
& spdy_session_request_map
=
259 factory_
->spdy_session_request_map_
;
260 DCHECK(ContainsKey(spdy_session_request_map
, *spdy_session_key_
));
261 RequestSet
& request_set
=
262 spdy_session_request_map
[*spdy_session_key_
];
263 DCHECK(ContainsKey(request_set
, this));
264 request_set
.erase(this);
265 if (request_set
.empty())
266 spdy_session_request_map
.erase(*spdy_session_key_
);
267 spdy_session_key_
.reset();
272 HttpStreamFactoryImpl::Request::RemoveRequestFromHttpPipeliningRequestMap() {
273 if (http_pipelining_key_
.get()) {
274 HttpPipeliningRequestMap
& http_pipelining_request_map
=
275 factory_
->http_pipelining_request_map_
;
276 DCHECK(ContainsKey(http_pipelining_request_map
, *http_pipelining_key_
));
277 RequestVector
& request_vector
=
278 http_pipelining_request_map
[*http_pipelining_key_
];
279 for (RequestVector::iterator it
= request_vector
.begin();
280 it
!= request_vector
.end(); ++it
) {
282 request_vector
.erase(it
);
286 if (request_vector
.empty())
287 http_pipelining_request_map
.erase(*http_pipelining_key_
);
288 http_pipelining_key_
.reset();
292 void HttpStreamFactoryImpl::Request::OnNewSpdySessionReady(
294 const base::WeakPtr
<SpdySession
>& spdy_session
,
297 DCHECK(job
->using_spdy());
299 // The first case is the usual case.
300 if (!bound_job_
.get()) {
301 OrphanJobsExcept(job
);
302 } else { // This is the case for HTTPS proxy tunneling.
303 DCHECK_EQ(bound_job_
.get(), job
);
304 DCHECK(jobs_
.empty());
307 // Cache these values in case the job gets deleted.
308 const SSLConfig used_ssl_config
= job
->server_ssl_config();
309 const ProxyInfo used_proxy_info
= job
->proxy_info();
310 const bool was_npn_negotiated
= job
->was_npn_negotiated();
311 const NextProto protocol_negotiated
=
312 job
->protocol_negotiated();
313 const bool using_spdy
= job
->using_spdy();
314 const BoundNetLog net_log
= job
->net_log();
316 Complete(was_npn_negotiated
, protocol_negotiated
, using_spdy
, net_log
);
318 // Cache this so we can still use it if the request is deleted.
319 HttpStreamFactoryImpl
* factory
= factory_
;
320 if (factory
->for_websockets_
) {
321 // TODO(ricea): Re-instate this code when WebSockets over SPDY is
325 bool use_relative_url
= direct
|| url().SchemeIs("https");
326 delegate_
->OnStreamReady(
327 job
->server_ssl_config(),
329 new SpdyHttpStream(spdy_session
, use_relative_url
));
331 // |this| may be deleted after this point.
332 factory
->OnNewSpdySessionReady(spdy_session
,
342 void HttpStreamFactoryImpl::Request::OrphanJobsExcept(Job
* job
) {
344 DCHECK(!bound_job_
.get());
345 DCHECK(ContainsKey(jobs_
, job
));
346 bound_job_
.reset(job
);
348 factory_
->request_map_
.erase(job
);
353 void HttpStreamFactoryImpl::Request::OrphanJobs() {
354 RemoveRequestFromSpdySessionRequestMap();
355 RemoveRequestFromHttpPipeliningRequestMap();
360 for (std::set
<Job
*>::iterator it
= tmp
.begin(); it
!= tmp
.end(); ++it
)
361 factory_
->OrphanJob(*it
, this);
364 void HttpStreamFactoryImpl::Request::OnJobSucceeded(Job
* job
) {
365 // |job| should only be NULL if we're being serviced by a late bound
366 // SpdySession or HttpPipelinedConnection (one that was not created by a job
367 // in our |jobs_| set).
369 DCHECK(!bound_job_
.get());
370 DCHECK(!jobs_
.empty());
371 // NOTE(willchan): We do *NOT* call OrphanJobs() here. The reason is because
372 // we *WANT* to cancel the unnecessary Jobs from other requests if another
373 // Job completes first.
374 // TODO(mbelshe): Revisit this when we implement ip connection pooling of
375 // SpdySessions. Do we want to orphan the jobs for a different hostname so
376 // they complete? Or do we want to prevent connecting a new SpdySession if
377 // we've already got one available for a different hostname where the ip
378 // address matches up?
379 } else if (!bound_job_
.get()) {
380 // We may have other jobs in |jobs_|. For example, if we start multiple jobs
381 // for Alternate-Protocol.
382 OrphanJobsExcept(job
);
384 DCHECK(jobs_
.empty());