CookieStorage.h 5.9 KB

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