2008-08-27 16:04:36 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2008-08-27 16:04:36 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "CookieStorage.h"
|
2009-05-18 15:07:15 +00:00
|
|
|
|
2009-05-22 14:51:57 +00:00
|
|
|
#include <cstring>
|
2011-08-05 13:44:54 +00:00
|
|
|
#include <cstdio>
|
2009-05-18 15:07:15 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2008-08-27 16:04:36 +00:00
|
|
|
#include "LogFactory.h"
|
|
|
|
#include "Logger.h"
|
2009-05-18 15:07:15 +00:00
|
|
|
#include "DlAbortEx.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "fmt.h"
|
2008-08-27 16:04:36 +00:00
|
|
|
#include "NsCookieParser.h"
|
2009-06-29 08:42:58 +00:00
|
|
|
#include "File.h"
|
2010-01-28 14:01:50 +00:00
|
|
|
#include "a2functional.h"
|
2010-01-28 14:05:42 +00:00
|
|
|
#include "A2STR.h"
|
2010-07-08 15:18:15 +00:00
|
|
|
#include "message.h"
|
2010-10-09 14:22:49 +00:00
|
|
|
#include "cookie_helper.h"
|
2008-08-27 16:04:36 +00:00
|
|
|
#ifdef HAVE_SQLITE3
|
2010-07-08 15:18:15 +00:00
|
|
|
# include "Sqlite3CookieParserImpl.h"
|
2008-08-27 16:04:36 +00:00
|
|
|
#endif // HAVE_SQLITE3
|
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
CookieStorage::DomainEntry::DomainEntry(const std::string& domain)
|
|
|
|
: key_(util::isNumericHost(domain)?domain:cookie::reverseDomainLevel(domain))
|
|
|
|
{}
|
|
|
|
|
2010-01-28 14:01:50 +00:00
|
|
|
CookieStorage::DomainEntry::DomainEntry
|
2010-11-14 07:17:55 +00:00
|
|
|
(const DomainEntry& c)
|
|
|
|
: key_(c.key_),
|
|
|
|
lastAccessTime_(c.lastAccessTime_),
|
|
|
|
cookies_(c.cookies_)
|
2010-10-09 14:22:49 +00:00
|
|
|
{}
|
2008-08-27 16:04:36 +00:00
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
CookieStorage::DomainEntry::~DomainEntry() {}
|
|
|
|
|
|
|
|
CookieStorage::DomainEntry& CookieStorage::DomainEntry::operator=
|
|
|
|
(const DomainEntry& c)
|
|
|
|
{
|
|
|
|
if(this != &c) {
|
|
|
|
key_ = c.key_;
|
|
|
|
lastAccessTime_ = c.lastAccessTime_;
|
|
|
|
cookies_ = c.cookies_;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-11-20 14:05:58 +00:00
|
|
|
void CookieStorage::DomainEntry::swap(CookieStorage::DomainEntry& c)
|
|
|
|
{
|
|
|
|
using std::swap;
|
|
|
|
swap(key_, c.key_);
|
|
|
|
swap(lastAccessTime_, c.lastAccessTime_);
|
|
|
|
swap(cookies_, c.cookies_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void swap(CookieStorage::DomainEntry& a, CookieStorage::DomainEntry& b)
|
|
|
|
{
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2010-10-09 14:22:49 +00:00
|
|
|
bool CookieStorage::DomainEntry::addCookie(const Cookie& cookie, time_t now)
|
2008-08-27 16:04:36 +00:00
|
|
|
{
|
2010-10-09 14:22:49 +00:00
|
|
|
setLastAccessTime(now);
|
|
|
|
std::deque<Cookie>::iterator i =
|
|
|
|
std::find(cookies_.begin(), cookies_.end(), cookie);
|
2010-06-21 13:51:56 +00:00
|
|
|
if(i == cookies_.end()) {
|
2010-10-09 14:22:49 +00:00
|
|
|
if(cookie.isExpired(now)) {
|
2008-08-27 16:04:36 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2010-06-21 13:51:56 +00:00
|
|
|
if(cookies_.size() >= CookieStorage::MAX_COOKIE_PER_DOMAIN) {
|
2010-10-10 09:22:04 +00:00
|
|
|
cookies_.erase
|
|
|
|
(std::remove_if(cookies_.begin(), cookies_.end(),
|
|
|
|
std::bind2nd
|
|
|
|
(std::mem_fun_ref(&Cookie::isExpired), now)),
|
|
|
|
cookies_.end());
|
|
|
|
if(cookies_.size() >= CookieStorage::MAX_COOKIE_PER_DOMAIN) {
|
|
|
|
std::deque<Cookie>::iterator m = std::min_element
|
|
|
|
(cookies_.begin(), cookies_.end(), LeastRecentAccess<Cookie>());
|
|
|
|
*m = cookie;
|
|
|
|
} else {
|
|
|
|
cookies_.push_back(cookie);
|
|
|
|
}
|
2010-01-28 14:01:50 +00:00
|
|
|
} else {
|
2010-06-21 13:51:56 +00:00
|
|
|
cookies_.push_back(cookie);
|
2010-01-28 14:01:50 +00:00
|
|
|
}
|
2008-08-27 16:04:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-10-09 14:22:49 +00:00
|
|
|
} else if(cookie.isExpired(now)) {
|
2010-06-21 13:51:56 +00:00
|
|
|
cookies_.erase(i);
|
2008-08-27 16:04:36 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
*i = cookie;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-28 14:01:50 +00:00
|
|
|
bool CookieStorage::DomainEntry::contains(const Cookie& cookie) const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return std::find(cookies_.begin(), cookies_.end(), cookie) != cookies_.end();
|
2010-01-28 14:01:50 +00:00
|
|
|
}
|
|
|
|
|
2011-08-05 13:44:54 +00:00
|
|
|
bool CookieStorage::DomainEntry::writeCookie(FILE* fp) const
|
2010-01-28 14:01:50 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
for(std::deque<Cookie>::const_iterator i = cookies_.begin(),
|
|
|
|
eoi = cookies_.end(); i != eoi; ++i) {
|
2011-08-05 13:44:54 +00:00
|
|
|
std::string data = (*i).toNsCookieFormat();
|
|
|
|
data += "\n";
|
|
|
|
if(fwrite(data.data(), 1, data.size(), fp) != data.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-01-28 14:01:50 +00:00
|
|
|
}
|
2011-08-05 13:44:54 +00:00
|
|
|
return true;
|
2010-01-28 14:01:50 +00:00
|
|
|
}
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
size_t CookieStorage::DomainEntry::countCookie() const
|
|
|
|
{
|
|
|
|
return cookies_.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CookieStorage::DomainEntry::operator<(const DomainEntry& de) const
|
|
|
|
{
|
|
|
|
return key_ < de.key_;
|
|
|
|
}
|
|
|
|
|
2010-11-20 08:21:36 +00:00
|
|
|
CookieStorage::CookieStorage() {}
|
2010-01-28 14:01:50 +00:00
|
|
|
|
|
|
|
CookieStorage::~CookieStorage() {}
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
2010-01-28 14:01:50 +00:00
|
|
|
// See CookieStorageTest::testDomainIsFull() in CookieStorageTest.cc
|
2010-10-30 16:02:15 +00:00
|
|
|
const size_t DOMAIN_EVICTION_TRIGGER = 2000;
|
2010-01-28 14:01:50 +00:00
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
const double DOMAIN_EVICTION_RATE = 0.1;
|
|
|
|
} // namespace
|
2010-01-28 14:01:50 +00:00
|
|
|
|
2010-10-09 14:22:49 +00:00
|
|
|
bool CookieStorage::store(const Cookie& cookie, time_t now)
|
2010-01-28 14:01:50 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
if(domains_.size() >= DOMAIN_EVICTION_TRIGGER) {
|
|
|
|
std::sort(domains_.begin(), domains_.end(),
|
2010-01-28 14:01:50 +00:00
|
|
|
LeastRecentAccess<DomainEntry>());
|
2010-06-21 13:51:56 +00:00
|
|
|
size_t delnum = (size_t)(domains_.size()*DOMAIN_EVICTION_RATE);
|
|
|
|
domains_.erase(domains_.begin(), domains_.begin()+delnum);
|
|
|
|
std::sort(domains_.begin(), domains_.end());
|
2010-01-28 14:01:50 +00:00
|
|
|
}
|
|
|
|
DomainEntry v(cookie.getDomain());
|
|
|
|
std::deque<DomainEntry>::iterator i =
|
2010-06-21 13:51:56 +00:00
|
|
|
std::lower_bound(domains_.begin(), domains_.end(), v);
|
2010-01-28 14:01:50 +00:00
|
|
|
bool added = false;
|
2010-06-21 13:51:56 +00:00
|
|
|
if(i != domains_.end() && (*i).getKey() == v.getKey()) {
|
2010-10-09 14:22:49 +00:00
|
|
|
added = (*i).addCookie(cookie, now);
|
2010-01-28 14:01:50 +00:00
|
|
|
} else {
|
2010-10-09 14:22:49 +00:00
|
|
|
added = v.addCookie(cookie, now);
|
2010-01-28 14:01:50 +00:00
|
|
|
if(added) {
|
2010-06-21 13:51:56 +00:00
|
|
|
domains_.insert(i, v);
|
2010-01-28 14:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return added;
|
|
|
|
}
|
|
|
|
|
2010-10-09 14:22:49 +00:00
|
|
|
bool CookieStorage::parseAndStore
|
|
|
|
(const std::string& setCookieString,
|
|
|
|
const std::string& requestHost,
|
|
|
|
const std::string& defaultPath,
|
|
|
|
time_t now)
|
2008-08-27 16:04:36 +00:00
|
|
|
{
|
2010-10-09 14:22:49 +00:00
|
|
|
Cookie cookie;
|
|
|
|
if(cookie::parse(cookie, setCookieString, requestHost, defaultPath, now)) {
|
|
|
|
return store(cookie, now);
|
2008-08-27 16:04:36 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-28 14:01:50 +00:00
|
|
|
struct CookiePathDivider {
|
2010-06-21 13:51:56 +00:00
|
|
|
Cookie cookie_;
|
|
|
|
int pathDepth_;
|
|
|
|
CookiePathDivider(const Cookie& cookie):cookie_(cookie)
|
2010-01-28 14:01:50 +00:00
|
|
|
{
|
|
|
|
std::vector<std::string> paths;
|
2010-06-21 13:51:56 +00:00
|
|
|
util::split(cookie_.getPath(), std::back_inserter(paths), A2STR::SLASH_C);
|
|
|
|
pathDepth_ = paths.size();
|
2010-01-28 14:01:50 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-08-31 13:33:05 +00:00
|
|
|
namespace {
|
2010-01-28 14:01:50 +00:00
|
|
|
class CookiePathDividerConverter {
|
2008-08-27 16:04:36 +00:00
|
|
|
public:
|
2010-01-28 14:01:50 +00:00
|
|
|
CookiePathDivider operator()(const Cookie& cookie) const
|
|
|
|
{
|
|
|
|
return CookiePathDivider(cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cookie operator()(const CookiePathDivider& cookiePathDivider) const
|
2008-08-27 16:04:36 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return cookiePathDivider.cookie_;
|
2008-08-27 16:04:36 +00:00
|
|
|
}
|
|
|
|
};
|
2010-10-30 14:53:40 +00:00
|
|
|
} // namespace
|
2008-08-27 16:04:36 +00:00
|
|
|
|
2010-08-31 13:33:05 +00:00
|
|
|
namespace {
|
2010-01-28 14:01:50 +00:00
|
|
|
class OrderByPathDepthDesc:public std::binary_function<Cookie, Cookie, bool> {
|
2008-08-27 16:04:36 +00:00
|
|
|
public:
|
2010-01-28 14:01:50 +00:00
|
|
|
bool operator()
|
|
|
|
(const CookiePathDivider& lhs, const CookiePathDivider& rhs) const
|
2008-08-27 16:04:36 +00:00
|
|
|
{
|
2011-05-14 12:23:59 +00:00
|
|
|
// From http://tools.ietf.org/html/rfc6265#section-5.4:
|
|
|
|
// 2. The user agent SHOULD sort the cookie-list in the following
|
|
|
|
// order:
|
2010-01-28 14:01:50 +00:00
|
|
|
//
|
2011-05-14 12:23:59 +00:00
|
|
|
// * Cookies with longer paths are listed before cookies with
|
|
|
|
// shorter paths.
|
2010-01-28 14:01:50 +00:00
|
|
|
//
|
2011-05-14 12:23:59 +00:00
|
|
|
// * Among cookies that have equal-length path fields, cookies with
|
|
|
|
// earlier creation-times are listed before cookies with later
|
|
|
|
// creation-times.
|
2010-10-09 14:22:49 +00:00
|
|
|
return lhs.pathDepth_ > rhs.pathDepth_ ||
|
|
|
|
(!(rhs.pathDepth_ > lhs.pathDepth_) &&
|
|
|
|
lhs.cookie_.getCreationTime() < rhs.cookie_.getCreationTime());
|
2008-08-27 16:04:36 +00:00
|
|
|
}
|
|
|
|
};
|
2010-10-30 14:53:40 +00:00
|
|
|
} // namespace
|
2008-08-27 16:04:36 +00:00
|
|
|
|
2010-10-09 14:22:49 +00:00
|
|
|
namespace {
|
2010-01-28 14:01:50 +00:00
|
|
|
template<typename DomainInputIterator, typename CookieOutputIterator>
|
2010-10-09 14:22:49 +00:00
|
|
|
void searchCookieByDomainSuffix
|
2010-01-28 14:01:50 +00:00
|
|
|
(const std::string& domain,
|
|
|
|
DomainInputIterator first, DomainInputIterator last, CookieOutputIterator out,
|
|
|
|
const std::string& requestHost,
|
|
|
|
const std::string& requestPath,
|
2010-10-09 14:22:49 +00:00
|
|
|
time_t now, bool secure)
|
2010-01-28 14:01:50 +00:00
|
|
|
{
|
|
|
|
CookieStorage::DomainEntry v(domain);
|
|
|
|
std::deque<CookieStorage::DomainEntry>::iterator i =
|
|
|
|
std::lower_bound(first, last, v);
|
|
|
|
if(i != last && (*i).getKey() == v.getKey()) {
|
2010-10-09 14:22:49 +00:00
|
|
|
(*i).setLastAccessTime(now);
|
|
|
|
(*i).findCookie(out, requestHost, requestPath, now, secure);
|
2010-01-28 14:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-30 14:53:40 +00:00
|
|
|
} // namespace
|
2010-01-28 14:01:50 +00:00
|
|
|
|
|
|
|
bool CookieStorage::contains(const Cookie& cookie) const
|
|
|
|
{
|
|
|
|
CookieStorage::DomainEntry v(cookie.getDomain());
|
|
|
|
std::deque<CookieStorage::DomainEntry>::const_iterator i =
|
2010-06-21 13:51:56 +00:00
|
|
|
std::lower_bound(domains_.begin(), domains_.end(), v);
|
|
|
|
if(i != domains_.end() && (*i).getKey() == v.getKey()) {
|
2010-01-28 14:01:50 +00:00
|
|
|
return (*i).contains(cookie);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-09 14:22:49 +00:00
|
|
|
std::vector<Cookie> CookieStorage::criteriaFind
|
|
|
|
(const std::string& requestHost,
|
|
|
|
const std::string& requestPath,
|
|
|
|
time_t now,
|
|
|
|
bool secure)
|
2008-08-27 16:04:36 +00:00
|
|
|
{
|
2010-02-28 12:30:11 +00:00
|
|
|
std::vector<Cookie> res;
|
2010-10-09 14:22:49 +00:00
|
|
|
if(requestPath.empty()) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
if(util::isNumericHost(requestHost)) {
|
|
|
|
searchCookieByDomainSuffix
|
|
|
|
(requestHost, domains_.begin(), domains_.end(), std::back_inserter(res),
|
2010-10-09 14:38:47 +00:00
|
|
|
requestHost, requestPath, now, secure);
|
2010-10-09 14:22:49 +00:00
|
|
|
} else {
|
|
|
|
std::vector<std::string> levels;
|
|
|
|
util::split(requestHost, std::back_inserter(levels),A2STR::DOT_C);
|
|
|
|
std::reverse(levels.begin(), levels.end());
|
|
|
|
std::string domain;
|
|
|
|
for(std::vector<std::string>::const_iterator i =
|
|
|
|
levels.begin(), eoi = levels.end();
|
|
|
|
i != eoi; ++i, domain.insert(domain.begin(), '.')) {
|
|
|
|
domain.insert(domain.begin(), (*i).begin(), (*i).end());
|
|
|
|
searchCookieByDomainSuffix
|
|
|
|
(domain, domains_.begin(), domains_.end(),
|
2010-10-09 14:38:47 +00:00
|
|
|
std::back_inserter(res), requestHost, requestPath, now, secure);
|
2010-01-28 14:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
std::vector<CookiePathDivider> divs;
|
|
|
|
std::transform(res.begin(), res.end(), std::back_inserter(divs),
|
|
|
|
CookiePathDividerConverter());
|
|
|
|
std::sort(divs.begin(), divs.end(), OrderByPathDepthDesc());
|
|
|
|
std::transform(divs.begin(), divs.end(), res.begin(),
|
|
|
|
CookiePathDividerConverter());
|
2008-08-27 16:04:36 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CookieStorage::size() const
|
|
|
|
{
|
2010-01-28 14:01:50 +00:00
|
|
|
size_t numCookie = 0;
|
2010-06-21 13:51:56 +00:00
|
|
|
for(std::deque<DomainEntry>::const_iterator i = domains_.begin(),
|
|
|
|
eoi = domains_.end(); i != eoi; ++i) {
|
2010-01-28 14:01:50 +00:00
|
|
|
numCookie += (*i).countCookie();
|
|
|
|
}
|
|
|
|
return numCookie;
|
2008-08-27 16:04:36 +00:00
|
|
|
}
|
|
|
|
|
2010-10-09 14:22:49 +00:00
|
|
|
bool CookieStorage::load(const std::string& filename, time_t now)
|
2008-08-27 16:04:36 +00:00
|
|
|
{
|
|
|
|
char header[16]; // "SQLite format 3" plus \0
|
2011-08-05 13:44:54 +00:00
|
|
|
FILE* fp = a2fopen(utf8ToWChar(filename).c_str(), "rb");
|
|
|
|
if(!fp) {
|
|
|
|
A2_LOG_ERROR(fmt("Failed to open cookie file %s",
|
|
|
|
utf8ToNative(filename).c_str()));
|
2009-07-22 13:01:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-08-05 13:44:54 +00:00
|
|
|
size_t r = fread(header, 1, sizeof(header), fp);
|
|
|
|
fclose(fp);
|
|
|
|
if(r != sizeof(header)) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_ERROR(fmt("Failed to read header of cookie file %s",
|
2011-08-05 13:44:54 +00:00
|
|
|
utf8ToNative(filename).c_str()));
|
2009-06-21 10:26:14 +00:00
|
|
|
return false;
|
2008-08-27 16:04:36 +00:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
if(std::string(header) == "SQLite format 3") {
|
|
|
|
#ifdef HAVE_SQLITE3
|
2010-07-08 15:18:15 +00:00
|
|
|
std::vector<Cookie> cookies;
|
|
|
|
try {
|
2010-10-10 09:53:30 +00:00
|
|
|
Sqlite3MozCookieParser(filename).parse(cookies);
|
2010-07-08 15:18:15 +00:00
|
|
|
} catch(RecoverableException& e) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_INFO_EX(EX_EXCEPTION_CAUGHT, e);
|
|
|
|
A2_LOG_INFO("This does not look like Firefox3 cookie file."
|
|
|
|
" Retrying, assuming it is Chromium cookie file.");
|
2010-07-08 15:18:15 +00:00
|
|
|
// Try chrome cookie format
|
2010-10-10 09:53:30 +00:00
|
|
|
Sqlite3ChromiumCookieParser(filename).parse(cookies);
|
2010-07-08 15:18:15 +00:00
|
|
|
}
|
2010-10-09 14:22:49 +00:00
|
|
|
storeCookies(cookies.begin(), cookies.end(), now);
|
2008-08-27 16:04:36 +00:00
|
|
|
#else // !HAVE_SQLITE3
|
2009-05-18 15:07:15 +00:00
|
|
|
throw DL_ABORT_EX
|
2010-01-05 16:01:46 +00:00
|
|
|
("Cannot read SQLite3 database because SQLite3 support is disabled by"
|
|
|
|
" configuration.");
|
2008-08-27 16:04:36 +00:00
|
|
|
#endif // !HAVE_SQLITE3
|
|
|
|
} else {
|
2010-10-09 14:22:49 +00:00
|
|
|
std::vector<Cookie> cookies = NsCookieParser().parse(filename, now);
|
|
|
|
storeCookies(cookies.begin(), cookies.end(), now);
|
2008-08-27 16:04:36 +00:00
|
|
|
}
|
2009-06-21 10:26:14 +00:00
|
|
|
return true;
|
2008-08-27 16:04:36 +00:00
|
|
|
} catch(RecoverableException& e) {
|
2011-08-05 13:44:54 +00:00
|
|
|
A2_LOG_ERROR(fmt("Failed to load cookies from %s",
|
|
|
|
utf8ToNative(filename).c_str()));
|
2009-06-21 10:26:14 +00:00
|
|
|
return false;
|
2008-08-27 16:04:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-21 10:26:14 +00:00
|
|
|
bool CookieStorage::saveNsFormat(const std::string& filename)
|
2009-05-22 14:51:57 +00:00
|
|
|
{
|
2009-06-21 10:26:14 +00:00
|
|
|
std::string tempfilename = filename+"__temp";
|
2009-07-21 15:19:43 +00:00
|
|
|
{
|
2011-08-05 13:44:54 +00:00
|
|
|
FILE* fp = a2fopen(utf8ToWChar(tempfilename).c_str(), "wb");
|
|
|
|
if(!fp) {
|
|
|
|
A2_LOG_ERROR(fmt("Cannot create cookie file %s",
|
|
|
|
utf8ToNative(filename).c_str()));
|
2009-06-21 10:26:14 +00:00
|
|
|
return false;
|
2009-05-22 14:51:57 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
for(std::deque<DomainEntry>::const_iterator i = domains_.begin(),
|
|
|
|
eoi = domains_.end(); i != eoi; ++i) {
|
2011-08-05 13:44:54 +00:00
|
|
|
if(!(*i).writeCookie(fp)) {
|
|
|
|
fclose(fp);
|
|
|
|
A2_LOG_ERROR(fmt("Failed to save cookies to %s",
|
|
|
|
utf8ToNative(filename).c_str()));
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-21 15:19:43 +00:00
|
|
|
}
|
2011-08-05 13:44:54 +00:00
|
|
|
if(fclose(fp) == EOF) {
|
|
|
|
A2_LOG_ERROR(fmt("Failed to save cookies to %s",
|
|
|
|
utf8ToNative(filename).c_str()));
|
2009-07-21 15:19:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-05-22 14:51:57 +00:00
|
|
|
}
|
2009-06-21 10:26:14 +00:00
|
|
|
if(File(tempfilename).renameTo(filename)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_ERROR(fmt("Could not rename file %s as %s",
|
2011-08-05 13:44:54 +00:00
|
|
|
utf8ToNative(tempfilename).c_str(),
|
|
|
|
utf8ToNative(filename).c_str()));
|
2009-06-21 10:26:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-05-22 14:51:57 +00:00
|
|
|
}
|
|
|
|
|
2008-08-27 16:04:36 +00:00
|
|
|
} // namespace aria2
|
2010-11-20 14:05:58 +00:00
|
|
|
|
|
|
|
namespace std {
|
|
|
|
template<>
|
|
|
|
void swap<aria2::CookieStorage::DomainEntry>
|
|
|
|
(aria2::CookieStorage::DomainEntry& a,
|
|
|
|
aria2::CookieStorage::DomainEntry& b)
|
|
|
|
{
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
} // namespace std
|
|
|
|
|