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