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.
4
5 #include "net/http/http_network_transaction.h"
6
7 #include <set>
8 #include <utility>
9 #include <vector>
10
11 #include "base/base64url.h"
12 #include "base/bind.h"
13 #include "base/callback_helpers.h"
14 #include "base/compiler_specific.h"
15 #include "base/format_macros.h"
16 #include "base/metrics/field_trial.h"
17 #include "base/metrics/histogram_functions.h"
18 #include "base/metrics/histogram_macros.h"
19 #include "base/metrics/sparse_histogram.h"
20 #include "base/stl_util.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_util.h"
23 #include "base/time/time.h"
24 #include "base/values.h"
25 #include "build/build_config.h"
26 #include "net/base/auth.h"
27 #include "net/base/host_port_pair.h"
28 #include "net/base/io_buffer.h"
29 #include "net/base/load_flags.h"
30 #include "net/base/load_timing_info.h"
31 #include "net/base/net_errors.h"
32 #include "net/base/proxy_server.h"
33 #include "net/base/transport_info.h"
34 #include "net/base/upload_data_stream.h"
35 #include "net/base/url_util.h"
36 #include "net/cert/cert_status_flags.h"
37 #include "net/filter/filter_source_stream.h"
38 #include "net/http/bidirectional_stream_impl.h"
39 #include "net/http/http_auth.h"
40 #include "net/http/http_auth_handler.h"
41 #include "net/http/http_auth_handler_factory.h"
42 #include "net/http/http_basic_stream.h"
43 #include "net/http/http_chunked_decoder.h"
44 #include "net/http/http_log_util.h"
45 #include "net/http/http_network_session.h"
46 #include "net/http/http_proxy_client_socket.h"
47 #include "net/http/http_request_headers.h"
48 #include "net/http/http_request_info.h"
49 #include "net/http/http_response_headers.h"
50 #include "net/http/http_response_info.h"
51 #include "net/http/http_server_properties.h"
52 #include "net/http/http_status_code.h"
53 #include "net/http/http_stream.h"
54 #include "net/http/http_stream_factory.h"
55 #include "net/http/http_util.h"
56 #include "net/http/transport_security_state.h"
57 #include "net/http/url_security_manager.h"
58 #include "net/log/net_log_event_type.h"
59 #include "net/socket/client_socket_factory.h"
60 #include "net/socket/next_proto.h"
61 #include "net/socket/transport_client_socket_pool.h"
62 #include "net/spdy/spdy_http_stream.h"
63 #include "net/spdy/spdy_session.h"
64 #include "net/spdy/spdy_session_pool.h"
65 #include "net/ssl/ssl_cert_request_info.h"
66 #include "net/ssl/ssl_connection_status_flags.h"
67 #include "net/ssl/ssl_info.h"
68 #include "net/ssl/ssl_private_key.h"
69 #include "url/gurl.h"
70 #include "url/scheme_host_port.h"
71 #include "url/url_canon.h"
72
73 #if BUILDFLAG(ENABLE_REPORTING)
74 #include "net/network_error_logging/network_error_logging_service.h"
75 #include "net/reporting/reporting_header_parser.h"
76 #include "net/reporting/reporting_service.h"
77 #endif // BUILDFLAG(ENABLE_REPORTING)
78
79 namespace net {
80
81 namespace {
82
83 // Max number of |retry_attempts| (excluding the initial request) after which
84 // we give up and show an error page.
85 const size_t kMaxRetryAttempts = 2;
86
87 // Max number of calls to RestartWith* allowed for a single connection. A single
88 // HttpNetworkTransaction should not signal very many restartable errors, but it
89 // may occur due to a bug (e.g. https://crbug.com/823387 or
90 // https://crbug.com/488043) or simply if the server or proxy requests
91 // authentication repeatedly. Although these calls are often associated with a
92 // user prompt, in other scenarios (remembered preferences, extensions,
93 // multi-leg authentication), they may be triggered automatically. To avoid
94 // looping forever, bound the number of restarts.
95 const size_t kMaxRestarts = 32;
96
SetProxyInfoInReponse(const ProxyInfo & proxy_info,HttpResponseInfo * response_info)97 void SetProxyInfoInReponse(const ProxyInfo& proxy_info,
98 HttpResponseInfo* response_info) {
99 response_info->was_fetched_via_proxy = !proxy_info.is_direct();
100 if (response_info->was_fetched_via_proxy && !proxy_info.is_empty())
101 response_info->proxy_server = proxy_info.proxy_server();
102 else if (!response_info->was_fetched_via_proxy && proxy_info.is_direct())
103 response_info->proxy_server = ProxyServer::Direct();
104 else
105 response_info->proxy_server = ProxyServer();
106 }
107
108 } // namespace
109
110 const int HttpNetworkTransaction::kDrainBodyBufferSize;
111
HttpNetworkTransaction(RequestPriority priority,HttpNetworkSession * session)112 HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
113 HttpNetworkSession* session)
114 : pending_auth_target_(HttpAuth::AUTH_NONE),
115 io_callback_(base::BindRepeating(&HttpNetworkTransaction::OnIOComplete,
116 base::Unretained(this))),
117 session_(session),
118 request_(nullptr),
119 priority_(priority),
120 headers_valid_(false),
121 can_send_early_data_(false),
122 configured_client_cert_for_server_(false),
123 request_headers_(),
124 #if BUILDFLAG(ENABLE_REPORTING)
125 network_error_logging_report_generated_(false),
126 request_reporting_upload_depth_(0),
127 #endif // BUILDFLAG(ENABLE_REPORTING)
128 read_buf_len_(0),
129 total_received_bytes_(0),
130 total_sent_bytes_(0),
131 next_state_(STATE_NONE),
132 establishing_tunnel_(false),
133 enable_ip_based_pooling_(true),
134 enable_alternative_services_(true),
135 websocket_handshake_stream_base_create_helper_(nullptr),
136 net_error_details_(),
137 retry_attempts_(0),
138 num_restarts_(0) {
139 }
140
~HttpNetworkTransaction()141 HttpNetworkTransaction::~HttpNetworkTransaction() {
142 #if BUILDFLAG(ENABLE_REPORTING)
143 // If no error or success report has been generated yet at this point, then
144 // this network transaction was prematurely cancelled.
145 GenerateNetworkErrorLoggingReport(ERR_ABORTED);
146 #endif // BUILDFLAG(ENABLE_REPORTING)
147 if (stream_.get()) {
148 // TODO(mbelshe): The stream_ should be able to compute whether or not the
149 // stream should be kept alive. No reason to compute here
150 // and pass it in.
151 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
152 stream_->Close(true /* not reusable */);
153 } else if (stream_->IsResponseBodyComplete()) {
154 // If the response body is complete, we can just reuse the socket.
155 stream_->Close(false /* reusable */);
156 } else {
157 // Otherwise, we try to drain the response body.
158 HttpStream* stream = stream_.release();
159 stream->Drain(session_);
160 }
161 }
162 if (request_ && request_->upload_data_stream)
163 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
164 }
165
Start(const HttpRequestInfo * request_info,CompletionOnceCallback callback,const NetLogWithSource & net_log)166 int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
167 CompletionOnceCallback callback,
168 const NetLogWithSource& net_log) {
169 if (request_info->load_flags & LOAD_ONLY_FROM_CACHE)
170 return ERR_CACHE_MISS;
171
172 DCHECK(request_info->traffic_annotation.is_valid());
173 net_log_ = net_log;
174 request_ = request_info;
175 url_ = request_->url;
176 network_isolation_key_ = request_->network_isolation_key;
177 #if BUILDFLAG(ENABLE_REPORTING)
178 // Store values for later use in NEL report generation.
179 request_method_ = request_->method;
180 request_->extra_headers.GetHeader(HttpRequestHeaders::kReferer,
181 &request_referrer_);
182 request_->extra_headers.GetHeader(HttpRequestHeaders::kUserAgent,
183 &request_user_agent_);
184 request_reporting_upload_depth_ = request_->reporting_upload_depth;
185 start_timeticks_ = base::TimeTicks::Now();
186 #endif // BUILDFLAG(ENABLE_REPORTING)
187
188 session_->GetSSLConfig(&server_ssl_config_, &proxy_ssl_config_);
189
190 if (request_->load_flags & LOAD_DISABLE_CERT_NETWORK_FETCHES) {
191 server_ssl_config_.disable_cert_verification_network_fetches = true;
192 proxy_ssl_config_.disable_cert_verification_network_fetches = true;
193 }
194
195 if (request_->idempotency == IDEMPOTENT ||
196 (request_->idempotency == DEFAULT_IDEMPOTENCY &&
197 HttpUtil::IsMethodSafe(request_info->method))) {
198 can_send_early_data_ = true;
199 }
200
201 if (request_->load_flags & LOAD_PREFETCH) {
202 response_.unused_since_prefetch = true;
203 }
204
205 if (request_->load_flags & LOAD_RESTRICTED_PREFETCH) {
206 DCHECK(response_.unused_since_prefetch);
207 response_.restricted_prefetch = true;
208 }
209
210 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
211 int rv = DoLoop(OK);
212 if (rv == ERR_IO_PENDING)
213 callback_ = std::move(callback);
214
215 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
216 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
217 // other net::Error can be returned.
218 DCHECK_EQ(rv, ERR_IO_PENDING);
219 return rv;
220 }
221
RestartIgnoringLastError(CompletionOnceCallback callback)222 int HttpNetworkTransaction::RestartIgnoringLastError(
223 CompletionOnceCallback callback) {
224 DCHECK(!stream_.get());
225 DCHECK(!stream_request_.get());
226 DCHECK_EQ(STATE_NONE, next_state_);
227
228 if (!CheckMaxRestarts())
229 return ERR_TOO_MANY_RETRIES;
230
231 next_state_ = STATE_CREATE_STREAM;
232
233 int rv = DoLoop(OK);
234 if (rv == ERR_IO_PENDING)
235 callback_ = std::move(callback);
236
237 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
238 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
239 // other net::Error can be returned.
240 DCHECK_EQ(rv, ERR_IO_PENDING);
241 return rv;
242 }
243
RestartWithCertificate(scoped_refptr<X509Certificate> client_cert,scoped_refptr<SSLPrivateKey> client_private_key,CompletionOnceCallback callback)244 int HttpNetworkTransaction::RestartWithCertificate(
245 scoped_refptr<X509Certificate> client_cert,
246 scoped_refptr<SSLPrivateKey> client_private_key,
247 CompletionOnceCallback callback) {
248 // When we receive ERR_SSL_CLIENT_AUTH_CERT_NEEDED, we always tear down
249 // existing streams and stream requests to force a new connection.
250 DCHECK(!stream_request_.get());
251 DCHECK(!stream_.get());
252 DCHECK_EQ(STATE_NONE, next_state_);
253
254 if (!CheckMaxRestarts())
255 return ERR_TOO_MANY_RETRIES;
256
257 // Add the credentials to the client auth cache. The next stream request will
258 // then pick them up.
259 session_->ssl_client_context()->SetClientCertificate(
260 response_.cert_request_info->host_and_port, std::move(client_cert),
261 std::move(client_private_key));
262
263 if (!response_.cert_request_info->is_proxy)
264 configured_client_cert_for_server_ = true;
265
266 // Reset the other member variables.
267 // Note: this is necessary only with SSL renegotiation.
268 ResetStateForRestart();
269 next_state_ = STATE_CREATE_STREAM;
270 int rv = DoLoop(OK);
271 if (rv == ERR_IO_PENDING)
272 callback_ = std::move(callback);
273
274 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
275 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
276 // other net::Error can be returned.
277 DCHECK_EQ(rv, ERR_IO_PENDING);
278 return rv;
279 }
280
RestartWithAuth(const AuthCredentials & credentials,CompletionOnceCallback callback)281 int HttpNetworkTransaction::RestartWithAuth(const AuthCredentials& credentials,
282 CompletionOnceCallback callback) {
283 if (!CheckMaxRestarts())
284 return ERR_TOO_MANY_RETRIES;
285
286 HttpAuth::Target target = pending_auth_target_;
287 if (target == HttpAuth::AUTH_NONE) {
288 NOTREACHED();
289 return ERR_UNEXPECTED;
290 }
291 pending_auth_target_ = HttpAuth::AUTH_NONE;
292
293 auth_controllers_[target]->ResetAuth(credentials);
294
295 DCHECK(callback_.is_null());
296
297 int rv = OK;
298 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
299 // In this case, we've gathered credentials for use with proxy
300 // authentication of a tunnel.
301 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
302 DCHECK(stream_request_ != nullptr);
303 auth_controllers_[target] = nullptr;
304 ResetStateForRestart();
305 rv = stream_request_->RestartTunnelWithProxyAuth();
306 } else {
307 // In this case, we've gathered credentials for the server or the proxy
308 // but it is not during the tunneling phase.
309 DCHECK(stream_request_ == nullptr);
310 PrepareForAuthRestart(target);
311 rv = DoLoop(OK);
312 // Note: If an error is encountered while draining the old response body, no
313 // Network Error Logging report will be generated, because the error was
314 // with the old request, which will already have had a NEL report generated
315 // for it due to the auth challenge (so we don't report a second error for
316 // that request).
317 }
318
319 if (rv == ERR_IO_PENDING)
320 callback_ = std::move(callback);
321 return rv;
322 }
323
PrepareForAuthRestart(HttpAuth::Target target)324 void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
325 DCHECK(HaveAuth(target));
326 DCHECK(!stream_request_.get());
327
328 // Authorization schemes incompatible with HTTP/2 are unsupported for proxies.
329 if (target == HttpAuth::AUTH_SERVER &&
330 auth_controllers_[target]->NeedsHTTP11()) {
331 session_->http_server_properties()->SetHTTP11Required(
332 url::SchemeHostPort(request_->url), network_isolation_key_);
333 }
334
335 bool keep_alive = false;
336 // Even if the server says the connection is keep-alive, we have to be
337 // able to find the end of each response in order to reuse the connection.
338 if (stream_->CanReuseConnection()) {
339 // If the response body hasn't been completely read, we need to drain
340 // it first.
341 if (!stream_->IsResponseBodyComplete()) {
342 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
343 read_buf_ = base::MakeRefCounted<IOBuffer>(
344 kDrainBodyBufferSize); // A bit bucket.
345 read_buf_len_ = kDrainBodyBufferSize;
346 return;
347 }
348 keep_alive = true;
349 }
350
351 // We don't need to drain the response body, so we act as if we had drained
352 // the response body.
353 DidDrainBodyForAuthRestart(keep_alive);
354 }
355
DidDrainBodyForAuthRestart(bool keep_alive)356 void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
357 DCHECK(!stream_request_.get());
358
359 if (stream_.get()) {
360 total_received_bytes_ += stream_->GetTotalReceivedBytes();
361 total_sent_bytes_ += stream_->GetTotalSentBytes();
362 HttpStream* new_stream = nullptr;
363 if (keep_alive && stream_->CanReuseConnection()) {
364 // We should call connection_->set_idle_time(), but this doesn't occur
365 // often enough to be worth the trouble.
366 stream_->SetConnectionReused();
367 new_stream = stream_->RenewStreamForAuth();
368 }
369
370 if (!new_stream) {
371 // Close the stream and mark it as not_reusable. Even in the
372 // keep_alive case, we've determined that the stream_ is not
373 // reusable if new_stream is NULL.
374 stream_->Close(true);
375 next_state_ = STATE_CREATE_STREAM;
376 } else {
377 // Renewed streams shouldn't carry over sent or received bytes.
378 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
379 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
380 next_state_ = STATE_INIT_STREAM;
381 }
382 stream_.reset(new_stream);
383 }
384
385 // Reset the other member variables.
386 ResetStateForAuthRestart();
387 }
388
IsReadyToRestartForAuth()389 bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
390 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
391 HaveAuth(pending_auth_target_);
392 }
393
Read(IOBuffer * buf,int buf_len,CompletionOnceCallback callback)394 int HttpNetworkTransaction::Read(IOBuffer* buf,
395 int buf_len,
396 CompletionOnceCallback callback) {
397 DCHECK(buf);
398 DCHECK_LT(0, buf_len);
399
400 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
401 if (headers_valid_ && headers.get() && stream_request_.get()) {
402 // We're trying to read the body of the response but we're still trying
403 // to establish an SSL tunnel through an HTTP proxy. We can't read these
404 // bytes when establishing a tunnel because they might be controlled by
405 // an active network attacker. We don't worry about this for HTTP
406 // because an active network attacker can already control HTTP sessions.
407 // We reach this case when the user cancels a 407 proxy auth prompt. We
408 // also don't worry about this for an HTTPS Proxy, because the
409 // communication with the proxy is secure.
410 // See http://crbug.com/8473.
411 DCHECK(proxy_info_.is_http_like());
412 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
413 return ERR_TUNNEL_CONNECTION_FAILED;
414 }
415
416 // Are we using SPDY or HTTP?
417 next_state_ = STATE_READ_BODY;
418
419 read_buf_ = buf;
420 read_buf_len_ = buf_len;
421
422 int rv = DoLoop(OK);
423 if (rv == ERR_IO_PENDING)
424 callback_ = std::move(callback);
425 return rv;
426 }
427
StopCaching()428 void HttpNetworkTransaction::StopCaching() {}
429
GetTotalReceivedBytes() const430 int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
431 int64_t total_received_bytes = total_received_bytes_;
432 if (stream_)
433 total_received_bytes += stream_->GetTotalReceivedBytes();
434 return total_received_bytes;
435 }
436
GetTotalSentBytes() const437 int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
438 int64_t total_sent_bytes = total_sent_bytes_;
439 if (stream_)
440 total_sent_bytes += stream_->GetTotalSentBytes();
441 return total_sent_bytes;
442 }
443
DoneReading()444 void HttpNetworkTransaction::DoneReading() {}
445
GetResponseInfo() const446 const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
447 return &response_;
448 }
449
GetLoadState() const450 LoadState HttpNetworkTransaction::GetLoadState() const {
451 // TODO(wtc): Define a new LoadState value for the
452 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
453 switch (next_state_) {
454 case STATE_CREATE_STREAM:
455 return LOAD_STATE_WAITING_FOR_DELEGATE;
456 case STATE_CREATE_STREAM_COMPLETE:
457 return stream_request_->GetLoadState();
458 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
459 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
460 case STATE_SEND_REQUEST_COMPLETE:
461 return LOAD_STATE_SENDING_REQUEST;
462 case STATE_READ_HEADERS_COMPLETE:
463 return LOAD_STATE_WAITING_FOR_RESPONSE;
464 case STATE_READ_BODY_COMPLETE:
465 return LOAD_STATE_READING_RESPONSE;
466 default:
467 return LOAD_STATE_IDLE;
468 }
469 }
470
SetQuicServerInfo(QuicServerInfo * quic_server_info)471 void HttpNetworkTransaction::SetQuicServerInfo(
472 QuicServerInfo* quic_server_info) {}
473
GetLoadTimingInfo(LoadTimingInfo * load_timing_info) const474 bool HttpNetworkTransaction::GetLoadTimingInfo(
475 LoadTimingInfo* load_timing_info) const {
476 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
477 return false;
478
479 load_timing_info->proxy_resolve_start =
480 proxy_info_.proxy_resolve_start_time();
481 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
482 load_timing_info->send_start = send_start_time_;
483 load_timing_info->send_end = send_end_time_;
484 return true;
485 }
486
GetRemoteEndpoint(IPEndPoint * endpoint) const487 bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
488 if (remote_endpoint_.address().empty())
489 return false;
490
491 *endpoint = remote_endpoint_;
492 return true;
493 }
494
PopulateNetErrorDetails(NetErrorDetails * details) const495 void HttpNetworkTransaction::PopulateNetErrorDetails(
496 NetErrorDetails* details) const {
497 *details = net_error_details_;
498 if (stream_)
499 stream_->PopulateNetErrorDetails(details);
500 }
501
SetPriority(RequestPriority priority)502 void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
503 priority_ = priority;
504
505 if (stream_request_)
506 stream_request_->SetPriority(priority);
507 if (stream_)
508 stream_->SetPriority(priority);
509
510 // The above call may have resulted in deleting |*this|.
511 }
512
SetWebSocketHandshakeStreamCreateHelper(WebSocketHandshakeStreamBase::CreateHelper * create_helper)513 void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
514 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
515 websocket_handshake_stream_base_create_helper_ = create_helper;
516 }
517
SetBeforeNetworkStartCallback(BeforeNetworkStartCallback callback)518 void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
519 BeforeNetworkStartCallback callback) {
520 before_network_start_callback_ = std::move(callback);
521 }
522
SetConnectedCallback(const ConnectedCallback & callback)523 void HttpNetworkTransaction::SetConnectedCallback(
524 const ConnectedCallback& callback) {
525 connected_callback_ = callback;
526 }
527
SetRequestHeadersCallback(RequestHeadersCallback callback)528 void HttpNetworkTransaction::SetRequestHeadersCallback(
529 RequestHeadersCallback callback) {
530 DCHECK(!stream_);
531 request_headers_callback_ = std::move(callback);
532 }
533
SetResponseHeadersCallback(ResponseHeadersCallback callback)534 void HttpNetworkTransaction::SetResponseHeadersCallback(
535 ResponseHeadersCallback callback) {
536 DCHECK(!stream_);
537 response_headers_callback_ = std::move(callback);
538 }
539
ResumeNetworkStart()540 int HttpNetworkTransaction::ResumeNetworkStart() {
541 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
542 return DoLoop(OK);
543 }
544
OnStreamReady(const SSLConfig & used_ssl_config,const ProxyInfo & used_proxy_info,std::unique_ptr<HttpStream> stream)545 void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
546 const ProxyInfo& used_proxy_info,
547 std::unique_ptr<HttpStream> stream) {
548 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
549 DCHECK(stream_request_.get());
550
551 if (stream_) {
552 total_received_bytes_ += stream_->GetTotalReceivedBytes();
553 total_sent_bytes_ += stream_->GetTotalSentBytes();
554 }
555 stream_ = std::move(stream);
556 stream_->SetRequestHeadersCallback(request_headers_callback_);
557 server_ssl_config_ = used_ssl_config;
558 proxy_info_ = used_proxy_info;
559 response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
560 response_.alpn_negotiated_protocol =
561 NextProtoToString(stream_request_->negotiated_protocol());
562 response_.was_fetched_via_spdy = stream_request_->using_spdy();
563 SetProxyInfoInReponse(used_proxy_info, &response_);
564 OnIOComplete(OK);
565 }
566
OnBidirectionalStreamImplReady(const SSLConfig & used_ssl_config,const ProxyInfo & used_proxy_info,std::unique_ptr<BidirectionalStreamImpl> stream)567 void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
568 const SSLConfig& used_ssl_config,
569 const ProxyInfo& used_proxy_info,
570 std::unique_ptr<BidirectionalStreamImpl> stream) {
571 NOTREACHED();
572 }
573
OnWebSocketHandshakeStreamReady(const SSLConfig & used_ssl_config,const ProxyInfo & used_proxy_info,std::unique_ptr<WebSocketHandshakeStreamBase> stream)574 void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
575 const SSLConfig& used_ssl_config,
576 const ProxyInfo& used_proxy_info,
577 std::unique_ptr<WebSocketHandshakeStreamBase> stream) {
578 OnStreamReady(used_ssl_config, used_proxy_info, std::move(stream));
579 }
580
OnStreamFailed(int result,const NetErrorDetails & net_error_details,const SSLConfig & used_ssl_config,const ProxyInfo & used_proxy_info,ResolveErrorInfo resolve_error_info)581 void HttpNetworkTransaction::OnStreamFailed(
582 int result,
583 const NetErrorDetails& net_error_details,
584 const SSLConfig& used_ssl_config,
585 const ProxyInfo& used_proxy_info,
586 ResolveErrorInfo resolve_error_info) {
587 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
588 DCHECK_NE(OK, result);
589 DCHECK(stream_request_.get());
590 DCHECK(!stream_.get());
591 server_ssl_config_ = used_ssl_config;
592 net_error_details_ = net_error_details;
593 proxy_info_ = used_proxy_info;
594 SetProxyInfoInReponse(used_proxy_info, &response_);
595 response_.resolve_error_info = resolve_error_info;
596
597 OnIOComplete(result);
598 }
599
OnCertificateError(int result,const SSLConfig & used_ssl_config,const SSLInfo & ssl_info)600 void HttpNetworkTransaction::OnCertificateError(
601 int result,
602 const SSLConfig& used_ssl_config,
603 const SSLInfo& ssl_info) {
604 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
605 DCHECK_NE(OK, result);
606 DCHECK(stream_request_.get());
607 DCHECK(!stream_.get());
608
609 response_.ssl_info = ssl_info;
610 server_ssl_config_ = used_ssl_config;
611
612 // TODO(mbelshe): For now, we're going to pass the error through, and that
613 // will close the stream_request in all cases. This means that we're always
614 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
615 // good and the user chooses to ignore the error. This is not ideal, but not
616 // the end of the world either.
617
618 OnIOComplete(result);
619 }
620
OnNeedsProxyAuth(const HttpResponseInfo & proxy_response,const SSLConfig & used_ssl_config,const ProxyInfo & used_proxy_info,HttpAuthController * auth_controller)621 void HttpNetworkTransaction::OnNeedsProxyAuth(
622 const HttpResponseInfo& proxy_response,
623 const SSLConfig& used_ssl_config,
624 const ProxyInfo& used_proxy_info,
625 HttpAuthController* auth_controller) {
626 DCHECK(stream_request_.get());
627 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
628
629 establishing_tunnel_ = true;
630 response_.headers = proxy_response.headers;
631 response_.auth_challenge = proxy_response.auth_challenge;
632 response_.did_use_http_auth = proxy_response.did_use_http_auth;
633
634 if (response_.headers.get() && !ContentEncodingsValid()) {
635 DoCallback(ERR_CONTENT_DECODING_FAILED);
636 return;
637 }
638
639 headers_valid_ = true;
640 server_ssl_config_ = used_ssl_config;
641 proxy_info_ = used_proxy_info;
642
643 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
644 pending_auth_target_ = HttpAuth::AUTH_PROXY;
645
646 DoCallback(OK);
647 }
648
OnNeedsClientAuth(const SSLConfig & used_ssl_config,SSLCertRequestInfo * cert_info)649 void HttpNetworkTransaction::OnNeedsClientAuth(
650 const SSLConfig& used_ssl_config,
651 SSLCertRequestInfo* cert_info) {
652 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
653
654 server_ssl_config_ = used_ssl_config;
655 response_.cert_request_info = cert_info;
656 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
657 }
658
OnQuicBroken()659 void HttpNetworkTransaction::OnQuicBroken() {
660 net_error_details_.quic_broken = true;
661 }
662
GetConnectionAttempts(ConnectionAttempts * out) const663 void HttpNetworkTransaction::GetConnectionAttempts(
664 ConnectionAttempts* out) const {
665 *out = connection_attempts_;
666 }
667
IsSecureRequest() const668 bool HttpNetworkTransaction::IsSecureRequest() const {
669 return request_->url.SchemeIsCryptographic();
670 }
671
UsingHttpProxyWithoutTunnel() const672 bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
673 return proxy_info_.is_http_like() &&
674 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
675 }
676
DoCallback(int rv)677 void HttpNetworkTransaction::DoCallback(int rv) {
678 DCHECK_NE(rv, ERR_IO_PENDING);
679 DCHECK(!callback_.is_null());
680
681 #if BUILDFLAG(ENABLE_REPORTING)
682 // Just before invoking the caller's completion callback, generate a NEL
683 // report about this network request if the result was an error.
684 GenerateNetworkErrorLoggingReportIfError(rv);
685 #endif // BUILDFLAG(ENABLE_REPORTING)
686
687 // Since Run may result in Read being called, clear user_callback_ up front.
688 std::move(callback_).Run(rv);
689 }
690
OnIOComplete(int result)691 void HttpNetworkTransaction::OnIOComplete(int result) {
692 int rv = DoLoop(result);
693 if (rv != ERR_IO_PENDING)
694 DoCallback(rv);
695 }
696
DoLoop(int result)697 int HttpNetworkTransaction::DoLoop(int result) {
698 DCHECK(next_state_ != STATE_NONE);
699
700 int rv = result;
701 do {
702 State state = next_state_;
703 next_state_ = STATE_NONE;
704 switch (state) {
705 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
706 DCHECK_EQ(OK, rv);
707 rv = DoNotifyBeforeCreateStream();
708 break;
709 case STATE_CREATE_STREAM:
710 DCHECK_EQ(OK, rv);
711 rv = DoCreateStream();
712 break;
713 case STATE_CREATE_STREAM_COMPLETE:
714 rv = DoCreateStreamComplete(rv);
715 break;
716 case STATE_INIT_STREAM:
717 DCHECK_EQ(OK, rv);
718 rv = DoInitStream();
719 break;
720 case STATE_INIT_STREAM_COMPLETE:
721 rv = DoInitStreamComplete(rv);
722 break;
723 case STATE_GENERATE_PROXY_AUTH_TOKEN:
724 DCHECK_EQ(OK, rv);
725 rv = DoGenerateProxyAuthToken();
726 break;
727 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
728 rv = DoGenerateProxyAuthTokenComplete(rv);
729 break;
730 case STATE_GENERATE_SERVER_AUTH_TOKEN:
731 DCHECK_EQ(OK, rv);
732 rv = DoGenerateServerAuthToken();
733 break;
734 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
735 rv = DoGenerateServerAuthTokenComplete(rv);
736 break;
737 case STATE_INIT_REQUEST_BODY:
738 DCHECK_EQ(OK, rv);
739 rv = DoInitRequestBody();
740 break;
741 case STATE_INIT_REQUEST_BODY_COMPLETE:
742 rv = DoInitRequestBodyComplete(rv);
743 break;
744 case STATE_BUILD_REQUEST:
745 DCHECK_EQ(OK, rv);
746 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
747 rv = DoBuildRequest();
748 break;
749 case STATE_BUILD_REQUEST_COMPLETE:
750 rv = DoBuildRequestComplete(rv);
751 break;
752 case STATE_SEND_REQUEST:
753 DCHECK_EQ(OK, rv);
754 rv = DoSendRequest();
755 break;
756 case STATE_SEND_REQUEST_COMPLETE:
757 rv = DoSendRequestComplete(rv);
758 net_log_.EndEventWithNetErrorCode(
759 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
760 break;
761 case STATE_READ_HEADERS:
762 DCHECK_EQ(OK, rv);
763 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
764 rv = DoReadHeaders();
765 break;
766 case STATE_READ_HEADERS_COMPLETE:
767 rv = DoReadHeadersComplete(rv);
768 net_log_.EndEventWithNetErrorCode(
769 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
770 break;
771 case STATE_READ_BODY:
772 DCHECK_EQ(OK, rv);
773 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
774 rv = DoReadBody();
775 break;
776 case STATE_READ_BODY_COMPLETE:
777 rv = DoReadBodyComplete(rv);
778 net_log_.EndEventWithNetErrorCode(
779 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
780 break;
781 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
782 DCHECK_EQ(OK, rv);
783 net_log_.BeginEvent(
784 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
785 rv = DoDrainBodyForAuthRestart();
786 break;
787 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
788 rv = DoDrainBodyForAuthRestartComplete(rv);
789 net_log_.EndEventWithNetErrorCode(
790 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
791 break;
792 default:
793 NOTREACHED() << "bad state";
794 rv = ERR_FAILED;
795 break;
796 }
797 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
798
799 return rv;
800 }
801
DoNotifyBeforeCreateStream()802 int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
803 next_state_ = STATE_CREATE_STREAM;
804 bool defer = false;
805 if (!before_network_start_callback_.is_null())
806 std::move(before_network_start_callback_).Run(&defer);
807 if (!defer)
808 return OK;
809 return ERR_IO_PENDING;
810 }
811
DoCreateStream()812 int HttpNetworkTransaction::DoCreateStream() {
813 response_.network_accessed = true;
814
815 next_state_ = STATE_CREATE_STREAM_COMPLETE;
816 // IP based pooling is only enabled on a retry after 421 Misdirected Request
817 // is received. Alternative Services are also disabled in this case (though
818 // they can also be disabled when retrying after a QUIC error).
819 if (!enable_ip_based_pooling_)
820 DCHECK(!enable_alternative_services_);
821 if (ForWebSocketHandshake()) {
822 stream_request_ =
823 session_->http_stream_factory()->RequestWebSocketHandshakeStream(
824 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
825 websocket_handshake_stream_base_create_helper_,
826 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
827 } else {
828 stream_request_ = session_->http_stream_factory()->RequestStream(
829 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
830 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
831 }
832 DCHECK(stream_request_.get());
833 return ERR_IO_PENDING;
834 }
835
DoCreateStreamComplete(int result)836 int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
837 CopyConnectionAttemptsFromStreamRequest();
838 if (result == OK) {
839 next_state_ = STATE_INIT_STREAM;
840 DCHECK(stream_.get());
841 } else if (result == ERR_HTTP_1_1_REQUIRED ||
842 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
843 return HandleHttp11Required(result);
844 }
845
846 // Handle possible client certificate errors that may have occurred if the
847 // stream used SSL for one or more of the layers.
848 result = HandleSSLClientAuthError(result);
849
850 // At this point we are done with the stream_request_.
851 stream_request_.reset();
852 return result;
853 }
854
DoInitStream()855 int HttpNetworkTransaction::DoInitStream() {
856 DCHECK(stream_.get());
857 next_state_ = STATE_INIT_STREAM_COMPLETE;
858
859 stream_->GetRemoteEndpoint(&remote_endpoint_);
860
861 return stream_->InitializeStream(request_, can_send_early_data_, priority_,
862 net_log_, io_callback_);
863 }
864
DoInitStreamComplete(int result)865 int HttpNetworkTransaction::DoInitStreamComplete(int result) {
866 if (result != OK) {
867 if (result < 0)
868 result = HandleIOError(result);
869
870 // The stream initialization failed, so this stream will never be useful.
871 if (stream_) {
872 total_received_bytes_ += stream_->GetTotalReceivedBytes();
873 total_sent_bytes_ += stream_->GetTotalSentBytes();
874 }
875 CacheNetErrorDetailsAndResetStream();
876
877 return result;
878 }
879
880 // Fire off notification that we have successfully connected.
881 if (!connected_callback_.is_null()) {
882 TransportType type = TransportType::kDirect;
883 if (!proxy_info_.is_direct()) {
884 type = TransportType::kProxied;
885 }
886 result = connected_callback_.Run(TransportInfo(type, remote_endpoint_));
887 DCHECK_NE(result, ERR_IO_PENDING);
888 }
889
890 if (result == OK) {
891 // Only transition if we succeeded. Otherwise stop at STATE_NONE.
892 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
893 }
894 return result;
895 }
896
DoGenerateProxyAuthToken()897 int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
898 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
899 if (!ShouldApplyProxyAuth())
900 return OK;
901 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
902 if (!auth_controllers_[target].get())
903 auth_controllers_[target] = base::MakeRefCounted<HttpAuthController>(
904 target, AuthURL(target), request_->network_isolation_key,
905 session_->http_auth_cache(), session_->http_auth_handler_factory(),
906 session_->host_resolver());
907 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
908 io_callback_,
909 net_log_);
910 }
911
DoGenerateProxyAuthTokenComplete(int rv)912 int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
913 DCHECK_NE(ERR_IO_PENDING, rv);
914 if (rv == OK)
915 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
916 return rv;
917 }
918
DoGenerateServerAuthToken()919 int HttpNetworkTransaction::DoGenerateServerAuthToken() {
920 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
921 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
922 if (!auth_controllers_[target].get()) {
923 auth_controllers_[target] = base::MakeRefCounted<HttpAuthController>(
924 target, AuthURL(target), request_->network_isolation_key,
925 session_->http_auth_cache(), session_->http_auth_handler_factory(),
926 session_->host_resolver());
927 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
928 auth_controllers_[target]->DisableEmbeddedIdentity();
929 }
930 if (!ShouldApplyServerAuth())
931 return OK;
932 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
933 io_callback_,
934 net_log_);
935 }
936
DoGenerateServerAuthTokenComplete(int rv)937 int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
938 DCHECK_NE(ERR_IO_PENDING, rv);
939 if (rv == OK)
940 next_state_ = STATE_INIT_REQUEST_BODY;
941 return rv;
942 }
943
BuildRequestHeaders(bool using_http_proxy_without_tunnel)944 int HttpNetworkTransaction::BuildRequestHeaders(
945 bool using_http_proxy_without_tunnel) {
946 request_headers_.SetHeader(HttpRequestHeaders::kHost,
947 GetHostAndOptionalPort(request_->url));
948
949 // For compat with HTTP/1.0 servers and proxies:
950 if (using_http_proxy_without_tunnel) {
951 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
952 "keep-alive");
953 } else {
954 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
955 }
956
957 // Add a content length header?
958 if (request_->upload_data_stream) {
959 if (request_->upload_data_stream->is_chunked()) {
960 request_headers_.SetHeader(
961 HttpRequestHeaders::kTransferEncoding, "chunked");
962 } else {
963 request_headers_.SetHeader(
964 HttpRequestHeaders::kContentLength,
965 base::NumberToString(request_->upload_data_stream->size()));
966 }
967 } else if (request_->method == "POST" || request_->method == "PUT") {
968 // An empty POST/PUT request still needs a content length. As for HEAD,
969 // IE and Safari also add a content length header. Presumably it is to
970 // support sending a HEAD request to an URL that only expects to be sent a
971 // POST or some other method that normally would have a message body.
972 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
973 // specify that it should not be sent due to undefined behavior.
974 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
975 }
976
977 // Honor load flags that impact proxy caches.
978 if (request_->load_flags & LOAD_BYPASS_CACHE) {
979 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
980 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
981 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
982 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
983 }
984
985 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
986 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
987 &request_headers_);
988 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
989 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
990 &request_headers_);
991
992 request_headers_.MergeFrom(request_->extra_headers);
993
994 response_.did_use_http_auth =
995 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
996 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
997 return OK;
998 }
999
DoInitRequestBody()1000 int HttpNetworkTransaction::DoInitRequestBody() {
1001 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
1002 int rv = OK;
1003 if (request_->upload_data_stream)
1004 rv = request_->upload_data_stream->Init(
1005 base::BindOnce(&HttpNetworkTransaction::OnIOComplete,
1006 base::Unretained(this)),
1007 net_log_);
1008 return rv;
1009 }
1010
DoInitRequestBodyComplete(int result)1011 int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1012 if (result == OK)
1013 next_state_ = STATE_BUILD_REQUEST;
1014 return result;
1015 }
1016
DoBuildRequest()1017 int HttpNetworkTransaction::DoBuildRequest() {
1018 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
1019 headers_valid_ = false;
1020
1021 // This is constructed lazily (instead of within our Start method), so that
1022 // we have proxy info available.
1023 if (request_headers_.IsEmpty()) {
1024 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
1025 return BuildRequestHeaders(using_http_proxy_without_tunnel);
1026 }
1027
1028 return OK;
1029 }
1030
DoBuildRequestComplete(int result)1031 int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
1032 if (result == OK)
1033 next_state_ = STATE_SEND_REQUEST;
1034 return result;
1035 }
1036
DoSendRequest()1037 int HttpNetworkTransaction::DoSendRequest() {
1038 send_start_time_ = base::TimeTicks::Now();
1039 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1040
1041 return stream_->SendRequest(request_headers_, &response_, io_callback_);
1042 }
1043
DoSendRequestComplete(int result)1044 int HttpNetworkTransaction::DoSendRequestComplete(int result) {
1045 send_end_time_ = base::TimeTicks::Now();
1046
1047 if (result == ERR_HTTP_1_1_REQUIRED ||
1048 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1049 return HandleHttp11Required(result);
1050 }
1051
1052 if (result < 0)
1053 return HandleIOError(result);
1054 next_state_ = STATE_READ_HEADERS;
1055 return OK;
1056 }
1057
DoReadHeaders()1058 int HttpNetworkTransaction::DoReadHeaders() {
1059 next_state_ = STATE_READ_HEADERS_COMPLETE;
1060 return stream_->ReadResponseHeaders(io_callback_);
1061 }
1062
DoReadHeadersComplete(int result)1063 int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
1064 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1065 // due to SSL renegotiation.
1066 if (IsCertificateError(result)) {
1067 // We don't handle a certificate error during SSL renegotiation, so we
1068 // have to return an error that's not in the certificate error range
1069 // (-2xx).
1070 //
1071 // TODO(davidben): Remove this error. This is impossible now that server
1072 // certificates are forbidden from changing in renegotiation.
1073 LOG(ERROR) << "Got a server certificate with error " << result
1074 << " during SSL renegotiation";
1075 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1076 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1077 DCHECK(stream_.get());
1078 DCHECK(IsSecureRequest());
1079 response_.cert_request_info = base::MakeRefCounted<SSLCertRequestInfo>();
1080 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
1081 total_received_bytes_ += stream_->GetTotalReceivedBytes();
1082 total_sent_bytes_ += stream_->GetTotalSentBytes();
1083 stream_->Close(true);
1084 CacheNetErrorDetailsAndResetStream();
1085 }
1086
1087 if (result == ERR_HTTP_1_1_REQUIRED ||
1088 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1089 return HandleHttp11Required(result);
1090 }
1091
1092 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1093 // response headers were received, we do the best we can to make sense of it
1094 // and send it back up the stack.
1095 //
1096 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1097 // bizarre for SPDY. Assuming this logic is useful at all.
1098 // TODO(davidben): Bubble the error code up so we do not cache?
1099 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1100 result = OK;
1101
1102 if (result < 0)
1103 return HandleIOError(result);
1104
1105 DCHECK(response_.headers.get());
1106
1107 if (response_.headers.get() && !ContentEncodingsValid())
1108 return ERR_CONTENT_DECODING_FAILED;
1109
1110 // On a 408 response from the server ("Request Timeout") on a stale socket,
1111 // retry the request for HTTP/1.1 but not HTTP/2 or QUIC because those
1112 // multiplex requests and have no need for 408.
1113 // Headers can be NULL because of http://crbug.com/384554.
1114 if (response_.headers.get() &&
1115 response_.headers->response_code() == HTTP_REQUEST_TIMEOUT &&
1116 HttpResponseInfo::ConnectionInfoToCoarse(response_.connection_info) ==
1117 HttpResponseInfo::CONNECTION_INFO_COARSE_HTTP1 &&
1118 stream_->IsConnectionReused()) {
1119 #if BUILDFLAG(ENABLE_REPORTING)
1120 GenerateNetworkErrorLoggingReport(OK);
1121 #endif // BUILDFLAG(ENABLE_REPORTING)
1122 net_log_.AddEventWithNetErrorCode(
1123 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
1124 response_.headers->response_code());
1125 // This will close the socket - it would be weird to try and reuse it, even
1126 // if the server doesn't actually close it.
1127 ResetConnectionAndRequestForResend();
1128 return OK;
1129 }
1130
1131 NetLogResponseHeaders(net_log_,
1132 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
1133 response_.headers.get());
1134 if (response_headers_callback_)
1135 response_headers_callback_.Run(response_.headers);
1136
1137 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
1138 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1139 // indicates a buggy server. See:
1140 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1141 if (request_->method == "PUT")
1142 return ERR_METHOD_NOT_SUPPORTED;
1143 }
1144
1145 if (can_send_early_data_ && response_.headers.get() &&
1146 response_.headers->response_code() == HTTP_TOO_EARLY) {
1147 return HandleIOError(ERR_EARLY_DATA_REJECTED);
1148 }
1149
1150 // Check for an intermediate 100 Continue response. An origin server is
1151 // allowed to send this response even if we didn't ask for it, so we just
1152 // need to skip over it.
1153 // We treat any other 1xx in this same way (although in practice getting
1154 // a 1xx that isn't a 100 is rare).
1155 // Unless this is a WebSocket request, in which case we pass it on up.
1156 if (response_.headers->response_code() / 100 == 1 &&
1157 !ForWebSocketHandshake()) {
1158 response_.headers = new HttpResponseHeaders(std::string());
1159 next_state_ = STATE_READ_HEADERS;
1160 return OK;
1161 }
1162
1163 if (response_.headers->response_code() == 421 &&
1164 (enable_ip_based_pooling_ || enable_alternative_services_)) {
1165 #if BUILDFLAG(ENABLE_REPORTING)
1166 GenerateNetworkErrorLoggingReport(OK);
1167 #endif // BUILDFLAG(ENABLE_REPORTING)
1168 // Retry the request with both IP based pooling and Alternative Services
1169 // disabled.
1170 enable_ip_based_pooling_ = false;
1171 enable_alternative_services_ = false;
1172 net_log_.AddEvent(
1173 NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST);
1174 ResetConnectionAndRequestForResend();
1175 return OK;
1176 }
1177
1178 if (IsSecureRequest()) {
1179 stream_->GetSSLInfo(&response_.ssl_info);
1180 if (response_.ssl_info.is_valid() &&
1181 !IsCertStatusError(response_.ssl_info.cert_status)) {
1182 session_->http_stream_factory()->ProcessAlternativeServices(
1183 session_, network_isolation_key_, response_.headers.get(),
1184 url::SchemeHostPort(request_->url));
1185 }
1186 }
1187
1188 int rv = HandleAuthChallenge();
1189 if (rv != OK)
1190 return rv;
1191
1192 #if BUILDFLAG(ENABLE_REPORTING)
1193 // Note: Unless there is a pre-existing NEL policy for this origin, any NEL
1194 // reports generated before the NEL header is processed here will just be
1195 // dropped by the NetworkErrorLoggingService.
1196 ProcessReportToHeader();
1197 ProcessNetworkErrorLoggingHeader();
1198
1199 // Generate NEL report here if we have to report an HTTP error (4xx or 5xx
1200 // code), or if the response body will not be read, or on a redirect.
1201 // Note: This will report a success for a redirect even if an error is
1202 // encountered later while draining the body.
1203 int response_code = response_.headers->response_code();
1204 if ((response_code >= 400 && response_code < 600) ||
1205 response_code == HTTP_NO_CONTENT || response_code == HTTP_RESET_CONTENT ||
1206 response_code == HTTP_NOT_MODIFIED || request_->method == "HEAD" ||
1207 response_.headers->GetContentLength() == 0 ||
1208 response_.headers->IsRedirect(nullptr /* location */)) {
1209 GenerateNetworkErrorLoggingReport(OK);
1210 }
1211 #endif // BUILDFLAG(ENABLE_REPORTING)
1212
1213 headers_valid_ = true;
1214
1215 // We have reached the end of Start state machine, set the RequestInfo to
1216 // null.
1217 // RequestInfo is a member of the HttpTransaction's consumer and is useful
1218 // only until the final response headers are received. Clearing it will ensure
1219 // that HttpRequestInfo is only used up until final response headers are
1220 // received. Clearing is allowed so that the transaction can be disassociated
1221 // from its creating consumer in cases where it is shared for writing to the
1222 // cache. It is also safe to set it to null at this point since
1223 // upload_data_stream is also not used in the Read state machine.
1224 if (pending_auth_target_ == HttpAuth::AUTH_NONE)
1225 request_ = nullptr;
1226
1227 return OK;
1228 }
1229
DoReadBody()1230 int HttpNetworkTransaction::DoReadBody() {
1231 DCHECK(read_buf_.get());
1232 DCHECK_GT(read_buf_len_, 0);
1233 DCHECK(stream_ != nullptr);
1234
1235 next_state_ = STATE_READ_BODY_COMPLETE;
1236 return stream_->ReadResponseBody(
1237 read_buf_.get(), read_buf_len_, io_callback_);
1238 }
1239
DoReadBodyComplete(int result)1240 int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1241 // We are done with the Read call.
1242 bool done = false;
1243 if (result <= 0) {
1244 DCHECK_NE(ERR_IO_PENDING, result);
1245 done = true;
1246 }
1247
1248 // Clean up connection if we are done.
1249 if (done) {
1250 // Note: Just because IsResponseBodyComplete is true, we're not
1251 // necessarily "done". We're only "done" when it is the last
1252 // read on this HttpNetworkTransaction, which will be signified
1253 // by a zero-length read.
1254 // TODO(mbelshe): The keep-alive property is really a property of
1255 // the stream. No need to compute it here just to pass back
1256 // to the stream's Close function.
1257 bool keep_alive =
1258 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
1259
1260 stream_->Close(!keep_alive);
1261 // Note: we don't reset the stream here. We've closed it, but we still
1262 // need it around so that callers can call methods such as
1263 // GetUploadProgress() and have them be meaningful.
1264 // TODO(mbelshe): This means we closed the stream here, and we close it
1265 // again in ~HttpNetworkTransaction. Clean that up.
1266
1267 // The next Read call will return 0 (EOF).
1268
1269 // This transaction was successful. If it had been retried because of an
1270 // error with an alternative service, mark that alternative service broken.
1271 if (!enable_alternative_services_ &&
1272 retried_alternative_service_.protocol != kProtoUnknown) {
1273 HistogramBrokenAlternateProtocolLocation(
1274 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_NETWORK_TRANSACTION);
1275 session_->http_server_properties()->MarkAlternativeServiceBroken(
1276 retried_alternative_service_, network_isolation_key_);
1277 }
1278
1279 #if BUILDFLAG(ENABLE_REPORTING)
1280 GenerateNetworkErrorLoggingReport(result);
1281 #endif // BUILDFLAG(ENABLE_REPORTING)
1282 }
1283
1284 // Clear these to avoid leaving around old state.
1285 read_buf_ = nullptr;
1286 read_buf_len_ = 0;
1287
1288 return result;
1289 }
1290
DoDrainBodyForAuthRestart()1291 int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1292 // This method differs from DoReadBody only in the next_state_. So we just
1293 // call DoReadBody and override the next_state_. Perhaps there is a more
1294 // elegant way for these two methods to share code.
1295 int rv = DoReadBody();
1296 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1297 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1298 return rv;
1299 }
1300
1301 // TODO(wtc): This method and the DoReadBodyComplete method are almost
1302 // the same. Figure out a good way for these two methods to share code.
DoDrainBodyForAuthRestartComplete(int result)1303 int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
1304 // keep_alive defaults to true because the very reason we're draining the
1305 // response body is to reuse the connection for auth restart.
1306 bool done = false, keep_alive = true;
1307 if (result < 0) {
1308 // Error or closed connection while reading the socket.
1309 // Note: No Network Error Logging report is generated here because a report
1310 // will have already been generated for the original request due to the auth
1311 // challenge, so a second report is not generated for the same request here.
1312 done = true;
1313 keep_alive = false;
1314 } else if (stream_->IsResponseBodyComplete()) {
1315 done = true;
1316 }
1317
1318 if (done) {
1319 DidDrainBodyForAuthRestart(keep_alive);
1320 } else {
1321 // Keep draining.
1322 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1323 }
1324
1325 return OK;
1326 }
1327
1328 #if BUILDFLAG(ENABLE_REPORTING)
ProcessReportToHeader()1329 void HttpNetworkTransaction::ProcessReportToHeader() {
1330 std::string value;
1331 if (!response_.headers->GetNormalizedHeader("Report-To", &value))
1332 return;
1333
1334 ReportingService* reporting_service = session_->reporting_service();
1335 if (!reporting_service)
1336 return;
1337
1338 // Only accept Report-To headers on HTTPS connections that have no
1339 // certificate errors.
1340 if (!response_.ssl_info.is_valid())
1341 return;
1342 if (IsCertStatusError(response_.ssl_info.cert_status))
1343 return;
1344
1345 reporting_service->ProcessHeader(url_.GetOrigin(), network_isolation_key_,
1346 value);
1347 }
1348
ProcessNetworkErrorLoggingHeader()1349 void HttpNetworkTransaction::ProcessNetworkErrorLoggingHeader() {
1350 std::string value;
1351 if (!response_.headers->GetNormalizedHeader(
1352 NetworkErrorLoggingService::kHeaderName, &value)) {
1353 return;
1354 }
1355
1356 NetworkErrorLoggingService* network_error_logging_service =
1357 session_->network_error_logging_service();
1358 if (!network_error_logging_service)
1359 return;
1360
1361 // Don't accept NEL headers received via a proxy, because the IP address of
1362 // the destination server is not known.
1363 if (response_.was_fetched_via_proxy)
1364 return;
1365
1366 // Only accept NEL headers on HTTPS connections that have no certificate
1367 // errors.
1368 if (!response_.ssl_info.is_valid() ||
1369 IsCertStatusError(response_.ssl_info.cert_status)) {
1370 return;
1371 }
1372
1373 if (remote_endpoint_.address().empty())
1374 return;
1375
1376 network_error_logging_service->OnHeader(network_isolation_key_,
1377 url::Origin::Create(url_),
1378 remote_endpoint_.address(), value);
1379 }
1380
GenerateNetworkErrorLoggingReportIfError(int rv)1381 void HttpNetworkTransaction::GenerateNetworkErrorLoggingReportIfError(int rv) {
1382 if (rv < 0 && rv != ERR_IO_PENDING)
1383 GenerateNetworkErrorLoggingReport(rv);
1384 }
1385
GenerateNetworkErrorLoggingReport(int rv)1386 void HttpNetworkTransaction::GenerateNetworkErrorLoggingReport(int rv) {
1387 // |rv| should be a valid net::Error
1388 DCHECK_NE(rv, ERR_IO_PENDING);
1389 DCHECK_LE(rv, 0);
1390
1391 if (network_error_logging_report_generated_)
1392 return;
1393 network_error_logging_report_generated_ = true;
1394
1395 NetworkErrorLoggingService* service =
1396 session_->network_error_logging_service();
1397 if (!service)
1398 return;
1399
1400 // Don't report on proxy auth challenges.
1401 if (response_.headers && response_.headers->response_code() ==
1402 HTTP_PROXY_AUTHENTICATION_REQUIRED) {
1403 return;
1404 }
1405
1406 // Don't generate NEL reports if we are behind a proxy, to avoid leaking
1407 // internal network details.
1408 if (response_.was_fetched_via_proxy)
1409 return;
1410
1411 // Ignore errors from non-HTTPS origins.
1412 if (!url_.SchemeIsCryptographic())
1413 return;
1414
1415 NetworkErrorLoggingService::RequestDetails details;
1416
1417 details.network_isolation_key = network_isolation_key_;
1418 details.uri = url_;
1419 if (!request_referrer_.empty())
1420 details.referrer = GURL(request_referrer_);
1421 details.user_agent = request_user_agent_;
1422 if (!remote_endpoint_.address().empty()) {
1423 details.server_ip = remote_endpoint_.address();
1424 } else {
1425 details.server_ip = IPAddress();
1426 }
1427 // HttpResponseHeaders::response_code() returns 0 if response code couldn't
1428 // be parsed, which is also how NEL represents the same.
1429 if (response_.headers) {
1430 details.status_code = response_.headers->response_code();
1431 } else {
1432 details.status_code = 0;
1433 }
1434 // If we got response headers, assume that the connection used HTTP/1.1
1435 // unless ALPN negotiation tells us otherwise (handled below).
1436 if (response_.was_alpn_negotiated) {
1437 details.protocol = response_.alpn_negotiated_protocol;
1438 } else {
1439 details.protocol = "http/1.1";
1440 }
1441 details.method = request_method_;
1442 details.elapsed_time = base::TimeTicks::Now() - start_timeticks_;
1443 details.type = static_cast<Error>(rv);
1444 details.reporting_upload_depth = request_reporting_upload_depth_;
1445
1446 service->OnRequest(std::move(details));
1447 }
1448 #endif // BUILDFLAG(ENABLE_REPORTING)
1449
HandleHttp11Required(int error)1450 int HttpNetworkTransaction::HandleHttp11Required(int error) {
1451 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1452 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1453
1454 // HttpServerProperties should have been updated, so when the request is sent
1455 // again, it will automatically use HTTP/1.1.
1456 ResetConnectionAndRequestForResend();
1457 return OK;
1458 }
1459
HandleSSLClientAuthError(int error)1460 int HttpNetworkTransaction::HandleSSLClientAuthError(int error) {
1461 // Client certificate errors may come from either the origin server or the
1462 // proxy.
1463 //
1464 // Origin errors are handled here, while most proxy errors are handled in the
1465 // HttpStreamFactory and below. However, if the request is not tunneled (i.e.
1466 // the origin is HTTP, so there is no HTTPS connection) and the proxy does not
1467 // report a bad client certificate until after the TLS handshake completes.
1468 // The latter occurs in TLS 1.3 or TLS 1.2 with False Start (disabled for
1469 // proxies). The error will then surface out of Read() rather than Connect()
1470 // and ultimately surfaced out of DoReadHeadersComplete().
1471 //
1472 // See https://crbug.com/828965.
1473 bool is_server = !UsingHttpProxyWithoutTunnel();
1474 HostPortPair host_port_pair =
1475 is_server ? HostPortPair::FromURL(request_->url)
1476 : proxy_info_.proxy_server().host_port_pair();
1477
1478 if (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error)) {
1479 DCHECK((is_server && IsSecureRequest()) ||
1480 proxy_info_.is_secure_http_like());
1481 if (session_->ssl_client_context()->ClearClientCertificate(
1482 host_port_pair)) {
1483 // The private key handle may have gone stale due to, e.g., the user
1484 // unplugging their smartcard. Operating systems do not provide reliable
1485 // notifications for this, so if the signature failed and the user was
1486 // not already prompted for certificate on this request, retry to ask
1487 // the user for a new one.
1488 //
1489 // TODO(davidben): There is no corresponding feature for proxy client
1490 // certificates. Ideally this would live at a lower level, common to both,
1491 // but |configured_client_cert_for_server_| is not accessible below the
1492 // socket pools.
1493 if (is_server && error == ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED &&
1494 !configured_client_cert_for_server_ && !HasExceededMaxRetries()) {
1495 retry_attempts_++;
1496 net_log_.AddEventWithNetErrorCode(
1497 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1498 ResetConnectionAndRequestForResend();
1499 return OK;
1500 }
1501 }
1502 }
1503 return error;
1504 }
1505
1506 // This method determines whether it is safe to resend the request after an
1507 // IO error. It can only be called in response to request header or body
1508 // write errors or response header read errors. It should not be used in
1509 // other cases, such as a Connect error.
HandleIOError(int error)1510 int HttpNetworkTransaction::HandleIOError(int error) {
1511 // Because the peer may request renegotiation with client authentication at
1512 // any time, check and handle client authentication errors.
1513 error = HandleSSLClientAuthError(error);
1514
1515 #if BUILDFLAG(ENABLE_REPORTING)
1516 GenerateNetworkErrorLoggingReportIfError(error);
1517 #endif // BUILDFLAG(ENABLE_REPORTING)
1518
1519 switch (error) {
1520 // If we try to reuse a connection that the server is in the process of
1521 // closing, we may end up successfully writing out our request (or a
1522 // portion of our request) only to find a connection error when we try to
1523 // read from (or finish writing to) the socket.
1524 case ERR_CONNECTION_RESET:
1525 case ERR_CONNECTION_CLOSED:
1526 case ERR_CONNECTION_ABORTED:
1527 // There can be a race between the socket pool checking checking whether a
1528 // socket is still connected, receiving the FIN, and sending/reading data
1529 // on a reused socket. If we receive the FIN between the connectedness
1530 // check and writing/reading from the socket, we may first learn the socket
1531 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1532 // likely happen when trying to retrieve its IP address.
1533 // See http://crbug.com/105824 for more details.
1534 case ERR_SOCKET_NOT_CONNECTED:
1535 // If a socket is closed on its initial request, HttpStreamParser returns
1536 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1537 // preconnected but failed to be used before the server timed it out.
1538 case ERR_EMPTY_RESPONSE:
1539 if (ShouldResendRequest()) {
1540 net_log_.AddEventWithNetErrorCode(
1541 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1542 ResetConnectionAndRequestForResend();
1543 error = OK;
1544 }
1545 break;
1546 case ERR_EARLY_DATA_REJECTED:
1547 case ERR_WRONG_VERSION_ON_EARLY_DATA:
1548 net_log_.AddEventWithNetErrorCode(
1549 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1550 // Disable early data on the SSLConfig on a reset.
1551 can_send_early_data_ = false;
1552 ResetConnectionAndRequestForResend();
1553 error = OK;
1554 break;
1555 case ERR_HTTP2_PING_FAILED:
1556 case ERR_HTTP2_SERVER_REFUSED_STREAM:
1557 case ERR_HTTP2_PUSHED_STREAM_NOT_AVAILABLE:
1558 case ERR_HTTP2_CLAIMED_PUSHED_STREAM_RESET_BY_SERVER:
1559 case ERR_HTTP2_PUSHED_RESPONSE_DOES_NOT_MATCH:
1560 case ERR_QUIC_HANDSHAKE_FAILED:
1561 case ERR_QUIC_GOAWAY_REQUEST_CAN_BE_RETRIED:
1562 if (HasExceededMaxRetries())
1563 break;
1564 net_log_.AddEventWithNetErrorCode(
1565 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1566 retry_attempts_++;
1567 ResetConnectionAndRequestForResend();
1568 error = OK;
1569 break;
1570 case ERR_QUIC_PROTOCOL_ERROR:
1571 if (GetResponseHeaders() != nullptr ||
1572 !stream_->GetAlternativeService(&retried_alternative_service_)) {
1573 // If the response headers have already been recieved and passed up
1574 // then the request can not be retried. Also, if there was no
1575 // alternative service used for this request, then there is no
1576 // alternative service to be disabled.
1577 break;
1578 }
1579 if (HasExceededMaxRetries())
1580 break;
1581 if (session_->http_server_properties()->IsAlternativeServiceBroken(
1582 retried_alternative_service_, network_isolation_key_)) {
1583 // If the alternative service was marked as broken while the request
1584 // was in flight, retry the request which will not use the broken
1585 // alternative service.
1586 net_log_.AddEventWithNetErrorCode(
1587 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1588 retry_attempts_++;
1589 ResetConnectionAndRequestForResend();
1590 error = OK;
1591 } else if (session_->context()
1592 .quic_context->params()
1593 ->retry_without_alt_svc_on_quic_errors) {
1594 // Disable alternative services for this request and retry it. If the
1595 // retry succeeds, then the alternative service will be marked as
1596 // broken then.
1597 enable_alternative_services_ = false;
1598 net_log_.AddEventWithNetErrorCode(
1599 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1600 retry_attempts_++;
1601 ResetConnectionAndRequestForResend();
1602 error = OK;
1603 }
1604 break;
1605 }
1606 return error;
1607 }
1608
ResetStateForRestart()1609 void HttpNetworkTransaction::ResetStateForRestart() {
1610 ResetStateForAuthRestart();
1611 if (stream_) {
1612 total_received_bytes_ += stream_->GetTotalReceivedBytes();
1613 total_sent_bytes_ += stream_->GetTotalSentBytes();
1614 }
1615 CacheNetErrorDetailsAndResetStream();
1616 }
1617
ResetStateForAuthRestart()1618 void HttpNetworkTransaction::ResetStateForAuthRestart() {
1619 send_start_time_ = base::TimeTicks();
1620 send_end_time_ = base::TimeTicks();
1621
1622 pending_auth_target_ = HttpAuth::AUTH_NONE;
1623 read_buf_ = nullptr;
1624 read_buf_len_ = 0;
1625 headers_valid_ = false;
1626 request_headers_.Clear();
1627 response_ = HttpResponseInfo();
1628 establishing_tunnel_ = false;
1629 remote_endpoint_ = IPEndPoint();
1630 net_error_details_.quic_broken = false;
1631 net_error_details_.quic_connection_error = quic::QUIC_NO_ERROR;
1632 #if BUILDFLAG(ENABLE_REPORTING)
1633 network_error_logging_report_generated_ = false;
1634 start_timeticks_ = base::TimeTicks::Now();
1635 #endif // BUILDFLAG(ENABLE_REPORTING)
1636 }
1637
CacheNetErrorDetailsAndResetStream()1638 void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1639 if (stream_)
1640 stream_->PopulateNetErrorDetails(&net_error_details_);
1641 stream_.reset();
1642 }
1643
GetResponseHeaders() const1644 HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
1645 return response_.headers.get();
1646 }
1647
ShouldResendRequest() const1648 bool HttpNetworkTransaction::ShouldResendRequest() const {
1649 bool connection_is_proven = stream_->IsConnectionReused();
1650 bool has_received_headers = GetResponseHeaders() != nullptr;
1651
1652 // NOTE: we resend a request only if we reused a keep-alive connection.
1653 // This automatically prevents an infinite resend loop because we'll run
1654 // out of the cached keep-alive connections eventually.
1655 if (connection_is_proven && !has_received_headers)
1656 return true;
1657 return false;
1658 }
1659
HasExceededMaxRetries() const1660 bool HttpNetworkTransaction::HasExceededMaxRetries() const {
1661 return (retry_attempts_ >= kMaxRetryAttempts);
1662 }
1663
CheckMaxRestarts()1664 bool HttpNetworkTransaction::CheckMaxRestarts() {
1665 num_restarts_++;
1666 return num_restarts_ < kMaxRestarts;
1667 }
1668
ResetConnectionAndRequestForResend()1669 void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
1670 if (stream_.get()) {
1671 stream_->Close(true);
1672 CacheNetErrorDetailsAndResetStream();
1673 }
1674
1675 // We need to clear request_headers_ because it contains the real request
1676 // headers, but we may need to resend the CONNECT request first to recreate
1677 // the SSL tunnel.
1678 request_headers_.Clear();
1679 next_state_ = STATE_CREATE_STREAM; // Resend the request.
1680
1681 #if BUILDFLAG(ENABLE_REPORTING)
1682 // Reset for new request.
1683 network_error_logging_report_generated_ = false;
1684 start_timeticks_ = base::TimeTicks::Now();
1685 #endif // BUILDFLAG(ENABLE_REPORTING)
1686
1687 ResetStateForRestart();
1688 }
1689
ShouldApplyProxyAuth() const1690 bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
1691 return UsingHttpProxyWithoutTunnel();
1692 }
1693
ShouldApplyServerAuth() const1694 bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
1695 return request_->privacy_mode == PRIVACY_MODE_DISABLED;
1696 }
1697
HandleAuthChallenge()1698 int HttpNetworkTransaction::HandleAuthChallenge() {
1699 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
1700 DCHECK(headers.get());
1701
1702 int status = headers->response_code();
1703 if (status != HTTP_UNAUTHORIZED &&
1704 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
1705 return OK;
1706 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
1707 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
1708 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1709 return ERR_UNEXPECTED_PROXY_AUTH;
1710
1711 // This case can trigger when an HTTPS server responds with a "Proxy
1712 // authentication required" status code through a non-authenticating
1713 // proxy.
1714 if (!auth_controllers_[target].get())
1715 return ERR_UNEXPECTED_PROXY_AUTH;
1716
1717 int rv = auth_controllers_[target]->HandleAuthChallenge(
1718 headers, response_.ssl_info, !ShouldApplyServerAuth(), false, net_log_);
1719 if (auth_controllers_[target]->HaveAuthHandler())
1720 pending_auth_target_ = target;
1721
1722 auth_controllers_[target]->TakeAuthInfo(&response_.auth_challenge);
1723
1724 return rv;
1725 }
1726
HaveAuth(HttpAuth::Target target) const1727 bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1728 return auth_controllers_[target].get() &&
1729 auth_controllers_[target]->HaveAuth();
1730 }
1731
AuthURL(HttpAuth::Target target) const1732 GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1733 switch (target) {
1734 case HttpAuth::AUTH_PROXY: {
1735 if (!proxy_info_.proxy_server().is_valid() ||
1736 proxy_info_.proxy_server().is_direct()) {
1737 return GURL(); // There is no proxy server.
1738 }
1739 // TODO(https://crbug.com/1103768): Mapping proxy addresses to
1740 // URLs is a lossy conversion, shouldn't do this.
1741 const char* scheme =
1742 proxy_info_.is_secure_http_like() ? "https://" : "http://";
1743 return GURL(scheme +
1744 proxy_info_.proxy_server().host_port_pair().ToString());
1745 }
1746 case HttpAuth::AUTH_SERVER:
1747 if (ForWebSocketHandshake()) {
1748 return net::ChangeWebSocketSchemeToHttpScheme(request_->url);
1749 }
1750 return request_->url;
1751 default:
1752 return GURL();
1753 }
1754 }
1755
ForWebSocketHandshake() const1756 bool HttpNetworkTransaction::ForWebSocketHandshake() const {
1757 return websocket_handshake_stream_base_create_helper_ &&
1758 request_->url.SchemeIsWSOrWSS();
1759 }
1760
CopyConnectionAttemptsFromStreamRequest()1761 void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1762 DCHECK(stream_request_);
1763
1764 // Since the transaction can restart with auth credentials, it may create a
1765 // stream more than once. Accumulate all of the connection attempts across
1766 // those streams by appending them to the vector:
1767 for (const auto& attempt : stream_request_->connection_attempts())
1768 connection_attempts_.push_back(attempt);
1769 }
1770
ContentEncodingsValid() const1771 bool HttpNetworkTransaction::ContentEncodingsValid() const {
1772 HttpResponseHeaders* headers = GetResponseHeaders();
1773 DCHECK(headers);
1774
1775 std::string accept_encoding;
1776 request_headers_.GetHeader(HttpRequestHeaders::kAcceptEncoding,
1777 &accept_encoding);
1778 std::set<std::string> allowed_encodings;
1779 if (!HttpUtil::ParseAcceptEncoding(accept_encoding, &allowed_encodings))
1780 return false;
1781
1782 std::string content_encoding;
1783 headers->GetNormalizedHeader("Content-Encoding", &content_encoding);
1784 std::set<std::string> used_encodings;
1785 if (!HttpUtil::ParseContentEncoding(content_encoding, &used_encodings))
1786 return false;
1787
1788 // When "Accept-Encoding" is not specified, it is parsed as "*".
1789 // If "*" encoding is advertised, then any encoding should be "accepted".
1790 // This does not mean, that it will be successfully decoded.
1791 if (allowed_encodings.find("*") != allowed_encodings.end())
1792 return true;
1793
1794 bool result = true;
1795 for (auto const& encoding : used_encodings) {
1796 SourceStream::SourceType source_type =
1797 FilterSourceStream::ParseEncodingType(encoding);
1798 // We don't reject encodings we are not aware. They just will not decode.
1799 if (source_type == SourceStream::TYPE_UNKNOWN)
1800 continue;
1801 if (allowed_encodings.find(encoding) == allowed_encodings.end()) {
1802 result = false;
1803 break;
1804 }
1805 }
1806
1807 // Temporary workaround for http://crbug.com/714514
1808 if (headers->IsRedirect(nullptr)) {
1809 UMA_HISTOGRAM_BOOLEAN("Net.RedirectWithUnadvertisedContentEncoding",
1810 !result);
1811 return true;
1812 }
1813
1814 return result;
1815 }
1816
1817 } // namespace net
1818