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