This source file includes following definitions.
- IsPortDigit
- FindNextAuthorityTerminator
- ParseUserInfo
- ParseServerInfo
- DoParseAuthority
- ParsePath
- DoExtractScheme
- DoParseAfterScheme
- DoParseStandardURL
- DoParseFileSystemURL
- DoParsePathURL
- DoParseMailtoURL
- DoParsePort
- DoExtractFileName
- DoExtractQueryKeyValue
- inner_parsed_
- Length
- CountCharactersBefore
- GetContent
- ExtractScheme
- ExtractScheme
- IsAuthorityTerminator
- ExtractFileName
- ExtractFileName
- ExtractQueryKeyValue
- ExtractQueryKeyValue
- ParseAuthority
- ParseAuthority
- ParsePort
- ParsePort
- ParseStandardURL
- ParseStandardURL
- ParsePathURL
- ParsePathURL
- ParseFileSystemURL
- ParseFileSystemURL
- ParseMailtoURL
- ParseMailtoURL
- ParsePathInternal
- ParsePathInternal
- ParseAfterScheme
- ParseAfterScheme
#include "url/third_party/mozilla/url_parse.h"
#include <stdlib.h>
#include "base/logging.h"
#include "url/url_parse_internal.h"
#include "url/url_util.h"
#include "url/url_util_internal.h"
namespace url_parse {
namespace {
inline bool IsPortDigit(base::char16 ch) {
return ch >= '0' && ch <= '9';
}
template<typename CHAR>
int FindNextAuthorityTerminator(const CHAR* spec,
int start_offset,
int spec_len) {
for (int i = start_offset; i < spec_len; i++) {
if (IsAuthorityTerminator(spec[i]))
return i;
}
return spec_len;
}
template<typename CHAR>
void ParseUserInfo(const CHAR* spec,
const Component& user,
Component* username,
Component* password) {
int colon_offset = 0;
while (colon_offset < user.len && spec[user.begin + colon_offset] != ':')
colon_offset++;
if (colon_offset < user.len) {
*username = Component(user.begin, colon_offset);
*password = MakeRange(user.begin + colon_offset + 1,
user.begin + user.len);
} else {
*username = user;
*password = Component();
}
}
template<typename CHAR>
void ParseServerInfo(const CHAR* spec,
const Component& serverinfo,
Component* hostname,
Component* port_num) {
if (serverinfo.len == 0) {
hostname->reset();
port_num->reset();
return;
}
int ipv6_terminator = spec[serverinfo.begin] == '[' ? serverinfo.end() : -1;
int colon = -1;
for (int i = serverinfo.begin; i < serverinfo.end(); i++) {
switch (spec[i]) {
case ']':
ipv6_terminator = i;
break;
case ':':
colon = i;
break;
}
}
if (colon > ipv6_terminator) {
*hostname = MakeRange(serverinfo.begin, colon);
if (hostname->len == 0)
hostname->reset();
*port_num = MakeRange(colon + 1, serverinfo.end());
} else {
*hostname = serverinfo;
port_num->reset();
}
}
template<typename CHAR>
void DoParseAuthority(const CHAR* spec,
const Component& auth,
Component* username,
Component* password,
Component* hostname,
Component* port_num) {
DCHECK(auth.is_valid()) << "We should always get an authority";
if (auth.len == 0) {
username->reset();
password->reset();
hostname->reset();
port_num->reset();
return;
}
int i = auth.begin + auth.len - 1;
while (i > auth.begin && spec[i] != '@')
i--;
if (spec[i] == '@') {
ParseUserInfo(spec, Component(auth.begin, i - auth.begin),
username, password);
ParseServerInfo(spec, MakeRange(i + 1, auth.begin + auth.len),
hostname, port_num);
} else {
username->reset();
password->reset();
ParseServerInfo(spec, auth, hostname, port_num);
}
}
template<typename CHAR>
void ParsePath(const CHAR* spec,
const Component& path,
Component* filepath,
Component* query,
Component* ref) {
if (path.len == -1) {
filepath->reset();
query->reset();
ref->reset();
return;
}
DCHECK(path.len > 0) << "We should never have 0 length paths";
int path_end = path.begin + path.len;
int query_separator = -1;
int ref_separator = -1;
for (int i = path.begin; i < path_end; i++) {
switch (spec[i]) {
case '?':
if (ref_separator < 0 && query_separator < 0)
query_separator = i;
break;
case '#':
if (ref_separator < 0)
ref_separator = i;
break;
}
}
int file_end, query_end;
if (ref_separator >= 0) {
file_end = query_end = ref_separator;
*ref = MakeRange(ref_separator + 1, path_end);
} else {
file_end = query_end = path_end;
ref->reset();
}
if (query_separator >= 0) {
file_end = query_separator;
*query = MakeRange(query_separator + 1, query_end);
} else {
query->reset();
}
if (file_end != path.begin)
*filepath = MakeRange(path.begin, file_end);
else
filepath->reset();
}
template<typename CHAR>
bool DoExtractScheme(const CHAR* url,
int url_len,
Component* scheme) {
int begin = 0;
while (begin < url_len && ShouldTrimFromURL(url[begin]))
begin++;
if (begin == url_len)
return false;
for (int i = begin; i < url_len; i++) {
if (url[i] == ':') {
*scheme = MakeRange(begin, i);
return true;
}
}
return false;
}
template <typename CHAR>
void DoParseAfterScheme(const CHAR* spec,
int spec_len,
int after_scheme,
Parsed* parsed) {
int num_slashes = CountConsecutiveSlashes(spec, after_scheme, spec_len);
int after_slashes = after_scheme + num_slashes;
Component authority;
Component full_path;
int end_auth = FindNextAuthorityTerminator(spec, after_slashes, spec_len);
authority = Component(after_slashes, end_auth - after_slashes);
if (end_auth == spec_len)
full_path = Component();
else
full_path = Component(end_auth, spec_len - end_auth);
DoParseAuthority(spec, authority, &parsed->username, &parsed->password,
&parsed->host, &parsed->port);
ParsePath(spec, full_path, &parsed->path, &parsed->query, &parsed->ref);
}
template<typename CHAR>
void DoParseStandardURL(const CHAR* spec, int spec_len, Parsed* parsed) {
DCHECK(spec_len >= 0);
int begin = 0;
TrimURL(spec, &begin, &spec_len);
int after_scheme;
if (DoExtractScheme(spec, spec_len, &parsed->scheme)) {
after_scheme = parsed->scheme.end() + 1;
} else {
parsed->scheme.reset();
after_scheme = begin;
}
DoParseAfterScheme(spec, spec_len, after_scheme, parsed);
}
template<typename CHAR>
void DoParseFileSystemURL(const CHAR* spec, int spec_len, Parsed* parsed) {
DCHECK(spec_len >= 0);
parsed->username.reset();
parsed->password.reset();
parsed->host.reset();
parsed->port.reset();
parsed->path.reset();
parsed->ref.reset();
parsed->query.reset();
parsed->clear_inner_parsed();
int begin = 0;
TrimURL(spec, &begin, &spec_len);
if (begin == spec_len) {
parsed->scheme.reset();
return;
}
int inner_start = -1;
if (DoExtractScheme(&spec[begin], spec_len - begin, &parsed->scheme)) {
parsed->scheme.begin += begin;
if (parsed->scheme.end() == spec_len - 1)
return;
inner_start = parsed->scheme.end() + 1;
} else {
parsed->scheme.reset();
return;
}
url_parse::Component inner_scheme;
const CHAR* inner_spec = &spec[inner_start];
int inner_spec_len = spec_len - inner_start;
if (DoExtractScheme(inner_spec, inner_spec_len, &inner_scheme)) {
inner_scheme.begin += inner_start;
if (inner_scheme.end() == spec_len - 1)
return;
} else {
return;
}
Parsed inner_parsed;
if (url_util::CompareSchemeComponent(
spec, inner_scheme, url_util::kFileScheme)) {
ParseFileURL(inner_spec, inner_spec_len, &inner_parsed);
} else if (url_util::CompareSchemeComponent(spec, inner_scheme,
url_util::kFileSystemScheme)) {
return;
} else if (url_util::IsStandard(spec, inner_scheme)) {
DoParseStandardURL(inner_spec, inner_spec_len, &inner_parsed);
} else {
return;
}
inner_parsed.scheme.begin += inner_start;
inner_parsed.username.begin += inner_start;
inner_parsed.password.begin += inner_start;
inner_parsed.host.begin += inner_start;
inner_parsed.port.begin += inner_start;
inner_parsed.query.begin += inner_start;
inner_parsed.ref.begin += inner_start;
inner_parsed.path.begin += inner_start;
parsed->query = inner_parsed.query;
inner_parsed.query.reset();
parsed->ref = inner_parsed.ref;
inner_parsed.ref.reset();
parsed->set_inner_parsed(inner_parsed);
if (!inner_parsed.scheme.is_valid() || !inner_parsed.path.is_valid() ||
inner_parsed.inner_parsed()) {
return;
}
if (!IsURLSlash(spec[inner_parsed.path.begin])) {
return;
}
int inner_path_end = inner_parsed.path.begin + 1;
while (inner_path_end < spec_len &&
!IsURLSlash(spec[inner_path_end]))
++inner_path_end;
parsed->path.begin = inner_path_end;
int new_inner_path_length = inner_path_end - inner_parsed.path.begin;
parsed->path.len = inner_parsed.path.len - new_inner_path_length;
parsed->inner_parsed()->path.len = new_inner_path_length;
}
template<typename CHAR>
void DoParsePathURL(const CHAR* spec, int spec_len,
bool trim_path_end,
Parsed* parsed) {
parsed->username.reset();
parsed->password.reset();
parsed->host.reset();
parsed->port.reset();
parsed->path.reset();
parsed->query.reset();
parsed->ref.reset();
int scheme_begin = 0;
TrimURL(spec, &scheme_begin, &spec_len, trim_path_end);
if (scheme_begin == spec_len) {
parsed->scheme.reset();
parsed->path.reset();
return;
}
int path_begin;
if (ExtractScheme(&spec[scheme_begin], spec_len - scheme_begin,
&parsed->scheme)) {
parsed->scheme.begin += scheme_begin;
path_begin = parsed->scheme.end() + 1;
} else {
parsed->scheme.reset();
path_begin = scheme_begin;
}
if (path_begin == spec_len)
return;
DCHECK_LT(path_begin, spec_len);
ParsePath(spec,
MakeRange(path_begin, spec_len),
&parsed->path,
&parsed->query,
&parsed->ref);
}
template<typename CHAR>
void DoParseMailtoURL(const CHAR* spec, int spec_len, Parsed* parsed) {
DCHECK(spec_len >= 0);
parsed->username.reset();
parsed->password.reset();
parsed->host.reset();
parsed->port.reset();
parsed->ref.reset();
parsed->query.reset();
int begin = 0;
TrimURL(spec, &begin, &spec_len);
if (begin == spec_len) {
parsed->scheme.reset();
parsed->path.reset();
return;
}
int path_begin = -1;
int path_end = -1;
if (ExtractScheme(&spec[begin], spec_len - begin, &parsed->scheme)) {
parsed->scheme.begin += begin;
if (parsed->scheme.end() != spec_len - 1) {
path_begin = parsed->scheme.end() + 1;
path_end = spec_len;
}
} else {
parsed->scheme.reset();
path_begin = begin;
path_end = spec_len;
}
for (int i = path_begin; i < path_end; ++i) {
if (spec[i] == '?') {
parsed->query = MakeRange(i + 1, path_end);
path_end = i;
break;
}
}
if (path_begin == path_end) {
parsed->path.reset();
} else {
parsed->path = MakeRange(path_begin, path_end);
}
}
template<typename CHAR>
int DoParsePort(const CHAR* spec, const Component& component) {
const int kMaxDigits = 5;
if (!component.is_nonempty())
return PORT_UNSPECIFIED;
Component digits_comp(component.end(), 0);
for (int i = 0; i < component.len; i++) {
if (spec[component.begin + i] != '0') {
digits_comp = MakeRange(component.begin + i, component.end());
break;
}
}
if (digits_comp.len == 0)
return 0;
if (digits_comp.len > kMaxDigits)
return PORT_INVALID;
char digits[kMaxDigits + 1];
for (int i = 0; i < digits_comp.len; i++) {
CHAR ch = spec[digits_comp.begin + i];
if (!IsPortDigit(ch)) {
return PORT_INVALID;
}
digits[i] = static_cast<char>(ch);
}
digits[digits_comp.len] = 0;
int port = atoi(digits);
if (port > 65535)
return PORT_INVALID;
return port;
}
template<typename CHAR>
void DoExtractFileName(const CHAR* spec,
const Component& path,
Component* file_name) {
if (!path.is_nonempty()) {
file_name->reset();
return;
}
int file_end = path.end();
for (int i = path.end() - 1; i > path.begin; i--) {
if (spec[i] == ';') {
file_end = i;
break;
}
}
for (int i = file_end - 1; i >= path.begin; i--) {
if (IsURLSlash(spec[i])) {
*file_name = MakeRange(i + 1, file_end);
return;
}
}
*file_name = MakeRange(path.begin, file_end);
return;
}
template<typename CHAR>
bool DoExtractQueryKeyValue(const CHAR* spec,
Component* query,
Component* key,
Component* value) {
if (!query->is_nonempty())
return false;
int start = query->begin;
int cur = start;
int end = query->end();
key->begin = cur;
while (cur < end && spec[cur] != '&' && spec[cur] != '=')
cur++;
key->len = cur - key->begin;
if (cur < end && spec[cur] == '=')
cur++;
value->begin = cur;
while (cur < end && spec[cur] != '&')
cur++;
value->len = cur - value->begin;
if (cur < end && spec[cur] == '&')
cur++;
*query = url_parse::MakeRange(cur, end);
return true;
}
}
Parsed::Parsed() : inner_parsed_(NULL) {
}
Parsed::Parsed(const Parsed& other) :
scheme(other.scheme),
username(other.username),
password(other.password),
host(other.host),
port(other.port),
path(other.path),
query(other.query),
ref(other.ref),
inner_parsed_(NULL) {
if (other.inner_parsed_)
set_inner_parsed(*other.inner_parsed_);
}
Parsed& Parsed::operator=(const Parsed& other) {
if (this != &other) {
scheme = other.scheme;
username = other.username;
password = other.password;
host = other.host;
port = other.port;
path = other.path;
query = other.query;
ref = other.ref;
if (other.inner_parsed_)
set_inner_parsed(*other.inner_parsed_);
else
clear_inner_parsed();
}
return *this;
}
Parsed::~Parsed() {
delete inner_parsed_;
}
int Parsed::Length() const {
if (ref.is_valid())
return ref.end();
return CountCharactersBefore(REF, false);
}
int Parsed::CountCharactersBefore(ComponentType type,
bool include_delimiter) const {
if (type == SCHEME)
return scheme.begin;
int cur = 0;
if (scheme.is_valid())
cur = scheme.end() + 1;
if (username.is_valid()) {
if (type <= USERNAME)
return username.begin;
cur = username.end() + 1;
}
if (password.is_valid()) {
if (type <= PASSWORD)
return password.begin;
cur = password.end() + 1;
}
if (host.is_valid()) {
if (type <= HOST)
return host.begin;
cur = host.end();
}
if (port.is_valid()) {
if (type < PORT || (type == PORT && include_delimiter))
return port.begin - 1;
if (type == PORT)
return port.begin;
cur = port.end();
}
if (path.is_valid()) {
if (type <= PATH)
return path.begin;
cur = path.end();
}
if (query.is_valid()) {
if (type < QUERY || (type == QUERY && include_delimiter))
return query.begin - 1;
if (type == QUERY)
return query.begin;
cur = query.end();
}
if (ref.is_valid()) {
if (type == REF && !include_delimiter)
return ref.begin;
return ref.begin - 1;
}
return cur;
}
Component Parsed::GetContent() const {
const int begin = CountCharactersBefore(USERNAME, false);
const int len = Length() - begin;
return len ? Component(begin, len) : Component();
}
bool ExtractScheme(const char* url, int url_len, Component* scheme) {
return DoExtractScheme(url, url_len, scheme);
}
bool ExtractScheme(const base::char16* url, int url_len, Component* scheme) {
return DoExtractScheme(url, url_len, scheme);
}
bool IsAuthorityTerminator(base::char16 ch) {
return IsURLSlash(ch) || ch == '?' || ch == '#';
}
void ExtractFileName(const char* url,
const Component& path,
Component* file_name) {
DoExtractFileName(url, path, file_name);
}
void ExtractFileName(const base::char16* url,
const Component& path,
Component* file_name) {
DoExtractFileName(url, path, file_name);
}
bool ExtractQueryKeyValue(const char* url,
Component* query,
Component* key,
Component* value) {
return DoExtractQueryKeyValue(url, query, key, value);
}
bool ExtractQueryKeyValue(const base::char16* url,
Component* query,
Component* key,
Component* value) {
return DoExtractQueryKeyValue(url, query, key, value);
}
void ParseAuthority(const char* spec,
const Component& auth,
Component* username,
Component* password,
Component* hostname,
Component* port_num) {
DoParseAuthority(spec, auth, username, password, hostname, port_num);
}
void ParseAuthority(const base::char16* spec,
const Component& auth,
Component* username,
Component* password,
Component* hostname,
Component* port_num) {
DoParseAuthority(spec, auth, username, password, hostname, port_num);
}
int ParsePort(const char* url, const Component& port) {
return DoParsePort(url, port);
}
int ParsePort(const base::char16* url, const Component& port) {
return DoParsePort(url, port);
}
void ParseStandardURL(const char* url, int url_len, Parsed* parsed) {
DoParseStandardURL(url, url_len, parsed);
}
void ParseStandardURL(const base::char16* url, int url_len, Parsed* parsed) {
DoParseStandardURL(url, url_len, parsed);
}
void ParsePathURL(const char* url,
int url_len,
bool trim_path_end,
Parsed* parsed) {
DoParsePathURL(url, url_len, trim_path_end, parsed);
}
void ParsePathURL(const base::char16* url,
int url_len,
bool trim_path_end,
Parsed* parsed) {
DoParsePathURL(url, url_len, trim_path_end, parsed);
}
void ParseFileSystemURL(const char* url, int url_len, Parsed* parsed) {
DoParseFileSystemURL(url, url_len, parsed);
}
void ParseFileSystemURL(const base::char16* url, int url_len, Parsed* parsed) {
DoParseFileSystemURL(url, url_len, parsed);
}
void ParseMailtoURL(const char* url, int url_len, Parsed* parsed) {
DoParseMailtoURL(url, url_len, parsed);
}
void ParseMailtoURL(const base::char16* url, int url_len, Parsed* parsed) {
DoParseMailtoURL(url, url_len, parsed);
}
void ParsePathInternal(const char* spec,
const Component& path,
Component* filepath,
Component* query,
Component* ref) {
ParsePath(spec, path, filepath, query, ref);
}
void ParsePathInternal(const base::char16* spec,
const Component& path,
Component* filepath,
Component* query,
Component* ref) {
ParsePath(spec, path, filepath, query, ref);
}
void ParseAfterScheme(const char* spec,
int spec_len,
int after_scheme,
Parsed* parsed) {
DoParseAfterScheme(spec, spec_len, after_scheme, parsed);
}
void ParseAfterScheme(const base::char16* spec,
int spec_len,
int after_scheme,
Parsed* parsed) {
DoParseAfterScheme(spec, spec_len, after_scheme, parsed);
}
}