1 // Copyright 2013 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.
5 #ifndef URL_URL_UTIL_H_
6 #define URL_URL_UTIL_H_
10 #include <string_view>
13 #include "base/component_export.h"
14 #include "url/third_party/mozilla/url_parse.h"
15 #include "url/url_canon.h"
16 #include "url/url_constants.h"
20 // Init ------------------------------------------------------------------------
22 // Used for tests that need to reset schemes. Note that this can only be used
23 // in conjunction with ScopedSchemeRegistryForTests.
24 COMPONENT_EXPORT(URL) void ClearSchemesForTests();
26 class ScopedSchemeRegistryInternal;
28 // Stores the SchemeRegistry upon creation, allowing tests to modify a copy of
29 // it, and restores the original SchemeRegistry when deleted.
30 class COMPONENT_EXPORT(URL) ScopedSchemeRegistryForTests {
32 ScopedSchemeRegistryForTests();
33 ~ScopedSchemeRegistryForTests();
36 std::unique_ptr<ScopedSchemeRegistryInternal> internal_;
39 // Schemes ---------------------------------------------------------------------
41 // Changes the behavior of SchemeHostPort / Origin to allow non-standard schemes
42 // to be specified, instead of canonicalizing them to an invalid SchemeHostPort
43 // or opaque Origin, respectively. This is used for Android WebView backwards
44 // compatibility, which allows the use of custom schemes: content hosted in
45 // Android WebView assumes that one URL with a non-standard scheme will be
46 // same-origin to another URL with the same non-standard scheme.
49 COMPONENT_EXPORT(URL) void EnableNonStandardSchemesForAndroidWebView();
51 // Whether or not SchemeHostPort and Origin allow non-standard schemes.
52 COMPONENT_EXPORT(URL) bool AllowNonStandardSchemesForAndroidWebView();
54 // The following Add*Scheme method are not threadsafe and can not be called
55 // concurrently with any other url_util function. They will assert if the lists
56 // of schemes have been locked (see LockSchemeRegistries), or used.
58 // Adds an application-defined scheme to the internal list of "standard-format"
59 // URL schemes. A standard-format scheme adheres to what RFC 3986 calls "generic
60 // URI syntax" (https://tools.ietf.org/html/rfc3986#section-3).
63 void AddStandardScheme(const char* new_scheme, SchemeType scheme_type);
65 // Returns the list of schemes registered for "standard" URLs. Note, this
66 // should not be used if you just need to check if your protocol is standard
67 // or not. Instead use the IsStandard() function above as its much more
68 // efficient. This function should only be used where you need to perform
69 // other operations against the standard scheme list.
71 std::vector<std::string> GetStandardSchemes();
73 // Adds an application-defined scheme to the internal list of schemes allowed
76 void AddReferrerScheme(const char* new_scheme, SchemeType scheme_type);
78 // Adds an application-defined scheme to the list of schemes that do not trigger
79 // mixed content warnings.
80 COMPONENT_EXPORT(URL) void AddSecureScheme(const char* new_scheme);
81 COMPONENT_EXPORT(URL) const std::vector<std::string>& GetSecureSchemes();
83 // Adds an application-defined scheme to the list of schemes that normal pages
84 // cannot link to or access (i.e., with the same security rules as those applied
86 COMPONENT_EXPORT(URL) void AddLocalScheme(const char* new_scheme);
87 COMPONENT_EXPORT(URL) const std::vector<std::string>& GetLocalSchemes();
89 // Adds an application-defined scheme to the list of schemes that cause pages
90 // loaded with them to not have access to pages loaded with any other URL
92 COMPONENT_EXPORT(URL) void AddNoAccessScheme(const char* new_scheme);
93 COMPONENT_EXPORT(URL) const std::vector<std::string>& GetNoAccessSchemes();
95 // Adds an application-defined scheme to the list of schemes that can be sent
97 COMPONENT_EXPORT(URL) void AddCorsEnabledScheme(const char* new_scheme);
98 COMPONENT_EXPORT(URL) const std::vector<std::string>& GetCorsEnabledSchemes();
100 // Adds an application-defined scheme to the list of web schemes that can be
101 // used by web to store data (e.g. cookies, local storage, ...). This is
102 // to differentiate them from schemes that can store data but are not used on
103 // web (e.g. application's internal schemes) or schemes that are used on web but
104 // cannot store data.
105 COMPONENT_EXPORT(URL) void AddWebStorageScheme(const char* new_scheme);
106 COMPONENT_EXPORT(URL) const std::vector<std::string>& GetWebStorageSchemes();
108 // Adds an application-defined scheme to the list of schemes that can bypass the
109 // Content-Security-Policy (CSP) checks.
110 COMPONENT_EXPORT(URL) void AddCSPBypassingScheme(const char* new_scheme);
111 COMPONENT_EXPORT(URL) const std::vector<std::string>& GetCSPBypassingSchemes();
113 // Adds an application-defined scheme to the list of schemes that are strictly
114 // empty documents, allowing them to commit synchronously.
115 COMPONENT_EXPORT(URL) void AddEmptyDocumentScheme(const char* new_scheme);
116 COMPONENT_EXPORT(URL) const std::vector<std::string>& GetEmptyDocumentSchemes();
118 // Adds a scheme with a predefined default handler.
120 // This pair of strings must be normalized protocol handler parameters as
121 // described in the Custom Handler specification.
122 // https://html.spec.whatwg.org/multipage/system-state.html#normalize-protocol-handler-parameters
123 COMPONENT_EXPORT(URL)
124 void AddPredefinedHandlerScheme(const char* new_scheme, const char* handler);
125 COMPONENT_EXPORT(URL)
126 std::vector<std::pair<std::string, std::string>> GetPredefinedHandlerSchemes();
128 // Sets a flag to prevent future calls to Add*Scheme from succeeding.
130 // This is designed to help prevent errors for multithreaded applications.
131 // Normal usage would be to call Add*Scheme for your custom schemes at
132 // the beginning of program initialization, and then LockSchemeRegistries. This
133 // prevents future callers from mistakenly calling Add*Scheme when the
134 // program is running with multiple threads, where such usage would be
137 // We could have had Add*Scheme use a lock instead, but that would add
138 // some platform-specific dependencies we don't otherwise have now, and is
139 // overkill considering the normal usage is so simple.
140 COMPONENT_EXPORT(URL) void LockSchemeRegistries();
142 // Locates the scheme in the given string and places it into |found_scheme|,
143 // which may be NULL to indicate the caller does not care about the range.
145 // Returns whether the given |compare| scheme matches the scheme found in the
146 // input (if any). The |compare| scheme must be a valid canonical scheme or
147 // the result of the comparison is undefined.
148 COMPONENT_EXPORT(URL)
149 bool FindAndCompareScheme(const char* str,
152 Component* found_scheme);
153 COMPONENT_EXPORT(URL)
154 bool FindAndCompareScheme(const char16_t* str,
157 Component* found_scheme);
158 inline bool FindAndCompareScheme(const std::string& str,
160 Component* found_scheme) {
161 return FindAndCompareScheme(str.data(), static_cast<int>(str.size()),
162 compare, found_scheme);
164 inline bool FindAndCompareScheme(const std::u16string& str,
166 Component* found_scheme) {
167 return FindAndCompareScheme(str.data(), static_cast<int>(str.size()),
168 compare, found_scheme);
171 // Returns true if the given scheme identified by |scheme| within |spec| is in
172 // the list of known standard-format schemes (see AddStandardScheme).
173 COMPONENT_EXPORT(URL)
174 bool IsStandard(const char* spec, const Component& scheme);
175 COMPONENT_EXPORT(URL)
176 bool IsStandard(const char16_t* spec, const Component& scheme);
178 // Returns true if the given scheme identified by |scheme| within |spec| is in
179 // the list of allowed schemes for referrers (see AddReferrerScheme).
180 COMPONENT_EXPORT(URL)
181 bool IsReferrerScheme(const char* spec, const Component& scheme);
183 // Returns true and sets |type| to the SchemeType of the given scheme
184 // identified by |scheme| within |spec| if the scheme is in the list of known
185 // standard-format schemes (see AddStandardScheme).
186 COMPONENT_EXPORT(URL)
187 bool GetStandardSchemeType(const char* spec,
188 const Component& scheme,
190 COMPONENT_EXPORT(URL)
191 bool GetStandardSchemeType(const char16_t* spec,
192 const Component& scheme,
195 // Hosts ----------------------------------------------------------------------
197 // Returns true if the |canonical_host| matches or is in the same domain as the
198 // given |canonical_domain| string. For example, if the canonicalized hostname
199 // is "www.google.com", this will return true for "com", "google.com", and
200 // "www.google.com" domains.
202 // If either of the input StringPieces is empty, the return value is false. The
203 // input domain should match host canonicalization rules. i.e. it should be
204 // lowercase except for escape chars.
205 COMPONENT_EXPORT(URL)
206 bool DomainIs(std::string_view canonical_host,
207 std::string_view canonical_domain);
209 // Returns true if the hostname is an IP address. Note: this function isn't very
210 // cheap, as it must re-parse the host to verify.
211 COMPONENT_EXPORT(URL) bool HostIsIPAddress(std::string_view host);
213 // URL library wrappers --------------------------------------------------------
215 // Parses the given spec according to the extracted scheme type. Normal users
216 // should use the URL object, although this may be useful if performance is
217 // critical and you don't want to do the heap allocation for the std::string.
219 // As with the Canonicalize* functions, the charset converter can
220 // be NULL to use UTF-8 (it will be faster in this case).
222 // Returns true if a valid URL was produced, false if not. On failure, the
223 // output and parsed structures will still be filled and will be consistent,
224 // but they will not represent a loadable URL.
225 COMPONENT_EXPORT(URL)
226 bool Canonicalize(const char* spec,
229 CharsetConverter* charset_converter,
231 Parsed* output_parsed);
232 COMPONENT_EXPORT(URL)
233 bool Canonicalize(const char16_t* spec,
236 CharsetConverter* charset_converter,
238 Parsed* output_parsed);
240 // Resolves a potentially relative URL relative to the given parsed base URL.
241 // The base MUST be valid. The resulting canonical URL and parsed information
242 // will be placed in to the given out variables.
244 // The relative need not be relative. If we discover that it's absolute, this
245 // will produce a canonical version of that URL. See Canonicalize() for more
246 // about the charset_converter.
248 // Returns true if the output is valid, false if the input could not produce
250 COMPONENT_EXPORT(URL)
251 bool ResolveRelative(const char* base_spec,
253 const Parsed& base_parsed,
254 const char* relative,
256 CharsetConverter* charset_converter,
258 Parsed* output_parsed);
259 COMPONENT_EXPORT(URL)
260 bool ResolveRelative(const char* base_spec,
262 const Parsed& base_parsed,
263 const char16_t* relative,
265 CharsetConverter* charset_converter,
267 Parsed* output_parsed);
269 // Replaces components in the given VALID input URL. The new canonical URL info
270 // is written to output and out_parsed.
272 // Returns true if the resulting URL is valid.
273 COMPONENT_EXPORT(URL)
274 bool ReplaceComponents(const char* spec,
276 const Parsed& parsed,
277 const Replacements<char>& replacements,
278 CharsetConverter* charset_converter,
281 COMPONENT_EXPORT(URL)
282 bool ReplaceComponents(const char* spec,
284 const Parsed& parsed,
285 const Replacements<char16_t>& replacements,
286 CharsetConverter* charset_converter,
290 // String helper functions -----------------------------------------------------
292 enum class DecodeURLMode {
293 // UTF-8 decode only. Invalid byte sequences are replaced with U+FFFD.
295 // Try UTF-8 decoding. If the input contains byte sequences invalid
296 // for UTF-8, apply byte to Unicode mapping.
300 // Unescapes the given string using URL escaping rules.
301 COMPONENT_EXPORT(URL)
302 void DecodeURLEscapeSequences(std::string_view input,
304 CanonOutputW* output);
306 // Escapes the given string as defined by the JS method encodeURIComponent. See
307 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent
308 COMPONENT_EXPORT(URL)
309 void EncodeURIComponent(std::string_view input, CanonOutput* output);
311 // Returns true if `c` is a character that does not require escaping in
312 // encodeURIComponent.
313 // TODO(crbug.com/1481056): Remove this when event-level reportEvent is removed
314 // (if it is still this function's only consumer).
315 COMPONENT_EXPORT(URL)
316 bool IsURIComponentChar(char c);
318 // Checks an arbitrary string for invalid escape sequences.
320 // A valid percent-encoding is '%' followed by exactly two hex-digits. This
321 // function returns true if an occurrence of '%' is found and followed by
322 // anything other than two hex-digits.
323 COMPONENT_EXPORT(URL)
324 bool HasInvalidURLEscapeSequences(std::string_view input);
326 COMPONENT_EXPORT(URL)
327 bool CompareSchemeComponent(const char* spec,
328 const Component& component,
329 const char* compare_to);
331 COMPONENT_EXPORT(URL)
332 bool CompareSchemeComponent(const char16_t* spec,
333 const Component& component,
334 const char* compare_to);
337 #endif // URL_URL_UTIL_H_