CookieStorage.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #ifndef _D_COOKIE_STORAGE_H_
  36. #define _D_COOKIE_STORAGE_H_
  37. #include "common.h"
  38. #include <string>
  39. #include <deque>
  40. #include <algorithm>
  41. #include "a2time.h"
  42. #include "Cookie.h"
  43. #include "CookieParser.h"
  44. namespace aria2 {
  45. class Logger;
  46. class CookieStorage {
  47. public:
  48. static const size_t MAX_COOKIE_PER_DOMAIN = 50;
  49. class DomainEntry {
  50. private:
  51. std::string _key;
  52. time_t _lastAccess;
  53. std::deque<Cookie> _cookies;
  54. public:
  55. DomainEntry(const std::string& domain);
  56. const std::string& getKey() const
  57. {
  58. return _key;
  59. }
  60. template<typename OutputIterator>
  61. OutputIterator findCookie
  62. (OutputIterator out,
  63. const std::string& requestHost,
  64. const std::string& requestPath,
  65. time_t date, bool secure)
  66. {
  67. for(std::deque<Cookie>::iterator i = _cookies.begin();
  68. i != _cookies.end(); ++i) {
  69. if((*i).match(requestHost, requestPath, date, secure)) {
  70. (*i).updateLastAccess();
  71. out++ = *i;
  72. }
  73. }
  74. return out;
  75. }
  76. size_t countCookie() const
  77. {
  78. return _cookies.size();
  79. }
  80. bool addCookie(const Cookie& cookie);
  81. void updateLastAccess();
  82. time_t getLastAccess() const
  83. {
  84. return _lastAccess;
  85. }
  86. void writeCookie(std::ostream& o) const;
  87. bool contains(const Cookie& cookie) const;
  88. template<typename OutputIterator>
  89. OutputIterator dumpCookie(OutputIterator out) const
  90. {
  91. return std::copy(_cookies.begin(), _cookies.end(), out);
  92. }
  93. bool operator<(const DomainEntry& de) const
  94. {
  95. return _key < de._key;
  96. }
  97. };
  98. private:
  99. std::deque<DomainEntry> _domains;
  100. CookieParser _parser;
  101. Logger* _logger;
  102. void storeCookies(const std::deque<Cookie>& cookies);
  103. public:
  104. CookieStorage();
  105. ~CookieStorage();
  106. // Returns true if cookie is stored or updated existing cookie.
  107. // Returns false if cookie is expired.
  108. bool store(const Cookie& cookie);
  109. // Returns true if cookie is stored or updated existing cookie.
  110. // Otherwise, returns false.
  111. bool parseAndStore(const std::string& setCookieString,
  112. const std::string& requestHost,
  113. const std::string& requestPath);
  114. // Finds cookies matched with given criteria and returns them.
  115. // Matched cookies' _lastAccess property is updated.
  116. std::deque<Cookie> criteriaFind(const std::string& requestHost,
  117. const std::string& requestPath,
  118. time_t date, bool secure);
  119. // Loads Cookies from file denoted by filename. If compiled with
  120. // libsqlite3, this method automatically detects the specified file
  121. // is sqlite3 or just plain text file and calls appropriate parser
  122. // implementation class. If Cookies are successfully loaded, this
  123. // method returns true. Otherwise, this method returns false.
  124. bool load(const std::string& filename);
  125. // Saves Cookies in Netspace format which is used in
  126. // Firefox1.2/Netscape/Mozilla. If Cookies are successfully saved,
  127. // this method returns true, otherwise returns false.
  128. bool saveNsFormat(const std::string& filename);
  129. // Returns the number of cookies this object stores.
  130. size_t size() const;
  131. // Returns true if this object contains a cookie x where x == cookie
  132. // satisfies.
  133. bool contains(const Cookie& cookie) const;
  134. template<typename OutputIterator>
  135. OutputIterator dumpCookie(OutputIterator out) const
  136. {
  137. for(std::deque<DomainEntry>::const_iterator i = _domains.begin();
  138. i != _domains.end(); ++i) {
  139. out = (*i).dumpCookie(out);
  140. }
  141. return out;
  142. }
  143. };
  144. } // namespace aria2
  145. #endif // _D_COOKIE_STORAGE_H_