Line data Source code
1 : #ifndef THINGER_HTTP_COOKIE_HPP
2 : #define THINGER_HTTP_COOKIE_HPP
3 :
4 : #include <string>
5 : #include <cstdint>
6 : #include <optional>
7 :
8 : namespace thinger::http{
9 :
10 : enum class same_site_policy {
11 : none,
12 : lax,
13 : strict
14 : };
15 :
16 : class cookie {
17 : public:
18 : // constructors
19 96 : cookie() = default;
20 : cookie(std::string name, std::string value);
21 160 : virtual ~cookie() = default;
22 :
23 : // parsing
24 : static cookie parse(const std::string& cookie_string);
25 :
26 : // setters
27 : cookie& set_name(std::string name);
28 : cookie& set_value(std::string value);
29 : cookie& set_path(std::string path);
30 : cookie& set_domain(std::string domain);
31 : cookie& set_expires(int64_t expires);
32 : cookie& set_max_age(std::optional<int64_t> max_age);
33 : cookie& set_secure(bool secure);
34 : cookie& set_http_only(bool http_only);
35 : cookie& set_same_site(same_site_policy policy);
36 :
37 : // getters
38 : [[nodiscard]] const std::string& get_name() const;
39 : [[nodiscard]] const std::string& get_value() const;
40 : [[nodiscard]] const std::string& get_path() const;
41 : [[nodiscard]] const std::string& get_domain() const;
42 : [[nodiscard]] int64_t get_expires() const;
43 : [[nodiscard]] std::optional<int64_t> get_max_age() const;
44 : [[nodiscard]] bool is_secure() const;
45 : [[nodiscard]] bool is_http_only() const;
46 : [[nodiscard]] same_site_policy get_same_site() const;
47 :
48 : // validity
49 : [[nodiscard]] bool is_valid() const;
50 : [[nodiscard]] bool is_expired() const;
51 :
52 : // serialization
53 : [[nodiscard]] std::string to_string() const;
54 :
55 : private:
56 : std::string name_;
57 : std::string value_;
58 : std::string path_;
59 : std::string domain_;
60 : int64_t expires_ = 0;
61 : std::optional<int64_t> max_age_;
62 : bool secure_ = false;
63 : bool http_only_ = false;
64 : same_site_policy same_site_ = same_site_policy::lax;
65 :
66 : // helper for parsing
67 : static std::string trim(const std::string& str);
68 : static bool iequals(const std::string& a, const std::string& b);
69 : };
70 :
71 : }
72 :
73 : #endif
|