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