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.
5 #ifndef URL_URL_UTIL_H_
6 #define URL_URL_UTIL_H_
10 #include "base/string16.h"
11 #include "url/url_canon.h"
12 #include "url/url_parse.h"
16 // Init ------------------------------------------------------------------------
18 // Initialization is NOT required, it will be implicitly initialized when first
19 // used. However, this implicit initialization is NOT threadsafe. If you are
20 // using this library in a threaded environment and don't have a consistent
21 // "first call" (an example might be calling "AddStandardScheme" with your
22 // special application-specific schemes) then you will want to call initialize
23 // before spawning any threads.
25 // It is OK to call this function more than once, subsequent calls will simply
26 // "noop", unless Shutdown() was called in the mean time. This will also be a
27 // "noop" if other calls to the library have forced an initialization
31 // Cleanup is not required, except some strings may leak. For most user
32 // applications, this is fine. If you're using it in a library that may get
33 // loaded and unloaded, you'll want to unload to properly clean up your
37 // Schemes --------------------------------------------------------------------
39 // Adds an application-defined scheme to the internal list of "standard" URL
40 // schemes. This function is not threadsafe and can not be called concurrently
41 // with any other url_util function. It will assert if the list of standard
42 // schemes has been locked (see LockStandardSchemes).
43 void AddStandardScheme(const char* new_scheme
);
45 // Sets a flag to prevent future calls to AddStandardScheme from succeeding.
47 // This is designed to help prevent errors for multithreaded applications.
48 // Normal usage would be to call AddStandardScheme for your custom schemes at
49 // the beginning of program initialization, and then LockStandardSchemes. This
50 // prevents future callers from mistakenly calling AddStandardScheme when the
51 // program is running with multiple threads, where such usage would be
54 // We could have had AddStandardScheme use a lock instead, but that would add
55 // some platform-specific dependencies we don't otherwise have now, and is
56 // overkill considering the normal usage is so simple.
57 void LockStandardSchemes();
59 // Locates the scheme in the given string and places it into |found_scheme|,
60 // which may be NULL to indicate the caller does not care about the range.
62 // Returns whether the given |compare| scheme matches the scheme found in the
63 // input (if any). The |compare| scheme must be a valid canonical scheme or
64 // the result of the comparison is undefined.
65 bool FindAndCompareScheme(const char* str
,
68 url_parse::Component
* found_scheme
);
69 bool FindAndCompareScheme(const char16
* str
,
72 url_parse::Component
* found_scheme
);
73 inline bool FindAndCompareScheme(const std::string
& str
,
75 url_parse::Component
* found_scheme
) {
76 return FindAndCompareScheme(str
.data(), static_cast<int>(str
.size()),
77 compare
, found_scheme
);
79 inline bool FindAndCompareScheme(const string16
& str
,
81 url_parse::Component
* found_scheme
) {
82 return FindAndCompareScheme(str
.data(), static_cast<int>(str
.size()),
83 compare
, found_scheme
);
86 // Returns true if the given string represents a standard URL. This means that
87 // either the scheme is in the list of known standard schemes.
88 bool IsStandard(const char* spec
,
89 const url_parse::Component
& scheme
);
90 bool IsStandard(const char16
* spec
,
91 const url_parse::Component
& scheme
);
93 // TODO(brettw) remove this. This is a temporary compatibility hack to avoid
94 // breaking the WebKit build when this version is synced via Chrome.
95 inline bool IsStandard(const char* spec
, int spec_len
,
96 const url_parse::Component
& scheme
) {
97 return IsStandard(spec
, scheme
);
100 // URL library wrappers -------------------------------------------------------
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.
106 // As with the url_canon::Canonicalize* functions, the charset converter can
107 // be NULL to use UTF-8 (it will be faster in this case).
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.
112 bool Canonicalize(const char* spec
,
114 url_canon::CharsetConverter
* charset_converter
,
115 url_canon::CanonOutput
* output
,
116 url_parse::Parsed
* output_parsed
);
117 bool Canonicalize(const char16
* spec
,
119 url_canon::CharsetConverter
* charset_converter
,
120 url_canon::CanonOutput
* output
,
121 url_parse::Parsed
* output_parsed
);
123 // Resolves a potentially relative URL relative to the given parsed base URL.
124 // The base MUST be valid. The resulting canonical URL and parsed information
125 // will be placed in to the given out variables.
127 // The relative need not be relative. If we discover that it's absolute, this
128 // will produce a canonical version of that URL. See Canonicalize() for more
129 // about the charset_converter.
131 // Returns true if the output is valid, false if the input could not produce
133 bool ResolveRelative(const char* base_spec
,
135 const url_parse::Parsed
& base_parsed
,
136 const char* relative
,
138 url_canon::CharsetConverter
* charset_converter
,
139 url_canon::CanonOutput
* output
,
140 url_parse::Parsed
* output_parsed
);
141 bool ResolveRelative(const char* base_spec
,
143 const url_parse::Parsed
& base_parsed
,
144 const char16
* relative
,
146 url_canon::CharsetConverter
* charset_converter
,
147 url_canon::CanonOutput
* output
,
148 url_parse::Parsed
* output_parsed
);
150 // Replaces components in the given VALID input url. The new canonical URL info
151 // is written to output and out_parsed.
153 // Returns true if the resulting URL is valid.
154 bool ReplaceComponents(
157 const url_parse::Parsed
& parsed
,
158 const url_canon::Replacements
<char>& replacements
,
159 url_canon::CharsetConverter
* charset_converter
,
160 url_canon::CanonOutput
* output
,
161 url_parse::Parsed
* out_parsed
);
162 bool ReplaceComponents(
165 const url_parse::Parsed
& parsed
,
166 const url_canon::Replacements
<char16
>& replacements
,
167 url_canon::CharsetConverter
* charset_converter
,
168 url_canon::CanonOutput
* output
,
169 url_parse::Parsed
* out_parsed
);
171 // String helper functions ----------------------------------------------------
173 // Compare the lower-case form of the given string against the given ASCII
174 // string. This is useful for doing checking if an input string matches some
175 // token, and it is optimized to avoid intermediate string copies.
177 // The versions of this function that don't take a b_end assume that the b
178 // string is NULL terminated.
179 bool LowerCaseEqualsASCII(const char* a_begin
,
182 bool LowerCaseEqualsASCII(const char* a_begin
,
186 bool LowerCaseEqualsASCII(const char16
* a_begin
,
190 // Unescapes the given string using URL escaping rules.
191 void DecodeURLEscapeSequences(const char* input
, int length
,
192 url_canon::CanonOutputW
* output
);
194 // Escapes the given string as defined by the JS method encodeURIComponent. See
195 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent
196 void EncodeURIComponent(const char* input
, int length
,
197 url_canon::CanonOutput
* output
);
200 } // namespace url_util
202 #endif // URL_URL_UTIL_H_