xref: /aosp_15_r20/external/cronet/url/scheme_host_port.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2015 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef URL_SCHEME_HOST_PORT_H_
6*6777b538SAndroid Build Coastguard Worker #define URL_SCHEME_HOST_PORT_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
9*6777b538SAndroid Build Coastguard Worker 
10*6777b538SAndroid Build Coastguard Worker #include <string>
11*6777b538SAndroid Build Coastguard Worker #include <string_view>
12*6777b538SAndroid Build Coastguard Worker 
13*6777b538SAndroid Build Coastguard Worker #include "base/component_export.h"
14*6777b538SAndroid Build Coastguard Worker 
15*6777b538SAndroid Build Coastguard Worker class GURL;
16*6777b538SAndroid Build Coastguard Worker 
17*6777b538SAndroid Build Coastguard Worker namespace url {
18*6777b538SAndroid Build Coastguard Worker 
19*6777b538SAndroid Build Coastguard Worker struct Parsed;
20*6777b538SAndroid Build Coastguard Worker 
21*6777b538SAndroid Build Coastguard Worker // This class represents a (scheme, host, port) tuple extracted from a URL.
22*6777b538SAndroid Build Coastguard Worker //
23*6777b538SAndroid Build Coastguard Worker // The primary purpose of this class is to represent relevant network-authority
24*6777b538SAndroid Build Coastguard Worker // information for a URL. It is _not_ an Origin, as described in RFC 6454. In
25*6777b538SAndroid Build Coastguard Worker // particular, it is generally NOT the right thing to use for security
26*6777b538SAndroid Build Coastguard Worker // decisions.
27*6777b538SAndroid Build Coastguard Worker //
28*6777b538SAndroid Build Coastguard Worker // Instead, this class is a mechanism for simplifying URLs with standard schemes
29*6777b538SAndroid Build Coastguard Worker // (that is, those which follow the generic syntax of RFC 3986) down to the
30*6777b538SAndroid Build Coastguard Worker // uniquely identifying information necessary for network fetches. This makes it
31*6777b538SAndroid Build Coastguard Worker // suitable as a cache key for a collection of active connections, for instance.
32*6777b538SAndroid Build Coastguard Worker // It may, however, be inappropriate to use as a cache key for persistent
33*6777b538SAndroid Build Coastguard Worker // storage associated with a host.
34*6777b538SAndroid Build Coastguard Worker //
35*6777b538SAndroid Build Coastguard Worker // In particular, note that:
36*6777b538SAndroid Build Coastguard Worker //
37*6777b538SAndroid Build Coastguard Worker // * SchemeHostPort can only represent schemes which follow the RFC 3986 syntax
38*6777b538SAndroid Build Coastguard Worker //   (e.g. those registered with GURL as "standard schemes"). Non-standard
39*6777b538SAndroid Build Coastguard Worker //   schemes such as "blob", "filesystem", "data", and "javascript" can only be
40*6777b538SAndroid Build Coastguard Worker //   represented as invalid SchemeHostPort objects.
41*6777b538SAndroid Build Coastguard Worker //
42*6777b538SAndroid Build Coastguard Worker // * For example, the "file" scheme follows the standard syntax, but it is
43*6777b538SAndroid Build Coastguard Worker //   important to note that the authority portion (host, port) is optional.
44*6777b538SAndroid Build Coastguard Worker //   URLs without an authority portion will be represented with an empty string
45*6777b538SAndroid Build Coastguard Worker //   for the host, and a port of 0 (e.g. "file:///etc/hosts" =>
46*6777b538SAndroid Build Coastguard Worker //   ("file", "", 0)), and URLs with a host-only authority portion will be
47*6777b538SAndroid Build Coastguard Worker //   represented with a port of 0 (e.g. "file://example.com/etc/hosts" =>
48*6777b538SAndroid Build Coastguard Worker //   ("file", "example.com", 0)). See Section 3 of RFC 3986 to better understand
49*6777b538SAndroid Build Coastguard Worker //   these constructs.
50*6777b538SAndroid Build Coastguard Worker //
51*6777b538SAndroid Build Coastguard Worker // * SchemeHostPort has no notion of the Origin concept (RFC 6454), and in
52*6777b538SAndroid Build Coastguard Worker //   particular, it has no notion of an opaque Origin. If you need to take
53*6777b538SAndroid Build Coastguard Worker //   opaque origins into account (and, if you're making security-relevant
54*6777b538SAndroid Build Coastguard Worker //   decisions then you absolutely do), please use 'url::Origin' instead.
55*6777b538SAndroid Build Coastguard Worker //
56*6777b538SAndroid Build Coastguard Worker // Usage:
57*6777b538SAndroid Build Coastguard Worker //
58*6777b538SAndroid Build Coastguard Worker // * SchemeHostPort objects are commonly created from GURL objects:
59*6777b538SAndroid Build Coastguard Worker //
60*6777b538SAndroid Build Coastguard Worker //     GURL url("https://example.com/");
61*6777b538SAndroid Build Coastguard Worker //     url::SchemeHostPort tuple(url);
62*6777b538SAndroid Build Coastguard Worker //     tuple.scheme(); // "https"
63*6777b538SAndroid Build Coastguard Worker //     tuple.host(); // "example.com"
64*6777b538SAndroid Build Coastguard Worker //     tuple.port(); // 443
65*6777b538SAndroid Build Coastguard Worker //
66*6777b538SAndroid Build Coastguard Worker // * Objects may also be explicitly created and compared:
67*6777b538SAndroid Build Coastguard Worker //
68*6777b538SAndroid Build Coastguard Worker //     url::SchemeHostPort tuple(url::kHttpsScheme, "example.com", 443);
69*6777b538SAndroid Build Coastguard Worker //     tuple.scheme(); // "https"
70*6777b538SAndroid Build Coastguard Worker //     tuple.host(); // "example.com"
71*6777b538SAndroid Build Coastguard Worker //     tuple.port(); // 443
72*6777b538SAndroid Build Coastguard Worker //
73*6777b538SAndroid Build Coastguard Worker //     GURL url("https://example.com/");
74*6777b538SAndroid Build Coastguard Worker //     tuple == url::SchemeHostPort(url); // true
COMPONENT_EXPORT(URL)75*6777b538SAndroid Build Coastguard Worker class COMPONENT_EXPORT(URL) SchemeHostPort {
76*6777b538SAndroid Build Coastguard Worker  public:
77*6777b538SAndroid Build Coastguard Worker   // Creates an invalid (scheme, host, port) tuple, which represents an invalid
78*6777b538SAndroid Build Coastguard Worker   // or non-standard URL.
79*6777b538SAndroid Build Coastguard Worker   SchemeHostPort();
80*6777b538SAndroid Build Coastguard Worker 
81*6777b538SAndroid Build Coastguard Worker   // Creates a (scheme, host, port) tuple. |host| must be a canonicalized
82*6777b538SAndroid Build Coastguard Worker   // A-label (that is, '☃.net' must be provided as 'xn--n3h.net'). |scheme|
83*6777b538SAndroid Build Coastguard Worker   // must be a standard scheme. |port| must be 0 if |scheme| does not support
84*6777b538SAndroid Build Coastguard Worker   // ports (e.g. 'file').
85*6777b538SAndroid Build Coastguard Worker   //
86*6777b538SAndroid Build Coastguard Worker   // Copies the data in |scheme| and |host|.
87*6777b538SAndroid Build Coastguard Worker   SchemeHostPort(std::string_view scheme, std::string_view host, uint16_t port);
88*6777b538SAndroid Build Coastguard Worker 
89*6777b538SAndroid Build Coastguard Worker   // Metadata influencing whether or not the constructor should sanity check
90*6777b538SAndroid Build Coastguard Worker   // host canonicalization.
91*6777b538SAndroid Build Coastguard Worker   enum ConstructPolicy { CHECK_CANONICALIZATION, ALREADY_CANONICALIZED };
92*6777b538SAndroid Build Coastguard Worker 
93*6777b538SAndroid Build Coastguard Worker   // Creates a (scheme, host, port) tuple without performing sanity checking
94*6777b538SAndroid Build Coastguard Worker   // that the host and port are canonicalized. This should only be used when
95*6777b538SAndroid Build Coastguard Worker   // converting between already normalized types, and should NOT be used for
96*6777b538SAndroid Build Coastguard Worker   // IPC.
97*6777b538SAndroid Build Coastguard Worker   SchemeHostPort(std::string scheme,
98*6777b538SAndroid Build Coastguard Worker                  std::string host,
99*6777b538SAndroid Build Coastguard Worker                  uint16_t port,
100*6777b538SAndroid Build Coastguard Worker                  ConstructPolicy policy);
101*6777b538SAndroid Build Coastguard Worker 
102*6777b538SAndroid Build Coastguard Worker   // Creates a (scheme, host, port) tuple from |url|, as described at
103*6777b538SAndroid Build Coastguard Worker   // https://tools.ietf.org/html/rfc6454#section-4
104*6777b538SAndroid Build Coastguard Worker   //
105*6777b538SAndroid Build Coastguard Worker   // If |url| is invalid or non-standard, the result will be an invalid
106*6777b538SAndroid Build Coastguard Worker   // SchemeHostPort object.
107*6777b538SAndroid Build Coastguard Worker   explicit SchemeHostPort(const GURL& url);
108*6777b538SAndroid Build Coastguard Worker 
109*6777b538SAndroid Build Coastguard Worker   // Copyable and movable.
110*6777b538SAndroid Build Coastguard Worker   SchemeHostPort(const SchemeHostPort&) = default;
111*6777b538SAndroid Build Coastguard Worker   SchemeHostPort& operator=(const SchemeHostPort&) = default;
112*6777b538SAndroid Build Coastguard Worker   SchemeHostPort(SchemeHostPort&&) noexcept = default;
113*6777b538SAndroid Build Coastguard Worker   SchemeHostPort& operator=(SchemeHostPort&&) noexcept = default;
114*6777b538SAndroid Build Coastguard Worker 
115*6777b538SAndroid Build Coastguard Worker   ~SchemeHostPort();
116*6777b538SAndroid Build Coastguard Worker 
117*6777b538SAndroid Build Coastguard Worker   // Returns the host component, in URL form. That is all IDN domain names will
118*6777b538SAndroid Build Coastguard Worker   // be expressed as A-Labels ('☃.net' will be returned as 'xn--n3h.net'), and
119*6777b538SAndroid Build Coastguard Worker   // and all IPv6 addresses will be enclosed in brackets ("[2001:db8::1]").
120*6777b538SAndroid Build Coastguard Worker   const std::string& host() const { return host_; }
121*6777b538SAndroid Build Coastguard Worker   const std::string& scheme() const { return scheme_; }
122*6777b538SAndroid Build Coastguard Worker   uint16_t port() const { return port_; }
123*6777b538SAndroid Build Coastguard Worker   bool IsValid() const;
124*6777b538SAndroid Build Coastguard Worker 
125*6777b538SAndroid Build Coastguard Worker   // Serializes the SchemeHostPort tuple to a canonical form.
126*6777b538SAndroid Build Coastguard Worker   //
127*6777b538SAndroid Build Coastguard Worker   // While this string form resembles the Origin serialization specified in
128*6777b538SAndroid Build Coastguard Worker   // Section 6.2 of RFC 6454, it is important to note that invalid
129*6777b538SAndroid Build Coastguard Worker   // SchemeHostPort tuples serialize to the empty string, rather than being
130*6777b538SAndroid Build Coastguard Worker   // serialized as would an opaque Origin.
131*6777b538SAndroid Build Coastguard Worker   std::string Serialize() const;
132*6777b538SAndroid Build Coastguard Worker 
133*6777b538SAndroid Build Coastguard Worker   // Efficiently returns what GURL(Serialize()) would return, without needing to
134*6777b538SAndroid Build Coastguard Worker   // re-parse the URL. Note: this still performs allocations to copy data into
135*6777b538SAndroid Build Coastguard Worker   // GURL, so please avoid using this method if you only need to work on
136*6777b538SAndroid Build Coastguard Worker   // schemes, hosts, or ports individually.
137*6777b538SAndroid Build Coastguard Worker   // For example, see crrev.com/c/3637099/comments/782360d0_e14757be.
138*6777b538SAndroid Build Coastguard Worker   GURL GetURL() const;
139*6777b538SAndroid Build Coastguard Worker 
140*6777b538SAndroid Build Coastguard Worker   // Estimates dynamic memory usage.
141*6777b538SAndroid Build Coastguard Worker   // See base/trace_event/memory_usage_estimator.h for more info.
142*6777b538SAndroid Build Coastguard Worker   size_t EstimateMemoryUsage() const;
143*6777b538SAndroid Build Coastguard Worker 
144*6777b538SAndroid Build Coastguard Worker   // Two SchemeHostPort objects are "equal" iff their schemes, hosts, and ports
145*6777b538SAndroid Build Coastguard Worker   // are exact matches.
146*6777b538SAndroid Build Coastguard Worker   //
147*6777b538SAndroid Build Coastguard Worker   // Note that this comparison is _not_ the same as an origin-based comparison.
148*6777b538SAndroid Build Coastguard Worker   // In particular, invalid SchemeHostPort objects match each other (and
149*6777b538SAndroid Build Coastguard Worker   // themselves). Opaque origins, on the other hand, would not.
150*6777b538SAndroid Build Coastguard Worker   bool operator==(const SchemeHostPort& other) const {
151*6777b538SAndroid Build Coastguard Worker     return port_ == other.port() && scheme_ == other.scheme() &&
152*6777b538SAndroid Build Coastguard Worker            host_ == other.host();
153*6777b538SAndroid Build Coastguard Worker   }
154*6777b538SAndroid Build Coastguard Worker   bool operator!=(const SchemeHostPort& other) const {
155*6777b538SAndroid Build Coastguard Worker     return !(*this == other);
156*6777b538SAndroid Build Coastguard Worker   }
157*6777b538SAndroid Build Coastguard Worker   // Allows SchemeHostPort to be used as a key in STL (for example, a std::set
158*6777b538SAndroid Build Coastguard Worker   // or std::map).
159*6777b538SAndroid Build Coastguard Worker   bool operator<(const SchemeHostPort& other) const;
160*6777b538SAndroid Build Coastguard Worker 
161*6777b538SAndroid Build Coastguard Worker   // Whether to discard host and port information for a specific scheme.
162*6777b538SAndroid Build Coastguard Worker   //
163*6777b538SAndroid Build Coastguard Worker   // Note that this hack is required to avoid breaking existing Android WebView
164*6777b538SAndroid Build Coastguard Worker   // behaviors. Currently, Android WebView doesn't use host and port information
165*6777b538SAndroid Build Coastguard Worker   // for non-special URLs. See https://crbug.com/40063064 for details.
166*6777b538SAndroid Build Coastguard Worker   static bool ShouldDiscardHostAndPort(const std::string_view scheme);
167*6777b538SAndroid Build Coastguard Worker 
168*6777b538SAndroid Build Coastguard Worker   std::string SerializeInternal(url::Parsed* parsed) const;
169*6777b538SAndroid Build Coastguard Worker 
170*6777b538SAndroid Build Coastguard Worker   std::string scheme_;
171*6777b538SAndroid Build Coastguard Worker   std::string host_;
172*6777b538SAndroid Build Coastguard Worker   uint16_t port_ = 0;
173*6777b538SAndroid Build Coastguard Worker };
174*6777b538SAndroid Build Coastguard Worker 
175*6777b538SAndroid Build Coastguard Worker COMPONENT_EXPORT(URL)
176*6777b538SAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& out,
177*6777b538SAndroid Build Coastguard Worker                          const SchemeHostPort& scheme_host_port);
178*6777b538SAndroid Build Coastguard Worker 
179*6777b538SAndroid Build Coastguard Worker }  // namespace url
180*6777b538SAndroid Build Coastguard Worker 
181*6777b538SAndroid Build Coastguard Worker #endif  // URL_SCHEME_HOST_PORT_H_
182