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 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
|
// Copyright 2016 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/http/http_stream_factory_job_controller.h"
#include <string>
#include <utility>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/task/single_thread_task_runner.h"
#include "base/values.h"
#include "net/base/features.h"
#include "net/base/host_mapping_rules.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/base/privacy_mode.h"
#include "net/base/proxy_chain.h"
#include "net/base/proxy_string_util.h"
#include "net/base/session_usage.h"
#include "net/base/url_util.h"
#include "net/http/alternative_service.h"
#include "net/http/bidirectional_stream_impl.h"
#include "net/http/http_stream_key.h"
#include "net/http/http_stream_pool.h"
#include "net/http/http_stream_pool_request_info.h"
#include "net/http/transport_security_state.h"
#include "net/log/net_log.h"
#include "net/log/net_log_event_type.h"
#include "net/log/net_log_with_source.h"
#include "net/proxy_resolution/proxy_info.h"
#include "net/proxy_resolution/proxy_resolution_request.h"
#include "net/proxy_resolution/proxy_resolution_service.h"
#include "net/quic/quic_session_key.h"
#include "net/socket/next_proto.h"
#include "net/spdy/spdy_session.h"
#include "net/third_party/quiche/src/quiche/quic/core/quic_versions.h"
#include "url/gurl.h"
#include "url/scheme_host_port.h"
#include "url/url_constants.h"
namespace net {
namespace {
// Returns parameters associated with the proxy resolution.
base::Value::Dict NetLogHttpStreamJobProxyChainResolved(
const ProxyChain& proxy_chain) {
base::Value::Dict dict;
dict.Set("proxy_chain",
proxy_chain.IsValid() ? proxy_chain.ToDebugString() : std::string());
return dict;
}
GURL CreateAltSvcUrl(const GURL& origin_url,
const HostPortPair& alternative_destination) {
DCHECK(origin_url.is_valid());
DCHECK(origin_url.IsStandard());
GURL::Replacements replacements;
std::string port_str = base::NumberToString(alternative_destination.port());
replacements.SetPortStr(port_str);
replacements.SetHostStr(alternative_destination.host());
return origin_url.ReplaceComponents(replacements);
}
void ConvertWsToHttp(url::SchemeHostPort& input) {
if (base::EqualsCaseInsensitiveASCII(input.scheme(), url::kHttpScheme) ||
base::EqualsCaseInsensitiveASCII(input.scheme(), url::kHttpsScheme)) {
return;
}
if (base::EqualsCaseInsensitiveASCII(input.scheme(), url::kWsScheme)) {
input = url::SchemeHostPort(url::kHttpScheme, input.host(), input.port());
return;
}
DCHECK(base::EqualsCaseInsensitiveASCII(input.scheme(), url::kWssScheme));
input = url::SchemeHostPort(url::kHttpsScheme, input.host(), input.port());
}
void HistogramProxyUsed(const ProxyInfo& proxy_info, bool success) {
const ProxyServer::Scheme max_scheme = ProxyServer::Scheme::SCHEME_QUIC;
ProxyServer::Scheme proxy_scheme = ProxyServer::Scheme::SCHEME_INVALID;
if (!proxy_info.is_empty() && !proxy_info.is_direct()) {
if (proxy_info.proxy_chain().is_multi_proxy()) {
// TODO(crbug.com/40284947): Update this histogram to have a new
// bucket for multi-chain proxies. Until then, don't influence the
// existing metric counts which have historically been only for single-hop
// proxies.
return;
}
proxy_scheme = proxy_info.proxy_chain().is_direct()
? static_cast<ProxyServer::Scheme>(1)
: proxy_info.proxy_chain().First().scheme();
}
if (success) {
UMA_HISTOGRAM_ENUMERATION("Net.HttpJob.ProxyTypeSuccess", proxy_scheme,
max_scheme);
} else {
UMA_HISTOGRAM_ENUMERATION("Net.HttpJob.ProxyTypeFailed", proxy_scheme,
max_scheme);
}
}
// Generate a AlternativeService for DNS alt job. Note: Chrome does not yet
// support different port DNS alpn.
AlternativeService GetAlternativeServiceForDnsJob(const GURL& url) {
return AlternativeService(NextProto::kProtoQUIC, HostPortPair::FromURL(url));
}
base::Value::Dict NetLogAltSvcParams(const AlternativeServiceInfo* alt_svc_info,
bool is_broken) {
base::Value::Dict dict;
dict.Set("alt_svc", alt_svc_info->ToString());
dict.Set("is_broken", is_broken);
return dict;
}
} // namespace
// The maximum time to wait for the alternate job to complete before resuming
// the main job.
const int kMaxDelayTimeForMainJobSecs = 3;
HttpStreamFactory::JobController::JobController(
HttpStreamFactory* factory,
HttpStreamRequest::Delegate* delegate,
HttpNetworkSession* session,
JobFactory* job_factory,
const HttpRequestInfo& http_request_info,
bool is_preconnect,
bool is_websocket,
bool enable_ip_based_pooling,
bool enable_alternative_services,
bool delay_main_job_with_available_spdy_session,
const std::vector<SSLConfig::CertAndStatus>& allowed_bad_certs)
: factory_(factory),
session_(session),
job_factory_(job_factory),
delegate_(delegate),
is_preconnect_(is_preconnect),
is_websocket_(is_websocket),
enable_ip_based_pooling_(enable_ip_based_pooling),
enable_alternative_services_(enable_alternative_services),
delay_main_job_with_available_spdy_session_(
delay_main_job_with_available_spdy_session),
management_config_(http_request_info.connection_management_config),
http_request_info_url_(http_request_info.url),
origin_url_(DuplicateUrlWithHostMappingRules(http_request_info.url)),
request_info_(http_request_info),
allowed_bad_certs_(allowed_bad_certs),
net_log_(NetLogWithSource::Make(
session->net_log(),
NetLogSourceType::HTTP_STREAM_JOB_CONTROLLER)) {
DCHECK(factory_);
DCHECK(session_);
DCHECK(job_factory_);
DCHECK(base::EqualsCaseInsensitiveASCII(origin_url_.scheme_piece(),
url::kHttpScheme) ||
base::EqualsCaseInsensitiveASCII(origin_url_.scheme_piece(),
url::kHttpsScheme) ||
base::EqualsCaseInsensitiveASCII(origin_url_.scheme_piece(),
url::kWsScheme) ||
base::EqualsCaseInsensitiveASCII(origin_url_.scheme_piece(),
url::kWssScheme));
net_log_.BeginEvent(NetLogEventType::HTTP_STREAM_JOB_CONTROLLER, [&] {
base::Value::Dict dict;
dict.Set("url", http_request_info.url.possibly_invalid_spec());
if (origin_url_ != http_request_info.url) {
dict.Set("url_after_host_mapping", origin_url_.possibly_invalid_spec());
}
dict.Set("is_preconnect", is_preconnect_);
dict.Set("privacy_mode",
PrivacyModeToDebugString(request_info_.privacy_mode));
base::Value::List allowed_bad_certs_list;
for (const auto& cert_and_status : allowed_bad_certs_) {
allowed_bad_certs_list.Append(
cert_and_status.cert->subject().GetDisplayName());
}
dict.Set("allowed_bad_certs", std::move(allowed_bad_certs_list));
return dict;
});
}
HttpStreamFactory::JobController::~JobController() {
bound_job_ = nullptr;
main_job_.reset();
alternative_job_.reset();
dns_alpn_h3_job_.reset();
if (proxy_resolve_request_) {
DCHECK_EQ(STATE_RESOLVE_PROXY_COMPLETE, next_state_);
proxy_resolve_request_.reset();
}
net_log_.EndEvent(NetLogEventType::HTTP_STREAM_JOB_CONTROLLER);
}
std::unique_ptr<HttpStreamRequest> HttpStreamFactory::JobController::Start(
HttpStreamRequest::Delegate* delegate,
WebSocketHandshakeStreamBase::CreateHelper*
websocket_handshake_stream_create_helper,
const NetLogWithSource& source_net_log,
HttpStreamRequest::StreamType stream_type,
RequestPriority priority) {
DCHECK(!request_);
stream_type_ = stream_type;
priority_ = priority;
auto request = std::make_unique<HttpStreamRequest>(
this, websocket_handshake_stream_create_helper, source_net_log,
stream_type);
// Keep a raw pointer but release ownership of HttpStreamRequest instance.
request_ = request.get();
// Associates |net_log_| with |source_net_log|.
source_net_log.AddEventReferencingSource(
NetLogEventType::HTTP_STREAM_JOB_CONTROLLER_BOUND, net_log_.source());
net_log_.AddEventReferencingSource(
NetLogEventType::HTTP_STREAM_JOB_CONTROLLER_BOUND,
source_net_log.source());
RunLoop(OK);
return request;
}
void HttpStreamFactory::JobController::Preconnect(int num_streams) {
DCHECK(!main_job_);
DCHECK(!alternative_job_);
DCHECK(is_preconnect_);
stream_type_ = HttpStreamRequest::HTTP_STREAM;
num_streams_ = num_streams;
RunLoop(OK);
}
LoadState HttpStreamFactory::JobController::GetLoadState() const {
DCHECK(request_);
if (next_state_ == STATE_RESOLVE_PROXY_COMPLETE) {
return proxy_resolve_request_->GetLoadState();
}
if (bound_job_) {
return bound_job_->GetLoadState();
}
if (main_job_) {
return main_job_->GetLoadState();
}
if (alternative_job_) {
return alternative_job_->GetLoadState();
}
if (dns_alpn_h3_job_) {
return dns_alpn_h3_job_->GetLoadState();
}
// When proxy resolution fails, there is no job created and
// NotifyRequestFailed() is executed one message loop iteration later.
return LOAD_STATE_IDLE;
}
void HttpStreamFactory::JobController::OnRequestComplete() {
DCHECK(request_);
CHECK(!switched_to_http_stream_pool_);
request_ = nullptr;
// This is called when the delegate is destroying its HttpStreamRequest, so
// it's no longer safe to call into it after this point.
delegate_ = nullptr;
if (!job_bound_) {
alternative_job_.reset();
main_job_.reset();
dns_alpn_h3_job_.reset();
} else {
if (bound_job_->job_type() == MAIN) {
bound_job_ = nullptr;
main_job_.reset();
} else if (bound_job_->job_type() == ALTERNATIVE) {
bound_job_ = nullptr;
alternative_job_.reset();
} else {
DCHECK(bound_job_->job_type() == DNS_ALPN_H3);
bound_job_ = nullptr;
dns_alpn_h3_job_.reset();
}
}
MaybeNotifyFactoryOfCompletion();
}
int HttpStreamFactory::JobController::RestartTunnelWithProxyAuth() {
DCHECK(bound_job_);
return bound_job_->RestartTunnelWithProxyAuth();
}
void HttpStreamFactory::JobController::SetPriority(RequestPriority priority) {
if (main_job_) {
main_job_->SetPriority(priority);
}
if (alternative_job_) {
alternative_job_->SetPriority(priority);
}
if (dns_alpn_h3_job_) {
dns_alpn_h3_job_->SetPriority(priority);
}
if (preconnect_backup_job_) {
preconnect_backup_job_->SetPriority(priority);
}
}
void HttpStreamFactory::JobController::OnStreamReady(Job* job) {
DCHECK(job);
if (IsJobOrphaned(job)) {
// We have bound a job to the associated HttpStreamRequest, |job| has been
// orphaned.
OnOrphanedJobComplete(job);
return;
}
std::unique_ptr<HttpStream> stream = job->ReleaseStream();
DCHECK(stream);
MarkRequestComplete(job);
if (!request_) {
return;
}
DCHECK(!is_websocket_);
DCHECK_EQ(HttpStreamRequest::HTTP_STREAM, request_->stream_type());
OnJobSucceeded(job);
// TODO(bnc): Remove when https://crbug.com/461981 is fixed.
CHECK(request_);
DCHECK(request_->completed());
HistogramProxyUsed(job->proxy_info(), /*success=*/true);
delegate_->OnStreamReady(job->proxy_info(), std::move(stream));
}
void HttpStreamFactory::JobController::OnBidirectionalStreamImplReady(
Job* job,
const ProxyInfo& used_proxy_info) {
DCHECK(job);
if (IsJobOrphaned(job)) {
// We have bound a job to the associated HttpStreamRequest, |job| has been
// orphaned.
OnOrphanedJobComplete(job);
return;
}
MarkRequestComplete(job);
if (!request_) {
return;
}
std::unique_ptr<BidirectionalStreamImpl> stream =
job->ReleaseBidirectionalStream();
DCHECK(stream);
DCHECK(!is_websocket_);
DCHECK_EQ(HttpStreamRequest::BIDIRECTIONAL_STREAM, request_->stream_type());
OnJobSucceeded(job);
DCHECK(request_->completed());
delegate_->OnBidirectionalStreamImplReady(used_proxy_info, std::move(stream));
}
void HttpStreamFactory::JobController::OnWebSocketHandshakeStreamReady(
Job* job,
const ProxyInfo& used_proxy_info,
std::unique_ptr<WebSocketHandshakeStreamBase> stream) {
DCHECK(job);
MarkRequestComplete(job);
if (!request_) {
return;
}
DCHECK(is_websocket_);
DCHECK_EQ(HttpStreamRequest::HTTP_STREAM, request_->stream_type());
DCHECK(stream);
OnJobSucceeded(job);
DCHECK(request_->completed());
delegate_->OnWebSocketHandshakeStreamReady(used_proxy_info,
std::move(stream));
}
void HttpStreamFactory::JobController::OnQuicHostResolution(
const url::SchemeHostPort& destination,
base::TimeTicks dns_resolution_start_time,
base::TimeTicks dns_resolution_end_time) {
if (!request_) {
return;
}
if (destination != url::SchemeHostPort(origin_url_)) {
// Ignores different destination alternative job's DNS resolution time.
return;
}
// QUIC jobs (ALTERNATIVE, DNS_ALPN_H3) are started before the non-QUIC (MAIN)
// job. So we set the DNS resolution overrides to use the DNS timing of the
// QUIC jobs.
request_->SetDnsResolutionTimeOverrides(dns_resolution_start_time,
dns_resolution_end_time);
}
void HttpStreamFactory::JobController::OnStreamFailed(Job* job, int status) {
DCHECK_NE(OK, status);
if (job->job_type() == MAIN) {
DCHECK_EQ(main_job_.get(), job);
main_job_net_error_ = status;
} else if (job->job_type() == ALTERNATIVE) {
DCHECK_EQ(alternative_job_.get(), job);
DCHECK_NE(NextProto::kProtoUnknown, alternative_service_info_.protocol());
alternative_job_net_error_ = status;
} else {
DCHECK_EQ(job->job_type(), DNS_ALPN_H3);
DCHECK_EQ(dns_alpn_h3_job_.get(), job);
dns_alpn_h3_job_net_error_ = status;
}
MaybeResumeMainJob(job, base::TimeDelta());
if (IsJobOrphaned(job)) {
// We have bound a job to the associated HttpStreamRequest, |job| has been
// orphaned.
OnOrphanedJobComplete(job);
return;
}
if (!request_) {
return;
}
DCHECK_NE(OK, status);
DCHECK(job);
if (!bound_job_) {
if (GetJobCount() >= 2) {
// Hey, we've got other jobs! Maybe one of them will succeed, let's just
// ignore this failure.
if (job->job_type() == MAIN) {
DCHECK_EQ(main_job_.get(), job);
main_job_.reset();
} else if (job->job_type() == ALTERNATIVE) {
DCHECK_EQ(alternative_job_.get(), job);
alternative_job_.reset();
} else {
DCHECK_EQ(job->job_type(), DNS_ALPN_H3);
DCHECK_EQ(dns_alpn_h3_job_.get(), job);
dns_alpn_h3_job_.reset();
}
return;
} else {
BindJob(job);
}
}
status = ReconsiderProxyAfterError(job, status);
if (next_state_ == STATE_RESOLVE_PROXY_COMPLETE) {
if (status == ERR_IO_PENDING) {
return;
}
DCHECK_EQ(OK, status);
RunLoop(status);
return;
}
HistogramProxyUsed(job->proxy_info(), /*success=*/false);
delegate_->OnStreamFailed(status, *job->net_error_details(),
job->proxy_info(), job->resolve_error_info());
}
void HttpStreamFactory::JobController::OnFailedOnDefaultNetwork(Job* job) {
if (job->job_type() == ALTERNATIVE) {
DCHECK_EQ(alternative_job_.get(), job);
alternative_job_failed_on_default_network_ = true;
} else {
DCHECK_EQ(job->job_type(), DNS_ALPN_H3);
DCHECK_EQ(dns_alpn_h3_job_.get(), job);
dns_alpn_h3_job_failed_on_default_network_ = true;
}
}
void HttpStreamFactory::JobController::OnCertificateError(
Job* job,
int status,
const SSLInfo& ssl_info) {
MaybeResumeMainJob(job, base::TimeDelta());
if (IsJobOrphaned(job)) {
// We have bound a job to the associated HttpStreamRequest, |job| has been
// orphaned.
OnOrphanedJobComplete(job);
return;
}
if (!request_) {
return;
}
DCHECK_NE(OK, status);
if (!bound_job_) {
BindJob(job);
}
delegate_->OnCertificateError(status, ssl_info);
}
void HttpStreamFactory::JobController::OnNeedsClientAuth(
Job* job,
SSLCertRequestInfo* cert_info) {
MaybeResumeMainJob(job, base::TimeDelta());
if (IsJobOrphaned(job)) {
// We have bound a job to the associated HttpStreamRequest, |job| has been
// orphaned.
OnOrphanedJobComplete(job);
return;
}
if (!request_) {
return;
}
if (!bound_job_) {
BindJob(job);
}
delegate_->OnNeedsClientAuth(cert_info);
}
void HttpStreamFactory::JobController::OnNeedsProxyAuth(
Job* job,
const HttpResponseInfo& proxy_response,
const ProxyInfo& used_proxy_info,
HttpAuthController* auth_controller) {
MaybeResumeMainJob(job, base::TimeDelta());
if (IsJobOrphaned(job)) {
// We have bound a job to the associated HttpStreamRequest, |job| has been
// orphaned.
OnOrphanedJobComplete(job);
return;
}
if (!request_) {
return;
}
if (!bound_job_) {
BindJob(job);
}
delegate_->OnNeedsProxyAuth(proxy_response, used_proxy_info, auth_controller);
}
void HttpStreamFactory::JobController::OnPreconnectsComplete(Job* job,
int result) {
// Preconnects only run as `main_job_`, never `alternative_job_` or
// `dns_alpn_h3_job_`.
DCHECK_EQ(main_job_.get(), job);
// If the job failed because there were no matching HTTPS records in DNS, run
// the backup job. A TCP-based protocol may work instead.
if (result == ERR_DNS_NO_MATCHING_SUPPORTED_ALPN && preconnect_backup_job_) {
DCHECK_EQ(job->job_type(), PRECONNECT_DNS_ALPN_H3);
main_job_ = std::move(preconnect_backup_job_);
main_job_->Preconnect(num_streams_);
return;
}
main_job_.reset();
preconnect_backup_job_.reset();
ResetErrorStatusForJobs();
factory_->OnPreconnectsCompleteInternal();
MaybeNotifyFactoryOfCompletion();
}
void HttpStreamFactory::JobController::OnOrphanedJobComplete(const Job* job) {
if (job->job_type() == MAIN) {
DCHECK_EQ(main_job_.get(), job);
main_job_.reset();
} else if (job->job_type() == ALTERNATIVE) {
DCHECK_EQ(alternative_job_.get(), job);
alternative_job_.reset();
} else {
DCHECK_EQ(job->job_type(), DNS_ALPN_H3);
DCHECK_EQ(dns_alpn_h3_job_.get(), job);
dns_alpn_h3_job_.reset();
}
MaybeNotifyFactoryOfCompletion();
}
void HttpStreamFactory::JobController::AddConnectionAttemptsToRequest(
Job* job,
const ConnectionAttempts& attempts) {
if (is_preconnect_ || IsJobOrphaned(job)) {
return;
}
request_->AddConnectionAttempts(attempts);
}
void HttpStreamFactory::JobController::ResumeMainJobLater(
const base::TimeDelta& delay) {
net_log_.AddEventWithInt64Params(NetLogEventType::HTTP_STREAM_JOB_DELAYED,
"delay", delay.InMilliseconds());
resume_main_job_callback_.Reset(
base::BindOnce(&HttpStreamFactory::JobController::ResumeMainJob,
ptr_factory_.GetWeakPtr()));
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE, resume_main_job_callback_.callback(), delay);
}
void HttpStreamFactory::JobController::ResumeMainJob() {
DCHECK(main_job_);
if (main_job_is_resumed_) {
return;
}
main_job_is_resumed_ = true;
main_job_->net_log().AddEventWithInt64Params(
NetLogEventType::HTTP_STREAM_JOB_RESUMED, "delay",
main_job_wait_time_.InMilliseconds());
main_job_->Resume();
main_job_wait_time_ = base::TimeDelta();
}
void HttpStreamFactory::JobController::ResetErrorStatusForJobs() {
main_job_net_error_ = OK;
alternative_job_net_error_ = OK;
alternative_job_failed_on_default_network_ = false;
dns_alpn_h3_job_net_error_ = OK;
dns_alpn_h3_job_failed_on_default_network_ = false;
}
void HttpStreamFactory::JobController::MaybeResumeMainJob(
Job* job,
const base::TimeDelta& delay) {
DCHECK(delay == base::TimeDelta() || delay == main_job_wait_time_);
DCHECK(job == main_job_.get() || job == alternative_job_.get() ||
job == dns_alpn_h3_job_.get());
if (job == main_job_.get()) {
return;
}
if (job == dns_alpn_h3_job_.get() && alternative_job_) {
return;
}
if (!main_job_) {
return;
}
main_job_is_blocked_ = false;
if (!main_job_->is_waiting()) {
// There are two cases where the main job is not in WAIT state:
// 1) The main job hasn't got to waiting state, do not yet post a task to
// resume since that will happen in ShouldWait().
// 2) The main job has passed waiting state, so the main job does not need
// to be resumed.
return;
}
main_job_wait_time_ = delay;
ResumeMainJobLater(main_job_wait_time_);
}
void HttpStreamFactory::JobController::OnConnectionInitialized(Job* job,
int rv) {
if (rv != OK) {
// Resume the main job as there's an error raised in connection
// initiation.
return MaybeResumeMainJob(job, main_job_wait_time_);
}
}
bool HttpStreamFactory::JobController::ShouldWait(Job* job) {
// The alternative job never waits.
if (job == alternative_job_.get() || job == dns_alpn_h3_job_.get()) {
return false;
}
DCHECK_EQ(main_job_.get(), job);
if (main_job_is_blocked_) {
return true;
}
if (main_job_wait_time_.is_zero()) {
return false;
}
ResumeMainJobLater(main_job_wait_time_);
return true;
}
const NetLogWithSource* HttpStreamFactory::JobController::GetNetLog() const {
return &net_log_;
}
void HttpStreamFactory::JobController::MaybeSetWaitTimeForMainJob(
const base::TimeDelta& delay) {
if (main_job_is_blocked_) {
const bool has_available_spdy_session =
main_job_->HasAvailableSpdySession();
if (!delay_main_job_with_available_spdy_session_ &&
has_available_spdy_session) {
main_job_wait_time_ = base::TimeDelta();
} else {
main_job_wait_time_ =
std::min(delay, base::Seconds(kMaxDelayTimeForMainJobSecs));
}
if (has_available_spdy_session) {
UMA_HISTOGRAM_TIMES("Net.HttpJob.MainJobWaitTimeWithAvailableSpdySession",
main_job_wait_time_);
} else {
UMA_HISTOGRAM_TIMES(
"Net.HttpJob.MainJobWaitTimeWithoutAvailableSpdySession",
main_job_wait_time_);
}
}
}
bool HttpStreamFactory::JobController::HasPendingMainJob() const {
return main_job_.get() != nullptr;
}
bool HttpStreamFactory::JobController::HasPendingAltJob() const {
return alternative_job_.get() != nullptr;
}
WebSocketHandshakeStreamBase::CreateHelper*
HttpStreamFactory::JobController::websocket_handshake_stream_create_helper() {
DCHECK(request_);
return request_->websocket_handshake_stream_create_helper();
}
void HttpStreamFactory::JobController::OnIOComplete(int result) {
RunLoop(result);
}
void HttpStreamFactory::JobController::RunLoop(int result) {
int rv = DoLoop(result);
if (rv == ERR_IO_PENDING) {
return;
}
if (rv != OK) {
// DoLoop can only fail during proxy resolution step which happens before
// any jobs are created. Notify |request_| of the failure one message loop
// iteration later to avoid re-entrancy.
DCHECK(!main_job_);
DCHECK(!alternative_job_);
DCHECK(!dns_alpn_h3_job_);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&HttpStreamFactory::JobController::NotifyRequestFailed,
ptr_factory_.GetWeakPtr(), rv));
}
}
int HttpStreamFactory::JobController::DoLoop(int rv) {
DCHECK_NE(next_state_, STATE_NONE);
do {
State state = next_state_;
next_state_ = STATE_NONE;
switch (state) {
case STATE_RESOLVE_PROXY:
DCHECK_EQ(OK, rv);
rv = DoResolveProxy();
break;
case STATE_RESOLVE_PROXY_COMPLETE:
rv = DoResolveProxyComplete(rv);
break;
case STATE_CREATE_JOBS:
DCHECK_EQ(OK, rv);
rv = DoCreateJobs();
break;
default:
NOTREACHED() << "bad state";
}
} while (next_state_ != STATE_NONE && rv != ERR_IO_PENDING);
return rv;
}
int HttpStreamFactory::JobController::DoResolveProxy() {
DCHECK(!proxy_resolve_request_);
next_state_ = STATE_RESOLVE_PROXY_COMPLETE;
if (request_info_.load_flags & LOAD_BYPASS_PROXY) {
proxy_info_.UseDirect();
return OK;
}
CompletionOnceCallback io_callback =
base::BindOnce(&JobController::OnIOComplete, base::Unretained(this));
return session_->proxy_resolution_service()->ResolveProxy(
origin_url_, request_info_.method,
request_info_.network_anonymization_key, &proxy_info_,
std::move(io_callback), &proxy_resolve_request_, net_log_);
}
int HttpStreamFactory::JobController::DoResolveProxyComplete(int rv) {
DCHECK_NE(ERR_IO_PENDING, rv);
proxy_resolve_request_ = nullptr;
net_log_.AddEvent(
NetLogEventType::HTTP_STREAM_JOB_CONTROLLER_PROXY_SERVER_RESOLVED, [&] {
return NetLogHttpStreamJobProxyChainResolved(
proxy_info_.is_empty() ? ProxyChain() : proxy_info_.proxy_chain());
});
if (rv != OK) {
return rv;
}
// Remove unsupported proxies from the list.
int supported_proxies = ProxyServer::SCHEME_HTTP | ProxyServer::SCHEME_HTTPS |
ProxyServer::SCHEME_SOCKS4 |
ProxyServer::SCHEME_SOCKS5;
// WebSockets is not supported over QUIC.
if (session_->IsQuicEnabled() && !is_websocket_) {
supported_proxies |= ProxyServer::SCHEME_QUIC;
}
proxy_info_.RemoveProxiesWithoutScheme(supported_proxies);
if (proxy_info_.is_empty()) {
// No proxies/direct to choose from.
return ERR_NO_SUPPORTED_PROXIES;
}
next_state_ = STATE_CREATE_JOBS;
return rv;
}
int HttpStreamFactory::JobController::DoCreateJobs() {
DCHECK(!main_job_);
DCHECK(!alternative_job_);
DCHECK(origin_url_.is_valid());
DCHECK(origin_url_.IsStandard());
url::SchemeHostPort destination(origin_url_);
DCHECK(destination.IsValid());
ConvertWsToHttp(destination);
// Create an alternative job if alternative service is set up for this domain.
// This is applicable even if the connection will be made via a proxy.
alternative_service_info_ = GetAlternativeServiceInfoFor(
http_request_info_url_, request_info_, delegate_, stream_type_);
if (session_->host_resolver()->IsHappyEyeballsV3Enabled() &&
proxy_info_.is_direct() && !is_websocket_) {
SwitchToHttpStreamPool();
return OK;
}
quic::ParsedQuicVersion quic_version = quic::ParsedQuicVersion::Unsupported();
if (alternative_service_info_.protocol() == NextProto::kProtoQUIC) {
quic_version =
SelectQuicVersion(alternative_service_info_.advertised_versions());
DCHECK_NE(quic_version, quic::ParsedQuicVersion::Unsupported());
}
// Getting ALPN for H3 from DNS has a lot of preconditions. Among them:
// - proxied connections perform DNS on the proxy, so they can't get supported
// ALPNs from DNS
const bool dns_alpn_h3_job_enabled =
!session_->ShouldForceQuic(destination, proxy_info_, is_websocket_) &&
enable_alternative_services_ &&
session_->params().use_dns_https_svcb_alpn &&
base::EqualsCaseInsensitiveASCII(origin_url_.scheme(),
url::kHttpsScheme) &&
session_->IsQuicEnabled() && proxy_info_.is_direct() &&
!session_->http_server_properties()->IsAlternativeServiceBroken(
GetAlternativeServiceForDnsJob(origin_url_),
request_info_.network_anonymization_key);
if (is_preconnect_) {
// Due to how the socket pools handle priorities and idle sockets, only IDLE
// priority currently makes sense for preconnects. The priority for
// preconnects is currently ignored (see RequestSocketsForPool()), but could
// be used at some point for proxy resolution or something.
// Note: When `dns_alpn_h3_job_enabled` is true, we create a
// PRECONNECT_DNS_ALPN_H3 job. If no matching HTTPS DNS ALPN records are
// received, the PRECONNECT_DNS_ALPN_H3 job will fail with
// ERR_DNS_NO_MATCHING_SUPPORTED_ALPN, and `preconnect_backup_job_` will
// be started in OnPreconnectsComplete().
std::unique_ptr<Job> preconnect_job = job_factory_->CreateJob(
this, dns_alpn_h3_job_enabled ? PRECONNECT_DNS_ALPN_H3 : PRECONNECT,
session_, request_info_, IDLE, proxy_info_, allowed_bad_certs_,
destination, origin_url_, is_websocket_, enable_ip_based_pooling_,
net_log_.net_log(), NextProto::kProtoUnknown,
quic::ParsedQuicVersion::Unsupported(), management_config_);
// When there is an valid alternative service info, and `preconnect_job`
// has no existing QUIC session, create a job for the alternative service.
if (alternative_service_info_.protocol() != NextProto::kProtoUnknown &&
!preconnect_job->HasAvailableQuicSession()) {
GURL alternative_url = CreateAltSvcUrl(
origin_url_, alternative_service_info_.GetHostPortPair());
RewriteUrlWithHostMappingRules(alternative_url);
url::SchemeHostPort alternative_destination =
url::SchemeHostPort(alternative_url);
ConvertWsToHttp(alternative_destination);
main_job_ = job_factory_->CreateJob(
this, PRECONNECT, session_, request_info_, IDLE, proxy_info_,
allowed_bad_certs_, std::move(alternative_destination), origin_url_,
is_websocket_, enable_ip_based_pooling_, session_->net_log(),
alternative_service_info_.protocol(), quic_version,
management_config_);
} else {
main_job_ = std::move(preconnect_job);
if (dns_alpn_h3_job_enabled) {
preconnect_backup_job_ = job_factory_->CreateJob(
this, PRECONNECT, session_, request_info_, IDLE, proxy_info_,
allowed_bad_certs_, std::move(destination), origin_url_,
is_websocket_, enable_ip_based_pooling_, net_log_.net_log(),
NextProto::kProtoUnknown, quic::ParsedQuicVersion::Unsupported(),
management_config_);
}
}
main_job_->Preconnect(num_streams_);
return OK;
}
main_job_ = job_factory_->CreateJob(
this, MAIN, session_, request_info_, priority_, proxy_info_,
allowed_bad_certs_, std::move(destination), origin_url_, is_websocket_,
enable_ip_based_pooling_, net_log_.net_log(), NextProto::kProtoUnknown,
quic::ParsedQuicVersion::Unsupported(), management_config_);
// Alternative Service can only be set for HTTPS requests while Alternative
// Proxy is set for HTTP requests.
// The main job may use HTTP/3 if the origin is specified in
// `--origin-to-force-quic-on` switch. In that case, do not create
// `alternative_job_` and `dns_alpn_h3_job_`.
if ((alternative_service_info_.protocol() != NextProto::kProtoUnknown) &&
!main_job_->using_quic()) {
DCHECK(origin_url_.SchemeIs(url::kHttpsScheme));
DCHECK(!is_websocket_);
DVLOG(1) << "Selected alternative service (host: "
<< alternative_service_info_.GetHostPortPair().host()
<< " port: " << alternative_service_info_.GetHostPortPair().port()
<< " version: " << quic_version << ")";
GURL alternative_url = CreateAltSvcUrl(
origin_url_, alternative_service_info_.GetHostPortPair());
RewriteUrlWithHostMappingRules(alternative_url);
url::SchemeHostPort alternative_destination =
url::SchemeHostPort(alternative_url);
ConvertWsToHttp(alternative_destination);
alternative_job_ = job_factory_->CreateJob(
this, ALTERNATIVE, session_, request_info_, priority_, proxy_info_,
allowed_bad_certs_, std::move(alternative_destination), origin_url_,
is_websocket_, enable_ip_based_pooling_, net_log_.net_log(),
alternative_service_info_.protocol(), quic_version, management_config_);
}
if (dns_alpn_h3_job_enabled && !main_job_->using_quic()) {
DCHECK(!is_websocket_);
url::SchemeHostPort dns_alpn_h3_destination =
url::SchemeHostPort(origin_url_);
dns_alpn_h3_job_ = job_factory_->CreateJob(
this, DNS_ALPN_H3, session_, request_info_, priority_, proxy_info_,
allowed_bad_certs_, std::move(dns_alpn_h3_destination), origin_url_,
is_websocket_, enable_ip_based_pooling_, net_log_.net_log(),
NextProto::kProtoUnknown, quic::ParsedQuicVersion::Unsupported(),
management_config_);
}
ClearInappropriateJobs();
if (main_job_ && (alternative_job_ ||
(dns_alpn_h3_job_ &&
(!main_job_->TargettedSocketGroupHasActiveSocket() &&
!main_job_->HasAvailableSpdySession())))) {
// We don't block |main_job_| when |alternative_job_| doesn't exists and
// |dns_alpn_h3_job_| exists and an active socket is available for
// |main_job_|. This is intended to make the fallback logic faster.
main_job_is_blocked_ = true;
}
if (alternative_job_) {
alternative_job_->Start(request_->stream_type());
}
if (dns_alpn_h3_job_) {
dns_alpn_h3_job_->Start(request_->stream_type());
}
if (main_job_) {
main_job_->Start(request_->stream_type());
}
return OK;
}
void HttpStreamFactory::JobController::ClearInappropriateJobs() {
if (dns_alpn_h3_job_ && dns_alpn_h3_job_->HasAvailableQuicSession()) {
// Clear |main_job_| and |alternative_job_| here not to start them when
// there is an active session available for |dns_alpn_h3_job_|.
main_job_.reset();
alternative_job_.reset();
}
if (alternative_job_ && dns_alpn_h3_job_ &&
(alternative_job_->HasAvailableQuicSession() ||
(alternative_service_info_.alternative_service() ==
GetAlternativeServiceForDnsJob(http_request_info_url_)))) {
// Clear |dns_alpn_h3_job_|, when there is an active session available for
// |alternative_job_| or |alternative_job_| was created for the same
// destination.
dns_alpn_h3_job_.reset();
}
}
void HttpStreamFactory::JobController::BindJob(Job* job) {
DCHECK(request_);
DCHECK(job);
DCHECK(job == alternative_job_.get() || job == main_job_.get() ||
job == dns_alpn_h3_job_.get());
DCHECK(!job_bound_);
DCHECK(!bound_job_);
job_bound_ = true;
bound_job_ = job;
request_->net_log().AddEventReferencingSource(
NetLogEventType::HTTP_STREAM_REQUEST_BOUND_TO_JOB,
job->net_log().source());
job->net_log().AddEventReferencingSource(
NetLogEventType::HTTP_STREAM_JOB_BOUND_TO_REQUEST,
request_->net_log().source());
OrphanUnboundJob();
}
void HttpStreamFactory::JobController::OrphanUnboundJob() {
DCHECK(request_);
DCHECK(bound_job_);
if (bound_job_->job_type() == MAIN) {
// Allow |alternative_job_| and |dns_alpn_h3_job_| to run to completion,
// rather than resetting them to check if there is any broken alternative
// service to report. OnOrphanedJobComplete() will clean up |this| when the
// jobs complete.
if (alternative_job_) {
DCHECK(!is_websocket_);
alternative_job_->Orphan();
}
if (dns_alpn_h3_job_) {
DCHECK(!is_websocket_);
dns_alpn_h3_job_->Orphan();
}
return;
}
if (bound_job_->job_type() == ALTERNATIVE) {
if (!alternative_job_failed_on_default_network_ && !dns_alpn_h3_job_) {
// |request_| is bound to the alternative job and the alternative job
// succeeds on the default network, and there is no DNS alt job. This
// means that the main job is no longer needed, so cancel it now. Pending
// ConnectJobs will return established sockets to socket pools if
// applicable.
// https://crbug.com/757548.
// The main job still needs to run if the alternative job succeeds on the
// alternate network in order to figure out whether QUIC should be marked
// as broken until the default network changes. And also the main job
// still needs to run if the DNS alt job exists to figure out whether
// the DNS alpn service is broken.
DCHECK(!main_job_ || (alternative_job_net_error_ == OK));
main_job_.reset();
}
// Allow |dns_alpn_h3_job_| to run to completion, rather than resetting
// it to check if there is any broken alternative service to report.
// OnOrphanedJobComplete() will clean up |this| when the job completes.
if (dns_alpn_h3_job_) {
DCHECK(!is_websocket_);
dns_alpn_h3_job_->Orphan();
}
}
if (bound_job_->job_type() == DNS_ALPN_H3) {
if (!dns_alpn_h3_job_failed_on_default_network_ && !alternative_job_) {
DCHECK(!main_job_ || (dns_alpn_h3_job_net_error_ == OK));
main_job_.reset();
}
// Allow |alternative_job_| to run to completion, rather than resetting
// it to check if there is any broken alternative service to report.
// OnOrphanedJobComplete() will clean up |this| when the job completes.
if (alternative_job_) {
DCHECK(!is_websocket_);
alternative_job_->Orphan();
}
}
}
void HttpStreamFactory::JobController::OnJobSucceeded(Job* job) {
DCHECK(job);
if (!bound_job_) {
BindJob(job);
return;
}
}
void HttpStreamFactory::JobController::MarkRequestComplete(Job* job) {
if (request_) {
AlternateProtocolUsage alternate_protocol_usage =
CalculateAlternateProtocolUsage(job);
request_->Complete(job->negotiated_protocol(), alternate_protocol_usage);
ReportAlternateProtocolUsage(alternate_protocol_usage,
HasGoogleHost(job->origin_url()));
}
}
void HttpStreamFactory::JobController::MaybeReportBrokenAlternativeService(
const AlternativeService& alt_service,
int alt_job_net_error,
bool alt_job_failed_on_default_network,
const std::string& histogram_name_for_failure) {
// If alternative job succeeds on the default network, no brokenness to
// report.
if (alt_job_net_error == OK && !alt_job_failed_on_default_network) {
return;
}
// No brokenness to report if the main job fails.
if (main_job_net_error_ != OK) {
return;
}
// No need to record DNS_NO_MATCHING_SUPPORTED_ALPN error.
if (alt_job_net_error == ERR_DNS_NO_MATCHING_SUPPORTED_ALPN) {
return;
}
if (alt_job_failed_on_default_network && alt_job_net_error == OK) {
// Alternative job failed on the default network but succeeds on the
// non-default network, mark alternative service broken until the default
// network changes.
session_->http_server_properties()
->MarkAlternativeServiceBrokenUntilDefaultNetworkChanges(
alt_service, request_info_.network_anonymization_key);
return;
}
if (alt_job_net_error == ERR_NETWORK_CHANGED ||
alt_job_net_error == ERR_INTERNET_DISCONNECTED ||
(alt_job_net_error == ERR_NAME_NOT_RESOLVED &&
http_request_info_url_.host() == alt_service.host)) {
// No need to mark alternative service as broken.
return;
}
// Report brokenness if alternative job failed.
base::UmaHistogramSparse(histogram_name_for_failure, -alt_job_net_error);
HistogramBrokenAlternateProtocolLocation(
BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_STREAM_FACTORY_JOB_ALT);
session_->http_server_properties()->MarkAlternativeServiceBroken(
alt_service, request_info_.network_anonymization_key);
}
void HttpStreamFactory::JobController::MaybeNotifyFactoryOfCompletion() {
if (switched_to_http_stream_pool_) {
factory_->OnJobControllerComplete(this);
return;
}
if (main_job_ || alternative_job_ || dns_alpn_h3_job_) {
return;
}
// All jobs are gone.
// Report brokenness for the alternate jobs if apply.
MaybeReportBrokenAlternativeService(
alternative_service_info_.alternative_service(),
alternative_job_net_error_, alternative_job_failed_on_default_network_,
"Net.AlternateServiceFailed");
// Report for the DNS alt job if apply.
MaybeReportBrokenAlternativeService(
GetAlternativeServiceForDnsJob(http_request_info_url_),
dns_alpn_h3_job_net_error_, dns_alpn_h3_job_failed_on_default_network_,
"Net.AlternateServiceForDnsAlpnH3Failed");
// Reset error status for Jobs after reporting brokenness to avoid redundant
// reporting.
ResetErrorStatusForJobs();
if (request_) {
return;
}
DCHECK(!bound_job_);
factory_->OnJobControllerComplete(this);
}
void HttpStreamFactory::JobController::NotifyRequestFailed(int rv) {
if (!request_) {
return;
}
delegate_->OnStreamFailed(rv, NetErrorDetails(), ProxyInfo(),
ResolveErrorInfo());
}
void HttpStreamFactory::JobController::RewriteUrlWithHostMappingRules(
GURL& url) const {
session_->params().host_mapping_rules.RewriteUrl(url);
}
GURL HttpStreamFactory::JobController::DuplicateUrlWithHostMappingRules(
const GURL& url) const {
GURL copy = url;
RewriteUrlWithHostMappingRules(copy);
return copy;
}
AlternativeServiceInfo
HttpStreamFactory::JobController::GetAlternativeServiceInfoFor(
const GURL& http_request_info_url,
const StreamRequestInfo& request_info,
HttpStreamRequest::Delegate* delegate,
HttpStreamRequest::StreamType stream_type) {
if (!enable_alternative_services_) {
return AlternativeServiceInfo();
}
AlternativeServiceInfo alternative_service_info =
GetAlternativeServiceInfoInternal(http_request_info_url, request_info,
delegate, stream_type);
AlternativeServiceType type;
if (alternative_service_info.protocol() == NextProto::kProtoUnknown) {
type = NO_ALTERNATIVE_SERVICE;
} else if (alternative_service_info.protocol() == NextProto::kProtoQUIC) {
if (http_request_info_url.host_piece() ==
alternative_service_info.alternative_service().host) {
type = QUIC_SAME_DESTINATION;
} else {
type = QUIC_DIFFERENT_DESTINATION;
}
} else {
if (http_request_info_url.host_piece() ==
alternative_service_info.alternative_service().host) {
type = NOT_QUIC_SAME_DESTINATION;
} else {
type = NOT_QUIC_DIFFERENT_DESTINATION;
}
}
UMA_HISTOGRAM_ENUMERATION("Net.AlternativeServiceTypeForRequest", type,
MAX_ALTERNATIVE_SERVICE_TYPE);
return alternative_service_info;
}
AlternativeServiceInfo
HttpStreamFactory::JobController::GetAlternativeServiceInfoInternal(
const GURL& http_request_info_url,
const StreamRequestInfo& request_info,
HttpStreamRequest::Delegate* delegate,
HttpStreamRequest::StreamType stream_type) {
GURL original_url = http_request_info_url;
if (!original_url.SchemeIs(url::kHttpsScheme)) {
return AlternativeServiceInfo();
}
HttpServerProperties& http_server_properties =
*session_->http_server_properties();
const AlternativeServiceInfoVector alternative_service_info_vector =
http_server_properties.GetAlternativeServiceInfos(
url::SchemeHostPort(original_url),
request_info.network_anonymization_key);
if (alternative_service_info_vector.empty()) {
return AlternativeServiceInfo();
}
bool quic_advertised = false;
bool quic_all_broken = true;
// First alternative service that is not marked as broken.
AlternativeServiceInfo first_alternative_service_info;
bool is_any_broken = false;
for (const AlternativeServiceInfo& alternative_service_info :
alternative_service_info_vector) {
DCHECK(IsAlternateProtocolValid(alternative_service_info.protocol()));
if (!quic_advertised &&
alternative_service_info.protocol() == NextProto::kProtoQUIC) {
quic_advertised = true;
}
const bool is_broken = http_server_properties.IsAlternativeServiceBroken(
alternative_service_info.alternative_service(),
request_info.network_anonymization_key);
net_log_.AddEvent(
NetLogEventType::HTTP_STREAM_JOB_CONTROLLER_ALT_SVC_FOUND, [&] {
return NetLogAltSvcParams(&alternative_service_info, is_broken);
});
if (is_broken) {
if (!is_any_broken) {
// Only log the broken alternative service once per request.
is_any_broken = true;
HistogramAlternateProtocolUsage(ALTERNATE_PROTOCOL_USAGE_BROKEN,
HasGoogleHost(original_url));
}
continue;
}
// Some shared unix systems may have user home directories (like
// http://foo.com/~mike) which allow users to emit headers. This is a bad
// idea already, but with Alternate-Protocol, it provides the ability for a
// single user on a multi-user system to hijack the alternate protocol.
// These systems also enforce ports <1024 as restricted ports. So don't
// allow protocol upgrades to user-controllable ports.
const int kUnrestrictedPort = 1024;
if (!session_->params().enable_user_alternate_protocol_ports &&
(alternative_service_info.alternative_service().port >=
kUnrestrictedPort &&
original_url.EffectiveIntPort() < kUnrestrictedPort)) {
continue;
}
if (alternative_service_info.protocol() == NextProto::kProtoHTTP2) {
if (!session_->params().enable_http2_alternative_service) {
continue;
}
// Cache this entry if we don't have a non-broken Alt-Svc yet.
if (first_alternative_service_info.protocol() ==
NextProto::kProtoUnknown) {
first_alternative_service_info = alternative_service_info;
}
continue;
}
DCHECK_EQ(NextProto::kProtoQUIC, alternative_service_info.protocol());
quic_all_broken = false;
if (!session_->IsQuicEnabled()) {
continue;
}
if (!original_url.SchemeIs(url::kHttpsScheme)) {
continue;
}
// If there is no QUIC version in the advertised versions that is
// supported, ignore this entry.
if (SelectQuicVersion(alternative_service_info.advertised_versions()) ==
quic::ParsedQuicVersion::Unsupported()) {
continue;
}
// Check whether there is an existing QUIC session to use for this origin.
GURL mapped_origin = original_url;
RewriteUrlWithHostMappingRules(mapped_origin);
QuicSessionKey session_key(
HostPortPair::FromURL(mapped_origin), request_info.privacy_mode,
proxy_info_.proxy_chain(), SessionUsage::kDestination,
request_info.socket_tag, request_info.network_anonymization_key,
request_info.secure_dns_policy, /*require_dns_https_alpn=*/false);
GURL destination = CreateAltSvcUrl(
original_url, alternative_service_info.GetHostPortPair());
if (session_key.host() != destination.host_piece() &&
!session_->context().quic_context->params()->allow_remote_alt_svc) {
continue;
}
RewriteUrlWithHostMappingRules(destination);
if (session_->quic_session_pool()->CanUseExistingSession(
session_key, url::SchemeHostPort(destination))) {
return alternative_service_info;
}
if (!IsQuicAllowedForHost(destination.host())) {
continue;
}
// Cache this entry if we don't have a non-broken Alt-Svc yet.
if (first_alternative_service_info.protocol() == NextProto::kProtoUnknown) {
first_alternative_service_info = alternative_service_info;
}
}
// Ask delegate to mark QUIC as broken for the origin.
if (quic_advertised && quic_all_broken && delegate != nullptr) {
delegate->OnQuicBroken();
}
return first_alternative_service_info;
}
quic::ParsedQuicVersion HttpStreamFactory::JobController::SelectQuicVersion(
const quic::ParsedQuicVersionVector& advertised_versions) {
return session_->context().quic_context->SelectQuicVersion(
advertised_versions);
}
void HttpStreamFactory::JobController::ReportAlternateProtocolUsage(
AlternateProtocolUsage alternate_protocol_usage,
bool is_google_host) const {
DCHECK_LT(alternate_protocol_usage, ALTERNATE_PROTOCOL_USAGE_MAX);
HistogramAlternateProtocolUsage(alternate_protocol_usage, is_google_host);
}
bool HttpStreamFactory::JobController::IsJobOrphaned(Job* job) const {
return !request_ || (job_bound_ && bound_job_ != job);
}
AlternateProtocolUsage
HttpStreamFactory::JobController::CalculateAlternateProtocolUsage(
Job* job) const {
if ((main_job_ && alternative_job_) || dns_alpn_h3_job_) {
if (job == main_job_.get()) {
return ALTERNATE_PROTOCOL_USAGE_MAIN_JOB_WON_RACE;
}
if (job == alternative_job_.get()) {
if (job->using_existing_quic_session()) {
return ALTERNATE_PROTOCOL_USAGE_NO_RACE;
}
return ALTERNATE_PROTOCOL_USAGE_WON_RACE;
}
if (job == dns_alpn_h3_job_.get()) {
if (job->using_existing_quic_session()) {
return ALTERNATE_PROTOCOL_USAGE_DNS_ALPN_H3_JOB_WON_WITHOUT_RACE;
}
return ALTERNATE_PROTOCOL_USAGE_DNS_ALPN_H3_JOB_WON_RACE;
}
}
// TODO(crbug.com/40232167): Implement better logic to support uncovered
// cases.
return ALTERNATE_PROTOCOL_USAGE_UNSPECIFIED_REASON;
}
int HttpStreamFactory::JobController::ReconsiderProxyAfterError(Job* job,
int error) {
// ReconsiderProxyAfterError() should only be called when the last job fails.
DCHECK_EQ(1, GetJobCount());
DCHECK(!proxy_resolve_request_);
if (!job->should_reconsider_proxy()) {
return error;
}
if (request_info_.load_flags & LOAD_BYPASS_PROXY) {
return error;
}
// Clear client certificates for all proxies in the chain.
// TODO(crbug.com/40284947): client certificates for multi-proxy
// chains are not yet supported, and this is only tested with single-proxy
// chains.
for (auto& proxy_server : proxy_info_.proxy_chain().proxy_servers()) {
if (proxy_server.is_secure_http_like()) {
session_->ssl_client_context()->ClearClientCertificate(
proxy_server.host_port_pair());
}
}
if (!proxy_info_.Fallback(error, net_log_)) {
// If there is no more proxy to fallback to, fail the transaction
// with the last connection error we got.
return error;
}
// Abandon all Jobs and start over.
job_bound_ = false;
bound_job_ = nullptr;
dns_alpn_h3_job_.reset();
alternative_job_.reset();
main_job_.reset();
ResetErrorStatusForJobs();
// Also resets states that related to the old main job. In particular,
// cancels |resume_main_job_callback_| so there won't be any delayed
// ResumeMainJob() left in the task queue.
resume_main_job_callback_.Cancel();
main_job_is_resumed_ = false;
main_job_is_blocked_ = false;
next_state_ = STATE_RESOLVE_PROXY_COMPLETE;
return OK;
}
bool HttpStreamFactory::JobController::IsQuicAllowedForHost(
const std::string& host) {
const base::flat_set<std::string>& host_allowlist =
session_->params().quic_host_allowlist;
if (host_allowlist.empty()) {
return true;
}
std::string lowered_host = base::ToLowerASCII(host);
return base::Contains(host_allowlist, lowered_host);
}
void HttpStreamFactory::JobController::SwitchToHttpStreamPool() {
CHECK(request_info_.socket_tag == SocketTag());
CHECK_EQ(stream_type_, HttpStreamRequest::HTTP_STREAM);
CHECK(session_->host_resolver()->IsHappyEyeballsV3Enabled());
switched_to_http_stream_pool_ = true;
bool disable_cert_network_fetches =
!!(request_info_.load_flags & LOAD_DISABLE_CERT_NETWORK_FETCHES);
NextProtoSet allowed_alpns =
request_info_.is_http1_allowed
? NextProtoSet::All()
: NextProtoSet{NextProto::kProtoHTTP2, NextProto::kProtoQUIC};
url::SchemeHostPort destination(origin_url_);
session_->ApplyTestingFixedPort(destination);
HttpStreamPoolRequestInfo pool_request_info(
std::move(destination), request_info_.privacy_mode,
request_info_.socket_tag, request_info_.network_anonymization_key,
request_info_.secure_dns_policy, disable_cert_network_fetches,
alternative_service_info_, allowed_alpns, request_info_.load_flags,
proxy_info_, net_log_);
if (is_preconnect_) {
int rv = session_->http_stream_pool()->Preconnect(
std::move(pool_request_info), num_streams_,
base::BindOnce(&JobController::OnPoolPreconnectsComplete,
ptr_factory_.GetWeakPtr()));
if (rv != ERR_IO_PENDING) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&JobController::OnPoolPreconnectsComplete,
ptr_factory_.GetWeakPtr(), rv));
}
return;
}
// Exchange `request_` and `delegate_` to prevent them from being dangling.
session_->http_stream_pool()->HandleStreamRequest(
std::exchange(request_, nullptr), std::exchange(delegate_, nullptr),
std::move(pool_request_info), priority_, allowed_bad_certs_,
enable_ip_based_pooling_, enable_alternative_services_);
// Delete `this` later as this method is called while running DoLoop().
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&JobController::MaybeNotifyFactoryOfCompletion,
ptr_factory_.GetWeakPtr()));
}
void HttpStreamFactory::JobController::OnPoolPreconnectsComplete(int rv) {
CHECK(switched_to_http_stream_pool_);
factory_->OnPreconnectsCompleteInternal();
MaybeNotifyFactoryOfCompletion();
}
} // namespace net
|