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/strings/string16.h"
11 #include "url/third_party/mozilla/url_parse.h"
12 #include "url/url_canon.h"
13 #include "url/url_constants.h"
14 #include "url/url_export.h"
18 // Init ------------------------------------------------------------------------
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
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.
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.
30 URL_EXPORT
void Initialize();
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
36 URL_EXPORT
void Shutdown();
38 // Schemes --------------------------------------------------------------------
40 // Adds an application-defined scheme to the internal list of "standard-format"
41 // URL schemes. A standard-format scheme adheres to what RFC 3986 calls "generic
42 // URI syntax" (https://tools.ietf.org/html/rfc3986#section-3).
44 // This function is not threadsafe and can not be called concurrently with any
45 // other url_util function. It will assert if the list of standard schemes has
46 // been locked (see LockStandardSchemes).
47 URL_EXPORT
void AddStandardScheme(const char* new_scheme
);
49 // Sets a flag to prevent future calls to AddStandardScheme from succeeding.
51 // This is designed to help prevent errors for multithreaded applications.
52 // Normal usage would be to call AddStandardScheme for your custom schemes at
53 // the beginning of program initialization, and then LockStandardSchemes. This
54 // prevents future callers from mistakenly calling AddStandardScheme when the
55 // program is running with multiple threads, where such usage would be
58 // We could have had AddStandardScheme use a lock instead, but that would add
59 // some platform-specific dependencies we don't otherwise have now, and is
60 // overkill considering the normal usage is so simple.
61 URL_EXPORT
void LockStandardSchemes();
63 // Locates the scheme in the given string and places it into |found_scheme|,
64 // which may be NULL to indicate the caller does not care about the range.
66 // Returns whether the given |compare| scheme matches the scheme found in the
67 // input (if any). The |compare| scheme must be a valid canonical scheme or
68 // the result of the comparison is undefined.
69 URL_EXPORT
bool FindAndCompareScheme(const char* str
,
72 Component
* found_scheme
);
73 URL_EXPORT
bool FindAndCompareScheme(const base::char16
* str
,
76 Component
* found_scheme
);
77 inline bool FindAndCompareScheme(const std::string
& str
,
79 Component
* found_scheme
) {
80 return FindAndCompareScheme(str
.data(), static_cast<int>(str
.size()),
81 compare
, found_scheme
);
83 inline bool FindAndCompareScheme(const base::string16
& str
,
85 Component
* found_scheme
) {
86 return FindAndCompareScheme(str
.data(), static_cast<int>(str
.size()),
87 compare
, found_scheme
);
90 // Returns true if the given string represents a URL whose scheme is in the list
91 // of known standard-format schemes (see AddStandardScheme).
92 URL_EXPORT
bool IsStandard(const char* spec
, const Component
& scheme
);
93 URL_EXPORT
bool IsStandard(const base::char16
* spec
, const Component
& scheme
);
95 // URL library wrappers -------------------------------------------------------
97 // Parses the given spec according to the extracted scheme type. Normal users
98 // should use the URL object, although this may be useful if performance is
99 // critical and you don't want to do the heap allocation for the std::string.
101 // As with the Canonicalize* functions, the charset converter can
102 // be NULL to use UTF-8 (it will be faster in this case).
104 // Returns true if a valid URL was produced, false if not. On failure, the
105 // output and parsed structures will still be filled and will be consistent,
106 // but they will not represent a loadable URL.
107 URL_EXPORT
bool Canonicalize(const char* spec
,
110 CharsetConverter
* charset_converter
,
112 Parsed
* output_parsed
);
113 URL_EXPORT
bool Canonicalize(const base::char16
* spec
,
116 CharsetConverter
* charset_converter
,
118 Parsed
* output_parsed
);
120 // Resolves a potentially relative URL relative to the given parsed base URL.
121 // The base MUST be valid. The resulting canonical URL and parsed information
122 // will be placed in to the given out variables.
124 // The relative need not be relative. If we discover that it's absolute, this
125 // will produce a canonical version of that URL. See Canonicalize() for more
126 // about the charset_converter.
128 // Returns true if the output is valid, false if the input could not produce
130 URL_EXPORT
bool ResolveRelative(const char* base_spec
,
132 const Parsed
& base_parsed
,
133 const char* relative
,
135 CharsetConverter
* charset_converter
,
137 Parsed
* output_parsed
);
138 URL_EXPORT
bool ResolveRelative(const char* base_spec
,
140 const Parsed
& base_parsed
,
141 const base::char16
* relative
,
143 CharsetConverter
* charset_converter
,
145 Parsed
* output_parsed
);
147 // Replaces components in the given VALID input url. The new canonical URL info
148 // is written to output and out_parsed.
150 // Returns true if the resulting URL is valid.
151 URL_EXPORT
bool ReplaceComponents(const char* spec
,
153 const Parsed
& parsed
,
154 const Replacements
<char>& replacements
,
155 CharsetConverter
* charset_converter
,
158 URL_EXPORT
bool ReplaceComponents(
161 const Parsed
& parsed
,
162 const Replacements
<base::char16
>& replacements
,
163 CharsetConverter
* charset_converter
,
167 // String helper functions ----------------------------------------------------
169 // Unescapes the given string using URL escaping rules.
170 URL_EXPORT
void DecodeURLEscapeSequences(const char* input
,
172 CanonOutputW
* output
);
174 // Escapes the given string as defined by the JS method encodeURIComponent. See
175 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent
176 URL_EXPORT
void EncodeURIComponent(const char* input
,
178 CanonOutput
* output
);
182 #endif // URL_URL_UTIL_H_