1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/url_request/url_request.h"
#include <utility>
#include "base/compiler_specific.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/notreached.h"
#include "base/rand_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/lock.h"
#include "base/task/single_thread_task_runner.h"
#include "base/types/optional_util.h"
#include "base/types/pass_key.h"
#include "base/values.h"
#include "net/base/auth.h"
#include "net/base/features.h"
#include "net/base/io_buffer.h"
#include "net/base/load_flags.h"
#include "net/base/load_timing_info.h"
#include "net/base/net_errors.h"
#include "net/base/network_change_notifier.h"
#include "net/base/network_delegate.h"
#include "net/base/network_isolation_partition.h"
#include "net/base/upload_data_stream.h"
#include "net/cert/x509_certificate.h"
#include "net/cookies/cookie_setting_override.h"
#include "net/cookies/cookie_store.h"
#include "net/cookies/cookie_util.h"
#include "net/dns/public/secure_dns_policy.h"
#include "net/http/http_log_util.h"
#include "net/http/http_util.h"
#include "net/log/net_log.h"
#include "net/log/net_log_event_type.h"
#include "net/log/net_log_source_type.h"
#include "net/socket/next_proto.h"
#include "net/ssl/ssl_cert_request_info.h"
#include "net/ssl/ssl_private_key.h"
#include "net/storage_access_api/status.h"
#include "net/url_request/redirect_info.h"
#include "net/url_request/redirect_util.h"
#include "net/url_request/storage_access_status_cache.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_error_job.h"
#include "net/url_request/url_request_job.h"
#include "net/url_request/url_request_job_factory.h"
#include "net/url_request/url_request_netlog_params.h"
#include "net/url_request/url_request_redirect_job.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace net {
namespace {
// True once the first URLRequest was started.
bool g_url_requests_started = false;
// True if cookies are accepted by default.
bool g_default_can_use_cookies = true;
// When the URLRequest first assempts load timing information, it has the times
// at which each event occurred. The API requires the time which the request
// was blocked on each phase. This function handles the conversion.
//
// In the case of reusing a SPDY session, old proxy results may have been
// reused, so proxy resolution times may be before the request was started.
//
// Due to preconnect and late binding, it is also possible for the connection
// attempt to start before a request has been started, or proxy resolution
// completed.
//
// This functions fixes both those cases.
void ConvertRealLoadTimesToBlockingTimes(LoadTimingInfo* load_timing_info) {
DCHECK(!load_timing_info->request_start.is_null());
// Earliest time possible for the request to be blocking on connect events.
base::TimeTicks block_on_connect = load_timing_info->request_start;
if (!load_timing_info->proxy_resolve_start.is_null()) {
DCHECK(!load_timing_info->proxy_resolve_end.is_null());
// Make sure the proxy times are after request start.
if (load_timing_info->proxy_resolve_start < load_timing_info->request_start)
load_timing_info->proxy_resolve_start = load_timing_info->request_start;
if (load_timing_info->proxy_resolve_end < load_timing_info->request_start)
load_timing_info->proxy_resolve_end = load_timing_info->request_start;
// Connect times must also be after the proxy times.
block_on_connect = load_timing_info->proxy_resolve_end;
}
if (!load_timing_info->receive_headers_start.is_null() &&
load_timing_info->receive_headers_start < block_on_connect) {
load_timing_info->receive_headers_start = block_on_connect;
}
if (!load_timing_info->receive_non_informational_headers_start.is_null() &&
load_timing_info->receive_non_informational_headers_start <
block_on_connect) {
load_timing_info->receive_non_informational_headers_start =
block_on_connect;
}
// Make sure connection times are after start and proxy times.
LoadTimingInfo::ConnectTiming* connect_timing =
&load_timing_info->connect_timing;
if (!connect_timing->domain_lookup_start.is_null()) {
DCHECK(!connect_timing->domain_lookup_end.is_null());
if (connect_timing->domain_lookup_start < block_on_connect)
connect_timing->domain_lookup_start = block_on_connect;
if (connect_timing->domain_lookup_end < block_on_connect)
connect_timing->domain_lookup_end = block_on_connect;
}
if (!connect_timing->connect_start.is_null()) {
DCHECK(!connect_timing->connect_end.is_null());
if (connect_timing->connect_start < block_on_connect)
connect_timing->connect_start = block_on_connect;
if (connect_timing->connect_end < block_on_connect)
connect_timing->connect_end = block_on_connect;
}
if (!connect_timing->ssl_start.is_null()) {
DCHECK(!connect_timing->ssl_end.is_null());
if (connect_timing->ssl_start < block_on_connect)
connect_timing->ssl_start = block_on_connect;
if (connect_timing->ssl_end < block_on_connect)
connect_timing->ssl_end = block_on_connect;
}
}
NetLogWithSource CreateNetLogWithSource(
NetLog* net_log,
std::optional<net::NetLogSource> net_log_source) {
if (net_log_source) {
return NetLogWithSource::Make(net_log, net_log_source.value());
}
return NetLogWithSource::Make(net_log, NetLogSourceType::URL_REQUEST);
}
// TODO(https://crbug.com/366284840): remove this, once the "retry" header is
// handled in URLLoader.
net::cookie_util::StorageAccessStatusOutcome
ConvertSecFetchStorageAccessHeaderValueToOutcome(
net::cookie_util::StorageAccessStatus storage_access_status) {
using enum net::cookie_util::StorageAccessStatusOutcome;
switch (storage_access_status) {
case net::cookie_util::StorageAccessStatus::kInactive:
return kValueInactive;
case net::cookie_util::StorageAccessStatus::kActive:
return kValueActive;
case net::cookie_util::StorageAccessStatus::kNone:
return kValueNone;
}
NOTREACHED();
}
} // namespace
///////////////////////////////////////////////////////////////////////////////
// URLRequest::Delegate
int URLRequest::Delegate::OnConnected(URLRequest* request,
const TransportInfo& info,
CompletionOnceCallback callback) {
return OK;
}
void URLRequest::Delegate::OnReceivedRedirect(URLRequest* request,
const RedirectInfo& redirect_info,
bool* defer_redirect) {}
void URLRequest::Delegate::OnAuthRequired(URLRequest* request,
const AuthChallengeInfo& auth_info) {
request->CancelAuth();
}
void URLRequest::Delegate::OnCertificateRequested(
URLRequest* request,
SSLCertRequestInfo* cert_request_info) {
request->CancelWithError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
}
void URLRequest::Delegate::OnSSLCertificateError(URLRequest* request,
int net_error,
const SSLInfo& ssl_info,
bool is_hsts_ok) {
request->Cancel();
}
void URLRequest::Delegate::OnResponseStarted(URLRequest* request,
int net_error) {
NOTREACHED();
}
///////////////////////////////////////////////////////////////////////////////
// URLRequest
URLRequest::~URLRequest() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
Cancel();
if (network_delegate()) {
network_delegate()->NotifyURLRequestDestroyed(this);
if (job_.get())
job_->NotifyURLRequestDestroyed();
}
// Delete job before |this|, since subclasses may do weird things, like depend
// on UserData associated with |this| and poke at it during teardown.
job_.reset();
DCHECK_EQ(1u, context_->url_requests()->count(this));
context_->url_requests()->erase(this);
int net_error = OK;
// Log error only on failure, not cancellation, as even successful requests
// are "cancelled" on destruction.
if (status_ != ERR_ABORTED)
net_error = status_;
net_log_.EndEventWithNetErrorCode(NetLogEventType::REQUEST_ALIVE, net_error);
}
void URLRequest::set_upload(std::unique_ptr<UploadDataStream> upload) {
upload_data_stream_ = std::move(upload);
}
const UploadDataStream* URLRequest::get_upload_for_testing() const {
return upload_data_stream_.get();
}
bool URLRequest::has_upload() const {
return upload_data_stream_.get() != nullptr;
}
void URLRequest::SetExtraRequestHeaderByName(std::string_view name,
std::string_view value,
bool overwrite) {
DCHECK(!is_pending_ || is_redirecting_);
if (overwrite) {
extra_request_headers_.SetHeader(name, value);
} else {
extra_request_headers_.SetHeaderIfMissing(name, value);
}
}
void URLRequest::RemoveRequestHeaderByName(std::string_view name) {
DCHECK(!is_pending_ || is_redirecting_);
extra_request_headers_.RemoveHeader(name);
}
void URLRequest::SetExtraRequestHeaders(const HttpRequestHeaders& headers) {
DCHECK(!is_pending_);
extra_request_headers_ = headers;
// NOTE: This method will likely become non-trivial once the other setters
// for request headers are implemented.
}
int64_t URLRequest::GetTotalReceivedBytes() const {
if (!job_.get())
return 0;
return job_->GetTotalReceivedBytes();
}
int64_t URLRequest::GetTotalSentBytes() const {
if (!job_.get())
return 0;
return job_->GetTotalSentBytes();
}
int64_t URLRequest::GetRawBodyBytes() const {
if (!job_.get()) {
return 0;
}
if (int64_t bytes = job_->GetReceivedBodyBytes()) {
return bytes;
}
// GetReceivedBodyBytes() is available only when the body was received from
// the network. Otherwise, returns prefilter_bytes_read() instead.
return job_->prefilter_bytes_read();
}
LoadStateWithParam URLRequest::GetLoadState() const {
// The !blocked_by_.empty() check allows |this| to report it's blocked on a
// delegate before it has been started.
if (calling_delegate_ || !blocked_by_.empty()) {
return LoadStateWithParam(LOAD_STATE_WAITING_FOR_DELEGATE,
use_blocked_by_as_load_param_
? base::UTF8ToUTF16(blocked_by_)
: std::u16string());
}
return LoadStateWithParam(job_.get() ? job_->GetLoadState() : LOAD_STATE_IDLE,
std::u16string());
}
base::Value::Dict URLRequest::GetStateAsValue() const {
base::Value::Dict dict;
dict.Set("url", original_url().possibly_invalid_spec());
if (url_chain_.size() > 1) {
base::Value::List list;
for (const GURL& url : url_chain_) {
list.Append(url.possibly_invalid_spec());
}
dict.Set("url_chain", std::move(list));
}
dict.Set("load_flags", load_flags());
LoadStateWithParam load_state = GetLoadState();
dict.Set("load_state", load_state.state);
if (!load_state.param.empty())
dict.Set("load_state_param", load_state.param);
if (!blocked_by_.empty())
dict.Set("delegate_blocked_by", blocked_by_);
dict.Set("method", method_);
dict.Set("network_anonymization_key",
isolation_info_.network_anonymization_key().ToDebugString());
dict.Set("network_isolation_key",
isolation_info_.network_isolation_key().ToDebugString());
dict.Set("has_upload", has_upload());
dict.Set("is_pending", is_pending_);
dict.Set("traffic_annotation", traffic_annotation_.unique_id_hash_code);
if (status_ != OK)
dict.Set("net_error", status_);
return dict;
}
void URLRequest::LogBlockedBy(std::string_view blocked_by) {
DCHECK(!blocked_by.empty());
// Only log information to NetLog during startup and certain deferring calls
// to delegates. For all reads but the first, do nothing.
if (!calling_delegate_ && !response_info_.request_time.is_null())
return;
LogUnblocked();
blocked_by_ = std::string(blocked_by);
use_blocked_by_as_load_param_ = false;
net_log_.BeginEventWithStringParams(NetLogEventType::DELEGATE_INFO,
"delegate_blocked_by", blocked_by_);
}
void URLRequest::LogAndReportBlockedBy(std::string_view source) {
LogBlockedBy(source);
use_blocked_by_as_load_param_ = true;
}
void URLRequest::LogUnblocked() {
if (blocked_by_.empty())
return;
net_log_.EndEvent(NetLogEventType::DELEGATE_INFO);
blocked_by_.clear();
}
UploadProgress URLRequest::GetUploadProgress() const {
if (!job_.get()) {
// We haven't started or the request was cancelled
return UploadProgress();
}
if (final_upload_progress_.position()) {
// The first job completed and none of the subsequent series of
// GETs when following redirects will upload anything, so we return the
// cached results from the initial job, the POST.
return final_upload_progress_;
}
if (upload_data_stream_)
return upload_data_stream_->GetUploadProgress();
return UploadProgress();
}
std::string URLRequest::GetResponseHeaderByName(std::string_view name) const {
if (!response_info_.headers.get()) {
return std::string();
}
return response_info_.headers->GetNormalizedHeader(name).value_or(
std::string());
}
IPEndPoint URLRequest::GetResponseRemoteEndpoint() const {
DCHECK(job_.get());
return job_->GetResponseRemoteEndpoint();
}
HttpResponseHeaders* URLRequest::response_headers() const {
return response_info_.headers.get();
}
const std::optional<AuthChallengeInfo>& URLRequest::auth_challenge_info()
const {
return response_info_.auth_challenge;
}
void URLRequest::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
*load_timing_info = load_timing_info_;
}
LoadTimingInternalInfo URLRequest::GetLoadTimingInternalInfo() const {
return load_timing_internal_info_;
}
void URLRequest::PopulateNetErrorDetails(NetErrorDetails* details) const {
if (!job_)
return;
return job_->PopulateNetErrorDetails(details);
}
bool URLRequest::GetTransactionRemoteEndpoint(IPEndPoint* endpoint) const {
if (!job_)
return false;
return job_->GetTransactionRemoteEndpoint(endpoint);
}
void URLRequest::GetMimeType(std::string* mime_type) const {
DCHECK(job_.get());
job_->GetMimeType(mime_type);
}
void URLRequest::GetCharset(std::string* charset) const {
DCHECK(job_.get());
job_->GetCharset(charset);
}
void URLRequest::GetClientSideContentDecodingTypes(
std::vector<net::SourceStreamType>* types) const {
CHECK(job_.get());
job_->GetClientSideContentDecodingTypes(types);
}
int URLRequest::GetResponseCode() const {
DCHECK(job_.get());
return job_->GetResponseCode();
}
void URLRequest::set_maybe_sent_cookies(CookieAccessResultList cookies) {
maybe_sent_cookies_ = std::move(cookies);
}
void URLRequest::set_maybe_stored_cookies(
CookieAndLineAccessResultList cookies) {
maybe_stored_cookies_ = std::move(cookies);
}
void URLRequest::SetLoadFlags(int flags) {
if ((load_flags() & LOAD_IGNORE_LIMITS) != (flags & LOAD_IGNORE_LIMITS)) {
DCHECK(!job_.get());
DCHECK(flags & LOAD_IGNORE_LIMITS);
DCHECK_EQ(priority_, MAXIMUM_PRIORITY);
}
partial_load_flags_ = flags;
// This should be a no-op given the above DCHECKs, but do this
// anyway for release mode.
if ((load_flags() & LOAD_IGNORE_LIMITS) != 0) {
SetPriority(MAXIMUM_PRIORITY);
}
}
void URLRequest::SetSecureDnsPolicy(SecureDnsPolicy secure_dns_policy) {
secure_dns_policy_ = secure_dns_policy;
}
// static
void URLRequest::SetDefaultCookiePolicyToBlock() {
CHECK(!g_url_requests_started);
g_default_can_use_cookies = false;
}
void URLRequest::SetURLChain(const std::vector<GURL>& url_chain) {
DCHECK(!job_);
DCHECK(!is_pending_);
DCHECK_EQ(url_chain_.size(), 1u);
if (url_chain.size() < 2)
return;
// In most cases the current request URL will match the last URL in the
// explicitly set URL chain. In some cases, however, a throttle will modify
// the request URL resulting in a different request URL. We handle this by
// using previous values from the explicitly set URL chain, but with the
// request URL as the final entry in the chain.
url_chain_.insert(url_chain_.begin(), url_chain.begin(),
url_chain.begin() + url_chain.size() - 1);
}
void URLRequest::set_site_for_cookies(const SiteForCookies& site_for_cookies) {
DCHECK(!is_pending_);
site_for_cookies_ = site_for_cookies;
}
void URLRequest::set_isolation_info(const IsolationInfo& isolation_info,
std::optional<GURL> redirect_info_new_url) {
isolation_info_ = isolation_info;
bool is_main_frame_navigation = isolation_info.IsMainFrameRequest() ||
force_main_frame_for_same_site_cookies();
cookie_partition_key_ = CookiePartitionKey::FromNetworkIsolationKey(
isolation_info.network_isolation_key(), isolation_info.site_for_cookies(),
net::SchemefulSite(redirect_info_new_url.has_value()
? redirect_info_new_url.value()
: url_chain_.back()),
is_main_frame_navigation);
}
void URLRequest::set_isolation_info_from_network_anonymization_key(
const NetworkAnonymizationKey& network_anonymization_key) {
set_isolation_info(URLRequest::CreateIsolationInfoFromNetworkAnonymizationKey(
network_anonymization_key));
is_created_from_network_anonymization_key_ = true;
}
void URLRequest::set_first_party_url_policy(
RedirectInfo::FirstPartyURLPolicy first_party_url_policy) {
DCHECK(!is_pending_);
first_party_url_policy_ = first_party_url_policy;
}
void URLRequest::set_initiator(const std::optional<url::Origin>& initiator) {
DCHECK(!is_pending_);
DCHECK(!initiator.has_value() || initiator.value().opaque() ||
initiator.value().GetURL().is_valid());
initiator_ = initiator;
}
void URLRequest::set_method(std::string_view method) {
DCHECK(!is_pending_);
method_ = std::string(method);
}
#if BUILDFLAG(ENABLE_REPORTING)
void URLRequest::set_reporting_upload_depth(int reporting_upload_depth) {
DCHECK(!is_pending_);
reporting_upload_depth_ = reporting_upload_depth;
}
#endif
void URLRequest::SetReferrer(std::string_view referrer) {
DCHECK(!is_pending_);
GURL referrer_url(referrer);
if (referrer_url.is_valid()) {
referrer_ = referrer_url.GetAsReferrer().spec();
} else {
referrer_ = std::string(referrer);
}
}
void URLRequest::set_referrer_policy(ReferrerPolicy referrer_policy) {
DCHECK(!is_pending_);
referrer_policy_ = referrer_policy;
}
void URLRequest::set_allow_credentials(bool allow_credentials) {
allow_credentials_ = allow_credentials;
if (allow_credentials) {
partial_load_flags_ &= ~LOAD_DO_NOT_SAVE_COOKIES;
} else {
partial_load_flags_ |= LOAD_DO_NOT_SAVE_COOKIES;
}
}
void URLRequest::Start() {
DCHECK(delegate_);
// We do not support credentials with a non-general
// NetworkIsolationPartition.
CHECK(isolation_info_.GetNetworkIsolationPartition() ==
NetworkIsolationPartition::kGeneral ||
!allow_credentials());
if (status_ != OK)
return;
if (context_->require_network_anonymization_key()) {
DCHECK(!isolation_info_.IsEmpty());
}
// Some values can be NULL, but the job factory must not be.
DCHECK(context_->job_factory());
// Anything that sets |blocked_by_| before start should have cleaned up after
// itself.
DCHECK(blocked_by_.empty());
g_url_requests_started = true;
response_info_.request_time = base::Time::Now();
load_timing_info_ = LoadTimingInfo();
load_timing_info_.request_start_time = response_info_.request_time;
load_timing_info_.request_start = base::TimeTicks::Now();
if (network_delegate()) {
OnCallToDelegate(NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST);
int error = network_delegate()->NotifyBeforeURLRequest(
this,
base::BindOnce(&URLRequest::BeforeRequestComplete,
base::Unretained(this)),
&delegate_redirect_url_);
// If ERR_IO_PENDING is returned, the delegate will invoke
// |BeforeRequestComplete| later.
if (error != ERR_IO_PENDING)
BeforeRequestComplete(error);
return;
}
StartJob(context_->job_factory()->CreateJob(this));
}
///////////////////////////////////////////////////////////////////////////////
URLRequest::URLRequest(base::PassKey<URLRequestContext> pass_key,
const GURL& url,
RequestPriority priority,
Delegate* delegate,
const URLRequestContext* context,
NetworkTrafficAnnotationTag traffic_annotation,
bool is_for_websockets,
std::optional<net::NetLogSource> net_log_source)
: context_(context),
net_log_(CreateNetLogWithSource(context->net_log(), net_log_source)),
url_chain_(1, url),
method_("GET"),
delegate_(delegate),
is_for_websockets_(is_for_websockets),
redirect_limit_(kMaxRedirects),
priority_(priority),
creation_time_(base::TimeTicks::Now()),
traffic_annotation_(traffic_annotation) {
// Sanity check out environment.
DCHECK(base::SingleThreadTaskRunner::HasCurrentDefault());
context->url_requests()->insert(this);
net_log_.BeginEvent(NetLogEventType::REQUEST_ALIVE, [&] {
return NetLogURLRequestConstructorParams(url, priority_,
traffic_annotation_);
});
}
void URLRequest::BeforeRequestComplete(int error) {
DCHECK(!job_.get());
DCHECK_NE(ERR_IO_PENDING, error);
// Check that there are no callbacks to already failed or canceled requests.
DCHECK(!failed());
OnCallToDelegateComplete();
if (error != OK) {
net_log_.AddEventWithStringParams(NetLogEventType::CANCELLED, "source",
"delegate");
StartJob(std::make_unique<URLRequestErrorJob>(this, error));
} else if (!delegate_redirect_url_.is_empty()) {
GURL new_url;
new_url.Swap(&delegate_redirect_url_);
StartJob(std::make_unique<URLRequestRedirectJob>(
this, new_url,
// Use status code 307 to preserve the method, so POST requests work.
RedirectUtil::ResponseCode::REDIRECT_307_TEMPORARY_REDIRECT,
"Delegate"));
} else {
StartJob(context_->job_factory()->CreateJob(this));
}
}
void URLRequest::StartJob(std::unique_ptr<URLRequestJob> job) {
DCHECK(!is_pending_);
DCHECK(!job_);
if (is_created_from_network_anonymization_key_) {
DCHECK(load_flags() & LOAD_DISABLE_CACHE);
DCHECK(!allow_credentials_);
}
net_log_.BeginEvent(NetLogEventType::URL_REQUEST_START_JOB, [&] {
return NetLogURLRequestStartParams(
url(), method_, load_flags(), isolation_info_, site_for_cookies_,
initiator_,
upload_data_stream_ ? upload_data_stream_->identifier() : -1);
});
job_ = std::move(job);
job_->SetExtraRequestHeaders(extra_request_headers_);
job_->SetPriority(priority_);
job_->SetRequestHeadersCallback(request_headers_callback_);
job_->SetEarlyResponseHeadersCallback(early_response_headers_callback_);
if (is_shared_dictionary_read_allowed_callback_) {
job_->SetIsSharedDictionaryReadAllowedCallback(
is_shared_dictionary_read_allowed_callback_);
}
job_->SetResponseHeadersCallback(response_headers_callback_);
if (shared_dictionary_getter_) {
job_->SetSharedDictionaryGetter(shared_dictionary_getter_);
}
if (upload_data_stream_.get())
job_->SetUpload(upload_data_stream_.get());
is_pending_ = true;
is_redirecting_ = false;
deferred_redirect_info_.reset();
response_info_.was_cached = false;
maybe_sent_cookies_.clear();
maybe_stored_cookies_.clear();
GURL referrer_url(referrer_);
bool same_origin_for_metrics;
if (referrer_url !=
URLRequestJob::ComputeReferrerForPolicy(
referrer_policy_, referrer_url, url(), &same_origin_for_metrics)) {
if (!network_delegate() ||
!network_delegate()->CancelURLRequestWithPolicyViolatingReferrerHeader(
*this, url(), referrer_url)) {
referrer_.clear();
} else {
// We need to clear the referrer anyway to avoid an infinite recursion
// when starting the error job.
referrer_.clear();
net_log_.AddEventWithStringParams(NetLogEventType::CANCELLED, "source",
"delegate");
RestartWithJob(
std::make_unique<URLRequestErrorJob>(this, ERR_BLOCKED_BY_CLIENT));
return;
}
}
RecordReferrerGranularityMetrics(same_origin_for_metrics);
// Start() always completes asynchronously.
//
// Status is generally set by URLRequestJob itself, but Start() calls
// directly into the URLRequestJob subclass, so URLRequestJob can't set it
// here.
// TODO(mmenke): Make the URLRequest manage its own status.
status_ = ERR_IO_PENDING;
job_->Start();
}
void URLRequest::RestartWithJob(std::unique_ptr<URLRequestJob> job) {
DCHECK(job->request() == this);
PrepareToRestart();
StartJob(std::move(job));
}
int URLRequest::Cancel() {
return DoCancel(ERR_ABORTED, SSLInfo());
}
int URLRequest::CancelWithError(int error) {
return DoCancel(error, SSLInfo());
}
void URLRequest::CancelWithSSLError(int error, const SSLInfo& ssl_info) {
// This should only be called on a started request.
if (!is_pending_ || !job_.get() || job_->has_response_started()) {
NOTREACHED();
}
DoCancel(error, ssl_info);
}
int URLRequest::DoCancel(int error, const SSLInfo& ssl_info) {
DCHECK_LT(error, 0);
// If cancelled while calling a delegate, clear delegate info.
if (calling_delegate_) {
LogUnblocked();
OnCallToDelegateComplete();
}
// If the URL request already has an error status, then canceling is a no-op.
// Plus, we don't want to change the error status once it has been set.
if (!failed()) {
status_ = error;
response_info_.ssl_info = ssl_info;
// If the request hasn't already been completed, log a cancellation event.
if (!has_notified_completion_) {
// Don't log an error code on ERR_ABORTED, since that's redundant.
net_log_.AddEventWithNetErrorCode(NetLogEventType::CANCELLED,
error == ERR_ABORTED ? OK : error);
}
}
if (is_pending_ && job_.get())
job_->Kill();
// We need to notify about the end of this job here synchronously. The
// Job sends an asynchronous notification but by the time this is processed,
// our |context_| is NULL.
NotifyRequestCompleted();
// The Job will call our NotifyDone method asynchronously. This is done so
// that the Delegate implementation can call Cancel without having to worry
// about being called recursively.
return status_;
}
int URLRequest::Read(IOBuffer* dest, int dest_size) {
DCHECK(job_.get());
DCHECK_NE(ERR_IO_PENDING, status_);
// If this is the first read, end the delegate call that may have started in
// OnResponseStarted.
OnCallToDelegateComplete();
// If the request has failed, Read() will return actual network error code.
if (status_ != OK)
return status_;
// This handles reads after the request already completed successfully.
// TODO(ahendrickson): DCHECK() that it is not done after
// http://crbug.com/115705 is fixed.
if (job_->is_done())
return status_;
if (dest_size == 0) {
// Caller is not too bright. I guess we've done what they asked.
return OK;
}
// Caller should provide a buffer.
DCHECK(dest && dest->data());
int rv = job_->Read(dest, dest_size);
if (rv == ERR_IO_PENDING) {
set_status(ERR_IO_PENDING);
} else if (rv <= 0) {
NotifyRequestCompleted();
}
// If rv is not 0 or actual bytes read, the status cannot be success.
DCHECK(rv >= 0 || status_ != OK);
return rv;
}
void URLRequest::set_status(int status) {
DCHECK_LE(status, 0);
DCHECK(!failed() || (status != OK && status != ERR_IO_PENDING));
status_ = status;
}
bool URLRequest::failed() const {
return (status_ != OK && status_ != ERR_IO_PENDING);
}
int URLRequest::NotifyConnected(const TransportInfo& info,
CompletionOnceCallback callback) {
OnCallToDelegate(NetLogEventType::URL_REQUEST_DELEGATE_CONNECTED);
int result = delegate_->OnConnected(
this, info,
base::BindOnce(
[](URLRequest* request, CompletionOnceCallback callback, int result) {
request->OnCallToDelegateComplete(result);
std::move(callback).Run(result);
},
this, std::move(callback)));
if (result != ERR_IO_PENDING)
OnCallToDelegateComplete(result);
return result;
}
void URLRequest::ReceivedRedirect(RedirectInfo redirect_info) {
DCHECK_EQ(OK, status_);
is_redirecting_ = true;
OnCallToDelegate(NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT);
// When notifying the URLRequest::Delegate, it can destroy the request,
// which will destroy |this|. After calling to the URLRequest::Delegate,
// pointer must be checked to see if |this| still exists, and if not, the
// code must return immediately.
base::WeakPtr<URLRequest> weak_this(weak_factory_.GetWeakPtr());
bool defer_redirect = false;
per_hop_load_flags_ = LOAD_NORMAL;
delegate_->OnReceivedRedirect(this, redirect_info, &defer_redirect);
// Ensure that the request wasn't detached, destroyed, or canceled in
// NotifyReceivedRedirect.
if (!weak_this || failed()) {
return;
}
if (defer_redirect) {
deferred_redirect_info_ = std::move(redirect_info);
} else {
Redirect(redirect_info, /*removed_headers=*/std::nullopt,
/*modified_headers=*/std::nullopt);
}
// |this| may be have been destroyed here.
}
void URLRequest::NotifyResponseStarted(int net_error) {
DCHECK_LE(net_error, 0);
// Change status if there was an error.
if (net_error != OK)
set_status(net_error);
// |status_| should not be ERR_IO_PENDING when calling into the
// URLRequest::Delegate().
DCHECK_NE(ERR_IO_PENDING, status_);
net_log_.EndEventWithNetErrorCode(NetLogEventType::URL_REQUEST_START_JOB,
net_error);
// In some cases (e.g. an event was canceled), we might have sent the
// completion event and receive a NotifyResponseStarted() later.
if (!has_notified_completion_ && net_error == OK) {
if (network_delegate())
network_delegate()->NotifyResponseStarted(this, net_error);
}
// Notify in case the entire URL Request has been finished.
if (!has_notified_completion_ && net_error != OK)
NotifyRequestCompleted();
OnCallToDelegate(NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED);
delegate_->OnResponseStarted(this, net_error);
// Nothing may appear below this line as OnResponseStarted may delete
// |this|.
}
void URLRequest::FollowDeferredRedirect(
const std::optional<std::vector<std::string>>& removed_headers,
const std::optional<net::HttpRequestHeaders>& modified_headers) {
DCHECK(job_.get());
DCHECK_EQ(OK, status_);
DCHECK(is_redirecting_);
DCHECK(deferred_redirect_info_);
maybe_sent_cookies_.clear();
maybe_stored_cookies_.clear();
status_ = ERR_IO_PENDING;
// While this move is not strictly needed, Redirect() will start a new Job,
// which will delete `deferred_redirect_info_`. While `redirect_info` should
// not be needed after it's been deleted, it's best to not have a reference to
// a deleted object on the stack.
RedirectInfo redirect_info = std::move(deferred_redirect_info_).value();
Redirect(redirect_info, removed_headers, modified_headers);
}
void URLRequest::SetAuth(const AuthCredentials& credentials) {
DCHECK(job_.get());
DCHECK(job_->NeedsAuth());
maybe_sent_cookies_.clear();
maybe_stored_cookies_.clear();
status_ = ERR_IO_PENDING;
job_->SetAuth(credentials);
}
void URLRequest::CancelAuth() {
DCHECK(job_.get());
DCHECK(job_->NeedsAuth());
status_ = ERR_IO_PENDING;
job_->CancelAuth();
}
void URLRequest::ContinueWithCertificate(
scoped_refptr<X509Certificate> client_cert,
scoped_refptr<SSLPrivateKey> client_private_key) {
DCHECK(job_.get());
// Matches the call in NotifyCertificateRequested.
OnCallToDelegateComplete();
status_ = ERR_IO_PENDING;
job_->ContinueWithCertificate(std::move(client_cert),
std::move(client_private_key));
}
void URLRequest::ContinueDespiteLastError() {
DCHECK(job_.get());
// Matches the call in NotifySSLCertificateError.
OnCallToDelegateComplete();
status_ = ERR_IO_PENDING;
job_->ContinueDespiteLastError();
}
void URLRequest::AbortAndCloseConnection() {
DCHECK_EQ(OK, status_);
DCHECK(!has_notified_completion_);
DCHECK(job_);
job_->CloseConnectionOnDestruction();
job_.reset();
}
void URLRequest::PrepareToRestart() {
DCHECK(job_.get());
// Close the current URL_REQUEST_START_JOB, since we will be starting a new
// one.
net_log_.EndEvent(NetLogEventType::URL_REQUEST_START_JOB);
job_.reset();
response_info_ = HttpResponseInfo();
response_info_.request_time = base::Time::Now();
load_timing_info_ = LoadTimingInfo();
load_timing_info_.request_start_time = response_info_.request_time;
load_timing_info_.request_start = base::TimeTicks::Now();
load_timing_internal_info_ = LoadTimingInternalInfo();
status_ = OK;
is_pending_ = false;
proxy_chain_ = ProxyChain();
}
void URLRequest::Redirect(
const RedirectInfo& redirect_info,
const std::optional<std::vector<std::string>>& removed_headers,
const std::optional<net::HttpRequestHeaders>& modified_headers) {
// This method always succeeds. Whether |job_| is allowed to redirect to
// |redirect_info| is checked in URLRequestJob::CanFollowRedirect, before
// NotifyReceivedRedirect. This means the delegate can assume that, if it
// accepted the redirect, future calls to OnResponseStarted correspond to
// |redirect_info.new_url|.
OnCallToDelegateComplete();
if (net_log_.IsCapturing()) {
net_log_.AddEventWithStringParams(
NetLogEventType::URL_REQUEST_REDIRECTED, "location",
redirect_info.new_url.possibly_invalid_spec());
}
if (network_delegate())
network_delegate()->NotifyBeforeRedirect(this, redirect_info.new_url);
if (!final_upload_progress_.position() && upload_data_stream_)
final_upload_progress_ = upload_data_stream_->GetUploadProgress();
PrepareToRestart();
bool clear_body = false;
net::RedirectUtil::UpdateHttpRequest(url(), method_, redirect_info,
removed_headers, modified_headers,
&extra_request_headers_, &clear_body);
if (clear_body)
upload_data_stream_.reset();
method_ = redirect_info.new_method;
referrer_ = redirect_info.new_referrer;
referrer_policy_ = redirect_info.new_referrer_policy;
site_for_cookies_ = redirect_info.new_site_for_cookies;
set_isolation_info(isolation_info_.CreateForRedirect(
url::Origin::Create(redirect_info.new_url)),
redirect_info.new_url);
if ((load_flags() & LOAD_CAN_USE_SHARED_DICTIONARY) &&
(load_flags() &
LOAD_DISABLE_SHARED_DICTIONARY_AFTER_CROSS_ORIGIN_REDIRECT) &&
!url::Origin::Create(url()).IsSameOriginWith(redirect_info.new_url)) {
partial_load_flags_ &= ~LOAD_CAN_USE_SHARED_DICTIONARY;
}
url_chain_.push_back(redirect_info.new_url);
--redirect_limit_;
Start();
}
void URLRequest::RetryWithStorageAccess() {
CHECK(!cookie_setting_overrides().Has(
CookieSettingOverride::kStorageAccessGrantEligibleViaHeader));
CHECK(!cookie_setting_overrides().Has(
CookieSettingOverride::kStorageAccessGrantEligible));
net_log_.AddEvent(NetLogEventType::URL_REQUEST_RETRY_WITH_STORAGE_ACCESS);
if (network_delegate()) {
network_delegate()->NotifyBeforeRetry(this);
}
// TODO(https://crbug.com/366284840): this state mutation should reuse the
// Sec- header helpers at a higher layer, not within //net.
cookie_setting_overrides().Put(
CookieSettingOverride::kStorageAccessGrantEligibleViaHeader);
set_per_hop_load_flags(LOAD_BYPASS_CACHE);
set_storage_access_status(CalculateStorageAccessStatus());
// This code is only reachable if the status was previously "inactive", which
// implies that the URL is "potentially trustworthy" and that adding the
// `kStorageAccessGrantEligibleViaHeader` override is sufficient to make the
// status "active".
CHECK(storage_access_status().GetStatusForThirdPartyContext());
CHECK_EQ(static_cast<int>(
storage_access_status().GetStatusForThirdPartyContext().value()),
static_cast<int>(cookie_util::StorageAccessStatus::kActive));
extra_request_headers_.SetHeader("Sec-Fetch-Storage-Access", "active");
base::UmaHistogramEnumeration(
"API.StorageAccessHeader.SecFetchStorageAccessOutcome",
cookie_util::SecFetchStorageAccessOutcome::kValueActive);
if (!final_upload_progress_.position() && upload_data_stream_) {
final_upload_progress_ = upload_data_stream_->GetUploadProgress();
}
PrepareToRestart();
// This isn't really a proper redirect, but we add to the `url_chain_` and
// count it against the redirect limit anyway, to avoid unbounded retries.
url_chain_.push_back(url());
--redirect_limit_;
Start();
}
// static
bool URLRequest::DefaultCanUseCookies() {
return g_default_can_use_cookies;
}
const URLRequestContext* URLRequest::context() const {
return context_;
}
NetworkDelegate* URLRequest::network_delegate() const {
return context_->network_delegate();
}
int64_t URLRequest::GetExpectedContentSize() const {
int64_t expected_content_size = -1;
if (job_.get())
expected_content_size = job_->expected_content_size();
return expected_content_size;
}
void URLRequest::SetPriority(RequestPriority priority) {
DCHECK_GE(priority, MINIMUM_PRIORITY);
DCHECK_LE(priority, MAXIMUM_PRIORITY);
if ((load_flags() & LOAD_IGNORE_LIMITS) && (priority != MAXIMUM_PRIORITY)) {
NOTREACHED();
}
if (priority_ == priority)
return;
priority_ = priority;
net_log_.AddEventWithStringParams(NetLogEventType::URL_REQUEST_SET_PRIORITY,
"priority",
RequestPriorityToString(priority_));
if (job_.get())
job_->SetPriority(priority_);
}
void URLRequest::SetPriorityIncremental(bool priority_incremental) {
priority_incremental_ = priority_incremental;
}
void URLRequest::NotifyAuthRequired(
std::unique_ptr<AuthChallengeInfo> auth_info) {
DCHECK_EQ(OK, status_);
DCHECK(auth_info);
// Check that there are no callbacks to already failed or cancelled requests.
DCHECK(!failed());
delegate_->OnAuthRequired(this, *auth_info.get());
}
void URLRequest::NotifyCertificateRequested(
SSLCertRequestInfo* cert_request_info) {
status_ = OK;
OnCallToDelegate(NetLogEventType::URL_REQUEST_DELEGATE_CERTIFICATE_REQUESTED);
delegate_->OnCertificateRequested(this, cert_request_info);
}
void URLRequest::NotifySSLCertificateError(int net_error,
const SSLInfo& ssl_info,
bool fatal) {
status_ = OK;
OnCallToDelegate(NetLogEventType::URL_REQUEST_DELEGATE_SSL_CERTIFICATE_ERROR);
delegate_->OnSSLCertificateError(this, net_error, ssl_info, fatal);
}
bool URLRequest::CanSetCookie(
const net::CanonicalCookie& cookie,
CookieOptions* options,
const net::FirstPartySetMetadata& first_party_set_metadata,
CookieInclusionStatus* inclusion_status) const {
DCHECK(!(load_flags() & LOAD_DO_NOT_SAVE_COOKIES));
bool can_set_cookies = g_default_can_use_cookies;
if (network_delegate()) {
can_set_cookies = network_delegate()->CanSetCookie(
*this, cookie, options, first_party_set_metadata, inclusion_status);
}
if (!can_set_cookies)
net_log_.AddEvent(NetLogEventType::COOKIE_SET_BLOCKED_BY_NETWORK_DELEGATE);
return can_set_cookies;
}
void URLRequest::NotifyReadCompleted(int bytes_read) {
if (bytes_read > 0)
set_status(OK);
// Notify in case the entire URL Request has been finished.
if (bytes_read <= 0)
NotifyRequestCompleted();
// When URLRequestJob notices there was an error in URLRequest's |status_|,
// it calls this method with |bytes_read| set to -1. Set it to a real error
// here.
// TODO(maksims): NotifyReadCompleted take the error code as an argument on
// failure, rather than -1.
if (bytes_read == -1) {
// |status_| should indicate an error.
DCHECK(failed());
bytes_read = status_;
}
delegate_->OnReadCompleted(this, bytes_read);
// Nothing below this line as OnReadCompleted may delete |this|.
}
void URLRequest::OnHeadersComplete() {
// The URLRequest status should still be IO_PENDING, which it was set to
// before the URLRequestJob was started. On error or cancellation, this
// method should not be called.
DCHECK_EQ(ERR_IO_PENDING, status_);
set_status(OK);
// Cache load timing information now, as information will be lost once the
// socket is closed and the ClientSocketHandle is Reset, which will happen
// once the body is complete. The start times should already be populated.
if (job_.get()) {
// Keep a copy of the two times the URLRequest sets.
base::TimeTicks request_start = load_timing_info_.request_start;
base::Time request_start_time = load_timing_info_.request_start_time;
// Clear load times. Shouldn't be neded, but gives the GetLoadTimingInfo a
// consistent place to start from.
load_timing_info_ = LoadTimingInfo();
job_->GetLoadTimingInfo(&load_timing_info_);
load_timing_info_.request_start = request_start;
load_timing_info_.request_start_time = request_start_time;
ConvertRealLoadTimesToBlockingTimes(&load_timing_info_);
job_->PopulateLoadTimingInternalInfo(&load_timing_internal_info_);
}
}
void URLRequest::NotifyRequestCompleted() {
// TODO(battre): Get rid of this check, according to willchan it should
// not be needed.
if (has_notified_completion_)
return;
is_pending_ = false;
is_redirecting_ = false;
deferred_redirect_info_.reset();
has_notified_completion_ = true;
if (network_delegate())
network_delegate()->NotifyCompleted(this, job_.get() != nullptr, status_);
}
void URLRequest::OnCallToDelegate(NetLogEventType type) {
DCHECK(!calling_delegate_);
DCHECK(blocked_by_.empty());
calling_delegate_ = true;
delegate_event_type_ = type;
net_log_.BeginEvent(type);
}
void URLRequest::OnCallToDelegateComplete(int error) {
// This should have been cleared before resuming the request.
DCHECK(blocked_by_.empty());
if (!calling_delegate_)
return;
calling_delegate_ = false;
net_log_.EndEventWithNetErrorCode(delegate_event_type_, error);
delegate_event_type_ = NetLogEventType::FAILED;
}
void URLRequest::RecordReferrerGranularityMetrics(
bool request_is_same_origin) const {
GURL referrer_url(referrer_);
bool referrer_more_descriptive_than_its_origin =
referrer_url.is_valid() && referrer_url.PathForRequestPiece().size() > 1;
// To avoid renaming the existing enum, we have to use the three-argument
// histogram macro.
if (request_is_same_origin) {
UMA_HISTOGRAM_ENUMERATION(
"Net.URLRequest.ReferrerPolicyForRequest.SameOrigin", referrer_policy_,
static_cast<int>(ReferrerPolicy::MAX) + 1);
UMA_HISTOGRAM_BOOLEAN(
"Net.URLRequest.ReferrerHasInformativePath.SameOrigin",
referrer_more_descriptive_than_its_origin);
} else {
UMA_HISTOGRAM_ENUMERATION(
"Net.URLRequest.ReferrerPolicyForRequest.CrossOrigin", referrer_policy_,
static_cast<int>(ReferrerPolicy::MAX) + 1);
UMA_HISTOGRAM_BOOLEAN(
"Net.URLRequest.ReferrerHasInformativePath.CrossOrigin",
referrer_more_descriptive_than_its_origin);
}
}
IsolationInfo URLRequest::CreateIsolationInfoFromNetworkAnonymizationKey(
const NetworkAnonymizationKey& network_anonymization_key) {
if (!network_anonymization_key.IsFullyPopulated()) {
return IsolationInfo();
}
url::Origin top_frame_origin =
network_anonymization_key.GetTopFrameSite()->site_as_origin_;
std::optional<url::Origin> frame_origin;
if (network_anonymization_key.IsCrossSite()) {
// If we know that the origin is cross site to the top level site, create an
// empty origin to use as the frame origin for the isolation info. This
// should be cross site with the top level origin.
frame_origin = url::Origin();
} else {
// If we don't know that it's cross site to the top level site, use the top
// frame site to set the frame origin.
frame_origin = top_frame_origin;
}
auto isolation_info = IsolationInfo::Create(
IsolationInfo::RequestType::kOther, top_frame_origin,
frame_origin.value(), SiteForCookies(),
network_anonymization_key.GetNonce());
// TODO(crbug.com/40852603): DCHECK isolation info is fully populated.
return isolation_info;
}
ConnectionAttempts URLRequest::GetConnectionAttempts() const {
if (job_)
return job_->GetConnectionAttempts();
return {};
}
void URLRequest::SetRequestHeadersCallback(RequestHeadersCallback callback) {
DCHECK(!job_.get());
DCHECK(request_headers_callback_.is_null());
request_headers_callback_ = std::move(callback);
}
void URLRequest::SetResponseHeadersCallback(ResponseHeadersCallback callback) {
DCHECK(!job_.get());
DCHECK(response_headers_callback_.is_null());
response_headers_callback_ = std::move(callback);
}
void URLRequest::SetEarlyResponseHeadersCallback(
ResponseHeadersCallback callback) {
DCHECK(!job_.get());
DCHECK(early_response_headers_callback_.is_null());
early_response_headers_callback_ = std::move(callback);
}
void URLRequest::SetIsSharedDictionaryReadAllowedCallback(
base::RepeatingCallback<bool()> callback) {
DCHECK(!job_.get());
DCHECK(is_shared_dictionary_read_allowed_callback_.is_null());
is_shared_dictionary_read_allowed_callback_ = std::move(callback);
}
void URLRequest::SetDeviceBoundSessionAccessCallback(
base::RepeatingCallback<void(const device_bound_sessions::SessionAccess&)>
callback) {
device_bound_session_access_callback_ = std::move(callback);
}
void URLRequest::set_socket_tag(const SocketTag& socket_tag) {
DCHECK(!is_pending_);
DCHECK(url().SchemeIsHTTPOrHTTPS());
socket_tag_ = socket_tag;
}
StorageAccessStatusCache URLRequest::CalculateStorageAccessStatus() const {
// `Delegate::OnReceivedRedirect` may set `defer_redirect` inside of
// `URLRequest::ReceivedRedirect` to true, which in turn sets the
// `deferred_redirect_info_` that has to be used when calculating new storage
// access status.
std::optional<net::cookie_util::StorageAccessStatus> storage_access_status =
network_delegate()->GetStorageAccessStatus(*this,
deferred_redirect_info_);
base::UmaHistogramEnumeration(
"API.StorageAccessHeader.StorageAccessStatusOutcome",
storage_access_status
? ConvertSecFetchStorageAccessHeaderValueToOutcome(
storage_access_status.value())
: net::cookie_util::StorageAccessStatusOutcome::kOmittedSameSite);
return StorageAccessStatusCache(storage_access_status);
}
void URLRequest::SetSharedDictionaryGetter(
SharedDictionaryGetter shared_dictionary_getter) {
CHECK(!job_.get());
CHECK(shared_dictionary_getter_.is_null());
shared_dictionary_getter_ = std::move(shared_dictionary_getter);
}
base::WeakPtr<URLRequest> URLRequest::GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
} // namespace net
|