CookieStorage.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "CookieStorage.h"
  36. #include <cstring>
  37. #include <algorithm>
  38. #include <fstream>
  39. #include "util.h"
  40. #include "LogFactory.h"
  41. #include "Logger.h"
  42. #include "DlAbortEx.h"
  43. #include "StringFormat.h"
  44. #include "NsCookieParser.h"
  45. #include "File.h"
  46. #include "a2functional.h"
  47. #include "A2STR.h"
  48. #include "message.h"
  49. #ifdef HAVE_SQLITE3
  50. # include "Sqlite3CookieParserImpl.h"
  51. #endif // HAVE_SQLITE3
  52. namespace aria2 {
  53. CookieStorage::DomainEntry::DomainEntry
  54. (const std::string& domain):key_(domain)
  55. {
  56. std::reverse(key_.begin(), key_.end());
  57. }
  58. void CookieStorage::DomainEntry::updateLastAccess()
  59. {
  60. lastAccess_ = time(0);
  61. }
  62. bool CookieStorage::DomainEntry::addCookie(const Cookie& cookie)
  63. {
  64. updateLastAccess();
  65. std::deque<Cookie>::iterator i = std::find(cookies_.begin(), cookies_.end(),
  66. cookie);
  67. if(i == cookies_.end()) {
  68. if(cookie.isExpired()) {
  69. return false;
  70. } else {
  71. if(cookies_.size() >= CookieStorage::MAX_COOKIE_PER_DOMAIN) {
  72. std::deque<Cookie>::iterator m = std::min_element
  73. (cookies_.begin(), cookies_.end(), LeastRecentAccess<Cookie>());
  74. *m = cookie;
  75. } else {
  76. cookies_.push_back(cookie);
  77. }
  78. return true;
  79. }
  80. } else if(cookie.isExpired()) {
  81. cookies_.erase(i);
  82. return false;
  83. } else {
  84. *i = cookie;
  85. return true;
  86. }
  87. }
  88. bool CookieStorage::DomainEntry::contains(const Cookie& cookie) const
  89. {
  90. return std::find(cookies_.begin(), cookies_.end(), cookie) != cookies_.end();
  91. }
  92. void CookieStorage::DomainEntry::writeCookie(std::ostream& o) const
  93. {
  94. for(std::deque<Cookie>::const_iterator i = cookies_.begin(),
  95. eoi = cookies_.end(); i != eoi; ++i) {
  96. o << (*i).toNsCookieFormat() << "\n";
  97. }
  98. }
  99. CookieStorage::CookieStorage():logger_(LogFactory::getInstance()) {}
  100. CookieStorage::~CookieStorage() {}
  101. // See CookieStorageTest::testDomainIsFull() in CookieStorageTest.cc
  102. static const size_t DOMAIN_EVICTION_TRIGGER = 2000;
  103. static const double DOMAIN_EVICTION_RATE = 0.1;
  104. bool CookieStorage::store(const Cookie& cookie)
  105. {
  106. if(!cookie.good()) {
  107. return false;
  108. }
  109. if(domains_.size() >= DOMAIN_EVICTION_TRIGGER) {
  110. std::sort(domains_.begin(), domains_.end(),
  111. LeastRecentAccess<DomainEntry>());
  112. size_t delnum = (size_t)(domains_.size()*DOMAIN_EVICTION_RATE);
  113. domains_.erase(domains_.begin(), domains_.begin()+delnum);
  114. std::sort(domains_.begin(), domains_.end());
  115. }
  116. DomainEntry v(cookie.getDomain());
  117. std::deque<DomainEntry>::iterator i =
  118. std::lower_bound(domains_.begin(), domains_.end(), v);
  119. bool added = false;
  120. if(i != domains_.end() && (*i).getKey() == v.getKey()) {
  121. added = (*i).addCookie(cookie);
  122. } else {
  123. added = v.addCookie(cookie);
  124. if(added) {
  125. domains_.insert(i, v);
  126. }
  127. }
  128. return added;
  129. }
  130. bool CookieStorage::parseAndStore(const std::string& setCookieString,
  131. const std::string& requestHost,
  132. const std::string& requestPath)
  133. {
  134. Cookie cookie = parser_.parse(setCookieString, requestHost, requestPath);
  135. if(cookie.validate(requestHost, requestPath)) {
  136. return store(cookie);
  137. } else {
  138. return false;
  139. }
  140. }
  141. struct CookiePathDivider {
  142. Cookie cookie_;
  143. int pathDepth_;
  144. CookiePathDivider(const Cookie& cookie):cookie_(cookie)
  145. {
  146. std::vector<std::string> paths;
  147. util::split(cookie_.getPath(), std::back_inserter(paths), A2STR::SLASH_C);
  148. pathDepth_ = paths.size();
  149. }
  150. };
  151. class CookiePathDividerConverter {
  152. public:
  153. CookiePathDivider operator()(const Cookie& cookie) const
  154. {
  155. return CookiePathDivider(cookie);
  156. }
  157. Cookie operator()(const CookiePathDivider& cookiePathDivider) const
  158. {
  159. return cookiePathDivider.cookie_;
  160. }
  161. };
  162. class OrderByPathDepthDesc:public std::binary_function<Cookie, Cookie, bool> {
  163. public:
  164. bool operator()
  165. (const CookiePathDivider& lhs, const CookiePathDivider& rhs) const
  166. {
  167. // Sort by path-length.
  168. //
  169. // RFC2965 says: Note that the NAME=VALUE pair for the cookie with
  170. // the more specific Path attribute, /acme/ammo, comes before the
  171. // one with the less specific Path attribute, /acme. Further note
  172. // that the same cookie name appears more than once.
  173. //
  174. // Netscape spec says: When sending cookies to a server, all
  175. // cookies with a more specific path mapping should be sent before
  176. // cookies with less specific path mappings. For example, a cookie
  177. // "name1=foo" with a path mapping of "/" should be sent after a
  178. // cookie "name1=foo2" with a path mapping of "/bar" if they are
  179. // both to be sent.
  180. int comp = lhs.pathDepth_-rhs.pathDepth_;
  181. if(comp == 0) {
  182. return lhs.cookie_.getCreationTime() < rhs.cookie_.getCreationTime();
  183. } else {
  184. return comp > 0;
  185. }
  186. }
  187. };
  188. template<typename DomainInputIterator, typename CookieOutputIterator>
  189. static void searchCookieByDomainSuffix
  190. (const std::string& domain,
  191. DomainInputIterator first, DomainInputIterator last, CookieOutputIterator out,
  192. const std::string& requestHost,
  193. const std::string& requestPath,
  194. time_t date, bool secure)
  195. {
  196. CookieStorage::DomainEntry v(domain);
  197. std::deque<CookieStorage::DomainEntry>::iterator i =
  198. std::lower_bound(first, last, v);
  199. if(i != last && (*i).getKey() == v.getKey()) {
  200. (*i).updateLastAccess();
  201. (*i).findCookie(out, requestHost, requestPath, date, secure);
  202. }
  203. }
  204. bool CookieStorage::contains(const Cookie& cookie) const
  205. {
  206. CookieStorage::DomainEntry v(cookie.getDomain());
  207. std::deque<CookieStorage::DomainEntry>::const_iterator i =
  208. std::lower_bound(domains_.begin(), domains_.end(), v);
  209. if(i != domains_.end() && (*i).getKey() == v.getKey()) {
  210. return (*i).contains(cookie);
  211. } else {
  212. return false;
  213. }
  214. }
  215. std::vector<Cookie> CookieStorage::criteriaFind(const std::string& requestHost,
  216. const std::string& requestPath,
  217. time_t date, bool secure)
  218. {
  219. std::vector<Cookie> res;
  220. bool numericHost = util::isNumericHost(requestHost);
  221. searchCookieByDomainSuffix
  222. ((!numericHost && requestHost.find(A2STR::DOT_C) == std::string::npos)?
  223. requestHost+".local":requestHost,
  224. domains_.begin(), domains_.end(),
  225. std::back_inserter(res),
  226. requestHost, requestPath, date, secure);
  227. if(!numericHost) {
  228. std::string normRequestHost = Cookie::normalizeDomain(requestHost);
  229. std::vector<std::string> domainComponents;
  230. util::split(normRequestHost, std::back_inserter(domainComponents),
  231. A2STR::DOT_C);
  232. if(domainComponents.size() <= 1) {
  233. return res;
  234. }
  235. std::reverse(domainComponents.begin(), domainComponents.end());
  236. std::string domain = A2STR::DOT_C;
  237. domain += domainComponents[0];
  238. for(std::vector<std::string>::const_iterator di =
  239. domainComponents.begin()+1, eoi = domainComponents.end();
  240. di != eoi; ++di) {
  241. domain = strconcat(A2STR::DOT_C, *di, domain);
  242. searchCookieByDomainSuffix(domain, domains_.begin(), domains_.end(),
  243. std::back_inserter(res),
  244. normRequestHost, requestPath, date, secure);
  245. }
  246. }
  247. std::vector<CookiePathDivider> divs;
  248. std::transform(res.begin(), res.end(), std::back_inserter(divs),
  249. CookiePathDividerConverter());
  250. std::sort(divs.begin(), divs.end(), OrderByPathDepthDesc());
  251. std::transform(divs.begin(), divs.end(), res.begin(),
  252. CookiePathDividerConverter());
  253. return res;
  254. }
  255. size_t CookieStorage::size() const
  256. {
  257. size_t numCookie = 0;
  258. for(std::deque<DomainEntry>::const_iterator i = domains_.begin(),
  259. eoi = domains_.end(); i != eoi; ++i) {
  260. numCookie += (*i).countCookie();
  261. }
  262. return numCookie;
  263. }
  264. bool CookieStorage::load(const std::string& filename)
  265. {
  266. char header[16]; // "SQLite format 3" plus \0
  267. std::ifstream s(filename.c_str(), std::ios::binary);
  268. if(!s) {
  269. logger_->error("Failed to open cookie file %s", filename.c_str());
  270. return false;
  271. }
  272. s.get(header, sizeof(header));
  273. if(!s) {
  274. logger_->error("Failed to read header of cookie file %s",
  275. filename.c_str());
  276. return false;
  277. }
  278. try {
  279. if(std::string(header) == "SQLite format 3") {
  280. #ifdef HAVE_SQLITE3
  281. std::vector<Cookie> cookies;
  282. try {
  283. Sqlite3MozCookieParser(filename).parse(cookies);
  284. } catch(RecoverableException& e) {
  285. if(logger_->info()) {
  286. logger_->info(EX_EXCEPTION_CAUGHT, e);
  287. logger_->info("This does not look like Firefox3 cookie file."
  288. " Retrying, assuming it is Chromium cookie file.");
  289. }
  290. // Try chrome cookie format
  291. Sqlite3ChromiumCookieParser(filename).parse(cookies);
  292. }
  293. storeCookies(cookies.begin(), cookies.end());
  294. #else // !HAVE_SQLITE3
  295. throw DL_ABORT_EX
  296. ("Cannot read SQLite3 database because SQLite3 support is disabled by"
  297. " configuration.");
  298. #endif // !HAVE_SQLITE3
  299. } else {
  300. std::vector<Cookie> cookies = NsCookieParser().parse(filename);
  301. storeCookies(cookies.begin(), cookies.end());
  302. }
  303. return true;
  304. } catch(RecoverableException& e) {
  305. logger_->error("Failed to load cookies from %s", filename.c_str());
  306. return false;
  307. }
  308. }
  309. bool CookieStorage::saveNsFormat(const std::string& filename)
  310. {
  311. std::string tempfilename = filename+"__temp";
  312. {
  313. std::ofstream o(tempfilename.c_str(), std::ios::binary);
  314. if(!o) {
  315. logger_->error("Cannot create cookie file %s, cause %s",
  316. filename.c_str(), strerror(errno));
  317. return false;
  318. }
  319. for(std::deque<DomainEntry>::const_iterator i = domains_.begin(),
  320. eoi = domains_.end(); i != eoi; ++i) {
  321. (*i).writeCookie(o);
  322. }
  323. o.flush();
  324. if(!o) {
  325. logger_->error("Failed to save cookies to %s, cause %s",
  326. filename.c_str(), strerror(errno));
  327. return false;
  328. }
  329. }
  330. if(File(tempfilename).renameTo(filename)) {
  331. return true;
  332. } else {
  333. logger_->error("Could not rename file %s as %s",
  334. tempfilename.c_str(), filename.c_str());
  335. return false;
  336. }
  337. }
  338. } // namespace aria2