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