Malloy
Loading...
Searching...
No Matches
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
10namespace 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 [[nodiscard]]
116 constexpr
117 std::uint16_t
118 port() const noexcept
119 {
120 return m_port;
121 }
122
128 [[nodiscard]]
129 std::unordered_map<std::string, std::string>
130 cookies() const noexcept
131 {
132 return m_cookies;
133 }
134
140 [[nodiscard]]
141 bool
142 has_cookie(const std::string& name) const
143 {
144 return m_cookies.contains(name);
145 }
146
150 [[nodiscard]]
151 std::string_view
152 cookie(const std::string_view& name) const
153 {
154 const auto& it = std::find_if(
155 std::cbegin(m_cookies),
156 std::cend(m_cookies),
157 [&name](const auto& pair) {
158 return pair.first == name;
159 }
160 );
161
162 if (it == std::cend(m_cookies))
163 return { };
164
165 return it->second;
166 }
167
168 private:
169 std::uint16_t m_port = 0;
170 std::unordered_map<std::string, std::string> m_cookies;
171 };
172}
173
174#include <sstream>
175
176namespace malloy
177{
178
179 template<typename Body>
180 [[nodiscard]]
181 std::string
182 to_string(const http::request<Body>& r)
183 {
184 std::ostringstream ss;
185 ss << r;
186
187 return ss.str();
188 }
189
190}
Definition: request.hpp:19
request & operator=(request &&rhs) noexcept=default
request(const request &other)=default
constexpr std::uint16_t port() const noexcept
Definition: request.hpp:118
request & operator=(const request &rhs)=default
std::string_view cookie(const std::string_view &name) const
Definition: request.hpp:152
request(request &&other) noexcept=default
request(http::method method_, std::string_view host, const std::uint16_t port, std::string_view target_)
Definition: request.hpp:33
std::unordered_map< std::string, std::string > cookies() const noexcept
Definition: request.hpp:130
bool has_cookie(const std::string &name) const
Definition: request.hpp:142
virtual ~request()=default
request(msg_t &&raw)
Definition: request.hpp:48
Definition: cookie.hpp:8
boost::beast::http::verb method
Definition: types.hpp:18
Definition: controller.hpp:31
std::string_view to_string(const malloy::http::method method)
Definition: http.hpp:26