Malloy
Toggle main menu visibility
Loading...
Searching...
No Matches
lib
malloy
core
http
request.hpp
1
#pragma once
2
3
#include "cookie.hpp"
4
#include "types.hpp"
5
6
#include <boost/beast/http/string_body.hpp>
7
8
#include <unordered_map>
9
10
namespace
malloy::http
11
{
12
16
template
<
typename
Body = boost::beast::http::
string
_body>
17
class
request :
18
public
boost::beast::http::request<Body>
19
{
20
using
msg_t = boost::beast::http::request<Body>;
21
22
public
:
23
request() =
default
;
24
33
request
(
http::method
method_, std::string_view host,
const
std::uint16_t
port
, std::string_view target_) :
34
m_port(
port
)
35
{
36
msg_t::version(11);
37
msg_t::method(method_);
38
msg_t::target(target_);
39
msg_t::set(http::field::host, host);
40
}
41
47
explicit
48
request
(msg_t&& raw)
49
{
50
using namespace
boost::beast::http;
51
52
// Underlying
53
msg_t::operator=(std::move(raw));
54
55
// Cookies
56
{
57
const
auto
& [begin, end] = msg_t::base().equal_range(field::cookie);
58
for
(
auto
it = begin; it != end; it++) {
59
const
auto
&str = it->value();
60
61
const
auto
&sep_pos = it->value().find(
'='
);
62
if
(sep_pos == std::string::npos)
63
continue
;
64
65
std::string key{str.substr(0, sep_pos)};
66
std::string value{str.substr(sep_pos + 1)};
67
m_cookies.insert_or_assign(std::move(key), std::move(value));
68
}
69
}
70
}
71
77
request
(
const
request& other) =
default
;
78
84
request
(request&& other)
noexcept
=
default
;
85
89
virtual
90
~request
() =
default
;
91
98
request&
99
operator=
(
const
request& rhs) =
default
;
100
107
request&
108
operator=
(request&& rhs)
noexcept
=
default
;
109
115
constexpr
116
void
117
use_tls
(
const
bool
enabled)
noexcept
118
{
119
m_use_tls = enabled;
120
}
121
127
[[nodiscard]]
128
constexpr
129
bool
130
use_tls
() const noexcept
131
{
132
return
m_use_tls;
133
}
134
140
[[nodiscard]]
141
constexpr
142
std::uint16_t
143
port
() const noexcept
144
{
145
return
m_port;
146
}
147
153
[[nodiscard]]
154
std::unordered_map<std::string, std::string>
155
cookies
() const noexcept
156
{
157
return
m_cookies;
158
}
159
165
[[nodiscard]]
166
bool
167
has_cookie
(
const
std::string& name)
const
168
{
169
return
m_cookies.contains(name);
170
}
171
175
[[nodiscard]]
176
std::string_view
177
cookie
(
const
std::string_view& name)
const
178
{
179
const
auto
& it = std::find_if(
180
std::cbegin(m_cookies),
181
std::cend(m_cookies),
182
[&name](
const
auto
& pair) {
183
return
pair.first == name;
184
}
185
);
186
187
if
(it == std::cend(m_cookies))
188
return
{ };
189
190
return
it->second;
191
}
192
193
private
:
194
bool
m_use_tls =
false
;
195
std::uint16_t m_port = 0;
196
std::unordered_map<std::string, std::string> m_cookies;
197
};
198
}
199
200
#include <sstream>
201
202
namespace
malloy
203
{
204
205
template
<
typename
Body>
206
[[nodiscard]]
207
std::string
208
to_string
(
const
http::request<Body>& r)
209
{
210
std::ostringstream ss;
211
ss << r;
212
213
return
ss.str();
214
}
215
216
}
malloy::http::request::operator=
request & operator=(request &&rhs) noexcept=default
malloy::http::request::request
request(const request &other)=default
malloy::http::request::use_tls
constexpr bool use_tls() const noexcept
Definition
request.hpp:130
malloy::http::request::use_tls
constexpr void use_tls(const bool enabled) noexcept
Definition
request.hpp:117
malloy::http::request< boost::beast::http::file_body >::port
constexpr std::uint16_t port() const noexcept
Definition
request.hpp:143
malloy::http::request::operator=
request & operator=(const request &rhs)=default
malloy::http::request::cookie
std::string_view cookie(const std::string_view &name) const
Definition
request.hpp:177
malloy::http::request::request
request(request &&other) noexcept=default
malloy::http::request::request
request(http::method method_, std::string_view host, const std::uint16_t port, std::string_view target_)
Definition
request.hpp:33
malloy::http::request::cookies
std::unordered_map< std::string, std::string > cookies() const noexcept
Definition
request.hpp:155
malloy::http::request::has_cookie
bool has_cookie(const std::string &name) const
Definition
request.hpp:167
malloy::http::request::~request
virtual ~request()=default
malloy::http::request::request
request(msg_t &&raw)
Definition
request.hpp:48
malloy::http
Definition
cookie.hpp:8
malloy::http::method
boost::beast::http::verb method
Definition
types.hpp:18
malloy
Definition
controller.hpp:32
malloy::to_string
std::string_view to_string(const malloy::http::method method)
Definition
http.hpp:26
Generated by
1.18.0