CookieStorage.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #ifdef HAVE_SQLITE3
  47. # include "Sqlite3MozCookieParser.h"
  48. #endif // HAVE_SQLITE3
  49. namespace aria2 {
  50. CookieStorage::CookieStorage():_logger(LogFactory::getInstance()) {}
  51. CookieStorage::~CookieStorage() {}
  52. bool CookieStorage::store(const Cookie& cookie)
  53. {
  54. if(!cookie.good()) {
  55. return false;
  56. }
  57. std::deque<Cookie>::iterator i = std::find(_cookies.begin(), _cookies.end(),
  58. cookie);
  59. if(i == _cookies.end()) {
  60. if(cookie.isExpired()) {
  61. return false;
  62. } else {
  63. _cookies.push_back(cookie);
  64. return true;
  65. }
  66. } else if(cookie.isExpired()) {
  67. _cookies.erase(i);
  68. return false;
  69. } else {
  70. *i = cookie;
  71. return true;
  72. }
  73. }
  74. void CookieStorage::storeCookies(const std::deque<Cookie>& cookies)
  75. {
  76. for(std::deque<Cookie>::const_iterator i = cookies.begin();
  77. i != cookies.end(); ++i) {
  78. store(*i);
  79. }
  80. }
  81. bool CookieStorage::parseAndStore(const std::string& setCookieString,
  82. const std::string& requestHost,
  83. const std::string& requestPath)
  84. {
  85. Cookie cookie = _parser.parse(setCookieString, requestHost, requestPath);
  86. if(cookie.validate(requestHost, requestPath)) {
  87. return store(cookie);
  88. } else {
  89. return false;
  90. }
  91. }
  92. class CriteriaMatch:public std::unary_function<Cookie, bool> {
  93. private:
  94. std::string _requestHost;
  95. std::string _requestPath;
  96. time_t _date;
  97. bool _secure;
  98. public:
  99. CriteriaMatch(const std::string& requestHost, const std::string& requestPath,
  100. time_t date, bool secure):
  101. _requestHost(requestHost),
  102. _requestPath(requestPath),
  103. _date(date),
  104. _secure(secure) {}
  105. bool operator()(const Cookie& cookie) const
  106. {
  107. return cookie.match(_requestHost, _requestPath, _date, _secure);
  108. }
  109. };
  110. class OrderByPathDesc:public std::binary_function<Cookie, Cookie, bool> {
  111. public:
  112. bool operator()(const Cookie& lhs, const Cookie& rhs) const
  113. {
  114. return lhs.getPath() > rhs.getPath();
  115. }
  116. };
  117. std::deque<Cookie> CookieStorage::criteriaFind(const std::string& requestHost,
  118. const std::string& requestPath,
  119. time_t date, bool secure) const
  120. {
  121. std::deque<Cookie> res;
  122. std::remove_copy_if(_cookies.begin(), _cookies.end(), std::back_inserter(res),
  123. std::not1(CriteriaMatch(requestHost, requestPath, date, secure)));
  124. std::sort(res.begin(), res.end(), OrderByPathDesc());
  125. return res;
  126. }
  127. size_t CookieStorage::size() const
  128. {
  129. return _cookies.size();
  130. }
  131. bool CookieStorage::load(const std::string& filename)
  132. {
  133. char header[16]; // "SQLite format 3" plus \0
  134. std::ifstream s(filename.c_str(), std::ios::binary);
  135. s.get(header, sizeof(header));
  136. if(!s) {
  137. _logger->error("Failed to read header of cookie file %s",
  138. filename.c_str());
  139. return false;
  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 DL_ABORT_EX
  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. return true;
  154. } catch(RecoverableException& e) {
  155. _logger->error("Failed to load cookies from %s", filename.c_str());
  156. return false;
  157. }
  158. }
  159. bool CookieStorage::saveNsFormat(const std::string& filename)
  160. {
  161. std::string tempfilename = filename+"__temp";
  162. std::ofstream o(tempfilename.c_str(), std::ios::binary);
  163. if(!o) {
  164. _logger->error("Cannot create cookie file %s, cause %s",
  165. filename.c_str(), strerror(errno));
  166. return false;
  167. }
  168. for(std::deque<Cookie>::const_iterator i = _cookies.begin();
  169. i != _cookies.end(); ++i) {
  170. o << (*i).toNsCookieFormat() << "\n";
  171. if(!o) {
  172. _logger->error("Failed to save cookies to %s", filename.c_str());
  173. return false;
  174. }
  175. }
  176. if(File(tempfilename).renameTo(filename)) {
  177. return true;
  178. } else {
  179. _logger->error("Could not rename file %s as %s",
  180. tempfilename.c_str(), filename.c_str());
  181. return false;
  182. }
  183. }
  184. } // namespace aria2