xref: /aosp_15_r20/external/cronet/url/gurl.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2013 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_GURL_H_
6*6777b538SAndroid Build Coastguard Worker #define URL_GURL_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <stddef.h>
9*6777b538SAndroid Build Coastguard Worker 
10*6777b538SAndroid Build Coastguard Worker #include <iosfwd>
11*6777b538SAndroid Build Coastguard Worker #include <memory>
12*6777b538SAndroid Build Coastguard Worker #include <string>
13*6777b538SAndroid Build Coastguard Worker #include <string_view>
14*6777b538SAndroid Build Coastguard Worker 
15*6777b538SAndroid Build Coastguard Worker #include "base/component_export.h"
16*6777b538SAndroid Build Coastguard Worker #include "base/debug/alias.h"
17*6777b538SAndroid Build Coastguard Worker #include "base/debug/crash_logging.h"
18*6777b538SAndroid Build Coastguard Worker #include "base/trace_event/base_tracing_forward.h"
19*6777b538SAndroid Build Coastguard Worker #include "url/third_party/mozilla/url_parse.h"
20*6777b538SAndroid Build Coastguard Worker #include "url/url_canon.h"
21*6777b538SAndroid Build Coastguard Worker #include "url/url_canon_stdstring.h"
22*6777b538SAndroid Build Coastguard Worker #include "url/url_constants.h"
23*6777b538SAndroid Build Coastguard Worker 
24*6777b538SAndroid Build Coastguard Worker // Represents a URL. GURL is Google's URL parsing library.
25*6777b538SAndroid Build Coastguard Worker //
26*6777b538SAndroid Build Coastguard Worker // A parsed canonicalized URL is guaranteed to be UTF-8. Any non-ASCII input
27*6777b538SAndroid Build Coastguard Worker // characters are UTF-8 encoded and % escaped to ASCII.
28*6777b538SAndroid Build Coastguard Worker //
29*6777b538SAndroid Build Coastguard Worker // The string representation of a URL is called the spec(). Getting the
30*6777b538SAndroid Build Coastguard Worker // spec will assert if the URL is invalid to help protect against malicious
31*6777b538SAndroid Build Coastguard Worker // URLs. If you want the "best effort" canonicalization of an invalid URL, you
32*6777b538SAndroid Build Coastguard Worker // can use possibly_invalid_spec(). Test validity with is_valid(). Data and
33*6777b538SAndroid Build Coastguard Worker // javascript URLs use GetContent() to extract the data.
34*6777b538SAndroid Build Coastguard Worker //
35*6777b538SAndroid Build Coastguard Worker // This class has existence checkers and getters for the various components of
36*6777b538SAndroid Build Coastguard Worker // a URL. Existence is different than being nonempty. "http://www.google.com/?"
37*6777b538SAndroid Build Coastguard Worker // has a query that just happens to be empty, and has_query() will return true
38*6777b538SAndroid Build Coastguard Worker // while the query getters will return the empty string.
39*6777b538SAndroid Build Coastguard Worker //
40*6777b538SAndroid Build Coastguard Worker // Prefer not to modify a URL using string operations (though sometimes this is
41*6777b538SAndroid Build Coastguard Worker // unavoidable). Instead, use ReplaceComponents which can replace or delete
42*6777b538SAndroid Build Coastguard Worker // multiple parts of a URL in one step, doesn't re-canonicalize unchanged
43*6777b538SAndroid Build Coastguard Worker // sections, and avoids some screw-ups. An example is creating a URL with a
44*6777b538SAndroid Build Coastguard Worker // path that contains a literal '#'. Using string concatenation will generate a
45*6777b538SAndroid Build Coastguard Worker // URL with a truncated path and a reference fragment, while ReplaceComponents
46*6777b538SAndroid Build Coastguard Worker // will know to escape this and produce the desired result.
47*6777b538SAndroid Build Coastguard Worker //
48*6777b538SAndroid Build Coastguard Worker // WARNING: While there is no length limit on GURLs, the Mojo serialization
49*6777b538SAndroid Build Coastguard Worker // code will replace any very long URL with an invalid GURL.
50*6777b538SAndroid Build Coastguard Worker // See url::mojom::kMaxURLChars for more details.
COMPONENT_EXPORT(URL)51*6777b538SAndroid Build Coastguard Worker class COMPONENT_EXPORT(URL) GURL {
52*6777b538SAndroid Build Coastguard Worker  public:
53*6777b538SAndroid Build Coastguard Worker   using Replacements = url::StringViewReplacements<char>;
54*6777b538SAndroid Build Coastguard Worker   using ReplacementsW = url::StringViewReplacements<char16_t>;
55*6777b538SAndroid Build Coastguard Worker 
56*6777b538SAndroid Build Coastguard Worker   // Creates an empty, invalid URL.
57*6777b538SAndroid Build Coastguard Worker   GURL();
58*6777b538SAndroid Build Coastguard Worker 
59*6777b538SAndroid Build Coastguard Worker   // Copy construction is relatively inexpensive, with most of the time going
60*6777b538SAndroid Build Coastguard Worker   // to reallocating the string. It does not re-parse.
61*6777b538SAndroid Build Coastguard Worker   GURL(const GURL& other);
62*6777b538SAndroid Build Coastguard Worker   GURL(GURL&& other) noexcept;
63*6777b538SAndroid Build Coastguard Worker 
64*6777b538SAndroid Build Coastguard Worker   // The strings to this constructor should be UTF-8 / UTF-16.
65*6777b538SAndroid Build Coastguard Worker   explicit GURL(std::string_view url_string);
66*6777b538SAndroid Build Coastguard Worker   explicit GURL(std::u16string_view url_string);
67*6777b538SAndroid Build Coastguard Worker 
68*6777b538SAndroid Build Coastguard Worker   // Constructor for URLs that have already been parsed and canonicalized. This
69*6777b538SAndroid Build Coastguard Worker   // is used for conversions from KURL, for example. The caller must supply all
70*6777b538SAndroid Build Coastguard Worker   // information associated with the URL, which must be correct and consistent.
71*6777b538SAndroid Build Coastguard Worker   GURL(const char* canonical_spec,
72*6777b538SAndroid Build Coastguard Worker        size_t canonical_spec_len,
73*6777b538SAndroid Build Coastguard Worker        const url::Parsed& parsed,
74*6777b538SAndroid Build Coastguard Worker        bool is_valid);
75*6777b538SAndroid Build Coastguard Worker   // Notice that we take the canonical_spec by value so that we can convert
76*6777b538SAndroid Build Coastguard Worker   // from WebURL without copying the string. When we call this constructor
77*6777b538SAndroid Build Coastguard Worker   // we pass in a temporary std::string, which lets the compiler skip the
78*6777b538SAndroid Build Coastguard Worker   // copy and just move the std::string into the function argument. In the
79*6777b538SAndroid Build Coastguard Worker   // implementation, we use std::move to move the data into the GURL itself,
80*6777b538SAndroid Build Coastguard Worker   // which means we end up with zero copies.
81*6777b538SAndroid Build Coastguard Worker   GURL(std::string canonical_spec, const url::Parsed& parsed, bool is_valid);
82*6777b538SAndroid Build Coastguard Worker 
83*6777b538SAndroid Build Coastguard Worker   ~GURL();
84*6777b538SAndroid Build Coastguard Worker 
85*6777b538SAndroid Build Coastguard Worker   GURL& operator=(const GURL& other);
86*6777b538SAndroid Build Coastguard Worker   GURL& operator=(GURL&& other) noexcept;
87*6777b538SAndroid Build Coastguard Worker 
88*6777b538SAndroid Build Coastguard Worker   // Returns true when this object represents a valid parsed URL. When not
89*6777b538SAndroid Build Coastguard Worker   // valid, other functions will still succeed, but you will not get canonical
90*6777b538SAndroid Build Coastguard Worker   // data out in the format you may be expecting. Instead, we keep something
91*6777b538SAndroid Build Coastguard Worker   // "reasonable looking" so that the user can see how it's busted if
92*6777b538SAndroid Build Coastguard Worker   // displayed to them.
93*6777b538SAndroid Build Coastguard Worker   bool is_valid() const {
94*6777b538SAndroid Build Coastguard Worker     return is_valid_;
95*6777b538SAndroid Build Coastguard Worker   }
96*6777b538SAndroid Build Coastguard Worker 
97*6777b538SAndroid Build Coastguard Worker   // Returns true if the URL is zero-length. Note that empty URLs are also
98*6777b538SAndroid Build Coastguard Worker   // invalid, and is_valid() will return false for them. This is provided
99*6777b538SAndroid Build Coastguard Worker   // because some users may want to treat the empty case differently.
100*6777b538SAndroid Build Coastguard Worker   bool is_empty() const {
101*6777b538SAndroid Build Coastguard Worker     return spec_.empty();
102*6777b538SAndroid Build Coastguard Worker   }
103*6777b538SAndroid Build Coastguard Worker 
104*6777b538SAndroid Build Coastguard Worker   // Returns the raw spec, i.e., the full text of the URL, in canonical UTF-8,
105*6777b538SAndroid Build Coastguard Worker   // if the URL is valid. If the URL is not valid, this will assert and return
106*6777b538SAndroid Build Coastguard Worker   // the empty string (for safety in release builds, to keep them from being
107*6777b538SAndroid Build Coastguard Worker   // misused which might be a security problem).
108*6777b538SAndroid Build Coastguard Worker   //
109*6777b538SAndroid Build Coastguard Worker   // The URL will be ASCII (non-ASCII characters will be %-escaped UTF-8).
110*6777b538SAndroid Build Coastguard Worker   //
111*6777b538SAndroid Build Coastguard Worker   // The exception is for empty() URLs (which are !is_valid()) but this will
112*6777b538SAndroid Build Coastguard Worker   // return the empty string without asserting.
113*6777b538SAndroid Build Coastguard Worker   //
114*6777b538SAndroid Build Coastguard Worker   // Use invalid_spec() below to get the unusable spec of an invalid URL. This
115*6777b538SAndroid Build Coastguard Worker   // separation is designed to prevent errors that may cause security problems
116*6777b538SAndroid Build Coastguard Worker   // that could result from the mistaken use of an invalid URL.
117*6777b538SAndroid Build Coastguard Worker   const std::string& spec() const;
118*6777b538SAndroid Build Coastguard Worker 
119*6777b538SAndroid Build Coastguard Worker   // Returns the potentially invalid spec for a the URL. This spec MUST NOT be
120*6777b538SAndroid Build Coastguard Worker   // modified or sent over the network. It is designed to be displayed in error
121*6777b538SAndroid Build Coastguard Worker   // messages to the user, as the appearance of the spec may explain the error.
122*6777b538SAndroid Build Coastguard Worker   // If the spec is valid, the valid spec will be returned.
123*6777b538SAndroid Build Coastguard Worker   //
124*6777b538SAndroid Build Coastguard Worker   // The returned string is guaranteed to be valid UTF-8.
125*6777b538SAndroid Build Coastguard Worker   const std::string& possibly_invalid_spec() const {
126*6777b538SAndroid Build Coastguard Worker     return spec_;
127*6777b538SAndroid Build Coastguard Worker   }
128*6777b538SAndroid Build Coastguard Worker 
129*6777b538SAndroid Build Coastguard Worker   // Getter for the raw parsed structure. This allows callers to locate parts
130*6777b538SAndroid Build Coastguard Worker   // of the URL within the spec themselves. Most callers should consider using
131*6777b538SAndroid Build Coastguard Worker   // the individual component getters below.
132*6777b538SAndroid Build Coastguard Worker   //
133*6777b538SAndroid Build Coastguard Worker   // The returned parsed structure will reference into the raw spec, which may
134*6777b538SAndroid Build Coastguard Worker   // or may not be valid. If you are using this to index into the spec, BE
135*6777b538SAndroid Build Coastguard Worker   // SURE YOU ARE USING possibly_invalid_spec() to get the spec, and that you
136*6777b538SAndroid Build Coastguard Worker   // don't do anything "important" with invalid specs.
137*6777b538SAndroid Build Coastguard Worker   const url::Parsed& parsed_for_possibly_invalid_spec() const {
138*6777b538SAndroid Build Coastguard Worker     return parsed_;
139*6777b538SAndroid Build Coastguard Worker   }
140*6777b538SAndroid Build Coastguard Worker 
141*6777b538SAndroid Build Coastguard Worker   // Allows GURL to used as a key in STL (for example, a std::set or std::map).
142*6777b538SAndroid Build Coastguard Worker   bool operator<(const GURL& other) const;
143*6777b538SAndroid Build Coastguard Worker   bool operator>(const GURL& other) const;
144*6777b538SAndroid Build Coastguard Worker 
145*6777b538SAndroid Build Coastguard Worker   // Resolves a URL that's possibly relative to this object's URL, and returns
146*6777b538SAndroid Build Coastguard Worker   // it. Absolute URLs are also handled according to the rules of URLs on web
147*6777b538SAndroid Build Coastguard Worker   // pages.
148*6777b538SAndroid Build Coastguard Worker   //
149*6777b538SAndroid Build Coastguard Worker   // It may be impossible to resolve the URLs properly. If the input is not
150*6777b538SAndroid Build Coastguard Worker   // "standard" (IsStandard() == false) and the input looks relative, we can't
151*6777b538SAndroid Build Coastguard Worker   // resolve it. In these cases, the result will be an empty, invalid GURL.
152*6777b538SAndroid Build Coastguard Worker   //
153*6777b538SAndroid Build Coastguard Worker   // The result may also be a nonempty, invalid URL if the input has some kind
154*6777b538SAndroid Build Coastguard Worker   // of encoding error. In these cases, we will try to construct a "good" URL
155*6777b538SAndroid Build Coastguard Worker   // that may have meaning to the user, but it will be marked invalid.
156*6777b538SAndroid Build Coastguard Worker   //
157*6777b538SAndroid Build Coastguard Worker   // It is an error to resolve a URL relative to an invalid URL. The result
158*6777b538SAndroid Build Coastguard Worker   // will be the empty URL.
159*6777b538SAndroid Build Coastguard Worker   GURL Resolve(std::string_view relative) const;
160*6777b538SAndroid Build Coastguard Worker   GURL Resolve(std::u16string_view relative) const;
161*6777b538SAndroid Build Coastguard Worker 
162*6777b538SAndroid Build Coastguard Worker   // Creates a new GURL by replacing the current URL's components with the
163*6777b538SAndroid Build Coastguard Worker   // supplied versions. See the Replacements class in url_canon.h for more.
164*6777b538SAndroid Build Coastguard Worker   //
165*6777b538SAndroid Build Coastguard Worker   // These are not particularly quick, so avoid doing mutations when possible.
166*6777b538SAndroid Build Coastguard Worker   // Prefer the 8-bit version when possible.
167*6777b538SAndroid Build Coastguard Worker   //
168*6777b538SAndroid Build Coastguard Worker   // It is an error to replace components of an invalid URL. The result will
169*6777b538SAndroid Build Coastguard Worker   // be the empty URL.
170*6777b538SAndroid Build Coastguard Worker   //
171*6777b538SAndroid Build Coastguard Worker   // Note that this intentionally disallows direct use of url::Replacements,
172*6777b538SAndroid Build Coastguard Worker   // which is harder to use correctly.
173*6777b538SAndroid Build Coastguard Worker   GURL ReplaceComponents(const Replacements& replacements) const;
174*6777b538SAndroid Build Coastguard Worker   GURL ReplaceComponents(const ReplacementsW& replacements) const;
175*6777b538SAndroid Build Coastguard Worker 
176*6777b538SAndroid Build Coastguard Worker   // A helper function that is equivalent to replacing the path with a slash
177*6777b538SAndroid Build Coastguard Worker   // and clearing out everything after that. We sometimes need to know just the
178*6777b538SAndroid Build Coastguard Worker   // scheme and the authority. If this URL is not a standard URL (it doesn't
179*6777b538SAndroid Build Coastguard Worker   // have the regular authority and path sections), then the result will be
180*6777b538SAndroid Build Coastguard Worker   // an empty, invalid GURL. Note that this *does* work for file: URLs, which
181*6777b538SAndroid Build Coastguard Worker   // some callers may want to filter out before calling this.
182*6777b538SAndroid Build Coastguard Worker   //
183*6777b538SAndroid Build Coastguard Worker   // It is an error to get an empty path on an invalid URL. The result
184*6777b538SAndroid Build Coastguard Worker   // will be the empty URL.
185*6777b538SAndroid Build Coastguard Worker   GURL GetWithEmptyPath() const;
186*6777b538SAndroid Build Coastguard Worker 
187*6777b538SAndroid Build Coastguard Worker   // A helper function to return a GURL without the filename, query values, and
188*6777b538SAndroid Build Coastguard Worker   // fragment. For example,
189*6777b538SAndroid Build Coastguard Worker   // GURL("https://www.foo.com/index.html?q=test").GetWithoutFilename().spec()
190*6777b538SAndroid Build Coastguard Worker   // will return "https://www.foo.com/".
191*6777b538SAndroid Build Coastguard Worker   // GURL("https://www.foo.com/bar/").GetWithoutFilename().spec()
192*6777b538SAndroid Build Coastguard Worker   // will return "https://www.foo.com/bar/". If the GURL is invalid or missing a
193*6777b538SAndroid Build Coastguard Worker   // scheme, authority or path, it will return an empty, invalid GURL.
194*6777b538SAndroid Build Coastguard Worker   GURL GetWithoutFilename() const;
195*6777b538SAndroid Build Coastguard Worker 
196*6777b538SAndroid Build Coastguard Worker   // A helper function to return a GURL without the Ref (also named Fragment
197*6777b538SAndroid Build Coastguard Worker   // Identifier). For example,
198*6777b538SAndroid Build Coastguard Worker   // GURL("https://www.foo.com/index.html#test").GetWithoutRef().spec()
199*6777b538SAndroid Build Coastguard Worker   // will return "https://www.foo.com/index.html".
200*6777b538SAndroid Build Coastguard Worker   // If the GURL is invalid or missing a
201*6777b538SAndroid Build Coastguard Worker   // scheme, authority or path, it will return an empty, invalid GURL.
202*6777b538SAndroid Build Coastguard Worker   GURL GetWithoutRef() const;
203*6777b538SAndroid Build Coastguard Worker 
204*6777b538SAndroid Build Coastguard Worker   // A helper function to return a GURL containing just the scheme, host,
205*6777b538SAndroid Build Coastguard Worker   // and port from a URL. Equivalent to clearing any username and password,
206*6777b538SAndroid Build Coastguard Worker   // replacing the path with a slash, and clearing everything after that. If
207*6777b538SAndroid Build Coastguard Worker   // this URL is not a standard URL, then the result will be an empty,
208*6777b538SAndroid Build Coastguard Worker   // invalid GURL. If the URL has neither username nor password, this
209*6777b538SAndroid Build Coastguard Worker   // degenerates to GetWithEmptyPath().
210*6777b538SAndroid Build Coastguard Worker   //
211*6777b538SAndroid Build Coastguard Worker   // It is an error to get the origin of an invalid URL. The result
212*6777b538SAndroid Build Coastguard Worker   // will be the empty URL.
213*6777b538SAndroid Build Coastguard Worker   //
214*6777b538SAndroid Build Coastguard Worker   // WARNING: Please avoid converting urls into origins if at all possible!
215*6777b538SAndroid Build Coastguard Worker   // //docs/security/origin-vs-url.md is a list of gotchas that can result. Such
216*6777b538SAndroid Build Coastguard Worker   // conversions will likely return a wrong result for about:blank and/or
217*6777b538SAndroid Build Coastguard Worker   // in the presence of iframe.sandbox attribute. Prefer to get origins directly
218*6777b538SAndroid Build Coastguard Worker   // from the source (e.g. RenderFrameHost::GetLastCommittedOrigin).
219*6777b538SAndroid Build Coastguard Worker   GURL DeprecatedGetOriginAsURL() const;
220*6777b538SAndroid Build Coastguard Worker 
221*6777b538SAndroid Build Coastguard Worker   // A helper function to return a GURL stripped from the elements that are not
222*6777b538SAndroid Build Coastguard Worker   // supposed to be sent as HTTP referrer: username, password and ref fragment.
223*6777b538SAndroid Build Coastguard Worker   // For invalid URLs or URLs that no valid referrers, an empty URL will be
224*6777b538SAndroid Build Coastguard Worker   // returned.
225*6777b538SAndroid Build Coastguard Worker   GURL GetAsReferrer() const;
226*6777b538SAndroid Build Coastguard Worker 
227*6777b538SAndroid Build Coastguard Worker   // Returns true if the scheme for the current URL is a known "standard-format"
228*6777b538SAndroid Build Coastguard Worker   // scheme. A standard-format scheme adheres to what RFC 3986 calls "generic
229*6777b538SAndroid Build Coastguard Worker   // URI syntax" (https://tools.ietf.org/html/rfc3986#section-3). This includes
230*6777b538SAndroid Build Coastguard Worker   // file: and filesystem:, which some callers may want to filter out explicitly
231*6777b538SAndroid Build Coastguard Worker   // by calling SchemeIsFile[System].
232*6777b538SAndroid Build Coastguard Worker   bool IsStandard() const;
233*6777b538SAndroid Build Coastguard Worker 
234*6777b538SAndroid Build Coastguard Worker   // Returns true when the url is of the form about:blank, about:blank?foo or
235*6777b538SAndroid Build Coastguard Worker   // about:blank/#foo.
236*6777b538SAndroid Build Coastguard Worker   bool IsAboutBlank() const;
237*6777b538SAndroid Build Coastguard Worker 
238*6777b538SAndroid Build Coastguard Worker   // Returns true when the url is of the form about:srcdoc, about:srcdoc?foo or
239*6777b538SAndroid Build Coastguard Worker   // about:srcdoc/#foo.
240*6777b538SAndroid Build Coastguard Worker   bool IsAboutSrcdoc() const;
241*6777b538SAndroid Build Coastguard Worker 
242*6777b538SAndroid Build Coastguard Worker   // Returns true if the given parameter (should be lower-case ASCII to match
243*6777b538SAndroid Build Coastguard Worker   // the canonicalized scheme) is the scheme for this URL. Do not include a
244*6777b538SAndroid Build Coastguard Worker   // colon.
245*6777b538SAndroid Build Coastguard Worker   bool SchemeIs(std::string_view lower_ascii_scheme) const;
246*6777b538SAndroid Build Coastguard Worker 
247*6777b538SAndroid Build Coastguard Worker   // Returns true if the scheme is "http" or "https".
248*6777b538SAndroid Build Coastguard Worker   bool SchemeIsHTTPOrHTTPS() const;
249*6777b538SAndroid Build Coastguard Worker 
250*6777b538SAndroid Build Coastguard Worker   // Returns true is the scheme is "ws" or "wss".
251*6777b538SAndroid Build Coastguard Worker   bool SchemeIsWSOrWSS() const;
252*6777b538SAndroid Build Coastguard Worker 
253*6777b538SAndroid Build Coastguard Worker   // We often need to know if this is a file URL. File URLs are "standard", but
254*6777b538SAndroid Build Coastguard Worker   // are often treated separately by some programs.
255*6777b538SAndroid Build Coastguard Worker   bool SchemeIsFile() const {
256*6777b538SAndroid Build Coastguard Worker     return SchemeIs(url::kFileScheme);
257*6777b538SAndroid Build Coastguard Worker   }
258*6777b538SAndroid Build Coastguard Worker 
259*6777b538SAndroid Build Coastguard Worker   // FileSystem URLs need to be treated differently in some cases.
260*6777b538SAndroid Build Coastguard Worker   bool SchemeIsFileSystem() const {
261*6777b538SAndroid Build Coastguard Worker     return SchemeIs(url::kFileSystemScheme);
262*6777b538SAndroid Build Coastguard Worker   }
263*6777b538SAndroid Build Coastguard Worker 
264*6777b538SAndroid Build Coastguard Worker   // Returns true if the scheme indicates a network connection that uses TLS or
265*6777b538SAndroid Build Coastguard Worker   // some other cryptographic protocol (e.g. QUIC) for security.
266*6777b538SAndroid Build Coastguard Worker   //
267*6777b538SAndroid Build Coastguard Worker   // This function is a not a complete test of whether or not an origin's code
268*6777b538SAndroid Build Coastguard Worker   // is minimally trustworthy. For that, see Chromium's |IsOriginSecure| for a
269*6777b538SAndroid Build Coastguard Worker   // higher-level and more complete semantics. See that function's documentation
270*6777b538SAndroid Build Coastguard Worker   // for more detail.
271*6777b538SAndroid Build Coastguard Worker   bool SchemeIsCryptographic() const;
272*6777b538SAndroid Build Coastguard Worker 
273*6777b538SAndroid Build Coastguard Worker   // As above, but static. Parameter should be lower-case ASCII.
274*6777b538SAndroid Build Coastguard Worker   static bool SchemeIsCryptographic(std::string_view lower_ascii_scheme);
275*6777b538SAndroid Build Coastguard Worker 
276*6777b538SAndroid Build Coastguard Worker   // Returns true if the scheme is "blob".
277*6777b538SAndroid Build Coastguard Worker   bool SchemeIsBlob() const {
278*6777b538SAndroid Build Coastguard Worker     return SchemeIs(url::kBlobScheme);
279*6777b538SAndroid Build Coastguard Worker   }
280*6777b538SAndroid Build Coastguard Worker 
281*6777b538SAndroid Build Coastguard Worker   // Returns true if the scheme is a local scheme, as defined in Fetch:
282*6777b538SAndroid Build Coastguard Worker   // https://fetch.spec.whatwg.org/#local-scheme
283*6777b538SAndroid Build Coastguard Worker   bool SchemeIsLocal() const;
284*6777b538SAndroid Build Coastguard Worker 
285*6777b538SAndroid Build Coastguard Worker   // For most URLs, the "content" is everything after the scheme (skipping the
286*6777b538SAndroid Build Coastguard Worker   // scheme delimiting colon) and before the fragment (skipping the fragment
287*6777b538SAndroid Build Coastguard Worker   // delimiting octothorpe). For javascript URLs the "content" also includes the
288*6777b538SAndroid Build Coastguard Worker   // fragment delimiter and fragment.
289*6777b538SAndroid Build Coastguard Worker   //
290*6777b538SAndroid Build Coastguard Worker   // It is an error to get the content of an invalid URL: the result will be an
291*6777b538SAndroid Build Coastguard Worker   // empty string.
292*6777b538SAndroid Build Coastguard Worker   //
293*6777b538SAndroid Build Coastguard Worker   // Important note: The feature flag,
294*6777b538SAndroid Build Coastguard Worker   // url::kStandardCompliantNonSpecialSchemeURLParsing, changes the behavior of
295*6777b538SAndroid Build Coastguard Worker   // GetContent() and GetContentPiece() for some non-special URLs. See
296*6777b538SAndroid Build Coastguard Worker   // GURLTest::ContentForNonStandardURLs for the differences.
297*6777b538SAndroid Build Coastguard Worker   //
298*6777b538SAndroid Build Coastguard Worker   // Until the flag becomes enabled by default, you'll need to manually check
299*6777b538SAndroid Build Coastguard Worker   // the flag when using GetContent() and GetContentPiece() for non-special
300*6777b538SAndroid Build Coastguard Worker   // URLs. See http://crbug.com/40063064 for more details.
301*6777b538SAndroid Build Coastguard Worker   std::string GetContent() const;
302*6777b538SAndroid Build Coastguard Worker   std::string_view GetContentPiece() const;
303*6777b538SAndroid Build Coastguard Worker 
304*6777b538SAndroid Build Coastguard Worker   // Returns true if the hostname is an IP address. Note: this function isn't
305*6777b538SAndroid Build Coastguard Worker   // as cheap as a simple getter because it re-parses the hostname to verify.
306*6777b538SAndroid Build Coastguard Worker   bool HostIsIPAddress() const;
307*6777b538SAndroid Build Coastguard Worker 
308*6777b538SAndroid Build Coastguard Worker   // Not including the colon. If you are comparing schemes, prefer SchemeIs.
309*6777b538SAndroid Build Coastguard Worker   bool has_scheme() const { return parsed_.scheme.is_valid(); }
310*6777b538SAndroid Build Coastguard Worker   std::string scheme() const {
311*6777b538SAndroid Build Coastguard Worker     return ComponentString(parsed_.scheme);
312*6777b538SAndroid Build Coastguard Worker   }
313*6777b538SAndroid Build Coastguard Worker   std::string_view scheme_piece() const {
314*6777b538SAndroid Build Coastguard Worker     return ComponentStringPiece(parsed_.scheme);
315*6777b538SAndroid Build Coastguard Worker   }
316*6777b538SAndroid Build Coastguard Worker 
317*6777b538SAndroid Build Coastguard Worker   bool has_username() const { return parsed_.username.is_valid(); }
318*6777b538SAndroid Build Coastguard Worker   std::string username() const {
319*6777b538SAndroid Build Coastguard Worker     return ComponentString(parsed_.username);
320*6777b538SAndroid Build Coastguard Worker   }
321*6777b538SAndroid Build Coastguard Worker   std::string_view username_piece() const {
322*6777b538SAndroid Build Coastguard Worker     return ComponentStringPiece(parsed_.username);
323*6777b538SAndroid Build Coastguard Worker   }
324*6777b538SAndroid Build Coastguard Worker 
325*6777b538SAndroid Build Coastguard Worker   bool has_password() const { return parsed_.password.is_valid(); }
326*6777b538SAndroid Build Coastguard Worker   std::string password() const {
327*6777b538SAndroid Build Coastguard Worker     return ComponentString(parsed_.password);
328*6777b538SAndroid Build Coastguard Worker   }
329*6777b538SAndroid Build Coastguard Worker   std::string_view password_piece() const {
330*6777b538SAndroid Build Coastguard Worker     return ComponentStringPiece(parsed_.password);
331*6777b538SAndroid Build Coastguard Worker   }
332*6777b538SAndroid Build Coastguard Worker 
333*6777b538SAndroid Build Coastguard Worker   // The host may be a hostname, an IPv4 address, or an IPv6 literal surrounded
334*6777b538SAndroid Build Coastguard Worker   // by square brackets, like "[2001:db8::1]". To exclude these brackets, use
335*6777b538SAndroid Build Coastguard Worker   // HostNoBrackets() below.
336*6777b538SAndroid Build Coastguard Worker   bool has_host() const {
337*6777b538SAndroid Build Coastguard Worker     // Note that hosts are special, absence of host means length 0.
338*6777b538SAndroid Build Coastguard Worker     return parsed_.host.is_nonempty();
339*6777b538SAndroid Build Coastguard Worker   }
340*6777b538SAndroid Build Coastguard Worker   std::string host() const {
341*6777b538SAndroid Build Coastguard Worker     return ComponentString(parsed_.host);
342*6777b538SAndroid Build Coastguard Worker   }
343*6777b538SAndroid Build Coastguard Worker   std::string_view host_piece() const {
344*6777b538SAndroid Build Coastguard Worker     return ComponentStringPiece(parsed_.host);
345*6777b538SAndroid Build Coastguard Worker   }
346*6777b538SAndroid Build Coastguard Worker 
347*6777b538SAndroid Build Coastguard Worker   // The port if one is explicitly specified. Most callers will want IntPort()
348*6777b538SAndroid Build Coastguard Worker   // or EffectiveIntPort() instead of these. The getters will not include the
349*6777b538SAndroid Build Coastguard Worker   // ':'.
350*6777b538SAndroid Build Coastguard Worker   bool has_port() const { return parsed_.port.is_valid(); }
351*6777b538SAndroid Build Coastguard Worker   std::string port() const {
352*6777b538SAndroid Build Coastguard Worker     return ComponentString(parsed_.port);
353*6777b538SAndroid Build Coastguard Worker   }
354*6777b538SAndroid Build Coastguard Worker   std::string_view port_piece() const {
355*6777b538SAndroid Build Coastguard Worker     return ComponentStringPiece(parsed_.port);
356*6777b538SAndroid Build Coastguard Worker   }
357*6777b538SAndroid Build Coastguard Worker 
358*6777b538SAndroid Build Coastguard Worker   // Including first slash following host, up to the query. The URL
359*6777b538SAndroid Build Coastguard Worker   // "http://www.google.com/" has a path of "/".
360*6777b538SAndroid Build Coastguard Worker   bool has_path() const { return parsed_.path.is_valid(); }
361*6777b538SAndroid Build Coastguard Worker   std::string path() const {
362*6777b538SAndroid Build Coastguard Worker     return ComponentString(parsed_.path);
363*6777b538SAndroid Build Coastguard Worker   }
364*6777b538SAndroid Build Coastguard Worker   std::string_view path_piece() const {
365*6777b538SAndroid Build Coastguard Worker     return ComponentStringPiece(parsed_.path);
366*6777b538SAndroid Build Coastguard Worker   }
367*6777b538SAndroid Build Coastguard Worker 
368*6777b538SAndroid Build Coastguard Worker   // Stuff following '?' up to the ref. The getters will not include the '?'.
369*6777b538SAndroid Build Coastguard Worker   bool has_query() const { return parsed_.query.is_valid(); }
370*6777b538SAndroid Build Coastguard Worker   std::string query() const {
371*6777b538SAndroid Build Coastguard Worker     return ComponentString(parsed_.query);
372*6777b538SAndroid Build Coastguard Worker   }
373*6777b538SAndroid Build Coastguard Worker   std::string_view query_piece() const {
374*6777b538SAndroid Build Coastguard Worker     return ComponentStringPiece(parsed_.query);
375*6777b538SAndroid Build Coastguard Worker   }
376*6777b538SAndroid Build Coastguard Worker 
377*6777b538SAndroid Build Coastguard Worker   // Stuff following '#' to the end of the string. This will be %-escaped UTF-8.
378*6777b538SAndroid Build Coastguard Worker   // The getters will not include the '#'.
379*6777b538SAndroid Build Coastguard Worker   bool has_ref() const { return parsed_.ref.is_valid(); }
380*6777b538SAndroid Build Coastguard Worker   std::string ref() const {
381*6777b538SAndroid Build Coastguard Worker     return ComponentString(parsed_.ref);
382*6777b538SAndroid Build Coastguard Worker   }
383*6777b538SAndroid Build Coastguard Worker   std::string_view ref_piece() const {
384*6777b538SAndroid Build Coastguard Worker     return ComponentStringPiece(parsed_.ref);
385*6777b538SAndroid Build Coastguard Worker   }
386*6777b538SAndroid Build Coastguard Worker 
387*6777b538SAndroid Build Coastguard Worker   // Returns a parsed version of the port. Can also be any of the special
388*6777b538SAndroid Build Coastguard Worker   // values defined in Parsed for ExtractPort.
389*6777b538SAndroid Build Coastguard Worker   int IntPort() const;
390*6777b538SAndroid Build Coastguard Worker 
391*6777b538SAndroid Build Coastguard Worker   // Returns the port number of the URL, or the default port number.
392*6777b538SAndroid Build Coastguard Worker   // If the scheme has no concept of port (or unknown default) returns
393*6777b538SAndroid Build Coastguard Worker   // PORT_UNSPECIFIED.
394*6777b538SAndroid Build Coastguard Worker   int EffectiveIntPort() const;
395*6777b538SAndroid Build Coastguard Worker 
396*6777b538SAndroid Build Coastguard Worker   // Extracts the filename portion of the path and returns it. The filename
397*6777b538SAndroid Build Coastguard Worker   // is everything after the last slash in the path. This may be empty.
398*6777b538SAndroid Build Coastguard Worker   std::string ExtractFileName() const;
399*6777b538SAndroid Build Coastguard Worker 
400*6777b538SAndroid Build Coastguard Worker   // Returns the path that should be sent to the server. This is the path,
401*6777b538SAndroid Build Coastguard Worker   // parameter, and query portions of the URL. It is guaranteed to be ASCII.
402*6777b538SAndroid Build Coastguard Worker   std::string PathForRequest() const;
403*6777b538SAndroid Build Coastguard Worker 
404*6777b538SAndroid Build Coastguard Worker   // Returns the same characters as PathForRequest(), avoiding a copy.
405*6777b538SAndroid Build Coastguard Worker   std::string_view PathForRequestPiece() const;
406*6777b538SAndroid Build Coastguard Worker 
407*6777b538SAndroid Build Coastguard Worker   // Returns the host, excluding the square brackets surrounding IPv6 address
408*6777b538SAndroid Build Coastguard Worker   // literals. This can be useful for passing to getaddrinfo().
409*6777b538SAndroid Build Coastguard Worker   std::string HostNoBrackets() const;
410*6777b538SAndroid Build Coastguard Worker 
411*6777b538SAndroid Build Coastguard Worker   // Returns the same characters as HostNoBrackets(), avoiding a copy.
412*6777b538SAndroid Build Coastguard Worker   std::string_view HostNoBracketsPiece() const;
413*6777b538SAndroid Build Coastguard Worker 
414*6777b538SAndroid Build Coastguard Worker   // Returns true if this URL's host matches or is in the same domain as
415*6777b538SAndroid Build Coastguard Worker   // the given input string. For example, if the hostname of the URL is
416*6777b538SAndroid Build Coastguard Worker   // "www.google.com", this will return true for "com", "google.com", and
417*6777b538SAndroid Build Coastguard Worker   // "www.google.com".
418*6777b538SAndroid Build Coastguard Worker   //
419*6777b538SAndroid Build Coastguard Worker   // The input domain should match host canonicalization rules. i.e. the input
420*6777b538SAndroid Build Coastguard Worker   // should be lowercase except for escape chars.
421*6777b538SAndroid Build Coastguard Worker   //
422*6777b538SAndroid Build Coastguard Worker   // This call is more efficient than getting the host and checking whether the
423*6777b538SAndroid Build Coastguard Worker   // host has the specific domain or not because no copies or object
424*6777b538SAndroid Build Coastguard Worker   // constructions are done.
425*6777b538SAndroid Build Coastguard Worker   bool DomainIs(std::string_view canonical_domain) const;
426*6777b538SAndroid Build Coastguard Worker 
427*6777b538SAndroid Build Coastguard Worker   // Checks whether or not two URLs differ only in the ref (the part after
428*6777b538SAndroid Build Coastguard Worker   // the # character).
429*6777b538SAndroid Build Coastguard Worker   bool EqualsIgnoringRef(const GURL& other) const;
430*6777b538SAndroid Build Coastguard Worker 
431*6777b538SAndroid Build Coastguard Worker   // Swaps the contents of this GURL object with |other|, without doing
432*6777b538SAndroid Build Coastguard Worker   // any memory allocations.
433*6777b538SAndroid Build Coastguard Worker   void Swap(GURL* other);
434*6777b538SAndroid Build Coastguard Worker 
435*6777b538SAndroid Build Coastguard Worker   // Returns a reference to a singleton empty GURL. This object is for callers
436*6777b538SAndroid Build Coastguard Worker   // who return references but don't have anything to return in some cases.
437*6777b538SAndroid Build Coastguard Worker   // If you just want an empty URL for normal use, prefer GURL(). This function
438*6777b538SAndroid Build Coastguard Worker   // may be called from any thread.
439*6777b538SAndroid Build Coastguard Worker   static const GURL& EmptyGURL();
440*6777b538SAndroid Build Coastguard Worker 
441*6777b538SAndroid Build Coastguard Worker   // Returns the inner URL of a nested URL (currently only non-null for
442*6777b538SAndroid Build Coastguard Worker   // filesystem URLs).
443*6777b538SAndroid Build Coastguard Worker   //
444*6777b538SAndroid Build Coastguard Worker   // TODO(mmenke): inner_url().spec() currently returns the same value as
445*6777b538SAndroid Build Coastguard Worker   // caling spec() on the GURL itself. This should be fixed.
446*6777b538SAndroid Build Coastguard Worker   // See https://crbug.com/619596
447*6777b538SAndroid Build Coastguard Worker   const GURL* inner_url() const {
448*6777b538SAndroid Build Coastguard Worker     return inner_url_.get();
449*6777b538SAndroid Build Coastguard Worker   }
450*6777b538SAndroid Build Coastguard Worker 
451*6777b538SAndroid Build Coastguard Worker   // Estimates dynamic memory usage.
452*6777b538SAndroid Build Coastguard Worker   // See base/trace_event/memory_usage_estimator.h for more info.
453*6777b538SAndroid Build Coastguard Worker   size_t EstimateMemoryUsage() const;
454*6777b538SAndroid Build Coastguard Worker 
455*6777b538SAndroid Build Coastguard Worker   // Helper used by GURL::IsAboutUrl and KURL::IsAboutURL.
456*6777b538SAndroid Build Coastguard Worker   static bool IsAboutPath(std::string_view actual_path,
457*6777b538SAndroid Build Coastguard Worker                           std::string_view allowed_path);
458*6777b538SAndroid Build Coastguard Worker 
459*6777b538SAndroid Build Coastguard Worker   void WriteIntoTrace(perfetto::TracedValue context) const;
460*6777b538SAndroid Build Coastguard Worker 
461*6777b538SAndroid Build Coastguard Worker  private:
462*6777b538SAndroid Build Coastguard Worker   // Variant of the string parsing constructor that allows the caller to elect
463*6777b538SAndroid Build Coastguard Worker   // retain trailing whitespace, if any, on the passed URL spec, but only if
464*6777b538SAndroid Build Coastguard Worker   // the scheme is one that allows trailing whitespace. The primary use-case is
465*6777b538SAndroid Build Coastguard Worker   // for data: URLs. In most cases, you want to use the single parameter
466*6777b538SAndroid Build Coastguard Worker   // constructor above.
467*6777b538SAndroid Build Coastguard Worker   enum RetainWhiteSpaceSelector { RETAIN_TRAILING_PATH_WHITEPACE };
468*6777b538SAndroid Build Coastguard Worker   GURL(const std::string& url_string, RetainWhiteSpaceSelector);
469*6777b538SAndroid Build Coastguard Worker 
470*6777b538SAndroid Build Coastguard Worker   template <typename T, typename CharT = typename T::value_type>
471*6777b538SAndroid Build Coastguard Worker   void InitCanonical(T input_spec, bool trim_path_end);
472*6777b538SAndroid Build Coastguard Worker 
473*6777b538SAndroid Build Coastguard Worker   void InitializeFromCanonicalSpec();
474*6777b538SAndroid Build Coastguard Worker 
475*6777b538SAndroid Build Coastguard Worker   // Helper used by IsAboutBlank and IsAboutSrcdoc.
476*6777b538SAndroid Build Coastguard Worker   bool IsAboutUrl(std::string_view allowed_path) const;
477*6777b538SAndroid Build Coastguard Worker 
478*6777b538SAndroid Build Coastguard Worker   // Returns the substring of the input identified by the given component.
479*6777b538SAndroid Build Coastguard Worker   std::string ComponentString(const url::Component& comp) const {
480*6777b538SAndroid Build Coastguard Worker     return std::string(ComponentStringPiece(comp));
481*6777b538SAndroid Build Coastguard Worker   }
482*6777b538SAndroid Build Coastguard Worker   std::string_view ComponentStringPiece(const url::Component& comp) const {
483*6777b538SAndroid Build Coastguard Worker     if (comp.is_empty())
484*6777b538SAndroid Build Coastguard Worker       return std::string_view();
485*6777b538SAndroid Build Coastguard Worker     return std::string_view(spec_).substr(static_cast<size_t>(comp.begin),
486*6777b538SAndroid Build Coastguard Worker                                           static_cast<size_t>(comp.len));
487*6777b538SAndroid Build Coastguard Worker   }
488*6777b538SAndroid Build Coastguard Worker 
489*6777b538SAndroid Build Coastguard Worker   void ProcessFileSystemURLAfterReplaceComponents();
490*6777b538SAndroid Build Coastguard Worker 
491*6777b538SAndroid Build Coastguard Worker   // The actual text of the URL, in canonical ASCII form.
492*6777b538SAndroid Build Coastguard Worker   std::string spec_;
493*6777b538SAndroid Build Coastguard Worker 
494*6777b538SAndroid Build Coastguard Worker   // Set when the given URL is valid. Otherwise, we may still have a spec and
495*6777b538SAndroid Build Coastguard Worker   // components, but they may not identify valid resources (for example, an
496*6777b538SAndroid Build Coastguard Worker   // invalid port number, invalid characters in the scheme, etc.).
497*6777b538SAndroid Build Coastguard Worker   bool is_valid_;
498*6777b538SAndroid Build Coastguard Worker 
499*6777b538SAndroid Build Coastguard Worker   // Identified components of the canonical spec.
500*6777b538SAndroid Build Coastguard Worker   url::Parsed parsed_;
501*6777b538SAndroid Build Coastguard Worker 
502*6777b538SAndroid Build Coastguard Worker   // Used for nested schemes [currently only filesystem:].
503*6777b538SAndroid Build Coastguard Worker   std::unique_ptr<GURL> inner_url_;
504*6777b538SAndroid Build Coastguard Worker };
505*6777b538SAndroid Build Coastguard Worker 
506*6777b538SAndroid Build Coastguard Worker // Stream operator so GURL can be used in assertion statements.
507*6777b538SAndroid Build Coastguard Worker COMPONENT_EXPORT(URL)
508*6777b538SAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& out, const GURL& url);
509*6777b538SAndroid Build Coastguard Worker 
510*6777b538SAndroid Build Coastguard Worker COMPONENT_EXPORT(URL) bool operator==(const GURL& x, const GURL& y);
511*6777b538SAndroid Build Coastguard Worker COMPONENT_EXPORT(URL) bool operator!=(const GURL& x, const GURL& y);
512*6777b538SAndroid Build Coastguard Worker 
513*6777b538SAndroid Build Coastguard Worker // Equality operator for comparing raw spec_. This should be used in place of
514*6777b538SAndroid Build Coastguard Worker // url == GURL(spec) where |spec| is known (i.e. constants). This is to prevent
515*6777b538SAndroid Build Coastguard Worker // needlessly re-parsing |spec| into a temporary GURL.
516*6777b538SAndroid Build Coastguard Worker COMPONENT_EXPORT(URL)
517*6777b538SAndroid Build Coastguard Worker bool operator==(const GURL& x, std::string_view spec);
518*6777b538SAndroid Build Coastguard Worker COMPONENT_EXPORT(URL)
519*6777b538SAndroid Build Coastguard Worker bool operator==(std::string_view spec, const GURL& x);
520*6777b538SAndroid Build Coastguard Worker COMPONENT_EXPORT(URL)
521*6777b538SAndroid Build Coastguard Worker bool operator!=(const GURL& x, std::string_view spec);
522*6777b538SAndroid Build Coastguard Worker COMPONENT_EXPORT(URL)
523*6777b538SAndroid Build Coastguard Worker bool operator!=(std::string_view spec, const GURL& x);
524*6777b538SAndroid Build Coastguard Worker 
525*6777b538SAndroid Build Coastguard Worker // DEBUG_ALIAS_FOR_GURL(var_name, url) copies |url| into a new stack-allocated
526*6777b538SAndroid Build Coastguard Worker // variable named |<var_name>|.  This helps ensure that the value of |url| gets
527*6777b538SAndroid Build Coastguard Worker // preserved in crash dumps.
528*6777b538SAndroid Build Coastguard Worker #define DEBUG_ALIAS_FOR_GURL(var_name, url) \
529*6777b538SAndroid Build Coastguard Worker   DEBUG_ALIAS_FOR_CSTR(var_name, (url).possibly_invalid_spec().c_str(), 128)
530*6777b538SAndroid Build Coastguard Worker 
531*6777b538SAndroid Build Coastguard Worker namespace url::debug {
532*6777b538SAndroid Build Coastguard Worker 
COMPONENT_EXPORT(URL)533*6777b538SAndroid Build Coastguard Worker class COMPONENT_EXPORT(URL) ScopedUrlCrashKey {
534*6777b538SAndroid Build Coastguard Worker  public:
535*6777b538SAndroid Build Coastguard Worker   ScopedUrlCrashKey(base::debug::CrashKeyString* crash_key, const GURL& value);
536*6777b538SAndroid Build Coastguard Worker   ~ScopedUrlCrashKey();
537*6777b538SAndroid Build Coastguard Worker 
538*6777b538SAndroid Build Coastguard Worker   ScopedUrlCrashKey(const ScopedUrlCrashKey&) = delete;
539*6777b538SAndroid Build Coastguard Worker   ScopedUrlCrashKey& operator=(const ScopedUrlCrashKey&) = delete;
540*6777b538SAndroid Build Coastguard Worker 
541*6777b538SAndroid Build Coastguard Worker  private:
542*6777b538SAndroid Build Coastguard Worker   base::debug::ScopedCrashKeyString scoped_string_value_;
543*6777b538SAndroid Build Coastguard Worker };
544*6777b538SAndroid Build Coastguard Worker 
545*6777b538SAndroid Build Coastguard Worker }  // namespace url::debug
546*6777b538SAndroid Build Coastguard Worker 
547*6777b538SAndroid Build Coastguard Worker #endif  // URL_GURL_H_
548