MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
http_server_message.hpp
1#ifndef MSTL_NETWORK_HTTP_SERVER_MESSAGE_HPP__
2#define MSTL_NETWORK_HTTP_SERVER_MESSAGE_HPP__
3#include "session.hpp"
5
6struct MSTL_API http_request {
7private:
8 HTTP_METHOD method_ = HTTP_METHOD::GET;
9 string path_ = "/";
10 string version_ = "HTTP/1.1";
11 unordered_map<string, string> headers_;
12 unordered_map<string, string> cookies_;
13 unordered_map<string, string> parameters_; // query + body parameters
14 string query_{};
15 string body_{};
16 _MSTL session* session_ = nullptr;
17
18 static const string EMPTY_MARK;
19
20public:
21 http_request() = default;
22 http_request(const http_request&) = delete;
23 http_request& operator =(const http_request&) = delete;
24 http_request(http_request&& req) noexcept = default;
25 http_request& operator =(http_request&& req) noexcept = default;
26 ~http_request() = default;
27
28 void set_version(string version) noexcept { this->version_ = _MSTL move(version); }
29 MSTL_NODISCARD const string& version() const noexcept { return version_; }
30
31 MSTL_NODISCARD const unordered_map<string, string>& parameters() const noexcept { return parameters_; }
32 MSTL_NODISCARD unordered_map<string, string>& parameters() noexcept { return parameters_; }
33
34 MSTL_NODISCARD const unordered_map<string, string>& headers() const noexcept { return headers_; }
35 MSTL_NODISCARD unordered_map<string, string>& headers() noexcept { return headers_; }
36
37 void set_parameter(const string& name, string value) {
38 parameters_[name] = _MSTL move(value);
39 }
40 MSTL_NODISCARD const string& parameter(const string& name) const noexcept {
41 const auto it = parameters_.find(name);
42 return it != parameters_.end() ? it->second : EMPTY_MARK;
43 }
44
45 void set_cookie(const string& name, string value) {
46 cookies_[name] = _MSTL move(value);
47 }
48 MSTL_NODISCARD const string& cookie(const string& name) const noexcept {
49 const auto it = cookies_.find(name);
50 return it != cookies_.end() ? it->second : EMPTY_MARK;
51 }
52
53 void set_session(_MSTL session* session) noexcept { this->session_ = session; }
54 MSTL_NODISCARD const _MSTL session* session() const noexcept { return session_; }
55 MSTL_NODISCARD _MSTL session* session() noexcept { return session_; }
56
57 void set_header(const string& name, string value) {
58 headers_[name] = _MSTL move(value);
59 }
60 MSTL_NODISCARD const string& header(const string& name) const noexcept {
61 const auto it = headers_.find(name);
62 return it != headers_.end() ? it->second : EMPTY_MARK;
63 }
64
65 void set_content_type(string value) { cookies_["Content-Type"] = _MSTL move(value); }
66 MSTL_NODISCARD const string& content_type() const noexcept { return header("Content-Type"); }
67 MSTL_NODISCARD const string& header_cookie() const noexcept { return header("Cookie"); }
68
69 void set_method(HTTP_METHOD method) noexcept { this->method_ = _MSTL move(method); }
70 MSTL_NODISCARD const HTTP_METHOD& method() const noexcept { return method_; }
71
72 void set_path(string path) noexcept { this->path_ = _MSTL move(path); }
73 MSTL_NODISCARD const string& path() const noexcept { return path_; }
74
75 void set_query(string query) noexcept { this->query_ = _MSTL move(query); }
76 MSTL_NODISCARD const string& query() const noexcept { return query_; }
77
78 void set_body(string body) noexcept { this->body_ = _MSTL move(body); }
79 MSTL_NODISCARD const string& body() const noexcept { return body_; }
80
81 MSTL_NODISCARD const string& forward_protocol() const noexcept {
82 return header("X-Forwarded-Proto");
83 }
84 void set_forward_protocol(string proto) {
85 set_header("X-Forwarded-Proto", _MSTL move(proto));
86 }
87
88 MSTL_NODISCARD bool is_https() const noexcept { return forward_protocol() == "https"; }
89 MSTL_NODISCARD bool is_http() const noexcept { return forward_protocol() == "http"; }
90 void set_https() { set_forward_protocol("https"); }
91 void set_http() { set_forward_protocol("http"); }
92};
93
94
95struct MSTL_API http_response {
96private:
97 string version_ = "HTTP/1.1";
98 HTTP_STATUS status_ = HTTP_STATUS::S4_NOT_FOUNT;
99 string status_msg_ = "";
100 unordered_map<string, string> headers_;
101 vector<cookie> cookies_;
102 string body_{};
103 string redirect_url_{};
104 string forward_path_{};
105
106 static const string EMPTY_MARK;
107
108public:
109 http_response() {
110 set_content_type(HTTP_CONTENT::HTML_TEXT);
111 set_content_encode("utf-8");
112 set_header("Connection", "close");
113 }
114
115 http_response(const http_response&) = delete;
116 http_response& operator =(const http_response&) = delete;
117 http_response(http_response&& res) noexcept = default;
118 http_response& operator =(http_response&& res) noexcept = default;
119
120 void set_version(string version) noexcept { this->version_ = _MSTL move(version); }
121 MSTL_NODISCARD const string& version() const noexcept { return version_; }
122
123
124 void add_cookie(cookie cookie) { cookies_.emplace_back(_MSTL move(cookie)); }
125 void add_cookie(const HTTP_COOKIE_NAME& name, const string& value) { cookies_.emplace_back(name, value); }
126
127 MSTL_NODISCARD const vector<cookie>& cookies() const noexcept { return cookies_; }
128 MSTL_NODISCARD vector<cookie>& cookies() noexcept { return cookies_; }
129
130
131 void set_header(const string& name, const string& value) {
132 headers_[name] = value;
133 }
134 MSTL_NODISCARD const string& header(const string& name) const noexcept {
135 const auto it = headers_.find(name);
136 return it != headers_.end() ? it->second : EMPTY_MARK;
137 }
138
139 MSTL_NODISCARD const unordered_map<string, string>& headers() const noexcept { return headers_; }
140 MSTL_NODISCARD unordered_map<string, string>& headers() noexcept { return headers_; }
141
142
143 MSTL_NODISCARD const string& content_length() const noexcept {
144 return header("Content-Length");
145 }
146
147 void set_content_type(string value) {
148 headers_["Content-Type"] = _MSTL move(value);
149 }
150 void set_content_type(HTTP_CONTENT value) {
151 headers_["Content-Type"] = _MSTL move(value).content();
152 }
153 MSTL_NODISCARD const string& content_type() const noexcept {
154 return header("Content-Type");
155 }
156
157 void set_content_encode(string value) {
158 headers_["Content-Type"] += "; charset=" + _MSTL move(value);
159 }
160
161
162 void set_allow_method(string method) {
163 headers_["Access-Control-Allow-Methods"] = _MSTL move(method);
164 }
165 void set_allow_method(HTTP_METHOD method) {
166 headers_["Access-Control-Allow-Methods"] = _MSTL move(method).method();
167 }
168 MSTL_NODISCARD const string& allow_method() const noexcept {
169 return header("Access-Control-Allow-Methods");
170 }
171
172 void set_max_age(const size_t age) {
173 headers_["Access-Control-Max-Age"] = _MSTL to_string(age);
174 }
175 MSTL_NODISCARD size_t max_age() const noexcept {
176 return package_t<size_t>().try_parse(header("Access-Control-Max-Age").view());
177 }
178
179 void set_allow_credentials(const bool allow) {
180 if (allow) headers_["Access-Control-Allow-Credentials"] = "true";
181 else headers_["Access-Control-Allow-Credentials"] = "false";
182 }
183 MSTL_NODISCARD bool all_credentials() const noexcept {
184 return boolean().try_parse(header("Access-Control-Allow-Credentials").view());
185 }
186
187 void set_allow_origin(string origin) {
188 headers_["Access-Control-Allow-Origin"] = _MSTL move(origin);
189 }
190 MSTL_NODISCARD const string& allow_origin() const noexcept {
191 return header("Access-Control-Allow-Origin");
192 }
193
194 void set_allow_headers(string header) {
195 headers_["Access-Control-Allow-Headers"] = _MSTL move(header);
196 }
197 MSTL_NODISCARD const string& allow_headers() const noexcept {
198 return header("Access-Control-Allow-Headers");
199 }
200
201
202 void set_status(const HTTP_STATUS status) noexcept { status_ = status; }
203 void set_ok() noexcept { status_ = HTTP_STATUS::S2_OK; }
204 void set_not_found() noexcept { status_ = HTTP_STATUS::S4_NOT_FOUNT; }
205 void set_bad_request() noexcept { status_ = HTTP_STATUS::S4_BAD_REQUEST; }
206
207 MSTL_NODISCARD HTTP_STATUS status() const { return status_; }
208
209
210 void set_status_msg(string status_msg) { this->status_msg_ = _MSTL move(status_msg); }
211 MSTL_NODISCARD const string& status_msg() const { return status_msg_; }
212
213 void set_body(string body) noexcept { this->body_ = _MSTL move(body); }
214 MSTL_NODISCARD const string& body() const noexcept { return body_; }
215
216 void set_redirect(string url) noexcept { redirect_url_ = _MSTL move(url); }
217 MSTL_NODISCARD const string& redirect() const noexcept { return redirect_url_; }
218
219 void set_forward(string url) noexcept { forward_path_ = _MSTL move(url); }
220 MSTL_NODISCARD const string& forward() const noexcept { return forward_path_; }
221};
222
224#endif // MSTL_NETWORK_HTTP_SERVER_MESSAGE_HPP__
MSTL_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
typename package< T >::type package_t
package的便捷别名
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素