CookieStorage.h 8.2 KB

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