This source file includes following definitions.
- ToLowerASCII
- DoLowerCaseEqualsASCII
- InitStandardSchemes
- DoCompareSchemeComponent
- DoIsStandard
- DoFindAndCompareScheme
- DoCanonicalize
- DoResolveRelative
- DoReplaceComponents
- Initialize
- Shutdown
- AddStandardScheme
- LockStandardSchemes
- IsStandard
- IsStandard
- FindAndCompareScheme
- FindAndCompareScheme
- Canonicalize
- Canonicalize
- ResolveRelative
- ResolveRelative
- ReplaceComponents
- ReplaceComponents
- LowerCaseEqualsASCII
- LowerCaseEqualsASCII
- LowerCaseEqualsASCII
- DecodeURLEscapeSequences
- EncodeURIComponent
- CompareSchemeComponent
- CompareSchemeComponent
#include "url/url_util.h"
#include <string.h>
#include <vector>
#include "base/logging.h"
#include "url/url_canon_internal.h"
#include "url/url_file.h"
#include "url/url_util_internal.h"
namespace url_util {
const char kFileScheme[] = "file";
const char kFileSystemScheme[] = "filesystem";
const char kMailtoScheme[] = "mailto";
namespace {
template <class Char> inline Char ToLowerASCII(Char c) {
return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c;
}
template<typename Iter>
inline bool DoLowerCaseEqualsASCII(Iter a_begin, Iter a_end, const char* b) {
for (Iter it = a_begin; it != a_end; ++it, ++b) {
if (!*b || ToLowerASCII(*it) != *b)
return false;
}
return *b == 0;
}
const int kNumStandardURLSchemes = 8;
const char* kStandardURLSchemes[kNumStandardURLSchemes] = {
"http",
"https",
kFileScheme,
"ftp",
"gopher",
"ws",
"wss",
kFileSystemScheme,
};
std::vector<const char*>* standard_schemes = NULL;
bool standard_schemes_locked = false;
void InitStandardSchemes() {
if (standard_schemes)
return;
standard_schemes = new std::vector<const char*>;
for (int i = 0; i < kNumStandardURLSchemes; i++)
standard_schemes->push_back(kStandardURLSchemes[i]);
}
template<typename CHAR>
inline bool DoCompareSchemeComponent(const CHAR* spec,
const url_parse::Component& component,
const char* compare_to) {
if (!component.is_nonempty())
return compare_to[0] == 0;
return LowerCaseEqualsASCII(&spec[component.begin],
&spec[component.end()],
compare_to);
}
template<typename CHAR>
bool DoIsStandard(const CHAR* spec, const url_parse::Component& scheme) {
if (!scheme.is_nonempty())
return false;
InitStandardSchemes();
for (size_t i = 0; i < standard_schemes->size(); i++) {
if (LowerCaseEqualsASCII(&spec[scheme.begin], &spec[scheme.end()],
standard_schemes->at(i)))
return true;
}
return false;
}
template<typename CHAR>
bool DoFindAndCompareScheme(const CHAR* str,
int str_len,
const char* compare,
url_parse::Component* found_scheme) {
url_canon::RawCanonOutputT<CHAR> whitespace_buffer;
int spec_len;
const CHAR* spec = RemoveURLWhitespace(str, str_len,
&whitespace_buffer, &spec_len);
url_parse::Component our_scheme;
if (!url_parse::ExtractScheme(spec, spec_len, &our_scheme)) {
if (found_scheme)
*found_scheme = url_parse::Component();
return false;
}
if (found_scheme)
*found_scheme = our_scheme;
return DoCompareSchemeComponent(spec, our_scheme, compare);
}
template<typename CHAR>
bool DoCanonicalize(const CHAR* in_spec, int in_spec_len,
bool trim_path_end,
url_canon::CharsetConverter* charset_converter,
url_canon::CanonOutput* output,
url_parse::Parsed* output_parsed) {
url_canon::RawCanonOutputT<CHAR> whitespace_buffer;
int spec_len;
const CHAR* spec = RemoveURLWhitespace(in_spec, in_spec_len,
&whitespace_buffer, &spec_len);
url_parse::Parsed parsed_input;
#ifdef WIN32
if (url_parse::DoesBeginUNCPath(spec, 0, spec_len, false) ||
url_parse::DoesBeginWindowsDriveSpec(spec, 0, spec_len)) {
url_parse::ParseFileURL(spec, spec_len, &parsed_input);
return url_canon::CanonicalizeFileURL(spec, spec_len, parsed_input,
charset_converter,
output, output_parsed);
}
#endif
url_parse::Component scheme;
if (!url_parse::ExtractScheme(spec, spec_len, &scheme))
return false;
bool success;
if (DoCompareSchemeComponent(spec, scheme, kFileScheme)) {
url_parse::ParseFileURL(spec, spec_len, &parsed_input);
success = url_canon::CanonicalizeFileURL(spec, spec_len, parsed_input,
charset_converter, output,
output_parsed);
} else if (DoCompareSchemeComponent(spec, scheme, kFileSystemScheme)) {
url_parse::ParseFileSystemURL(spec, spec_len, &parsed_input);
success = url_canon::CanonicalizeFileSystemURL(spec, spec_len,
parsed_input,
charset_converter,
output, output_parsed);
} else if (DoIsStandard(spec, scheme)) {
url_parse::ParseStandardURL(spec, spec_len, &parsed_input);
success = url_canon::CanonicalizeStandardURL(spec, spec_len, parsed_input,
charset_converter,
output, output_parsed);
} else if (DoCompareSchemeComponent(spec, scheme, kMailtoScheme)) {
url_parse::ParseMailtoURL(spec, spec_len, &parsed_input);
success = url_canon::CanonicalizeMailtoURL(spec, spec_len, parsed_input,
output, output_parsed);
} else {
url_parse::ParsePathURL(spec, spec_len, trim_path_end, &parsed_input);
success = url_canon::CanonicalizePathURL(spec, spec_len, parsed_input,
output, output_parsed);
}
return success;
}
template<typename CHAR>
bool DoResolveRelative(const char* base_spec,
int base_spec_len,
const url_parse::Parsed& base_parsed,
const CHAR* in_relative,
int in_relative_length,
url_canon::CharsetConverter* charset_converter,
url_canon::CanonOutput* output,
url_parse::Parsed* output_parsed) {
url_canon::RawCanonOutputT<CHAR> whitespace_buffer;
int relative_length;
const CHAR* relative = RemoveURLWhitespace(in_relative, in_relative_length,
&whitespace_buffer,
&relative_length);
bool base_is_authority_based = false;
bool base_is_hierarchical = false;
if (base_spec &&
base_parsed.scheme.is_nonempty()) {
int after_scheme = base_parsed.scheme.end() + 1;
int num_slashes = url_parse::CountConsecutiveSlashes(
base_spec, after_scheme, base_spec_len);
base_is_authority_based = num_slashes > 1;
base_is_hierarchical = num_slashes > 0;
}
bool standard_base_scheme =
base_parsed.scheme.is_nonempty() &&
DoIsStandard(base_spec, base_parsed.scheme);
bool is_relative;
url_parse::Component relative_component;
if (!url_canon::IsRelativeURL(base_spec, base_parsed,
relative, relative_length,
(base_is_hierarchical || standard_base_scheme),
&is_relative,
&relative_component)) {
return false;
}
if (is_relative && base_is_authority_based && !standard_base_scheme) {
url_parse::Parsed base_parsed_authority;
ParseStandardURL(base_spec, base_spec_len, &base_parsed_authority);
if (base_parsed_authority.host.is_nonempty()) {
bool did_resolve_succeed =
url_canon::ResolveRelativeURL(base_spec, base_parsed_authority,
false, relative,
relative_component, charset_converter,
output, output_parsed);
ParsePathURL(output->data(), output->length(), true,
output_parsed);
return did_resolve_succeed;
}
} else if (is_relative) {
bool file_base_scheme = base_parsed.scheme.is_nonempty() &&
DoCompareSchemeComponent(base_spec, base_parsed.scheme, kFileScheme);
return url_canon::ResolveRelativeURL(base_spec, base_parsed,
file_base_scheme, relative,
relative_component, charset_converter,
output, output_parsed);
}
return DoCanonicalize(relative, relative_length, true, charset_converter,
output, output_parsed);
}
template<typename CHAR>
bool DoReplaceComponents(const char* spec,
int spec_len,
const url_parse::Parsed& parsed,
const url_canon::Replacements<CHAR>& replacements,
url_canon::CharsetConverter* charset_converter,
url_canon::CanonOutput* output,
url_parse::Parsed* out_parsed) {
if (replacements.IsSchemeOverridden()) {
url_canon::RawCanonOutput<128> scheme_replaced;
url_parse::Component scheme_replaced_parsed;
url_canon::CanonicalizeScheme(
replacements.sources().scheme,
replacements.components().scheme,
&scheme_replaced, &scheme_replaced_parsed);
int spec_after_colon = parsed.scheme.is_valid() ? parsed.scheme.end() + 1
: 1;
if (spec_len - spec_after_colon > 0) {
scheme_replaced.Append(&spec[spec_after_colon],
spec_len - spec_after_colon);
}
url_canon::RawCanonOutput<128> recanonicalized;
url_parse::Parsed recanonicalized_parsed;
DoCanonicalize(scheme_replaced.data(), scheme_replaced.length(), true,
charset_converter,
&recanonicalized, &recanonicalized_parsed);
url_canon::Replacements<CHAR> replacements_no_scheme = replacements;
replacements_no_scheme.SetScheme(NULL, url_parse::Component());
return DoReplaceComponents(recanonicalized.data(), recanonicalized.length(),
recanonicalized_parsed, replacements_no_scheme,
charset_converter, output, out_parsed);
}
if (DoCompareSchemeComponent(spec, parsed.scheme, kFileScheme)) {
return url_canon::ReplaceFileURL(spec, parsed, replacements,
charset_converter, output, out_parsed);
}
if (DoCompareSchemeComponent(spec, parsed.scheme, kFileSystemScheme)) {
return url_canon::ReplaceFileSystemURL(spec, parsed, replacements,
charset_converter, output,
out_parsed);
}
if (DoIsStandard(spec, parsed.scheme)) {
return url_canon::ReplaceStandardURL(spec, parsed, replacements,
charset_converter, output, out_parsed);
}
if (DoCompareSchemeComponent(spec, parsed.scheme, kMailtoScheme)) {
return url_canon::ReplaceMailtoURL(spec, parsed, replacements,
output, out_parsed);
}
return url_canon::ReplacePathURL(spec, parsed, replacements,
output, out_parsed);
}
}
void Initialize() {
InitStandardSchemes();
}
void Shutdown() {
if (standard_schemes) {
delete standard_schemes;
standard_schemes = NULL;
}
}
void AddStandardScheme(const char* new_scheme) {
DCHECK(!standard_schemes_locked) <<
"Trying to add a standard scheme after the list has been locked.";
size_t scheme_len = strlen(new_scheme);
if (scheme_len == 0)
return;
char* dup_scheme = new char[scheme_len + 1];
memcpy(dup_scheme, new_scheme, scheme_len + 1);
InitStandardSchemes();
standard_schemes->push_back(dup_scheme);
}
void LockStandardSchemes() {
standard_schemes_locked = true;
}
bool IsStandard(const char* spec, const url_parse::Component& scheme) {
return DoIsStandard(spec, scheme);
}
bool IsStandard(const base::char16* spec, const url_parse::Component& scheme) {
return DoIsStandard(spec, scheme);
}
bool FindAndCompareScheme(const char* str,
int str_len,
const char* compare,
url_parse::Component* found_scheme) {
return DoFindAndCompareScheme(str, str_len, compare, found_scheme);
}
bool FindAndCompareScheme(const base::char16* str,
int str_len,
const char* compare,
url_parse::Component* found_scheme) {
return DoFindAndCompareScheme(str, str_len, compare, found_scheme);
}
bool Canonicalize(const char* spec,
int spec_len,
bool trim_path_end,
url_canon::CharsetConverter* charset_converter,
url_canon::CanonOutput* output,
url_parse::Parsed* output_parsed) {
return DoCanonicalize(spec, spec_len, trim_path_end, charset_converter,
output, output_parsed);
}
bool Canonicalize(const base::char16* spec,
int spec_len,
bool trim_path_end,
url_canon::CharsetConverter* charset_converter,
url_canon::CanonOutput* output,
url_parse::Parsed* output_parsed) {
return DoCanonicalize(spec, spec_len, trim_path_end, charset_converter,
output, output_parsed);
}
bool ResolveRelative(const char* base_spec,
int base_spec_len,
const url_parse::Parsed& base_parsed,
const char* relative,
int relative_length,
url_canon::CharsetConverter* charset_converter,
url_canon::CanonOutput* output,
url_parse::Parsed* output_parsed) {
return DoResolveRelative(base_spec, base_spec_len, base_parsed,
relative, relative_length,
charset_converter, output, output_parsed);
}
bool ResolveRelative(const char* base_spec,
int base_spec_len,
const url_parse::Parsed& base_parsed,
const base::char16* relative,
int relative_length,
url_canon::CharsetConverter* charset_converter,
url_canon::CanonOutput* output,
url_parse::Parsed* output_parsed) {
return DoResolveRelative(base_spec, base_spec_len, base_parsed,
relative, relative_length,
charset_converter, output, output_parsed);
}
bool ReplaceComponents(const char* spec,
int spec_len,
const url_parse::Parsed& parsed,
const url_canon::Replacements<char>& replacements,
url_canon::CharsetConverter* charset_converter,
url_canon::CanonOutput* output,
url_parse::Parsed* out_parsed) {
return DoReplaceComponents(spec, spec_len, parsed, replacements,
charset_converter, output, out_parsed);
}
bool ReplaceComponents(
const char* spec,
int spec_len,
const url_parse::Parsed& parsed,
const url_canon::Replacements<base::char16>& replacements,
url_canon::CharsetConverter* charset_converter,
url_canon::CanonOutput* output,
url_parse::Parsed* out_parsed) {
return DoReplaceComponents(spec, spec_len, parsed, replacements,
charset_converter, output, out_parsed);
}
bool LowerCaseEqualsASCII(const char* a_begin,
const char* a_end,
const char* b) {
return DoLowerCaseEqualsASCII(a_begin, a_end, b);
}
bool LowerCaseEqualsASCII(const char* a_begin,
const char* a_end,
const char* b_begin,
const char* b_end) {
while (a_begin != a_end && b_begin != b_end &&
ToLowerASCII(*a_begin) == *b_begin) {
a_begin++;
b_begin++;
}
return a_begin == a_end && b_begin == b_end;
}
bool LowerCaseEqualsASCII(const base::char16* a_begin,
const base::char16* a_end,
const char* b) {
return DoLowerCaseEqualsASCII(a_begin, a_end, b);
}
void DecodeURLEscapeSequences(const char* input, int length,
url_canon::CanonOutputW* output) {
url_canon::RawCanonOutputT<char> unescaped_chars;
for (int i = 0; i < length; i++) {
if (input[i] == '%') {
unsigned char ch;
if (url_canon::DecodeEscaped(input, &i, length, &ch)) {
unescaped_chars.push_back(ch);
} else {
unescaped_chars.push_back('%');
}
} else {
unescaped_chars.push_back(input[i]);
}
}
for (int i = 0; i < unescaped_chars.length(); i++) {
unsigned char uch = static_cast<unsigned char>(unescaped_chars.at(i));
if (uch < 0x80) {
output->push_back(uch);
} else {
int next_character = i;
unsigned code_point;
if (url_canon::ReadUTFChar(unescaped_chars.data(), &next_character,
unescaped_chars.length(), &code_point)) {
url_canon::AppendUTF16Value(code_point, output);
i = next_character;
} else {
while (i < next_character) {
output->push_back(static_cast<unsigned char>(unescaped_chars.at(i)));
i++;
}
output->push_back(static_cast<unsigned char>(unescaped_chars.at(i)));
}
}
}
}
void EncodeURIComponent(const char* input, int length,
url_canon::CanonOutput* output) {
for (int i = 0; i < length; ++i) {
unsigned char c = static_cast<unsigned char>(input[i]);
if (url_canon::IsComponentChar(c))
output->push_back(c);
else
AppendEscapedChar(c, output);
}
}
bool CompareSchemeComponent(const char* spec,
const url_parse::Component& component,
const char* compare_to) {
return DoCompareSchemeComponent(spec, component, compare_to);
}
bool CompareSchemeComponent(const base::char16* spec,
const url_parse::Component& component,
const char* compare_to) {
return DoCompareSchemeComponent(spec, component, compare_to);
}
}