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"
12struct MSTL_API cookie : istringify<cookie> {
14 HTTP_COOKIE_NAME name_{};
20 bool http_only_ =
false;
21 string same_site_{
"Strict"};
26 cookie(
const cookie&) =
default;
27 cookie& operator =(
const cookie&) =
default;
29 cookie(cookie&& other)
noexcept {
32 cookie& operator =(cookie&& other)
noexcept {
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)) {}
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_; }
48 void set_value(
string value)
noexcept { this->value_ =
_MSTL move(value); }
49 MSTL_NODISCARD
const string& value() const noexcept {
return this->value_; }
51 void set_domain(
string domain)
noexcept { this->domain_ =
_MSTL move(domain); }
52 MSTL_NODISCARD
const string& domain() const noexcept {
return this->domain_; }
54 void set_path(
string path)
noexcept { this->path_ =
_MSTL move(path); }
55 MSTL_NODISCARD
const string& path() const noexcept {
return this->path_; }
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_; }
60 void set_secure(
const bool secure)
noexcept { this->secure_ = secure; }
61 MSTL_NODISCARD
bool secure() const noexcept {
return this->secure_; }
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_; }
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_; }
70 void set_expires(datetime expires)
noexcept { this->expires_ =
_MSTL move(expires); }
71 MSTL_NODISCARD datetime expires() const noexcept {
return this->expires_; }
74 void invalidate() noexcept {
76 set_expires(datetime::epoch());
79 void swap(cookie& other)
noexcept;
81 MSTL_NODISCARD
string to_string()
const;
85struct MSTL_API session : istringify<session> {
88 unordered_map<string, string> data_;
89 datetime last_access_;
90 datetime create_time_;
93 bool invalidated_ =
false;
96 explicit session(
string session_id)
98 last_access_(datetime::now()),
99 create_time_(last_access_) {}
101 ~session() =
default;
104 void set_id(
string id)
noexcept { this->id_ =
_MSTL move(
id); }
105 MSTL_NODISCARD
const string&
id() const noexcept {
return this->id_; }
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_; }
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_; }
113 void set_max_age(
const int seconds)
noexcept { max_age_ =
seconds; }
114 MSTL_NODISCARD
int max_age() const noexcept {
return max_age_; }
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_; }
119 MSTL_NODISCARD
const unordered_map<string, string>& get_data() const noexcept {
return data_; }
122 void invalidate() noexcept { invalidated_ =
true; data_.clear(); }
124 MSTL_NODISCARD
bool is_valid() const noexcept {
return !invalidated_ && !expired(); }
127 MSTL_NODISCARD
string& operator [](
const string& key) {
128 last_access_ = datetime::now();
132 MSTL_NODISCARD
bool contains(
const string& key)
const {
133 return data_.find(key) != data_.end();
137 void remove_attribute(
const string& key)
noexcept {
139 last_access_ = datetime::now();
142 MSTL_NODISCARD
bool expired(
int max_inactive = 0) const noexcept;
144 MSTL_NODISCARD
string to_string() const;
148class MSTL_API http_server;
151class session_manager {
153 unordered_map<string, session> sessions_;
155 _MSTL atomic<bool> cleanup_running_{
false};
156 _MSTL thread cleanup_thread_;
158 friend class _MSTL http_server;
160 MSTL_NODISCARD
static string generate_session_id();
165 MSTL_NODISCARD session* get_session(
const string& session_id,
bool create =
true);
166 MSTL_NODISCARD session* create_session() {
return get_session(
"",
true); }
168 void remove_session(
const string& session_id)
noexcept;
169 void cleanup_expired_sessions();
171 MSTL_NODISCARD
bool session_exists(
const string& session_id)
noexcept;
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
获取当前线程标识符