CookieStorage.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "Util.h"
  37. #include "LogFactory.h"
  38. #include "Logger.h"
  39. #include "RecoverableException.h"
  40. #include "StringFormat.h"
  41. #include "NsCookieParser.h"
  42. #ifdef HAVE_SQLITE3
  43. # include "Sqlite3MozCookieParser.h"
  44. #endif // HAVE_SQLITE3
  45. #include <algorithm>
  46. #include <fstream>
  47. namespace aria2 {
  48. CookieStorage::CookieStorage():_logger(LogFactory::getInstance()) {}
  49. CookieStorage::~CookieStorage() {}
  50. bool CookieStorage::store(const Cookie& cookie)
  51. {
  52. if(!cookie.good()) {
  53. return false;
  54. }
  55. std::deque<Cookie>::iterator i = std::find(_cookies.begin(), _cookies.end(),
  56. cookie);
  57. if(i == _cookies.end()) {
  58. if(cookie.isExpired()) {
  59. return false;
  60. } else {
  61. _cookies.push_back(cookie);
  62. return true;
  63. }
  64. } else if(cookie.isExpired()) {
  65. _cookies.erase(i);
  66. return false;
  67. } else {
  68. *i = cookie;
  69. return true;
  70. }
  71. }
  72. void CookieStorage::storeCookies(const std::deque<Cookie>& cookies)
  73. {
  74. for(std::deque<Cookie>::const_iterator i = cookies.begin();
  75. i != cookies.end(); ++i) {
  76. store(*i);
  77. }
  78. }
  79. bool CookieStorage::parseAndStore(const std::string& setCookieString,
  80. const std::string& requestHost,
  81. const std::string& requestPath)
  82. {
  83. Cookie cookie = _parser.parse(setCookieString, requestHost, requestPath);
  84. if(cookie.validate(requestHost, requestPath)) {
  85. return store(cookie);
  86. } else {
  87. return false;
  88. }
  89. }
  90. class CriteriaMatch:public std::unary_function<Cookie, bool> {
  91. private:
  92. std::string _requestHost;
  93. std::string _requestPath;
  94. time_t _date;
  95. bool _secure;
  96. public:
  97. CriteriaMatch(const std::string& requestHost, const std::string& requestPath,
  98. time_t date, bool secure):
  99. _requestHost(requestHost),
  100. _requestPath(requestPath),
  101. _date(date),
  102. _secure(secure) {}
  103. bool operator()(const Cookie& cookie) const
  104. {
  105. return cookie.match(_requestHost, _requestPath, _date, _secure);
  106. }
  107. };
  108. class OrderByPathDesc:public std::binary_function<Cookie, Cookie, bool> {
  109. public:
  110. bool operator()(const Cookie& lhs, const Cookie& rhs) const
  111. {
  112. return lhs.getPath() > rhs.getPath();
  113. }
  114. };
  115. std::deque<Cookie> CookieStorage::criteriaFind(const std::string& requestHost,
  116. const std::string& requestPath,
  117. time_t date, bool secure) const
  118. {
  119. std::deque<Cookie> res;
  120. std::remove_copy_if(_cookies.begin(), _cookies.end(), std::back_inserter(res),
  121. std::not1(CriteriaMatch(requestHost, requestPath, date, secure)));
  122. std::sort(res.begin(), res.end(), OrderByPathDesc());
  123. return res;
  124. }
  125. size_t CookieStorage::size() const
  126. {
  127. return _cookies.size();
  128. }
  129. void CookieStorage::load(const std::string& filename)
  130. {
  131. char header[16]; // "SQLite format 3" plus \0
  132. {
  133. std::ifstream s(filename.c_str());
  134. s.get(header, sizeof(header));
  135. if(s.bad()) {
  136. throw RecoverableException
  137. (StringFormat("Failed to read header of cookie file %s",
  138. filename.c_str()).str());
  139. }
  140. }
  141. try {
  142. if(std::string(header) == "SQLite format 3") {
  143. #ifdef HAVE_SQLITE3
  144. storeCookies(Sqlite3MozCookieParser().parse(filename));
  145. #else // !HAVE_SQLITE3
  146. throw RecoverableException
  147. ("Cannot read SQLite3 database because SQLite3 support is disabled by"
  148. " configuration.");
  149. #endif // !HAVE_SQLITE3
  150. } else {
  151. storeCookies(NsCookieParser().parse(filename));
  152. }
  153. } catch(RecoverableException& e) {
  154. throw RecoverableException
  155. (StringFormat("Failed to load cookies from %s", filename.c_str()).str(),
  156. e);
  157. }
  158. }
  159. } // namespace aria2