CookieStorage.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. #ifdef HAVE_SQLITE3
  49. # include "Sqlite3MozCookieParser.h"
  50. #endif // HAVE_SQLITE3
  51. namespace aria2 {
  52. CookieStorage::DomainEntry::DomainEntry
  53. (const std::string& domain):_key(domain)
  54. {
  55. std::reverse(_key.begin(), _key.end());
  56. }
  57. void CookieStorage::DomainEntry::updateLastAccess()
  58. {
  59. _lastAccess = time(0);
  60. }
  61. bool CookieStorage::DomainEntry::addCookie(const Cookie& cookie)
  62. {
  63. updateLastAccess();
  64. std::deque<Cookie>::iterator i = std::find(_cookies.begin(), _cookies.end(),
  65. cookie);
  66. if(i == _cookies.end()) {
  67. if(cookie.isExpired()) {
  68. return false;
  69. } else {
  70. if(_cookies.size() >= CookieStorage::MAX_COOKIE_PER_DOMAIN) {
  71. std::deque<Cookie>::iterator m = std::min_element
  72. (_cookies.begin(), _cookies.end(), LeastRecentAccess<Cookie>());
  73. *m = cookie;
  74. } else {
  75. _cookies.push_back(cookie);
  76. }
  77. return true;
  78. }
  79. } else if(cookie.isExpired()) {
  80. _cookies.erase(i);
  81. return false;
  82. } else {
  83. *i = cookie;
  84. return true;
  85. }
  86. }
  87. bool CookieStorage::DomainEntry::contains(const Cookie& cookie) const
  88. {
  89. return std::find(_cookies.begin(), _cookies.end(), cookie) != _cookies.end();
  90. }
  91. void CookieStorage::DomainEntry::writeCookie(std::ostream& o) const
  92. {
  93. for(std::deque<Cookie>::const_iterator i = _cookies.begin(),
  94. eoi = _cookies.end(); i != eoi; ++i) {
  95. o << (*i).toNsCookieFormat() << "\n";
  96. }
  97. }
  98. CookieStorage::CookieStorage():_logger(LogFactory::getInstance()) {}
  99. CookieStorage::~CookieStorage() {}
  100. // See CookieStorageTest::testDomainIsFull() in CookieStorageTest.cc
  101. static const size_t DOMAIN_EVICTION_TRIGGER = 600;
  102. static const double DOMAIN_EVICTION_RATE = 0.1;
  103. bool CookieStorage::store(const Cookie& cookie)
  104. {
  105. if(!cookie.good()) {
  106. return false;
  107. }
  108. if(_domains.size() >= DOMAIN_EVICTION_TRIGGER) {
  109. std::sort(_domains.begin(), _domains.end(),
  110. LeastRecentAccess<DomainEntry>());
  111. size_t delnum = (size_t)(_domains.size()*DOMAIN_EVICTION_RATE);
  112. _domains.erase(_domains.begin(), _domains.begin()+delnum);
  113. std::sort(_domains.begin(), _domains.end());
  114. }
  115. DomainEntry v(cookie.getDomain());
  116. std::deque<DomainEntry>::iterator i =
  117. std::lower_bound(_domains.begin(), _domains.end(), v);
  118. bool added = false;
  119. if(i != _domains.end() && (*i).getKey() == v.getKey()) {
  120. added = (*i).addCookie(cookie);
  121. } else {
  122. added = v.addCookie(cookie);
  123. if(added) {
  124. _domains.insert(i, v);
  125. }
  126. }
  127. return added;
  128. }
  129. bool CookieStorage::parseAndStore(const std::string& setCookieString,
  130. const std::string& requestHost,
  131. const std::string& requestPath)
  132. {
  133. Cookie cookie = _parser.parse(setCookieString, requestHost, requestPath);
  134. if(cookie.validate(requestHost, requestPath)) {
  135. return store(cookie);
  136. } else {
  137. return false;
  138. }
  139. }
  140. struct CookiePathDivider {
  141. Cookie _cookie;
  142. int _pathDepth;
  143. CookiePathDivider(const Cookie& cookie):_cookie(cookie)
  144. {
  145. std::vector<std::string> paths;
  146. util::split(_cookie.getPath(), std::back_inserter(paths), A2STR::SLASH_C);
  147. _pathDepth = paths.size();
  148. }
  149. };
  150. class CookiePathDividerConverter {
  151. public:
  152. CookiePathDivider operator()(const Cookie& cookie) const
  153. {
  154. return CookiePathDivider(cookie);
  155. }
  156. Cookie operator()(const CookiePathDivider& cookiePathDivider) const
  157. {
  158. return cookiePathDivider._cookie;
  159. }
  160. };
  161. class OrderByPathDepthDesc:public std::binary_function<Cookie, Cookie, bool> {
  162. public:
  163. bool operator()
  164. (const CookiePathDivider& lhs, const CookiePathDivider& rhs) const
  165. {
  166. // Sort by path-length.
  167. //
  168. // RFC2965 says: Note that the NAME=VALUE pair for the cookie with
  169. // the more specific Path attribute, /acme/ammo, comes before the
  170. // one with the less specific Path attribute, /acme. Further note
  171. // that the same cookie name appears more than once.
  172. //
  173. // Netscape spec says: When sending cookies to a server, all
  174. // cookies with a more specific path mapping should be sent before
  175. // cookies with less specific path mappings. For example, a cookie
  176. // "name1=foo" with a path mapping of "/" should be sent after a
  177. // cookie "name1=foo2" with a path mapping of "/bar" if they are
  178. // both to be sent.
  179. int comp = lhs._pathDepth-rhs._pathDepth;
  180. if(comp == 0) {
  181. return lhs._cookie.getCreationTime() < rhs._cookie.getCreationTime();
  182. } else {
  183. return comp > 0;
  184. }
  185. }
  186. };
  187. template<typename DomainInputIterator, typename CookieOutputIterator>
  188. static void searchCookieByDomainSuffix
  189. (const std::string& domain,
  190. DomainInputIterator first, DomainInputIterator last, CookieOutputIterator out,
  191. const std::string& requestHost,
  192. const std::string& requestPath,
  193. time_t date, bool secure)
  194. {
  195. CookieStorage::DomainEntry v(domain);
  196. std::deque<CookieStorage::DomainEntry>::iterator i =
  197. std::lower_bound(first, last, v);
  198. if(i != last && (*i).getKey() == v.getKey()) {
  199. (*i).updateLastAccess();
  200. (*i).findCookie(out, requestHost, requestPath, date, secure);
  201. }
  202. }
  203. bool CookieStorage::contains(const Cookie& cookie) const
  204. {
  205. CookieStorage::DomainEntry v(cookie.getDomain());
  206. std::deque<CookieStorage::DomainEntry>::const_iterator i =
  207. std::lower_bound(_domains.begin(), _domains.end(), v);
  208. if(i != _domains.end() && (*i).getKey() == v.getKey()) {
  209. return (*i).contains(cookie);
  210. } else {
  211. return false;
  212. }
  213. }
  214. std::vector<Cookie> CookieStorage::criteriaFind(const std::string& requestHost,
  215. const std::string& requestPath,
  216. time_t date, bool secure)
  217. {
  218. std::vector<Cookie> res;
  219. bool numericHost = util::isNumericHost(requestHost);
  220. searchCookieByDomainSuffix
  221. ((!numericHost && requestHost.find(A2STR::DOT_C) == std::string::npos)?
  222. requestHost+".local":requestHost,
  223. _domains.begin(), _domains.end(),
  224. std::back_inserter(res),
  225. requestHost, requestPath, date, secure);
  226. if(!numericHost) {
  227. std::string normRequestHost = Cookie::normalizeDomain(requestHost);
  228. std::vector<std::string> domainComponents;
  229. util::split(normRequestHost, std::back_inserter(domainComponents),
  230. A2STR::DOT_C);
  231. if(domainComponents.size() <= 1) {
  232. return res;
  233. }
  234. std::reverse(domainComponents.begin(), domainComponents.end());
  235. std::string domain = A2STR::DOT_C;
  236. domain += domainComponents[0];
  237. for(std::vector<std::string>::const_iterator di =
  238. domainComponents.begin()+1, eoi = domainComponents.end();
  239. di != eoi; ++di) {
  240. domain = strconcat(A2STR::DOT_C, *di, domain);
  241. searchCookieByDomainSuffix(domain, _domains.begin(), _domains.end(),
  242. std::back_inserter(res),
  243. normRequestHost, requestPath, date, secure);
  244. }
  245. }
  246. std::vector<CookiePathDivider> divs;
  247. std::transform(res.begin(), res.end(), std::back_inserter(divs),
  248. CookiePathDividerConverter());
  249. std::sort(divs.begin(), divs.end(), OrderByPathDepthDesc());
  250. std::transform(divs.begin(), divs.end(), res.begin(),
  251. CookiePathDividerConverter());
  252. return res;
  253. }
  254. size_t CookieStorage::size() const
  255. {
  256. size_t numCookie = 0;
  257. for(std::deque<DomainEntry>::const_iterator i = _domains.begin(),
  258. eoi = _domains.end(); i != eoi; ++i) {
  259. numCookie += (*i).countCookie();
  260. }
  261. return numCookie;
  262. }
  263. bool CookieStorage::load(const std::string& filename)
  264. {
  265. char header[16]; // "SQLite format 3" plus \0
  266. std::ifstream s(filename.c_str(), std::ios::binary);
  267. if(!s) {
  268. _logger->error("Failed to open cookie file %s", filename.c_str());
  269. return false;
  270. }
  271. s.get(header, sizeof(header));
  272. if(!s) {
  273. _logger->error("Failed to read header of cookie file %s",
  274. filename.c_str());
  275. return false;
  276. }
  277. try {
  278. if(std::string(header) == "SQLite format 3") {
  279. #ifdef HAVE_SQLITE3
  280. std::vector<Cookie> cookies = Sqlite3MozCookieParser().parse(filename);
  281. storeCookies(cookies.begin(), cookies.end());
  282. #else // !HAVE_SQLITE3
  283. throw DL_ABORT_EX
  284. ("Cannot read SQLite3 database because SQLite3 support is disabled by"
  285. " configuration.");
  286. #endif // !HAVE_SQLITE3
  287. } else {
  288. std::vector<Cookie> cookies = NsCookieParser().parse(filename);
  289. storeCookies(cookies.begin(), cookies.end());
  290. }
  291. return true;
  292. } catch(RecoverableException& e) {
  293. _logger->error("Failed to load cookies from %s", filename.c_str());
  294. return false;
  295. }
  296. }
  297. bool CookieStorage::saveNsFormat(const std::string& filename)
  298. {
  299. std::string tempfilename = filename+"__temp";
  300. {
  301. std::ofstream o(tempfilename.c_str(), std::ios::binary);
  302. if(!o) {
  303. _logger->error("Cannot create cookie file %s, cause %s",
  304. filename.c_str(), strerror(errno));
  305. return false;
  306. }
  307. for(std::deque<DomainEntry>::const_iterator i = _domains.begin(),
  308. eoi = _domains.end(); i != eoi; ++i) {
  309. (*i).writeCookie(o);
  310. }
  311. o.flush();
  312. if(!o) {
  313. _logger->error("Failed to save cookies to %s, cause %s",
  314. filename.c_str(), strerror(errno));
  315. return false;
  316. }
  317. }
  318. if(File(tempfilename).renameTo(filename)) {
  319. return true;
  320. } else {
  321. _logger->error("Could not rename file %s as %s",
  322. tempfilename.c_str(), filename.c_str());
  323. return false;
  324. }
  325. }
  326. } // namespace aria2