1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
30 #ifndef GOOGLEURL_SRC_URL_UTIL_H__
31 #define GOOGLEURL_SRC_URL_UTIL_H__
35 #include "base/string16.h"
36 #include "googleurl/src/url_parse.h"
37 #include "googleurl/src/url_canon.h"
41 // Schemes --------------------------------------------------------------------
43 // Adds an application-defined scheme to the internal list of "standard" URL
45 void AddStandardScheme(const char* new_scheme
);
47 // Locates the scheme in the given string and places it into |found_scheme|,
48 // which may be NULL to indicate the caller does not care about the range.
49 // Returns whether the given |compare| scheme matches the scheme found in the
51 bool FindAndCompareScheme(const char* str
,
54 url_parse::Component
* found_scheme
);
55 bool FindAndCompareScheme(const char16
* str
,
58 url_parse::Component
* found_scheme
);
59 inline bool FindAndCompareScheme(const std::string
& str
,
61 url_parse::Component
* found_scheme
) {
62 return FindAndCompareScheme(str
.data(), static_cast<int>(str
.size()),
63 compare
, found_scheme
);
65 inline bool FindAndCompareScheme(const string16
& str
,
67 url_parse::Component
* found_scheme
) {
68 return FindAndCompareScheme(str
.data(), static_cast<int>(str
.size()),
69 compare
, found_scheme
);
72 // Returns true if the given string represents a standard URL. This means that
73 // either the scheme is in the list of known standard schemes.
74 bool IsStandard(const char* spec
, const url_parse::Component
& scheme
);
75 bool IsStandard(const char16
* spec
, const url_parse::Component
& scheme
);
77 // TODO(brettw) remove this. This is a temporary compatibility hack to avoid
78 // breaking the WebKit build when this version is synced via Chrome.
79 bool IsStandard(const char* spec
, int spec_len
,
80 const url_parse::Component
& scheme
);
82 // URL library wrappers -------------------------------------------------------
84 // Parses the given spec according to the extracted scheme type. Normal users
85 // should use the URL object, although this may be useful if performance is
86 // critical and you don't want to do the heap allocation for the std::string.
88 // As with the url_canon::Canonicalize* functions, the charset converter can
89 // be NULL to use UTF-8 (it will be faster in this case).
91 // Returns true if a valid URL was produced, false if not. On failure, the
92 // output and parsed structures will still be filled and will be consistent,
93 // but they will not represent a loadable URL.
94 bool Canonicalize(const char* spec
,
96 url_canon::CharsetConverter
* charset_converter
,
97 url_canon::CanonOutput
* output
,
98 url_parse::Parsed
* output_parsed
);
99 bool Canonicalize(const char16
* spec
,
101 url_canon::CharsetConverter
* charset_converter
,
102 url_canon::CanonOutput
* output
,
103 url_parse::Parsed
* output_parsed
);
105 // Resolves a potentially relative URL relative to the given parsed base URL.
106 // The base MUST be valid. The resulting canonical URL and parsed information
107 // will be placed in to the given out variables.
109 // The relative need not be relative. If we discover that it's absolute, this
110 // will produce a canonical version of that URL. See Canonicalize() for more
111 // about the charset_converter.
113 // Returns true if the output is valid, false if the input could not produce
115 bool ResolveRelative(const char* base_spec
,
117 const url_parse::Parsed
& base_parsed
,
118 const char* relative
,
120 url_canon::CharsetConverter
* charset_converter
,
121 url_canon::CanonOutput
* output
,
122 url_parse::Parsed
* output_parsed
);
123 bool ResolveRelative(const char* base_spec
,
125 const url_parse::Parsed
& base_parsed
,
126 const char16
* relative
,
128 url_canon::CharsetConverter
* charset_converter
,
129 url_canon::CanonOutput
* output
,
130 url_parse::Parsed
* output_parsed
);
132 // Replaces components in the given VALID input url. The new canonical URL info
133 // is written to output and out_parsed.
135 // Returns true if the resulting URL is valid.
136 bool ReplaceComponents(const char* spec
,
138 const url_parse::Parsed
& parsed
,
139 const url_canon::Replacements
<char>& replacements
,
140 url_canon::CharsetConverter
* charset_converter
,
141 url_canon::CanonOutput
* output
,
142 url_parse::Parsed
* out_parsed
);
143 bool ReplaceComponents(const char* spec
,
145 const url_parse::Parsed
& parsed
,
146 const url_canon::Replacements
<char16
>& replacements
,
147 url_canon::CharsetConverter
* charset_converter
,
148 url_canon::CanonOutput
* output
,
149 url_parse::Parsed
* out_parsed
);
151 // String helper functions ----------------------------------------------------
153 // Compare the lower-case form of the given string against the given ASCII
154 // string. This is useful for doing checking if an input string matches some
155 // token, and it is optimized to avoid intermediate string copies.
157 // The versions of this function that don't take a b_end assume that the b
158 // string is NULL terminated.
159 bool LowerCaseEqualsASCII(const char* a_begin
,
162 bool LowerCaseEqualsASCII(const char* a_begin
,
166 bool LowerCaseEqualsASCII(const char16
* a_begin
,
170 } // namespace url_util
172 #endif // GOOGLEURL_SRC_URL_UTIL_H__