Bredbandskollen CLI  1.2
Asynchronous network task engine
cookiemanager.h
1 // Copyright (c) 2019 Internetstiftelsen
2 // Written by Göran Andersson <goran@init.se>
3 
4 // This class stores cookies in memory only.
5 // Create a subclass if you want persistent cookies.
6 
7 #pragma once
8 
9 #include <map>
10 #include <vector>
11 
12 #include "../framework/logger.h"
13 
14 class CookieManager : public Logger {
15 public:
16  CookieManager(const std::string &name = "CookieManager") :
17  Logger(name) {
18  }
19 
20  virtual ~CookieManager();
21 
22  void setCookie(const std::string &line, std::string domain,
23  std::string uri);
24 
25  // Return empty string if no cookies exist, or a HTTP header line
26  // "Cookie: name1=val1; name2=val2\r\n"
27  std::string httpHeaderLine(const std::string &domain,
28  const std::string &uri);
29 
30  // Return cookie value for domain and name; return empty if not found.
31  std::string getCookieVal(const std::string &name, std::string domain);
32 
33  // Return true if no cookies have been set.
34  bool empty() const {
35  return store.empty();
36  }
37 
38  void eraseCookies(const std::string &domain);
39 
40  // Store persistently, return true on success
41  virtual bool save();
42 
43 protected:
44  bool isDirty() const {
45  return dirty;
46  }
47  void clearDirty() {
48  dirty = false;
49  }
50 
51  // timeval contains only digits 0-9, doesn't start with 0.
52  bool isExpired(const std::string &timeval);
53 
54  // both must be non-empty, only digits, no leading zeroes.
55  static bool less(const std::string &tv1, const std::string &tv2) {
56  if (tv1.size() < tv2.size())
57  return true;
58  if (tv1.size() > tv2.size())
59  return false;
60  return tv1 < tv2;
61  }
62 
63  // Access to all cookies, to serialise/deserialise.
64  // The key is the domain for which the cookie is valid (also subdomains).
65  // The value is a vector with elements as follows:
66  // The first Cache::cache_len elements are used to cache values and must be
67  // ignored. The remaining elements are 0 or more groups of Field::field_len
68  // elements; each group describes the properties of a single cookie.
69  std::map<std::string, std::vector<std::string> > &cookies() {
70  return store;
71  }
72  enum Cache {
73  cookie_header, expiry, cache_len
74  };
75  enum Field {
76  name, value, domain, expires, path, secure, httponly, field_len
77  };
78 private:
79  // Map domain to cookie strings:
80  // First element of each store vector is a cache for httpHeaderLine():
81  // - empty string means nothing is cached
82  // - string of length 1 means there are no cookies for the domain
83  // - string of length > 1 is the cookie header line
84  // - NOTE! We only cache for path=/
85  // Second element is cache expiry time, empty for no expiry.
86  std::map<std::string, std::vector<std::string> > store;
87  // TODO: Store time of creation / last use?
88  // last use may ignore cache.
89 
90  static std::vector<std::string> cookieSplit(const std::string &line);
91  TimePoint expire_date;
92  bool dirty = false;
93  time_t t_now = 0;
94  std::string s_now = "0";
95 };
Definition: cookiemanager.h:14
A simple logger. All classes that want to write to the global log file should inherit from this class...
Definition: logger.h:86
Logger(std::string label)
Definition: logger.h:90
Measure elapsed time during execution, for example by timer events.