CookieStorage.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #include "CookieParser.h"
  45. namespace aria2 {
  46. class Logger;
  47. class CookieStorage {
  48. public:
  49. static const size_t MAX_COOKIE_PER_DOMAIN = 50;
  50. class DomainEntry {
  51. private:
  52. std::string key_;
  53. time_t lastAccess_;
  54. std::deque<Cookie> cookies_;
  55. public:
  56. DomainEntry(const std::string& domain);
  57. const std::string& getKey() const
  58. {
  59. return key_;
  60. }
  61. template<typename OutputIterator>
  62. OutputIterator findCookie
  63. (OutputIterator out,
  64. const std::string& requestHost,
  65. const std::string& requestPath,
  66. time_t date, bool secure)
  67. {
  68. for(std::deque<Cookie>::iterator i = cookies_.begin();
  69. i != cookies_.end(); ++i) {
  70. if((*i).match(requestHost, requestPath, date, secure)) {
  71. (*i).updateLastAccess();
  72. out++ = *i;
  73. }
  74. }
  75. return out;
  76. }
  77. size_t countCookie() const
  78. {
  79. return cookies_.size();
  80. }
  81. bool addCookie(const Cookie& cookie);
  82. void updateLastAccess();
  83. time_t getLastAccess() const
  84. {
  85. return lastAccess_;
  86. }
  87. void writeCookie(std::ostream& o) const;
  88. bool contains(const Cookie& cookie) const;
  89. template<typename OutputIterator>
  90. OutputIterator dumpCookie(OutputIterator out) const
  91. {
  92. return std::copy(cookies_.begin(), cookies_.end(), out);
  93. }
  94. bool operator<(const DomainEntry& de) const
  95. {
  96. return key_ < de.key_;
  97. }
  98. };
  99. private:
  100. std::deque<DomainEntry> domains_;
  101. CookieParser parser_;
  102. Logger* logger_;
  103. template<typename InputIterator>
  104. void storeCookies(InputIterator first, InputIterator last)
  105. {
  106. for(; first != last; ++first) {
  107. store(*first);
  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.
  115. bool store(const Cookie& cookie);
  116. // Returns true if cookie is stored or updated existing cookie.
  117. // Otherwise, returns false.
  118. bool parseAndStore(const std::string& setCookieString,
  119. const std::string& requestHost,
  120. const std::string& requestPath);
  121. // Finds cookies matched with given criteria and returns them.
  122. // Matched cookies' lastAccess_ property is updated.
  123. std::vector<Cookie> criteriaFind(const std::string& requestHost,
  124. const std::string& requestPath,
  125. time_t date, bool secure);
  126. // Loads Cookies from file denoted by filename. If compiled with
  127. // libsqlite3, this method automatically detects the specified file
  128. // is sqlite3 or just plain text file and calls appropriate parser
  129. // implementation class. If Cookies are successfully loaded, this
  130. // method returns true. Otherwise, this method returns false.
  131. bool load(const std::string& filename);
  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. template<typename OutputIterator>
  142. OutputIterator dumpCookie(OutputIterator out) const
  143. {
  144. for(std::deque<DomainEntry>::const_iterator i = domains_.begin(),
  145. eoi = domains_.end(); i != eoi; ++i) {
  146. out = (*i).dumpCookie(out);
  147. }
  148. return out;
  149. }
  150. };
  151. } // namespace aria2
  152. #endif // _D_COOKIE_STORAGE_H_