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 std::uint16_t
117 port() const noexcept
118 {
119 return m_port;
120 }
121
127 [[nodiscard]]
128 std::unordered_map<std::string, std::string>
129 cookies() const noexcept
130 {
131 return m_cookies;
132 }
133
139 [[nodiscard]]
140 bool
141 has_cookie(const std::string& name) const
142 {
143 return m_cookies.contains(name);
144 }
145
149 [[nodiscard]]
150 std::string_view
151 cookie(const std::string_view& name) const
152 {
153 const auto& it = std::find_if(
154 std::cbegin(m_cookies),
155 std::cend(m_cookies),
156 [&name](const auto& pair) {
157 return pair.first == name;
158 }
159 );
160
161 if (it == std::cend(m_cookies))
162 return { };
163
164 return it->second;
165 }
166
167 private:
168 std::uint16_t m_port = 0;
169 std::unordered_map<std::string, std::string> m_cookies;
170 };
171}
172
173#include <sstream>
174
175namespace malloy
176{
177
178 template<typename Body>
179 [[nodiscard]]
180 std::string
181 to_string(const http::request<Body>& r)
182 {
183 std::ostringstream ss;
184 ss << r;
185
186 return ss.str();
187 }
188
189}
Definition: request.hpp:19
request & operator=(request &&rhs) noexcept=default
request(const request &other)=default
std::uint16_t port() const noexcept
Definition: request.hpp:117
request & operator=(const request &rhs)=default
std::string_view cookie(const std::string_view &name) const
Definition: request.hpp:151
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:129
bool has_cookie(const std::string &name) const
Definition: request.hpp:141
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