[email protected] | 51bcc5d | 2013-04-24 01:41:37 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 4 | |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 5 | #ifndef URL_URL_UTIL_H_ |
| 6 | #define URL_URL_UTIL_H_ |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | |
[email protected] | 516f018 | 2013-06-11 22:51:56 | [diff] [blame] | 10 | #include "base/strings/string16.h" |
tfarina | 018de6e | 2015-05-26 17:41:20 | [diff] [blame] | 11 | #include "url/third_party/mozilla/url_parse.h" |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 12 | #include "url/url_canon.h" |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 13 | #include "url/url_constants.h" |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 14 | #include "url/url_export.h" |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 15 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 16 | namespace url { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 17 | |
| 18 | // Init ------------------------------------------------------------------------ |
| 19 | |
| 20 | // Initialization is NOT required, it will be implicitly initialized when first |
| 21 | // used. However, this implicit initialization is NOT threadsafe. If you are |
| 22 | // using this library in a threaded environment and don't have a consistent |
palmer | 29ae548 | 2015-05-19 08:43:37 | [diff] [blame] | 23 | // "first call" (an example might be calling AddStandardScheme with your special |
| 24 | // application-specific schemes) then you will want to call initialize before |
| 25 | // spawning any threads. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 26 | // |
palmer | 29ae548 | 2015-05-19 08:43:37 | [diff] [blame] | 27 | // It is OK to call this function more than once, subsequent calls will be |
| 28 | // no-ops, unless Shutdown was called in the mean time. This will also be a |
| 29 | // no-op if other calls to the library have forced an initialization beforehand. |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 30 | URL_EXPORT void Initialize(); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 31 | |
| 32 | // Cleanup is not required, except some strings may leak. For most user |
| 33 | // applications, this is fine. If you're using it in a library that may get |
| 34 | // loaded and unloaded, you'll want to unload to properly clean up your |
| 35 | // library. |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 36 | URL_EXPORT void Shutdown(); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 37 | |
| 38 | // Schemes -------------------------------------------------------------------- |
| 39 | |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 40 | // Types of a scheme representing the requirements on the data represented by |
| 41 | // the authority component of a URL with the scheme. |
Nico Weber | 204f0a7 | 2015-08-19 15:56:23 | [diff] [blame] | 42 | enum SchemeType { |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 43 | // The authority component of a URL with the scheme, if any, has the port |
| 44 | // (the default values may be omitted in a serialization). |
| 45 | SCHEME_WITH_PORT, |
| 46 | // The authority component of a URL with the scheme, if any, doesn't have a |
| 47 | // port. |
| 48 | SCHEME_WITHOUT_PORT, |
| 49 | // A URL with the scheme doesn't have the authority component. |
| 50 | SCHEME_WITHOUT_AUTHORITY, |
| 51 | }; |
| 52 | |
| 53 | // A pair for representing a standard scheme name and the SchemeType for it. |
| 54 | struct URL_EXPORT SchemeWithType { |
| 55 | const char* scheme; |
| 56 | SchemeType type; |
| 57 | }; |
| 58 | |
palmer | 29ae548 | 2015-05-19 08:43:37 | [diff] [blame] | 59 | // Adds an application-defined scheme to the internal list of "standard-format" |
| 60 | // URL schemes. A standard-format scheme adheres to what RFC 3986 calls "generic |
| 61 | // URI syntax" (https://tools.ietf.org/html/rfc3986#section-3). |
| 62 | // |
| 63 | // This function is not threadsafe and can not be called concurrently with any |
| 64 | // other url_util function. It will assert if the list of standard schemes has |
| 65 | // been locked (see LockStandardSchemes). |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 66 | URL_EXPORT void AddStandardScheme(const char* new_scheme, |
| 67 | SchemeType scheme_type); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 68 | |
| 69 | // Sets a flag to prevent future calls to AddStandardScheme from succeeding. |
| 70 | // |
| 71 | // This is designed to help prevent errors for multithreaded applications. |
| 72 | // Normal usage would be to call AddStandardScheme for your custom schemes at |
| 73 | // the beginning of program initialization, and then LockStandardSchemes. This |
| 74 | // prevents future callers from mistakenly calling AddStandardScheme when the |
| 75 | // program is running with multiple threads, where such usage would be |
| 76 | // dangerous. |
| 77 | // |
| 78 | // We could have had AddStandardScheme use a lock instead, but that would add |
| 79 | // some platform-specific dependencies we don't otherwise have now, and is |
| 80 | // overkill considering the normal usage is so simple. |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 81 | URL_EXPORT void LockStandardSchemes(); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 82 | |
| 83 | // Locates the scheme in the given string and places it into |found_scheme|, |
| 84 | // which may be NULL to indicate the caller does not care about the range. |
| 85 | // |
| 86 | // Returns whether the given |compare| scheme matches the scheme found in the |
| 87 | // input (if any). The |compare| scheme must be a valid canonical scheme or |
| 88 | // the result of the comparison is undefined. |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 89 | URL_EXPORT bool FindAndCompareScheme(const char* str, |
| 90 | int str_len, |
| 91 | const char* compare, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 92 | Component* found_scheme); |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 93 | URL_EXPORT bool FindAndCompareScheme(const base::char16* str, |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 94 | int str_len, |
| 95 | const char* compare, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 96 | Component* found_scheme); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 97 | inline bool FindAndCompareScheme(const std::string& str, |
| 98 | const char* compare, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 99 | Component* found_scheme) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 100 | return FindAndCompareScheme(str.data(), static_cast<int>(str.size()), |
| 101 | compare, found_scheme); |
| 102 | } |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 103 | inline bool FindAndCompareScheme(const base::string16& str, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 104 | const char* compare, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 105 | Component* found_scheme) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 106 | return FindAndCompareScheme(str.data(), static_cast<int>(str.size()), |
| 107 | compare, found_scheme); |
| 108 | } |
| 109 | |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 110 | // Returns true if the given scheme identified by |scheme| within |spec| is in |
| 111 | // the list of known standard-format schemes (see AddStandardScheme). |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 112 | URL_EXPORT bool IsStandard(const char* spec, const Component& scheme); |
| 113 | URL_EXPORT bool IsStandard(const base::char16* spec, const Component& scheme); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 114 | |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 115 | // Returns true and sets |type| to the SchemeType of the given scheme |
| 116 | // identified by |scheme| within |spec| if the scheme is in the list of known |
| 117 | // standard-format schemes (see AddStandardScheme). |
| 118 | URL_EXPORT bool GetStandardSchemeType(const char* spec, |
| 119 | const Component& scheme, |
| 120 | SchemeType* type); |
| 121 | |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 122 | // URL library wrappers ------------------------------------------------------- |
| 123 | |
| 124 | // Parses the given spec according to the extracted scheme type. Normal users |
| 125 | // should use the URL object, although this may be useful if performance is |
| 126 | // critical and you don't want to do the heap allocation for the std::string. |
| 127 | // |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 128 | // As with the Canonicalize* functions, the charset converter can |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 129 | // be NULL to use UTF-8 (it will be faster in this case). |
| 130 | // |
| 131 | // Returns true if a valid URL was produced, false if not. On failure, the |
| 132 | // output and parsed structures will still be filled and will be consistent, |
| 133 | // but they will not represent a loadable URL. |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 134 | URL_EXPORT bool Canonicalize(const char* spec, |
| 135 | int spec_len, |
[email protected] | 369e84f7 | 2013-11-23 01:53:52 | [diff] [blame] | 136 | bool trim_path_end, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 137 | CharsetConverter* charset_converter, |
| 138 | CanonOutput* output, |
| 139 | Parsed* output_parsed); |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 140 | URL_EXPORT bool Canonicalize(const base::char16* spec, |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 141 | int spec_len, |
[email protected] | 369e84f7 | 2013-11-23 01:53:52 | [diff] [blame] | 142 | bool trim_path_end, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 143 | CharsetConverter* charset_converter, |
| 144 | CanonOutput* output, |
| 145 | Parsed* output_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 146 | |
| 147 | // Resolves a potentially relative URL relative to the given parsed base URL. |
| 148 | // The base MUST be valid. The resulting canonical URL and parsed information |
| 149 | // will be placed in to the given out variables. |
| 150 | // |
| 151 | // The relative need not be relative. If we discover that it's absolute, this |
| 152 | // will produce a canonical version of that URL. See Canonicalize() for more |
| 153 | // about the charset_converter. |
| 154 | // |
| 155 | // Returns true if the output is valid, false if the input could not produce |
| 156 | // a valid URL. |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 157 | URL_EXPORT bool ResolveRelative(const char* base_spec, |
| 158 | int base_spec_len, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 159 | const Parsed& base_parsed, |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 160 | const char* relative, |
| 161 | int relative_length, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 162 | CharsetConverter* charset_converter, |
| 163 | CanonOutput* output, |
| 164 | Parsed* output_parsed); |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 165 | URL_EXPORT bool ResolveRelative(const char* base_spec, |
| 166 | int base_spec_len, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 167 | const Parsed& base_parsed, |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 168 | const base::char16* relative, |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 169 | int relative_length, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 170 | CharsetConverter* charset_converter, |
| 171 | CanonOutput* output, |
| 172 | Parsed* output_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 173 | |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 174 | // Replaces components in the given VALID input URL. The new canonical URL info |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 175 | // is written to output and out_parsed. |
| 176 | // |
| 177 | // Returns true if the resulting URL is valid. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 178 | URL_EXPORT bool ReplaceComponents(const char* spec, |
| 179 | int spec_len, |
| 180 | const Parsed& parsed, |
| 181 | const Replacements<char>& replacements, |
| 182 | CharsetConverter* charset_converter, |
| 183 | CanonOutput* output, |
| 184 | Parsed* out_parsed); |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 185 | URL_EXPORT bool ReplaceComponents( |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 186 | const char* spec, |
| 187 | int spec_len, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 188 | const Parsed& parsed, |
| 189 | const Replacements<base::char16>& replacements, |
| 190 | CharsetConverter* charset_converter, |
| 191 | CanonOutput* output, |
| 192 | Parsed* out_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 193 | |
| 194 | // String helper functions ---------------------------------------------------- |
| 195 | |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 196 | // Unescapes the given string using URL escaping rules. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 197 | URL_EXPORT void DecodeURLEscapeSequences(const char* input, |
| 198 | int length, |
| 199 | CanonOutputW* output); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 200 | |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 201 | // Escapes the given string as defined by the JS method encodeURIComponent. See |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 202 | // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 203 | URL_EXPORT void EncodeURIComponent(const char* input, |
| 204 | int length, |
| 205 | CanonOutput* output); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 206 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 207 | } // namespace url |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 208 | |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 209 | #endif // URL_URL_UTIL_H_ |