1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/http/http_stream_key.h"
6
7 #include "base/strings/strcat.h"
8 #include "net/base/network_anonymization_key.h"
9 #include "net/base/privacy_mode.h"
10 #include "net/dns/public/secure_dns_policy.h"
11 #include "net/socket/client_socket_pool.h"
12 #include "net/socket/socket_tag.h"
13 #include "net/spdy/spdy_session_key.h"
14 #include "url/gurl.h"
15 #include "url/scheme_host_port.h"
16
17 namespace net {
18
19 HttpStreamKey::HttpStreamKey() = default;
20
HttpStreamKey(url::SchemeHostPort destination,PrivacyMode privacy_mode,SocketTag socket_tag,NetworkAnonymizationKey network_anonymization_key,SecureDnsPolicy secure_dns_policy,bool disable_cert_network_fetches)21 HttpStreamKey::HttpStreamKey(url::SchemeHostPort destination,
22 PrivacyMode privacy_mode,
23 SocketTag socket_tag,
24 NetworkAnonymizationKey network_anonymization_key,
25 SecureDnsPolicy secure_dns_policy,
26 bool disable_cert_network_fetches)
27 : destination_(std::move(destination)),
28 privacy_mode_(privacy_mode),
29 socket_tag_(std::move(socket_tag)),
30 network_anonymization_key_(
31 NetworkAnonymizationKey::IsPartitioningEnabled()
32 ? std::move(network_anonymization_key)
33 : NetworkAnonymizationKey()),
34 secure_dns_policy_(secure_dns_policy),
35 disable_cert_network_fetches_(disable_cert_network_fetches) {
36 CHECK(socket_tag_ == SocketTag()) << "Socket tag is not supported yet";
37 }
38
39 HttpStreamKey::~HttpStreamKey() = default;
40
41 HttpStreamKey::HttpStreamKey(const HttpStreamKey& other) = default;
42
43 HttpStreamKey& HttpStreamKey::operator=(const HttpStreamKey& other) = default;
44
45 bool HttpStreamKey::operator==(const HttpStreamKey& other) const = default;
46
operator <(const HttpStreamKey & other) const47 bool HttpStreamKey::operator<(const HttpStreamKey& other) const {
48 return std::tie(destination_, privacy_mode_, socket_tag_,
49 network_anonymization_key_, secure_dns_policy_,
50 disable_cert_network_fetches_) <
51 std::tie(other.destination_, other.privacy_mode_, other.socket_tag_,
52 other.network_anonymization_key_, other.secure_dns_policy_,
53 other.disable_cert_network_fetches_);
54 }
55
ToString() const56 std::string HttpStreamKey::ToString() const {
57 return base::StrCat(
58 {disable_cert_network_fetches_ ? "disable_cert_network_fetches/" : "",
59 ClientSocketPool::GroupId::GetSecureDnsPolicyGroupIdPrefix(
60 secure_dns_policy_),
61 ClientSocketPool::GroupId::GetPrivacyModeGroupIdPrefix(privacy_mode_),
62 destination_.Serialize(),
63 NetworkAnonymizationKey::IsPartitioningEnabled()
64 ? base::StrCat(
65 {" <", network_anonymization_key_.ToDebugString(), ">"})
66 : ""});
67 }
68
ToValue() const69 base::Value::Dict HttpStreamKey::ToValue() const {
70 base::Value::Dict dict;
71 dict.Set("destination", destination_.Serialize());
72 dict.Set("privacy_mode", PrivacyModeToDebugString(privacy_mode_));
73 dict.Set("network_anonymization_key",
74 network_anonymization_key_.ToDebugString());
75 dict.Set("secure_dns_policy",
76 SecureDnsPolicyToDebugString(secure_dns_policy_));
77 dict.Set("disable_cert_network_fetches", disable_cert_network_fetches_);
78 return dict;
79 }
80
CalculateSpdySessionKey() const81 SpdySessionKey HttpStreamKey::CalculateSpdySessionKey() const {
82 HostPortPair host_port = GURL::SchemeIsCryptographic(destination().scheme())
83 ? HostPortPair::FromSchemeHostPort(destination())
84 : HostPortPair();
85 return SpdySessionKey(std::move(host_port), privacy_mode(),
86 ProxyChain::Direct(), SessionUsage::kDestination,
87 socket_tag(), network_anonymization_key(),
88 secure_dns_policy(), disable_cert_network_fetches());
89 }
90
CalculateQuicSessionAliasKey(std::optional<url::SchemeHostPort> optional_alias_name) const91 QuicSessionAliasKey HttpStreamKey::CalculateQuicSessionAliasKey(
92 std::optional<url::SchemeHostPort> optional_alias_name) const {
93 url::SchemeHostPort destination_for_name_resolution =
94 optional_alias_name.value_or(destination_);
95 CHECK_EQ(destination_for_name_resolution.scheme(), destination_.scheme());
96 if (!GURL::SchemeIsCryptographic(destination_for_name_resolution.scheme())) {
97 return QuicSessionAliasKey();
98 }
99 QuicSessionKey quic_session_key(
100 destination_.host(), destination_.port(), privacy_mode(),
101 ProxyChain::Direct(), SessionUsage::kDestination, socket_tag(),
102 network_anonymization_key(), secure_dns_policy(),
103 /*require_dns_https_alpn=*/false);
104 return QuicSessionAliasKey(std::move(destination_for_name_resolution),
105 std::move(quic_session_key));
106 }
107
108 } // namespace net
109