blob: 04a6ef4315d8388dc30def3a72722baf18a364f1 [file] [log] [blame]
[email protected]51bcc5d2013-04-24 01:41:371// 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.
[email protected]e7bba5f82013-04-10 20:10:524
[email protected]318076b2013-04-18 21:19:455#ifndef URL_URL_UTIL_H_
6#define URL_URL_UTIL_H_
[email protected]e7bba5f82013-04-10 20:10:527
8#include <string>
9
[email protected]516f0182013-06-11 22:51:5610#include "base/strings/string16.h"
tfarina018de6e2015-05-26 17:41:2011#include "url/third_party/mozilla/url_parse.h"
[email protected]318076b2013-04-18 21:19:4512#include "url/url_canon.h"
[email protected]cca6f392014-05-28 21:32:2613#include "url/url_constants.h"
[email protected]760ea502013-05-31 03:39:5114#include "url/url_export.h"
[email protected]e7bba5f82013-04-10 20:10:5215
[email protected]0318f922014-04-22 00:09:2316namespace url {
[email protected]e7bba5f82013-04-10 20:10:5217
18// Init ------------------------------------------------------------------------
19
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
palmer29ae5482015-05-19 08:43:3723// "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.
[email protected]e7bba5f82013-04-10 20:10:5226//
palmer29ae5482015-05-19 08:43:3727// 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.
[email protected]760ea502013-05-31 03:39:5130URL_EXPORT void Initialize();
[email protected]e7bba5f82013-04-10 20:10:5231
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
35// library.
[email protected]760ea502013-05-31 03:39:5136URL_EXPORT void Shutdown();
[email protected]e7bba5f82013-04-10 20:10:5237
38// Schemes --------------------------------------------------------------------
39
tyoshino11a7c9fe2015-08-19 08:51:4640// Types of a scheme representing the requirements on the data represented by
41// the authority component of a URL with the scheme.
Nico Weber204f0a72015-08-19 15:56:2342enum SchemeType {
tyoshino11a7c9fe2015-08-19 08:51:4643 // The authority component of a URL with the scheme, if any, has the port
44 // (the default values may be omitted in a serialization).
45 SCHEME_WITH_PORT,
46 // The authority component of a URL with the scheme, if any, doesn't have a
47 // port.
48 SCHEME_WITHOUT_PORT,
49 // A URL with the scheme doesn't have the authority component.
50 SCHEME_WITHOUT_AUTHORITY,
51};
52
53// A pair for representing a standard scheme name and the SchemeType for it.
54struct URL_EXPORT SchemeWithType {
55 const char* scheme;
56 SchemeType type;
57};
58
palmer29ae5482015-05-19 08:43:3759// 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).
62//
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).
tyoshino11a7c9fe2015-08-19 08:51:4666URL_EXPORT void AddStandardScheme(const char* new_scheme,
67 SchemeType scheme_type);
[email protected]e7bba5f82013-04-10 20:10:5268
69// Sets a flag to prevent future calls to AddStandardScheme from succeeding.
70//
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
76// dangerous.
77//
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.
[email protected]760ea502013-05-31 03:39:5181URL_EXPORT void LockStandardSchemes();
[email protected]e7bba5f82013-04-10 20:10:5282
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.
85//
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.
[email protected]760ea502013-05-31 03:39:5189URL_EXPORT bool FindAndCompareScheme(const char* str,
90 int str_len,
91 const char* compare,
[email protected]0318f922014-04-22 00:09:2392 Component* found_scheme);
[email protected]3774f832013-06-11 21:21:5793URL_EXPORT bool FindAndCompareScheme(const base::char16* str,
[email protected]760ea502013-05-31 03:39:5194 int str_len,
95 const char* compare,
[email protected]0318f922014-04-22 00:09:2396 Component* found_scheme);
[email protected]e7bba5f82013-04-10 20:10:5297inline bool FindAndCompareScheme(const std::string& str,
98 const char* compare,
[email protected]0318f922014-04-22 00:09:2399 Component* found_scheme) {
[email protected]e7bba5f82013-04-10 20:10:52100 return FindAndCompareScheme(str.data(), static_cast<int>(str.size()),
101 compare, found_scheme);
102}
[email protected]3774f832013-06-11 21:21:57103inline bool FindAndCompareScheme(const base::string16& str,
[email protected]e7bba5f82013-04-10 20:10:52104 const char* compare,
[email protected]0318f922014-04-22 00:09:23105 Component* found_scheme) {
[email protected]e7bba5f82013-04-10 20:10:52106 return FindAndCompareScheme(str.data(), static_cast<int>(str.size()),
107 compare, found_scheme);
108}
109
tyoshino11a7c9fe2015-08-19 08:51:46110// Returns true if the given scheme identified by |scheme| within |spec| is in
111// the list of known standard-format schemes (see AddStandardScheme).
[email protected]0318f922014-04-22 00:09:23112URL_EXPORT bool IsStandard(const char* spec, const Component& scheme);
113URL_EXPORT bool IsStandard(const base::char16* spec, const Component& scheme);
[email protected]e7bba5f82013-04-10 20:10:52114
tyoshino11a7c9fe2015-08-19 08:51:46115// 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).
118URL_EXPORT bool GetStandardSchemeType(const char* spec,
119 const Component& scheme,
120 SchemeType* type);
121
[email protected]e7bba5f82013-04-10 20:10:52122// URL library wrappers -------------------------------------------------------
123
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.
127//
[email protected]0318f922014-04-22 00:09:23128// As with the Canonicalize* functions, the charset converter can
[email protected]e7bba5f82013-04-10 20:10:52129// be NULL to use UTF-8 (it will be faster in this case).
130//
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.
[email protected]760ea502013-05-31 03:39:51134URL_EXPORT bool Canonicalize(const char* spec,
135 int spec_len,
[email protected]369e84f72013-11-23 01:53:52136 bool trim_path_end,
[email protected]0318f922014-04-22 00:09:23137 CharsetConverter* charset_converter,
138 CanonOutput* output,
139 Parsed* output_parsed);
[email protected]3774f832013-06-11 21:21:57140URL_EXPORT bool Canonicalize(const base::char16* spec,
[email protected]760ea502013-05-31 03:39:51141 int spec_len,
[email protected]369e84f72013-11-23 01:53:52142 bool trim_path_end,
[email protected]0318f922014-04-22 00:09:23143 CharsetConverter* charset_converter,
144 CanonOutput* output,
145 Parsed* output_parsed);
[email protected]e7bba5f82013-04-10 20:10:52146
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.
150//
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.
154//
155// Returns true if the output is valid, false if the input could not produce
156// a valid URL.
[email protected]760ea502013-05-31 03:39:51157URL_EXPORT bool ResolveRelative(const char* base_spec,
158 int base_spec_len,
[email protected]0318f922014-04-22 00:09:23159 const Parsed& base_parsed,
[email protected]760ea502013-05-31 03:39:51160 const char* relative,
161 int relative_length,
[email protected]0318f922014-04-22 00:09:23162 CharsetConverter* charset_converter,
163 CanonOutput* output,
164 Parsed* output_parsed);
[email protected]760ea502013-05-31 03:39:51165URL_EXPORT bool ResolveRelative(const char* base_spec,
166 int base_spec_len,
[email protected]0318f922014-04-22 00:09:23167 const Parsed& base_parsed,
[email protected]3774f832013-06-11 21:21:57168 const base::char16* relative,
[email protected]760ea502013-05-31 03:39:51169 int relative_length,
[email protected]0318f922014-04-22 00:09:23170 CharsetConverter* charset_converter,
171 CanonOutput* output,
172 Parsed* output_parsed);
[email protected]e7bba5f82013-04-10 20:10:52173
qyearsley2bc727d2015-08-14 20:17:15174// Replaces components in the given VALID input URL. The new canonical URL info
[email protected]e7bba5f82013-04-10 20:10:52175// is written to output and out_parsed.
176//
177// Returns true if the resulting URL is valid.
[email protected]0318f922014-04-22 00:09:23178URL_EXPORT bool ReplaceComponents(const char* spec,
179 int spec_len,
180 const Parsed& parsed,
181 const Replacements<char>& replacements,
182 CharsetConverter* charset_converter,
183 CanonOutput* output,
184 Parsed* out_parsed);
[email protected]760ea502013-05-31 03:39:51185URL_EXPORT bool ReplaceComponents(
[email protected]e7bba5f82013-04-10 20:10:52186 const char* spec,
187 int spec_len,
[email protected]0318f922014-04-22 00:09:23188 const Parsed& parsed,
189 const Replacements<base::char16>& replacements,
190 CharsetConverter* charset_converter,
191 CanonOutput* output,
192 Parsed* out_parsed);
[email protected]e7bba5f82013-04-10 20:10:52193
194// String helper functions ----------------------------------------------------
195
[email protected]e7bba5f82013-04-10 20:10:52196// Unescapes the given string using URL escaping rules.
[email protected]0318f922014-04-22 00:09:23197URL_EXPORT void DecodeURLEscapeSequences(const char* input,
198 int length,
199 CanonOutputW* output);
[email protected]e7bba5f82013-04-10 20:10:52200
qyearsley2bc727d2015-08-14 20:17:15201// Escapes the given string as defined by the JS method encodeURIComponent. See
[email protected]e7bba5f82013-04-10 20:10:52202// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent
[email protected]0318f922014-04-22 00:09:23203URL_EXPORT void EncodeURIComponent(const char* input,
204 int length,
205 CanonOutput* output);
[email protected]e7bba5f82013-04-10 20:10:52206
[email protected]0318f922014-04-22 00:09:23207} // namespace url
[email protected]e7bba5f82013-04-10 20:10:52208
[email protected]318076b2013-04-18 21:19:45209#endif // URL_URL_UTIL_H_