CookieStorage.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2013 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 <unordered_map>
  44. #include "a2time.h"
  45. #include "Cookie.h"
  46. #include "a2functional.h"
  47. namespace aria2 {
  48. class BufferedFile;
  49. // This object represents one domain label.
  50. class DomainNode {
  51. public:
  52. DomainNode(std::string label, DomainNode* parent);
  53. // Stores the matching cookies in |out|. The |now| is used to update
  54. // the last access time of this node.
  55. void findCookie(std::vector<const Cookie*>& out,
  56. const std::string& requestHost,
  57. const std::string& requestPath, time_t now, bool secure);
  58. // Returns the number of cookies this node has.
  59. size_t countCookie() const;
  60. // Add |cookie| using update time |now|. Returns true if the
  61. // function succeeds.
  62. bool addCookie(std::unique_ptr<Cookie> cookie, time_t now);
  63. // Sets the last access time of this node.
  64. void setLastAccessTime(time_t lastAccessTime);
  65. // Returns the last access time of this node.
  66. time_t getLastAccessTime() const;
  67. // Sets the time |t| as a time used as key in LRU tracker.
  68. void setLruAccessTime(time_t t);
  69. time_t getLruAccessTime() const;
  70. bool writeCookie(BufferedFile& fp) const;
  71. // Returns true if this node contains the |cookie|.
  72. bool contains(const Cookie& cookie) const;
  73. // Returns true if this node contains no cookie.
  74. bool empty() const;
  75. // Returns true if this node has any next nodes.
  76. bool hasNext() const;
  77. // Returns the parent node. If this is the root node, returns
  78. // nullptr.
  79. DomainNode* getParent() const;
  80. // Removes the child node |node|. Nothing happens if |node| is not a
  81. // child of this node.
  82. void removeNode(DomainNode* node);
  83. // Returns the child node having label |label. Returns nullptr if
  84. // there is no such node.
  85. DomainNode* findNext(const std::string& label) const;
  86. // Add the |node| as a child using label |label and returns the raw
  87. // pointer of |node|.
  88. DomainNode* addNext(std::string label, std::unique_ptr<DomainNode> node);
  89. // Returns the |label|.
  90. const std::string& getLabel() const;
  91. // Deletes all cookies this node has.
  92. void clearCookie();
  93. // Returns value set by setInLru(). This is typically used to know
  94. // this node is tracked by LRU tracker or not.
  95. bool getInLru() const;
  96. void setInLru(bool f);
  97. template <typename OutputIterator>
  98. OutputIterator dumpCookie(OutputIterator out) const
  99. {
  100. if (cookies_) {
  101. for (auto& c : *cookies_) {
  102. out++ = c.get();
  103. }
  104. }
  105. return out;
  106. }
  107. private:
  108. std::string label_;
  109. DomainNode* parent_;
  110. time_t lastAccessTime_;
  111. time_t lruAccessTime_;
  112. bool inLru_;
  113. std::unique_ptr<std::deque<std::unique_ptr<Cookie>>> cookies_;
  114. // domain label string to DomainNode
  115. // e.g. net, sourceforge
  116. // For numerical addresses, this is address itself.
  117. // e.g. 192.168.0.1
  118. std::unordered_map<std::string, std::unique_ptr<DomainNode>> next_;
  119. };
  120. class CookieStorage {
  121. public:
  122. static const size_t MAX_COOKIE_PER_DOMAIN = 50;
  123. private:
  124. // typedef std::set<std::shared_ptr<DomainEntry>,
  125. // DerefLess<std::shared_ptr<DomainEntry> > > DomainEntrySet;
  126. // DomainEntrySet domains_;
  127. public:
  128. CookieStorage();
  129. // Returns true if cookie is stored or updated existing cookie.
  130. // Returns false if cookie is expired. now is used as last access
  131. // time.
  132. bool store(std::unique_ptr<Cookie> cookie, time_t now);
  133. // Returns true if cookie is stored or updated existing cookie.
  134. // Otherwise, returns false. now is used as creation time and last
  135. // access time.
  136. bool parseAndStore(const std::string& setCookieString,
  137. const std::string& requestHost,
  138. const std::string& requestPath, time_t now);
  139. // Finds cookies matched with given criteria and returns them.
  140. // Matched cookies' lastAccess_ property is updated.
  141. std::vector<const Cookie*> criteriaFind(const std::string& requestHost,
  142. const std::string& requestPath,
  143. time_t now, bool secure);
  144. // Loads Cookies from file denoted by filename. If compiled with
  145. // libsqlite3, this method automatically detects the specified file
  146. // is sqlite3 or just plain text file and calls appropriate parser
  147. // implementation class. If Cookies are successfully loaded, this
  148. // method returns true. Otherwise, this method returns false. now
  149. // is used as creation time and last access time.
  150. bool load(const std::string& filename, time_t now);
  151. // Saves Cookies in Netspace format which is used in
  152. // Firefox1.2/Netscape/Mozilla. If Cookies are successfully saved,
  153. // this method returns true, otherwise returns false.
  154. bool saveNsFormat(const std::string& filename);
  155. // Returns the number of cookies this object stores.
  156. size_t size() const;
  157. // Returns true if this object contains a cookie x where x == cookie
  158. // satisfies.
  159. bool contains(const Cookie& cookie) const;
  160. template <typename OutputIterator>
  161. OutputIterator dumpCookie(OutputIterator out) const
  162. {
  163. for (auto& i : lruTracker_) {
  164. out = i.second->dumpCookie(out);
  165. }
  166. return out;
  167. }
  168. // Force eviction of delnum nodes. Exposed for unittest
  169. void evictNode(size_t delnum);
  170. // Returns size of LRU tracker. Exposed for unittest
  171. size_t getLruTrackerSize() const;
  172. // Returns root node. Exposed for unittest
  173. const DomainNode* getRootNode() const;
  174. private:
  175. template <typename InputIterator>
  176. void storeCookies(InputIterator first, InputIterator last, time_t now)
  177. {
  178. for (; first != last; ++first) {
  179. store(*first, now);
  180. }
  181. }
  182. void updateLru(DomainNode* node, time_t now);
  183. // rootNode_ is a root node of tree structure of reversed domain
  184. // labels. rootNode_ always contains no cookie. It has the child
  185. // nodes of the top level domain label (e.g., net, com and org). And
  186. // those top level domain nodes have 2nd domain label (e.g.,
  187. // sourceforge, github), and so on. The numeric host name are always
  188. // stored as a child node of rootNode_. So the domain name of a
  189. // particular node is constructed as follows. First traverse the
  190. // target node from root node. The concatenation of the visited
  191. // node's label in the reverse order, delimited by ".", is the
  192. std::unique_ptr<DomainNode> rootNode_;
  193. // This object tracks the node which has cookies or it once had. The
  194. // order is sorted by the least recent updated node first. This
  195. // object does not track the node which has not contain cookie. For
  196. // example, adding cookies in aria2.sourceforge.net, and no node
  197. // labeled "sourceforge" is present, only node labeled "aria2" is
  198. // tracked and node labeled "sourceforge" and "net" are not.
  199. std::set<std::pair<time_t, DomainNode*>> lruTracker_;
  200. };
  201. } // namespace aria2
  202. #endif // D_COOKIE_STORAGE_H