#include "config.h"
#include "ParsedCookie.h"
#include "CookieManager.h"
#include "KURL.h"
#include "Logging.h"
#include <curl/curl.h>
#include <wtf/CurrentTime.h>
#include <wtf/text/CString.h>
#include <wtf/text/StringBuilder.h>
namespace WebCore {
ParsedCookie::ParsedCookie(double currentTime)
: m_expiry(-1)
, m_creationTime(currentTime)
, m_lastAccessed(currentTime)
, m_isSecure(false)
, m_isHttpOnly(false)
, m_isSession(true)
, m_isForceExpired(false)
, m_domainIsIPAddress(false)
{
}
ParsedCookie::ParsedCookie(const String& name, const String& value, const String& domain, const String& protocol, const String& path, double expiry, double lastAccessed, double creationTime, bool isSecure, bool isHttpOnly)
: m_name(name)
, m_value(value)
, m_domain(domain)
, m_protocol(protocol)
, m_path(path)
, m_expiry(expiry)
, m_creationTime(creationTime)
, m_lastAccessed(lastAccessed)
, m_isSecure(isSecure)
, m_isHttpOnly(isHttpOnly)
, m_isSession(false)
, m_isForceExpired(false)
, m_domainIsIPAddress(false)
{
}
void ParsedCookie::setExpiry(const String& expiry)
{
if (m_expiry != -1 || expiry.isEmpty())
return;
m_isSession = false;
m_expiry = static_cast<double>(curl_getdate(expiry.utf8().data(), 0));
if (m_expiry == -1) {
LOG_ERROR("Could not parse date");
m_isSession = true;
}
}
void ParsedCookie::setMaxAge(const String& maxAge)
{
if (maxAge.isEmpty() || (maxAge[0] != '-' && !isASCIIDigit(maxAge[0]))) {
LOG_ERROR("Could not parse Max-Age : %s, first character can only be '-' or ascii digit.", maxAge.ascii().data());
return;
}
bool ok;
int value = maxAge.toIntStrict(&ok);
if (!ok) {
LOG_ERROR("Could not parse Max-Age : %s", maxAge.ascii().data());
return;
}
m_expiry = value;
m_isSession = false;
if (m_expiry > 0)
m_expiry += currentTime();
else
m_expiry = 0;
}
bool ParsedCookie::hasExpired() const
{
return !m_isSession && (m_isForceExpired || m_expiry < currentTime());
}
bool ParsedCookie::isUnderSizeLimit() const
{
return m_value.length() <= CookieManager::maxCookieLength() && m_name.length() <= CookieManager::maxCookieLength();
}
String ParsedCookie::toString() const
{
StringBuilder builder;
builder.append(name());
builder.append(" = ");
builder.append(value());
builder.append("; Domain = ");
builder.append(domain());
builder.append("; Path = ");
builder.append(path());
builder.append("; Protocol = ");
builder.append(protocol());
return builder.toString();
}
String ParsedCookie::toNameValuePair() const
{
static const String equal("=");
size_t cookieLength = m_name.length() + m_value.length() + 2;
Vector<UChar> result;
result.reserveInitialCapacity(cookieLength);
append(result, m_name);
append(result, equal);
append(result, m_value);
return String::adopt(result);
}
void ParsedCookie::appendWebCoreCookie(Vector<Cookie>& cookieVector) const
{
cookieVector.append(Cookie(String(m_name), String(m_value), String(m_domain),
String(m_path), (m_expiry * 1000), m_isHttpOnly, m_isSecure, m_isSession));
}
}