MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
session.hpp
1#ifndef MSTL_NETWORK_HTTP_COOKIE_HPP__
2#define MSTL_NETWORK_HTTP_COOKIE_HPP__
3#include "MSTL/core/container/unordered_map.hpp"
4#include "MSTL/core/time/datetime.hpp"
8#include "http_constants.hpp"
10
11
12struct MSTL_API cookie : istringify<cookie> {
13private:
14 HTTP_COOKIE_NAME name_{};
15 string value_{};
16 string domain_{};
17 string path_ = "/";
18 int max_age_ = -1; // -1 means session cookie
19 bool secure_ = false;
20 bool http_only_ = false;
21 string same_site_{"Strict"}; // "Strict", "Lax", "None"
22 datetime expires_{};
23
24public:
25 cookie() = default;
26 cookie(const cookie&) = default;
27 cookie& operator =(const cookie&) = default;
28
29 cookie(cookie&& other) noexcept {
30 swap(other);
31 }
32 cookie& operator =(cookie&& other) noexcept {
33 if (_MSTL addressof(other) == this) return *this;
34 swap(other);
35 return *this;
36 }
37
38 cookie(HTTP_COOKIE_NAME name, string value) : name_(_MSTL move(name)), value_(_MSTL move(value)) {}
39 cookie(const string_view name, const string_view value) : cookie(HTTP_COOKIE_NAME(string{name}), string(value)) {}
40 cookie(const char* name, const char* value) : cookie(HTTP_COOKIE_NAME(name), string(value)) {}
41
42 ~cookie() = default;
43
44
45 void set_name(HTTP_COOKIE_NAME name) noexcept { this->name_ = _MSTL move(name); }
46 MSTL_NODISCARD const HTTP_COOKIE_NAME& name() const noexcept { return this->name_; }
47
48 void set_value(string value) noexcept { this->value_ = _MSTL move(value); }
49 MSTL_NODISCARD const string& value() const noexcept { return this->value_; }
50
51 void set_domain(string domain) noexcept { this->domain_ = _MSTL move(domain); }
52 MSTL_NODISCARD const string& domain() const noexcept { return this->domain_; }
53
54 void set_path(string path) noexcept { this->path_ = _MSTL move(path); }
55 MSTL_NODISCARD const string& path() const noexcept { return this->path_; }
56
57 void set_max_age(const int age) noexcept { this->max_age_ = age; }
58 MSTL_NODISCARD int max_age() const noexcept { return this->max_age_; }
59
60 void set_secure(const bool secure) noexcept { this->secure_ = secure; }
61 MSTL_NODISCARD bool secure() const noexcept { return this->secure_; }
62
63 void set_http_only(const bool http_only) noexcept { this->http_only_ = http_only; }
64 MSTL_NODISCARD bool http_only() const noexcept { return this->http_only_; }
65
66 void set_same_site(string s) noexcept { this->same_site_ = _MSTL move(s); }
67 void set_same_site(const bool is_https) noexcept { this->same_site_ = is_https ? "Strict" : "Lax"; }
68 MSTL_NODISCARD const string& same_site() const noexcept { return this->same_site_; }
69
70 void set_expires(datetime expires) noexcept { this->expires_ = _MSTL move(expires); }
71 MSTL_NODISCARD datetime expires() const noexcept { return this->expires_; }
72
73
74 void invalidate() noexcept {
75 set_max_age(0);
76 set_expires(datetime::epoch());
77 }
78
79 void swap(cookie& other) noexcept;
80
81 MSTL_NODISCARD string to_string() const;
82};
83
84
85struct MSTL_API session : istringify<session> {
86private:
87 string id_;
88 unordered_map<string, string> data_;
89 datetime last_access_;
90 datetime create_time_;
91 int max_age_ = 1800; // 30 minutes default
92 bool is_new_ = true;
93 bool invalidated_ = false;
94
95public:
96 explicit session(string session_id)
97 : id_(_MSTL move(session_id)),
98 last_access_(datetime::now()),
99 create_time_(last_access_) {}
100
101 ~session() = default;
102
103
104 void set_id(string id) noexcept { this->id_ = _MSTL move(id); }
105 MSTL_NODISCARD const string& id() const noexcept { return this->id_; }
106
107 void set_last_access(datetime last_access) noexcept { this->last_access_ = _MSTL move(last_access); }
108 MSTL_NODISCARD datetime last_access() const noexcept { return this->last_access_; }
109
110 void set_create_time(datetime create_time) noexcept { this->create_time_ = _MSTL move(create_time); }
111 MSTL_NODISCARD datetime create_time() const noexcept { return this->create_time_; }
112
113 void set_max_age(const int seconds) noexcept { max_age_ = seconds; }
114 MSTL_NODISCARD int max_age() const noexcept { return max_age_; }
115
116 void set_is_new(const bool is_new) noexcept { this->is_new_ = is_new; }
117 MSTL_NODISCARD bool is_new() const noexcept { return this->is_new_; }
118
119 MSTL_NODISCARD const unordered_map<string, string>& get_data() const noexcept { return data_; }
120
121
122 void invalidate() noexcept { invalidated_ = true; data_.clear(); }
123
124 MSTL_NODISCARD bool is_valid() const noexcept { return !invalidated_ && !expired(); }
125
126
127 MSTL_NODISCARD string& operator [](const string& key) {
128 last_access_ = datetime::now();
129 return data_[key];
130 }
131
132 MSTL_NODISCARD bool contains(const string& key) const {
133 return data_.find(key) != data_.end();
134 }
135
136
137 void remove_attribute(const string& key) noexcept {
138 data_.erase(key);
139 last_access_ = datetime::now();
140 }
141
142 MSTL_NODISCARD bool expired(int max_inactive = 0) const noexcept;
143
144 MSTL_NODISCARD string to_string() const;
145};
146
147
148class MSTL_API http_server;
149
151class session_manager {
152private:
153 unordered_map<string, session> sessions_;
154 _MSTL mutex mutex_;
155 _MSTL atomic<bool> cleanup_running_{false};
156 _MSTL thread cleanup_thread_;
157
158 friend class _MSTL http_server;
159
160 MSTL_NODISCARD static string generate_session_id();
161
162 session_manager();
163 ~session_manager();
164
165 MSTL_NODISCARD session* get_session(const string& session_id, bool create = true);
166 MSTL_NODISCARD session* create_session() { return get_session("", true); }
167
168 void remove_session(const string& session_id) noexcept;
169 void cleanup_expired_sessions();
170
171 MSTL_NODISCARD bool session_exists(const string& session_id) noexcept;
172};
174
176#endif // MSTL_NETWORK_HTTP_COOKIE_HPP__
MSTL原子类型完整实现
MSTL_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
duration< int64_t > seconds
秒持续时间
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_INNER__
结束inner命名空间
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
#define MSTL_BEGIN_INNER__
开始inner命名空间
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素
void swap()=delete
删除无参数的swap重载
MSTL_ALWAYS_INLINE_INLINE thread::id id() noexcept
获取当前线程标识符
MSTL互斥锁
MSTL线程支持