OLD | NEW |
1 // Copyright 2007, Google Inc. | 1 // Copyright 2007, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
11 // copyright notice, this list of conditions and the following disclaimer | 11 // copyright notice, this list of conditions and the following disclaimer |
12 // in the documentation and/or other materials provided with the | 12 // in the documentation and/or other materials provided with the |
13 // distribution. | 13 // distribution. |
14 // * Neither the name of Google Inc. nor the names of its | 14 // * Neither the name of Google Inc. nor the names of its |
15 // contributors may be used to endorse or promote products derived from | 15 // contributors may be used to endorse or promote products derived from |
16 // this software without specific prior written permission. | 16 // this software without specific prior written permission. |
17 // | 17 // |
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | 29 |
30 #ifndef GOOGLEURL_SRC_URL_UTIL_H__ | 30 #ifndef URL_URL_UTIL_H_ |
31 #define GOOGLEURL_SRC_URL_UTIL_H__ | 31 #define URL_URL_UTIL_H_ |
32 | 32 |
33 #include <string> | 33 #include <string> |
34 | 34 |
35 #include "base/string16.h" | 35 #include "base/string16.h" |
36 #include "googleurl/src/url_common.h" | 36 #include "url/url_canon.h" |
37 #include "googleurl/src/url_parse.h" | 37 #include "url/url_parse.h" |
38 #include "googleurl/src/url_canon.h" | |
39 | 38 |
40 namespace url_util { | 39 namespace url_util { |
41 | 40 |
42 // Init ------------------------------------------------------------------------ | 41 // Init ------------------------------------------------------------------------ |
43 | 42 |
44 // Initialization is NOT required, it will be implicitly initialized when first | 43 // Initialization is NOT required, it will be implicitly initialized when first |
45 // used. However, this implicit initialization is NOT threadsafe. If you are | 44 // used. However, this implicit initialization is NOT threadsafe. If you are |
46 // using this library in a threaded environment and don't have a consistent | 45 // using this library in a threaded environment and don't have a consistent |
47 // "first call" (an example might be calling "AddStandardScheme" with your | 46 // "first call" (an example might be calling "AddStandardScheme" with your |
48 // special application-specific schemes) then you will want to call initialize | 47 // special application-specific schemes) then you will want to call initialize |
49 // before spawning any threads. | 48 // before spawning any threads. |
50 // | 49 // |
51 // It is OK to call this function more than once, subsequent calls will simply | 50 // It is OK to call this function more than once, subsequent calls will simply |
52 // "noop", unless Shutdown() was called in the mean time. This will also be a | 51 // "noop", unless Shutdown() was called in the mean time. This will also be a |
53 // "noop" if other calls to the library have forced an initialization | 52 // "noop" if other calls to the library have forced an initialization |
54 // beforehand. | 53 // beforehand. |
55 GURL_API void Initialize(); | 54 void Initialize(); |
56 | 55 |
57 // Cleanup is not required, except some strings may leak. For most user | 56 // Cleanup is not required, except some strings may leak. For most user |
58 // applications, this is fine. If you're using it in a library that may get | 57 // applications, this is fine. If you're using it in a library that may get |
59 // loaded and unloaded, you'll want to unload to properly clean up your | 58 // loaded and unloaded, you'll want to unload to properly clean up your |
60 // library. | 59 // library. |
61 GURL_API void Shutdown(); | 60 void Shutdown(); |
62 | 61 |
63 // Schemes -------------------------------------------------------------------- | 62 // Schemes -------------------------------------------------------------------- |
64 | 63 |
65 // Adds an application-defined scheme to the internal list of "standard" URL | 64 // Adds an application-defined scheme to the internal list of "standard" URL |
66 // schemes. This function is not threadsafe and can not be called concurrently | 65 // schemes. This function is not threadsafe and can not be called concurrently |
67 // with any other url_util function. It will assert if the list of standard | 66 // with any other url_util function. It will assert if the list of standard |
68 // schemes has been locked (see LockStandardSchemes). | 67 // schemes has been locked (see LockStandardSchemes). |
69 GURL_API void AddStandardScheme(const char* new_scheme); | 68 void AddStandardScheme(const char* new_scheme); |
70 | 69 |
71 // Sets a flag to prevent future calls to AddStandardScheme from succeeding. | 70 // Sets a flag to prevent future calls to AddStandardScheme from succeeding. |
72 // | 71 // |
73 // This is designed to help prevent errors for multithreaded applications. | 72 // This is designed to help prevent errors for multithreaded applications. |
74 // Normal usage would be to call AddStandardScheme for your custom schemes at | 73 // Normal usage would be to call AddStandardScheme for your custom schemes at |
75 // the beginning of program initialization, and then LockStandardSchemes. This | 74 // the beginning of program initialization, and then LockStandardSchemes. This |
76 // prevents future callers from mistakenly calling AddStandardScheme when the | 75 // prevents future callers from mistakenly calling AddStandardScheme when the |
77 // program is running with multiple threads, where such usage would be | 76 // program is running with multiple threads, where such usage would be |
78 // dangerous. | 77 // dangerous. |
79 // | 78 // |
80 // We could have had AddStandardScheme use a lock instead, but that would add | 79 // We could have had AddStandardScheme use a lock instead, but that would add |
81 // some platform-specific dependencies we don't otherwise have now, and is | 80 // some platform-specific dependencies we don't otherwise have now, and is |
82 // overkill considering the normal usage is so simple. | 81 // overkill considering the normal usage is so simple. |
83 GURL_API void LockStandardSchemes(); | 82 void LockStandardSchemes(); |
84 | 83 |
85 // Locates the scheme in the given string and places it into |found_scheme|, | 84 // Locates the scheme in the given string and places it into |found_scheme|, |
86 // which may be NULL to indicate the caller does not care about the range. | 85 // which may be NULL to indicate the caller does not care about the range. |
87 // | 86 // |
88 // Returns whether the given |compare| scheme matches the scheme found in the | 87 // Returns whether the given |compare| scheme matches the scheme found in the |
89 // input (if any). The |compare| scheme must be a valid canonical scheme or | 88 // input (if any). The |compare| scheme must be a valid canonical scheme or |
90 // the result of the comparison is undefined. | 89 // the result of the comparison is undefined. |
91 GURL_API bool FindAndCompareScheme(const char* str, | 90 bool FindAndCompareScheme(const char* str, |
92 int str_len, | 91 int str_len, |
93 const char* compare, | 92 const char* compare, |
94 url_parse::Component* found_scheme); | 93 url_parse::Component* found_scheme); |
95 GURL_API bool FindAndCompareScheme(const char16* str, | 94 bool FindAndCompareScheme(const char16* str, |
96 int str_len, | 95 int str_len, |
97 const char* compare, | 96 const char* compare, |
98 url_parse::Component* found_scheme); | 97 url_parse::Component* found_scheme); |
99 inline bool FindAndCompareScheme(const std::string& str, | 98 inline bool FindAndCompareScheme(const std::string& str, |
100 const char* compare, | 99 const char* compare, |
101 url_parse::Component* found_scheme) { | 100 url_parse::Component* found_scheme) { |
102 return FindAndCompareScheme(str.data(), static_cast<int>(str.size()), | 101 return FindAndCompareScheme(str.data(), static_cast<int>(str.size()), |
103 compare, found_scheme); | 102 compare, found_scheme); |
104 } | 103 } |
105 inline bool FindAndCompareScheme(const string16& str, | 104 inline bool FindAndCompareScheme(const string16& str, |
106 const char* compare, | 105 const char* compare, |
107 url_parse::Component* found_scheme) { | 106 url_parse::Component* found_scheme) { |
108 return FindAndCompareScheme(str.data(), static_cast<int>(str.size()), | 107 return FindAndCompareScheme(str.data(), static_cast<int>(str.size()), |
109 compare, found_scheme); | 108 compare, found_scheme); |
110 } | 109 } |
111 | 110 |
112 // Returns true if the given string represents a standard URL. This means that | 111 // Returns true if the given string represents a standard URL. This means that |
113 // either the scheme is in the list of known standard schemes. | 112 // either the scheme is in the list of known standard schemes. |
114 GURL_API bool IsStandard(const char* spec, | 113 bool IsStandard(const char* spec, |
115 const url_parse::Component& scheme); | 114 const url_parse::Component& scheme); |
116 GURL_API bool IsStandard(const char16* spec, | 115 bool IsStandard(const char16* spec, |
117 const url_parse::Component& scheme); | 116 const url_parse::Component& scheme); |
118 | 117 |
119 // TODO(brettw) remove this. This is a temporary compatibility hack to avoid | 118 // TODO(brettw) remove this. This is a temporary compatibility hack to avoid |
120 // breaking the WebKit build when this version is synced via Chrome. | 119 // breaking the WebKit build when this version is synced via Chrome. |
121 inline bool IsStandard(const char* spec, int spec_len, | 120 inline bool IsStandard(const char* spec, int spec_len, |
122 const url_parse::Component& scheme) { | 121 const url_parse::Component& scheme) { |
123 return IsStandard(spec, scheme); | 122 return IsStandard(spec, scheme); |
124 } | 123 } |
125 | 124 |
126 // URL library wrappers ------------------------------------------------------- | 125 // URL library wrappers ------------------------------------------------------- |
127 | 126 |
128 // Parses the given spec according to the extracted scheme type. Normal users | 127 // Parses the given spec according to the extracted scheme type. Normal users |
129 // should use the URL object, although this may be useful if performance is | 128 // should use the URL object, although this may be useful if performance is |
130 // critical and you don't want to do the heap allocation for the std::string. | 129 // critical and you don't want to do the heap allocation for the std::string. |
131 // | 130 // |
132 // As with the url_canon::Canonicalize* functions, the charset converter can | 131 // As with the url_canon::Canonicalize* functions, the charset converter can |
133 // be NULL to use UTF-8 (it will be faster in this case). | 132 // be NULL to use UTF-8 (it will be faster in this case). |
134 // | 133 // |
135 // Returns true if a valid URL was produced, false if not. On failure, the | 134 // Returns true if a valid URL was produced, false if not. On failure, the |
136 // output and parsed structures will still be filled and will be consistent, | 135 // output and parsed structures will still be filled and will be consistent, |
137 // but they will not represent a loadable URL. | 136 // but they will not represent a loadable URL. |
138 GURL_API bool Canonicalize(const char* spec, | 137 bool Canonicalize(const char* spec, |
139 int spec_len, | 138 int spec_len, |
140 url_canon::CharsetConverter* charset_converter, | 139 url_canon::CharsetConverter* charset_converter, |
141 url_canon::CanonOutput* output, | 140 url_canon::CanonOutput* output, |
142 url_parse::Parsed* output_parsed); | 141 url_parse::Parsed* output_parsed); |
143 GURL_API bool Canonicalize(const char16* spec, | 142 bool Canonicalize(const char16* spec, |
144 int spec_len, | 143 int spec_len, |
145 url_canon::CharsetConverter* charset_converter, | 144 url_canon::CharsetConverter* charset_converter, |
146 url_canon::CanonOutput* output, | 145 url_canon::CanonOutput* output, |
147 url_parse::Parsed* output_parsed); | 146 url_parse::Parsed* output_parsed); |
148 | 147 |
149 // Resolves a potentially relative URL relative to the given parsed base URL. | 148 // Resolves a potentially relative URL relative to the given parsed base URL. |
150 // The base MUST be valid. The resulting canonical URL and parsed information | 149 // The base MUST be valid. The resulting canonical URL and parsed information |
151 // will be placed in to the given out variables. | 150 // will be placed in to the given out variables. |
152 // | 151 // |
153 // The relative need not be relative. If we discover that it's absolute, this | 152 // The relative need not be relative. If we discover that it's absolute, this |
154 // will produce a canonical version of that URL. See Canonicalize() for more | 153 // will produce a canonical version of that URL. See Canonicalize() for more |
155 // about the charset_converter. | 154 // about the charset_converter. |
156 // | 155 // |
157 // Returns true if the output is valid, false if the input could not produce | 156 // Returns true if the output is valid, false if the input could not produce |
158 // a valid URL. | 157 // a valid URL. |
159 GURL_API bool ResolveRelative(const char* base_spec, | 158 bool ResolveRelative(const char* base_spec, |
160 int base_spec_len, | 159 int base_spec_len, |
161 const url_parse::Parsed& base_parsed, | 160 const url_parse::Parsed& base_parsed, |
162 const char* relative, | 161 const char* relative, |
163 int relative_length, | 162 int relative_length, |
164 url_canon::CharsetConverter* charset_converter, | 163 url_canon::CharsetConverter* charset_converter, |
165 url_canon::CanonOutput* output, | 164 url_canon::CanonOutput* output, |
166 url_parse::Parsed* output_parsed); | 165 url_parse::Parsed* output_parsed); |
167 GURL_API bool ResolveRelative(const char* base_spec, | 166 bool ResolveRelative(const char* base_spec, |
168 int base_spec_len, | 167 int base_spec_len, |
169 const url_parse::Parsed& base_parsed, | 168 const url_parse::Parsed& base_parsed, |
170 const char16* relative, | 169 const char16* relative, |
171 int relative_length, | 170 int relative_length, |
172 url_canon::CharsetConverter* charset_converter, | 171 url_canon::CharsetConverter* charset_converter, |
173 url_canon::CanonOutput* output, | 172 url_canon::CanonOutput* output, |
174 url_parse::Parsed* output_parsed); | 173 url_parse::Parsed* output_parsed); |
175 | 174 |
176 // Replaces components in the given VALID input url. The new canonical URL info | 175 // Replaces components in the given VALID input url. The new canonical URL info |
177 // is written to output and out_parsed. | 176 // is written to output and out_parsed. |
178 // | 177 // |
179 // Returns true if the resulting URL is valid. | 178 // Returns true if the resulting URL is valid. |
180 GURL_API bool ReplaceComponents( | 179 bool ReplaceComponents( |
181 const char* spec, | 180 const char* spec, |
182 int spec_len, | 181 int spec_len, |
183 const url_parse::Parsed& parsed, | 182 const url_parse::Parsed& parsed, |
184 const url_canon::Replacements<char>& replacements, | 183 const url_canon::Replacements<char>& replacements, |
185 url_canon::CharsetConverter* charset_converter, | 184 url_canon::CharsetConverter* charset_converter, |
186 url_canon::CanonOutput* output, | 185 url_canon::CanonOutput* output, |
187 url_parse::Parsed* out_parsed); | 186 url_parse::Parsed* out_parsed); |
188 GURL_API bool ReplaceComponents( | 187 bool ReplaceComponents( |
189 const char* spec, | 188 const char* spec, |
190 int spec_len, | 189 int spec_len, |
191 const url_parse::Parsed& parsed, | 190 const url_parse::Parsed& parsed, |
192 const url_canon::Replacements<char16>& replacements, | 191 const url_canon::Replacements<char16>& replacements, |
193 url_canon::CharsetConverter* charset_converter, | 192 url_canon::CharsetConverter* charset_converter, |
194 url_canon::CanonOutput* output, | 193 url_canon::CanonOutput* output, |
195 url_parse::Parsed* out_parsed); | 194 url_parse::Parsed* out_parsed); |
196 | 195 |
197 // String helper functions ---------------------------------------------------- | 196 // String helper functions ---------------------------------------------------- |
198 | 197 |
199 // Compare the lower-case form of the given string against the given ASCII | 198 // Compare the lower-case form of the given string against the given ASCII |
200 // string. This is useful for doing checking if an input string matches some | 199 // string. This is useful for doing checking if an input string matches some |
201 // token, and it is optimized to avoid intermediate string copies. | 200 // token, and it is optimized to avoid intermediate string copies. |
202 // | 201 // |
203 // The versions of this function that don't take a b_end assume that the b | 202 // The versions of this function that don't take a b_end assume that the b |
204 // string is NULL terminated. | 203 // string is NULL terminated. |
205 GURL_API bool LowerCaseEqualsASCII(const char* a_begin, | 204 bool LowerCaseEqualsASCII(const char* a_begin, |
206 const char* a_end, | 205 const char* a_end, |
207 const char* b); | 206 const char* b); |
208 GURL_API bool LowerCaseEqualsASCII(const char* a_begin, | 207 bool LowerCaseEqualsASCII(const char* a_begin, |
209 const char* a_end, | 208 const char* a_end, |
210 const char* b_begin, | 209 const char* b_begin, |
211 const char* b_end); | 210 const char* b_end); |
212 GURL_API bool LowerCaseEqualsASCII(const char16* a_begin, | 211 bool LowerCaseEqualsASCII(const char16* a_begin, |
213 const char16* a_end, | 212 const char16* a_end, |
214 const char* b); | 213 const char* b); |
215 | 214 |
216 // Unescapes the given string using URL escaping rules. | 215 // Unescapes the given string using URL escaping rules. |
217 GURL_API void DecodeURLEscapeSequences(const char* input, int length, | 216 void DecodeURLEscapeSequences(const char* input, int length, |
218 url_canon::CanonOutputW* output); | 217 url_canon::CanonOutputW* output); |
219 | 218 |
220 // Escapes the given string as defined by the JS method encodeURIComponent. See | 219 // Escapes the given string as defined by the JS method encodeURIComponent. See |
221 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeUR
IComponent | 220 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeUR
IComponent |
222 GURL_API void EncodeURIComponent(const char* input, int length, | 221 void EncodeURIComponent(const char* input, int length, |
223 url_canon::CanonOutput* output); | 222 url_canon::CanonOutput* output); |
224 | 223 |
225 | 224 |
226 } // namespace url_util | 225 } // namespace url_util |
227 | 226 |
228 #endif // GOOGLEURL_SRC_URL_UTIL_H__ | 227 #endif // URL_URL_UTIL_H_ |
OLD | NEW |