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 // Types of a scheme representing the requirements on the data represented by
41 // the authority component of a URL with the scheme.
43 // The authority component of a URL with the scheme, if any, has the port
44 // (the default values may be omitted in a serialization).
46 // The authority component of a URL with the scheme, if any, doesn't have a
49 // A URL with the scheme doesn't have the authority component.
50 SCHEME_WITHOUT_AUTHORITY
,
53 // A pair for representing a standard scheme name and the SchemeType for it.
54 struct URL_EXPORT SchemeWithType
{
59 // Adds an application-defined scheme to the internal list of "standard-format"
60 // URL schemes. A standard-format scheme adheres to what RFC 3986 calls "generic
61 // URI syntax" (https://tools.ietf.org/html/rfc3986#section-3).
63 // This function is not threadsafe and can not be called concurrently with any
64 // other url_util function. It will assert if the list of standard schemes has
65 // been locked (see LockStandardSchemes).
66 URL_EXPORT
void AddStandardScheme(const char* new_scheme
,
67 SchemeType scheme_type
);
69 // Sets a flag to prevent future calls to AddStandardScheme from succeeding.
71 // This is designed to help prevent errors for multithreaded applications.
72 // Normal usage would be to call AddStandardScheme for your custom schemes at
73 // the beginning of program initialization, and then LockStandardSchemes. This
74 // prevents future callers from mistakenly calling AddStandardScheme when the
75 // program is running with multiple threads, where such usage would be
78 // We could have had AddStandardScheme use a lock instead, but that would add
79 // some platform-specific dependencies we don't otherwise have now, and is
80 // overkill considering the normal usage is so simple.
81 URL_EXPORT
void LockStandardSchemes();
83 // Locates the scheme in the given string and places it into |found_scheme|,
84 // which may be NULL to indicate the caller does not care about the range.
86 // Returns whether the given |compare| scheme matches the scheme found in the
87 // input (if any). The |compare| scheme must be a valid canonical scheme or
88 // the result of the comparison is undefined.
89 URL_EXPORT
bool FindAndCompareScheme(const char* str
,
92 Component
* found_scheme
);
93 URL_EXPORT
bool FindAndCompareScheme(const base::char16
* str
,
96 Component
* found_scheme
);
97 inline bool FindAndCompareScheme(const std::string
& str
,
99 Component
* found_scheme
) {
100 return FindAndCompareScheme(str
.data(), static_cast<int>(str
.size()),
101 compare
, found_scheme
);
103 inline bool FindAndCompareScheme(const base::string16
& str
,
105 Component
* found_scheme
) {
106 return FindAndCompareScheme(str
.data(), static_cast<int>(str
.size()),
107 compare
, found_scheme
);
110 // Returns true if the given scheme identified by |scheme| within |spec| is in
111 // the list of known standard-format schemes (see AddStandardScheme).
112 URL_EXPORT
bool IsStandard(const char* spec
, const Component
& scheme
);
113 URL_EXPORT
bool IsStandard(const base::char16
* spec
, const Component
& scheme
);
115 // Returns true and sets |type| to the SchemeType of the given scheme
116 // identified by |scheme| within |spec| if the scheme is in the list of known
117 // standard-format schemes (see AddStandardScheme).
118 URL_EXPORT
bool GetStandardSchemeType(const char* spec
,
119 const Component
& scheme
,
122 // URL library wrappers -------------------------------------------------------
124 // Parses the given spec according to the extracted scheme type. Normal users
125 // should use the URL object, although this may be useful if performance is
126 // critical and you don't want to do the heap allocation for the std::string.
128 // As with the Canonicalize* functions, the charset converter can
129 // be NULL to use UTF-8 (it will be faster in this case).
131 // Returns true if a valid URL was produced, false if not. On failure, the
132 // output and parsed structures will still be filled and will be consistent,
133 // but they will not represent a loadable URL.
134 URL_EXPORT
bool Canonicalize(const char* spec
,
137 CharsetConverter
* charset_converter
,
139 Parsed
* output_parsed
);
140 URL_EXPORT
bool Canonicalize(const base::char16
* spec
,
143 CharsetConverter
* charset_converter
,
145 Parsed
* output_parsed
);
147 // Resolves a potentially relative URL relative to the given parsed base URL.
148 // The base MUST be valid. The resulting canonical URL and parsed information
149 // will be placed in to the given out variables.
151 // The relative need not be relative. If we discover that it's absolute, this
152 // will produce a canonical version of that URL. See Canonicalize() for more
153 // about the charset_converter.
155 // Returns true if the output is valid, false if the input could not produce
157 URL_EXPORT
bool ResolveRelative(const char* base_spec
,
159 const Parsed
& base_parsed
,
160 const char* relative
,
162 CharsetConverter
* charset_converter
,
164 Parsed
* output_parsed
);
165 URL_EXPORT
bool ResolveRelative(const char* base_spec
,
167 const Parsed
& base_parsed
,
168 const base::char16
* relative
,
170 CharsetConverter
* charset_converter
,
172 Parsed
* output_parsed
);
174 // Replaces components in the given VALID input URL. The new canonical URL info
175 // is written to output and out_parsed.
177 // Returns true if the resulting URL is valid.
178 URL_EXPORT
bool ReplaceComponents(const char* spec
,
180 const Parsed
& parsed
,
181 const Replacements
<char>& replacements
,
182 CharsetConverter
* charset_converter
,
185 URL_EXPORT
bool ReplaceComponents(
188 const Parsed
& parsed
,
189 const Replacements
<base::char16
>& replacements
,
190 CharsetConverter
* charset_converter
,
194 // String helper functions ----------------------------------------------------
196 // Unescapes the given string using URL escaping rules.
197 URL_EXPORT
void DecodeURLEscapeSequences(const char* input
,
199 CanonOutputW
* output
);
201 // Escapes the given string as defined by the JS method encodeURIComponent. See
202 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent
203 URL_EXPORT
void EncodeURIComponent(const char* input
,
205 CanonOutput
* output
);
209 #endif // URL_URL_UTIL_H_