Malloy
Toggle main menu visibility
Loading...
Searching...
No Matches
lib
malloy
core
http
session
session.hpp
1
#pragma once
2
3
#include "types.hpp"
4
#include "../cookie.hpp"
5
6
#include <functional>
7
#include <string>
8
#include <unordered_map>
9
#include <optional>
10
11
namespace
malloy::http::sessions
12
{
13
22
struct
session
23
{
24
using
key_type = std::string;
25
using
value_type = std::string;
26
using
id_type = std::string;
27
35
explicit
36
session
(id_type&&
id
) :
37
m_id(std::move(
id
))
38
{
39
}
40
44
virtual
45
~session
() =
default
;
46
52
session
(
const
session
& other) =
delete
;
53
59
session
(
session
&& other)
noexcept
=
delete
;
60
67
session
&
68
operator=
(
const
session
& other) =
delete
;
69
76
session
&
77
operator=
(
session
&& other)
noexcept
=
delete
;
78
89
bool
90
set
(
const
key_type& key, value_type value)
91
{
92
if
(key.empty() || value.empty())
93
return
false
;
94
95
update_access_time
();
96
return
storage_set
(key, std::move(value));
97
}
98
105
[[nodiscard]]
106
std::optional<key_type>
107
get
(
const
key_type& key)
108
{
109
if
(key.empty())
110
return
std::nullopt;
111
112
update_access_time
();
113
const
auto
& value_opt =
storage_get
(key);
114
115
// Prevent returning empty values
116
if
(value_opt && value_opt.value().empty())
117
return
std::nullopt;
118
119
return
value_opt;
120
}
121
128
[[nodiscard]]
129
virtual
130
bool
131
remove
(
const
key_type& key)
132
{
133
if
(key.empty())
134
return
false
;
135
136
update_access_time
();
137
return
storage_remove
(key);
138
}
139
145
[[nodiscard]]
146
id_type
147
id
() const noexcept
148
{
149
return
m_id;
150
}
151
158
[[nodiscard]]
159
cookie
160
generate_cookie
(std::string cookie_name)
const
161
{
162
return
cookie
{
163
.name = std::move(cookie_name),
164
.value = m_id,
165
.max_age = { },
166
.expires =
""
,
167
.domain =
""
,
168
.path =
""
,
169
.secure =
true
,
170
.http_only =
true
,
171
.same_site = cookie::same_site_t::strict,
172
};
173
}
174
175
protected
:
186
virtual
187
bool
188
storage_set
(
const
key_type& key, value_type value) = 0;
189
196
[[nodiscard]]
197
virtual
198
std::optional<value_type>
199
storage_get
(
const
key_type& key)
const
= 0;
200
207
[[nodiscard]]
208
virtual
209
bool
210
storage_remove
(
const
key_type& key) = 0;
211
217
virtual
218
void
219
update_access_time
() = 0;
220
221
private
:
222
id_type m_id;
223
};
224
230
template
<
typename
Clock>
231
struct
session_chrono
:
232
session
233
{
234
using
time_point_t = std::chrono::time_point<Clock>;
235
236
public
:
242
explicit
243
session_chrono
(id_type&&
id
) :
244
session
(std::move(
id
))
245
{
246
m_access_time = Clock::now();
247
}
248
254
time_point_t
255
access_time
() const noexcept
256
{
257
return
m_access_time;
258
}
259
267
template
<
typename
Rep,
typename
Period>
268
[[nodiscard]]
269
constexpr
270
bool
271
access_time_older_than
(
const
std::chrono::duration<Rep, Period>& max_lifetime)
const
272
{
273
return
(Clock::now() - m_access_time) > max_lifetime;
274
}
275
276
protected
:
280
void
281
update_access_time
()
override
282
{
283
m_access_time = Clock::now();
284
}
285
286
private
:
287
time_point_t m_access_time;
288
};
289
290
}
malloy::http::cookie
Definition
cookie.hpp:20
malloy::http::sessions::session_chrono::access_time
time_point_t access_time() const noexcept
Definition
session.hpp:255
malloy::http::sessions::session_chrono::session_chrono
session_chrono(id_type &&id)
Definition
session.hpp:243
malloy::http::sessions::session_chrono::access_time_older_than
constexpr bool access_time_older_than(const std::chrono::duration< Rep, Period > &max_lifetime) const
Definition
session.hpp:271
malloy::http::sessions::session_chrono::update_access_time
void update_access_time() override
Definition
session.hpp:281
malloy::http::sessions::session::update_access_time
virtual void update_access_time()=0
malloy::http::sessions::session::storage_set
virtual bool storage_set(const key_type &key, value_type value)=0
malloy::http::sessions::session::id
id_type id() const noexcept
Definition
session.hpp:147
malloy::http::sessions::session::session
session(const session &other)=delete
malloy::http::sessions::session::generate_cookie
cookie generate_cookie(std::string cookie_name) const
Definition
session.hpp:160
malloy::http::sessions::session::session
session(id_type &&id)
Definition
session.hpp:36
malloy::http::sessions::session::session
session(session &&other) noexcept=delete
malloy::http::sessions::session::operator=
session & operator=(session &&other) noexcept=delete
malloy::http::sessions::session::storage_remove
virtual bool storage_remove(const key_type &key)=0
malloy::http::sessions::session::~session
virtual ~session()=default
malloy::http::sessions::session::get
std::optional< key_type > get(const key_type &key)
Definition
session.hpp:107
malloy::http::sessions::session::remove
virtual bool remove(const key_type &key)
Definition
session.hpp:131
malloy::http::sessions::session::set
bool set(const key_type &key, value_type value)
Definition
session.hpp:90
malloy::http::sessions::session::operator=
session & operator=(const session &other)=delete
malloy::http::sessions::session::storage_get
virtual std::optional< value_type > storage_get(const key_type &key) const =0
Generated by
1.18.0