/* */ #include "Sqlite3CookieParser.h" #include #include #include "DlAbortEx.h" #include "util.h" #include "fmt.h" #include "A2STR.h" #include "cookie_helper.h" #ifndef HAVE_SQLITE3_OPEN_V2 # include "File.h" #endif // !HAVE_SQLITE3_OPEN_V2 namespace aria2 { Sqlite3CookieParser::Sqlite3CookieParser(const std::string& filename):db_(0) { int ret; #ifdef HAVE_SQLITE3_OPEN_V2 ret = sqlite3_open_v2(filename.c_str(), &db_, SQLITE_OPEN_READONLY, 0); #else // !HAVE_SQLITE3_OPEN_V2 if(!File(filename).isFile()) { return; } ret = sqlite3_open(filename.c_str(), &db_); #endif // !HAVE_SQLITE3_OPEN_V2 if(SQLITE_OK != ret) { sqlite3_close(db_); db_ = 0; } } Sqlite3CookieParser::~Sqlite3CookieParser() { sqlite3_close(db_); } namespace { std::string toString(const char* str) { if(str) { return str; } else { return A2STR::NIL; } } } // namespace namespace { bool parseTime(int64_t& time, const std::string& s) { if(!util::parseLLIntNoThrow(time, s)) { return false; } if(std::numeric_limits::max() < time) { time = std::numeric_limits::max(); } else if(std::numeric_limits::min() > time) { time = std::numeric_limits::min(); } return true; } } // namespace namespace { int cookieRowMapper(void* data, int columns, char** values, char** names) { if(columns != 7) { return 0; } std::vector& cookies = *reinterpret_cast*>(data); std::string cookieDomain = cookie::removePrecedingDots(toString(values[0])); std::string cookieName = toString(values[4]); std::string cookiePath = toString(values[1]); if(cookieName.empty() || cookieDomain.empty() || !cookie::goodPath(cookiePath)) { return 0; } int64_t expiryTime; if(!parseTime(expiryTime, toString(values[3]))) { return 0; } int64_t lastAccessTime; if(!parseTime(lastAccessTime, toString(values[6]))) { return 0; } Cookie c(cookieName, toString(values[5]), // value expiryTime, true, // persistent cookieDomain, util::isNumericHost(cookieDomain) || values[0][0] != '.', // hostOnly cookiePath, strcmp(toString(values[2]).c_str(), "1") == 0, //secure false, lastAccessTime // creation time. Set this later. ); cookies.push_back(c); return 0; } } // namespace void Sqlite3CookieParser::parse(std::vector& cookies) { if(!db_) { throw DL_ABORT_EX(fmt("SQLite3 database is not opened.")); } std::vector tcookies; char* sqlite3ErrMsg = 0; int ret = sqlite3_exec(db_, getQuery().c_str(), cookieRowMapper, &tcookies, &sqlite3ErrMsg); std::string errMsg; if(sqlite3ErrMsg) { errMsg = sqlite3ErrMsg; sqlite3_free(sqlite3ErrMsg); } if(SQLITE_OK != ret) { throw DL_ABORT_EX (fmt("Failed to read SQLite3 database: %s", errMsg.c_str())); } cookies.swap(tcookies); } } // namespace aria2