CookieStorage.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <vector>
  41. #include <set>
  42. #include <algorithm>
  43. #include "a2time.h"
  44. #include "Cookie.h"
  45. #include "a2functional.h"
  46. namespace aria2 {
  47. class BufferedFile;
  48. class CookieStorage {
  49. public:
  50. static const size_t MAX_COOKIE_PER_DOMAIN = 50;
  51. class DomainEntry {
  52. private:
  53. // This is reversed domain level string.
  54. // e.g. net.sourceforge.aria2
  55. // e.g. 192.168.0.1
  56. std::string key_;
  57. time_t lastAccessTime_;
  58. std::deque<Cookie> cookies_;
  59. public:
  60. DomainEntry(const std::string& domain);
  61. DomainEntry(const DomainEntry& c);
  62. ~DomainEntry();
  63. void swap(DomainEntry& c);
  64. DomainEntry& operator=(const DomainEntry& c);
  65. const std::string& getKey() const
  66. {
  67. return key_;
  68. }
  69. void findCookie
  70. (std::vector<Cookie>& out,
  71. const std::string& requestHost,
  72. const std::string& requestPath,
  73. time_t now, bool secure);
  74. size_t countCookie() const;
  75. bool addCookie(const Cookie& cookie, time_t now);
  76. void setLastAccessTime(time_t lastAccessTime)
  77. {
  78. lastAccessTime_ = lastAccessTime;
  79. }
  80. time_t getLastAccessTime() const
  81. {
  82. return lastAccessTime_;
  83. }
  84. bool writeCookie(BufferedFile& fp) const;
  85. bool contains(const Cookie& cookie) const;
  86. template<typename OutputIterator>
  87. OutputIterator dumpCookie(OutputIterator out) const
  88. {
  89. return std::copy(cookies_.begin(), cookies_.end(), out);
  90. }
  91. bool operator==(const DomainEntry& de) const;
  92. bool operator<(const DomainEntry& de) const;
  93. };
  94. private:
  95. typedef std::set<std::shared_ptr<DomainEntry>,
  96. DerefLess<std::shared_ptr<DomainEntry> > > DomainEntrySet;
  97. DomainEntrySet domains_;
  98. template<typename InputIterator>
  99. void storeCookies(InputIterator first, InputIterator last, time_t now)
  100. {
  101. for(; first != last; ++first) {
  102. store(*first, now);
  103. }
  104. }
  105. public:
  106. CookieStorage();
  107. ~CookieStorage();
  108. // Returns true if cookie is stored or updated existing cookie.
  109. // Returns false if cookie is expired. now is used as last access
  110. // time.
  111. bool store(const Cookie& cookie, time_t now);
  112. // Returns true if cookie is stored or updated existing cookie.
  113. // Otherwise, returns false. now is used as creation time and last
  114. // access time.
  115. bool parseAndStore
  116. (const std::string& setCookieString,
  117. const std::string& requestHost,
  118. const std::string& requestPath,
  119. time_t now);
  120. // Finds cookies matched with given criteria and returns them.
  121. // Matched cookies' lastAccess_ property is updated.
  122. std::vector<Cookie> criteriaFind(const std::string& requestHost,
  123. const std::string& requestPath,
  124. time_t now, bool secure);
  125. // Loads Cookies from file denoted by filename. If compiled with
  126. // libsqlite3, this method automatically detects the specified file
  127. // is sqlite3 or just plain text file and calls appropriate parser
  128. // implementation class. If Cookies are successfully loaded, this
  129. // method returns true. Otherwise, this method returns false. now
  130. // is used as creation time and last access time.
  131. bool load(const std::string& filename, time_t now);
  132. // Saves Cookies in Netspace format which is used in
  133. // Firefox1.2/Netscape/Mozilla. If Cookies are successfully saved,
  134. // this method returns true, otherwise returns false.
  135. bool saveNsFormat(const std::string& filename);
  136. // Returns the number of cookies this object stores.
  137. size_t size() const;
  138. // Returns true if this object contains a cookie x where x == cookie
  139. // satisfies.
  140. bool contains(const Cookie& cookie) const;
  141. // Searches Cookie using given domain, requestHost, requestPath,
  142. // current time and secure flag. The found Cookies are stored in
  143. // out.
  144. void searchCookieByDomainSuffix
  145. (std::vector<Cookie>& out,
  146. const std::string& domain,
  147. const std::string& requestHost,
  148. const std::string& requestPath,
  149. time_t now, bool secure);
  150. template<typename OutputIterator>
  151. OutputIterator dumpCookie(OutputIterator out) const
  152. {
  153. for(DomainEntrySet::iterator i = domains_.begin(), eoi = domains_.end();
  154. i != eoi; ++i) {
  155. out = (*i)->dumpCookie(out);
  156. }
  157. return out;
  158. }
  159. };
  160. void swap(CookieStorage::DomainEntry& a, CookieStorage::DomainEntry& b);
  161. } // namespace aria2
  162. namespace std {
  163. template<>
  164. void swap<aria2::CookieStorage::DomainEntry>
  165. (aria2::CookieStorage::DomainEntry& a,
  166. aria2::CookieStorage::DomainEntry& b);
  167. } // namespace std
  168. #endif // D_COOKIE_STORAGE_H