This source file includes following definitions.
- DomainIsHostOnly
- GetEffectiveDomain
- GetCookieDomainWithString
- ParseCookieTime
- CookieOriginToURL
#include "net/cookies/cookie_util.h"
#include <cstdio>
#include <cstdlib>
#include "base/logging.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "net/base/net_util.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "url/gurl.h"
namespace net {
namespace cookie_util {
bool DomainIsHostOnly(const std::string& domain_string) {
return (domain_string.empty() || domain_string[0] != '.');
}
std::string GetEffectiveDomain(const std::string& scheme,
const std::string& host) {
if (scheme == "http" || scheme == "https") {
return registry_controlled_domains::GetDomainAndRegistry(
host,
registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
}
if (!DomainIsHostOnly(host))
return host.substr(1);
return host;
}
bool GetCookieDomainWithString(const GURL& url,
const std::string& domain_string,
std::string* result) {
const std::string url_host(url.host());
if (domain_string.empty() ||
(url.HostIsIPAddress() && url_host == domain_string)) {
*result = url_host;
DCHECK(DomainIsHostOnly(*result));
return true;
}
url_canon::CanonHostInfo ignored;
std::string cookie_domain(CanonicalizeHost(domain_string, &ignored));
if (cookie_domain.empty())
return false;
if (cookie_domain[0] != '.')
cookie_domain = "." + cookie_domain;
const std::string url_scheme(url.scheme());
const std::string url_domain_and_registry(
GetEffectiveDomain(url_scheme, url_host));
if (url_domain_and_registry.empty())
return false;
const std::string cookie_domain_and_registry(
GetEffectiveDomain(url_scheme, cookie_domain));
if (url_domain_and_registry != cookie_domain_and_registry)
return false;
const bool is_suffix = (url_host.length() < cookie_domain.length()) ?
(cookie_domain != ("." + url_host)) :
(url_host.compare(url_host.length() - cookie_domain.length(),
cookie_domain.length(), cookie_domain) != 0);
if (is_suffix)
return false;
*result = cookie_domain;
return true;
}
base::Time ParseCookieTime(const std::string& time_string) {
static const char* kMonths[] = { "jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec" };
static const int kMonthsLen = arraysize(kMonths);
static const char* kDelimiters = "\t !\"#$%&'()*+,-./;<=>?@[\\]^_`{|}~";
base::Time::Exploded exploded = {0};
base::StringTokenizer tokenizer(time_string, kDelimiters);
bool found_day_of_month = false;
bool found_month = false;
bool found_time = false;
bool found_year = false;
while (tokenizer.GetNext()) {
const std::string token = tokenizer.token();
DCHECK(!token.empty());
bool numerical = IsAsciiDigit(token[0]);
if (!numerical) {
if (!found_month) {
for (int i = 0; i < kMonthsLen; ++i) {
if (base::strncasecmp(token.c_str(), kMonths[i], 3) == 0) {
exploded.month = i + 1;
found_month = true;
break;
}
}
} else {
}
} else if (token.find(':') != std::string::npos) {
if (!found_time &&
#ifdef COMPILER_MSVC
sscanf_s(
#else
sscanf(
#endif
token.c_str(), "%2u:%2u:%2u", &exploded.hour,
&exploded.minute, &exploded.second) == 3) {
found_time = true;
} else {
}
} else {
if (!found_day_of_month && token.length() <= 2) {
exploded.day_of_month = atoi(token.c_str());
found_day_of_month = true;
} else if (!found_year && token.length() <= 5) {
exploded.year = atoi(token.c_str());
found_year = true;
} else {
}
}
}
if (!found_day_of_month || !found_month || !found_time || !found_year) {
return base::Time();
}
if (exploded.year >= 69 && exploded.year <= 99)
exploded.year += 1900;
if (exploded.year >= 0 && exploded.year <= 68)
exploded.year += 2000;
if (exploded.day_of_month >= 1 && exploded.day_of_month <= 31 &&
exploded.month >= 1 && exploded.month <= 12 &&
exploded.year >= 1601 && exploded.year <= 30827 &&
exploded.hour <= 23 && exploded.minute <= 59 && exploded.second <= 59) {
return base::Time::FromUTCExploded(exploded);
}
return base::Time();
}
GURL CookieOriginToURL(const std::string& domain, bool is_https) {
if (domain.empty())
return GURL();
const std::string scheme = is_https ? "https" : "http";
const std::string host = domain[0] == '.' ? domain.substr(1) : domain;
return GURL(scheme + "://" + host);
}
}
}