[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" |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 11 | #include "url/url_canon.h" |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 12 | #include "url/url_export.h" |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 13 | #include "url/url_parse.h" |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 14 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 15 | namespace url { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 16 | |
| 17 | // Init ------------------------------------------------------------------------ |
| 18 | |
| 19 | // Initialization is NOT required, it will be implicitly initialized when first |
| 20 | // used. However, this implicit initialization is NOT threadsafe. If you are |
| 21 | // using this library in a threaded environment and don't have a consistent |
| 22 | // "first call" (an example might be calling "AddStandardScheme" with your |
| 23 | // special application-specific schemes) then you will want to call initialize |
| 24 | // before spawning any threads. |
| 25 | // |
| 26 | // It is OK to call this function more than once, subsequent calls will simply |
| 27 | // "noop", unless Shutdown() was called in the mean time. This will also be a |
| 28 | // "noop" if other calls to the library have forced an initialization |
| 29 | // 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 | |
| 40 | // Adds an application-defined scheme to the internal list of "standard" URL |
| 41 | // schemes. This function is not threadsafe and can not be called concurrently |
| 42 | // with any other url_util function. It will assert if the list of standard |
| 43 | // schemes has been locked (see LockStandardSchemes). |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 44 | URL_EXPORT void AddStandardScheme(const char* new_scheme); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 45 | |
| 46 | // Sets a flag to prevent future calls to AddStandardScheme from succeeding. |
| 47 | // |
| 48 | // This is designed to help prevent errors for multithreaded applications. |
| 49 | // Normal usage would be to call AddStandardScheme for your custom schemes at |
| 50 | // the beginning of program initialization, and then LockStandardSchemes. This |
| 51 | // prevents future callers from mistakenly calling AddStandardScheme when the |
| 52 | // program is running with multiple threads, where such usage would be |
| 53 | // dangerous. |
| 54 | // |
| 55 | // We could have had AddStandardScheme use a lock instead, but that would add |
| 56 | // some platform-specific dependencies we don't otherwise have now, and is |
| 57 | // overkill considering the normal usage is so simple. |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 58 | URL_EXPORT void LockStandardSchemes(); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 59 | |
| 60 | // Locates the scheme in the given string and places it into |found_scheme|, |
| 61 | // which may be NULL to indicate the caller does not care about the range. |
| 62 | // |
| 63 | // Returns whether the given |compare| scheme matches the scheme found in the |
| 64 | // input (if any). The |compare| scheme must be a valid canonical scheme or |
| 65 | // the result of the comparison is undefined. |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 66 | URL_EXPORT bool FindAndCompareScheme(const char* str, |
| 67 | int str_len, |
| 68 | const char* compare, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 69 | Component* found_scheme); |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 70 | URL_EXPORT bool FindAndCompareScheme(const base::char16* str, |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 71 | int str_len, |
| 72 | const char* compare, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 73 | Component* found_scheme); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 74 | inline bool FindAndCompareScheme(const std::string& str, |
| 75 | const char* compare, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 76 | Component* found_scheme) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 77 | return FindAndCompareScheme(str.data(), static_cast<int>(str.size()), |
| 78 | compare, found_scheme); |
| 79 | } |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 80 | inline bool FindAndCompareScheme(const base::string16& str, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 81 | const char* compare, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 82 | Component* found_scheme) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 83 | return FindAndCompareScheme(str.data(), static_cast<int>(str.size()), |
| 84 | compare, found_scheme); |
| 85 | } |
| 86 | |
| 87 | // Returns true if the given string represents a standard URL. This means that |
| 88 | // either the scheme is in the list of known standard schemes. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 89 | URL_EXPORT bool IsStandard(const char* spec, const Component& scheme); |
| 90 | URL_EXPORT bool IsStandard(const base::char16* spec, const Component& scheme); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 91 | |
| 92 | // TODO(brettw) remove this. This is a temporary compatibility hack to avoid |
| 93 | // breaking the WebKit build when this version is synced via Chrome. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 94 | inline bool IsStandard(const char* spec, |
| 95 | int spec_len, |
| 96 | const Component& scheme) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 97 | return IsStandard(spec, scheme); |
| 98 | } |
| 99 | |
| 100 | // URL library wrappers ------------------------------------------------------- |
| 101 | |
| 102 | // Parses the given spec according to the extracted scheme type. Normal users |
| 103 | // should use the URL object, although this may be useful if performance is |
| 104 | // critical and you don't want to do the heap allocation for the std::string. |
| 105 | // |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 106 | // As with the Canonicalize* functions, the charset converter can |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 107 | // be NULL to use UTF-8 (it will be faster in this case). |
| 108 | // |
| 109 | // Returns true if a valid URL was produced, false if not. On failure, the |
| 110 | // output and parsed structures will still be filled and will be consistent, |
| 111 | // but they will not represent a loadable URL. |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 112 | URL_EXPORT bool Canonicalize(const char* spec, |
| 113 | int spec_len, |
[email protected] | 369e84f7 | 2013-11-23 01:53:52 | [diff] [blame] | 114 | bool trim_path_end, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 115 | CharsetConverter* charset_converter, |
| 116 | CanonOutput* output, |
| 117 | Parsed* output_parsed); |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 118 | URL_EXPORT bool Canonicalize(const base::char16* spec, |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 119 | int spec_len, |
[email protected] | 369e84f7 | 2013-11-23 01:53:52 | [diff] [blame] | 120 | bool trim_path_end, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 121 | CharsetConverter* charset_converter, |
| 122 | CanonOutput* output, |
| 123 | Parsed* output_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 124 | |
| 125 | // Resolves a potentially relative URL relative to the given parsed base URL. |
| 126 | // The base MUST be valid. The resulting canonical URL and parsed information |
| 127 | // will be placed in to the given out variables. |
| 128 | // |
| 129 | // The relative need not be relative. If we discover that it's absolute, this |
| 130 | // will produce a canonical version of that URL. See Canonicalize() for more |
| 131 | // about the charset_converter. |
| 132 | // |
| 133 | // Returns true if the output is valid, false if the input could not produce |
| 134 | // a valid URL. |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 135 | URL_EXPORT bool ResolveRelative(const char* base_spec, |
| 136 | int base_spec_len, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 137 | const Parsed& base_parsed, |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 138 | const char* relative, |
| 139 | int relative_length, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 140 | CharsetConverter* charset_converter, |
| 141 | CanonOutput* output, |
| 142 | Parsed* output_parsed); |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 143 | URL_EXPORT bool ResolveRelative(const char* base_spec, |
| 144 | int base_spec_len, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 145 | const Parsed& base_parsed, |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 146 | const base::char16* relative, |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 147 | int relative_length, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 148 | CharsetConverter* charset_converter, |
| 149 | CanonOutput* output, |
| 150 | Parsed* output_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 151 | |
| 152 | // Replaces components in the given VALID input url. The new canonical URL info |
| 153 | // is written to output and out_parsed. |
| 154 | // |
| 155 | // Returns true if the resulting URL is valid. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 156 | URL_EXPORT bool ReplaceComponents(const char* spec, |
| 157 | int spec_len, |
| 158 | const Parsed& parsed, |
| 159 | const Replacements<char>& replacements, |
| 160 | CharsetConverter* charset_converter, |
| 161 | CanonOutput* output, |
| 162 | Parsed* out_parsed); |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 163 | URL_EXPORT bool ReplaceComponents( |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 164 | const char* spec, |
| 165 | int spec_len, |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 166 | const Parsed& parsed, |
| 167 | const Replacements<base::char16>& replacements, |
| 168 | CharsetConverter* charset_converter, |
| 169 | CanonOutput* output, |
| 170 | Parsed* out_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 171 | |
| 172 | // String helper functions ---------------------------------------------------- |
| 173 | |
| 174 | // Compare the lower-case form of the given string against the given ASCII |
| 175 | // string. This is useful for doing checking if an input string matches some |
| 176 | // token, and it is optimized to avoid intermediate string copies. |
| 177 | // |
| 178 | // The versions of this function that don't take a b_end assume that the b |
| 179 | // string is NULL terminated. |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 180 | URL_EXPORT bool LowerCaseEqualsASCII(const char* a_begin, |
| 181 | const char* a_end, |
| 182 | const char* b); |
| 183 | URL_EXPORT bool LowerCaseEqualsASCII(const char* a_begin, |
| 184 | const char* a_end, |
| 185 | const char* b_begin, |
| 186 | const char* b_end); |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 187 | URL_EXPORT bool LowerCaseEqualsASCII(const base::char16* a_begin, |
| 188 | const base::char16* a_end, |
[email protected] | 760ea50 | 2013-05-31 03:39:51 | [diff] [blame] | 189 | const char* b); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 190 | |
| 191 | // Unescapes the given string using URL escaping rules. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 192 | URL_EXPORT void DecodeURLEscapeSequences(const char* input, |
| 193 | int length, |
| 194 | CanonOutputW* output); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 195 | |
| 196 | // Escapes the given string as defined by the JS method encodeURIComponent. See |
| 197 | // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 198 | URL_EXPORT void EncodeURIComponent(const char* input, |
| 199 | int length, |
| 200 | CanonOutput* output); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 201 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame^] | 202 | } // namespace url |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 203 | |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 204 | #endif // URL_URL_UTIL_H_ |