MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
http_constants.hpp
1#ifndef MSTL_NETWORK_HTTP_CONSTANTS_HPP__
2#define MSTL_NETWORK_HTTP_CONSTANTS_HPP__
3#include "MSTL/core/interface/istringify.hpp"
5
6MSTL_ERROR_BUILD_FINAL_CLASS(http_exception, exception, "Http Actions Failed");
7
8
9enum class HTTP_STATUS : uint32_t {
10 // 1xx Message Codes
11
12 // Server received the request header and can continue to send the request body
13 S1_CONTINUE = 100,
14 // Consent to switch protocols
15 S1_SWITCH_PROTOCOL = 101,
16
17 // 2xx Success Codes
18
19 // Request succeed
20 S2_OK = 200,
21 // Request succeed and created new resources
22 S2_CREATED = 201,
23 // Request succeed but no content is returned
24 S2_NO_CONTENT = 204,
25 // Partial request succeed
26 S2_PARTIAL_CONTENT = 206,
27
28 // 3xx Redirect Codes
29
30 // Resources are permanently migrated to the new URL
31 S3_MOVED_PERMANENT = 301,
32 // Resources are temporarily migrated to the new URL
33 S3_FOUND = 302,
34 // Resources are unmodified and use the local cache to save
35 S3_NO_MODIFIED = 304,
36 // Temporary redirect
37 S3_TEMPORARY_REDIRECT = 307,
38 // Permanent redirect
39 S3_PERMANENT_REDIRECT = 308,
40
41 // 4xx Client Error Codes
42
43 // Request is in the wrong format
44 S4_BAD_REQUEST = 400,
45 // Identity verification is required
46 S4_UNAUTHORIZED = 401,
47 // Server rejects request
48 S4_FORBIDDEN = 403,
49 // The requested resource does not exist
50 S4_NOT_FOUNT = 404,
51 // The request method is not allowed or not existed
52 S4_METHOD_NOT_ALLOWED = 405,
53 // Client request timeout
54 S4_REQUEST_TIMEOUT = 408,
55 // The request is too large
56 S4_PAYLOAD_LARGE = 413,
57 // The URL of the request is too long
58 S4_URL_LONG = 414,
59 // Excessive number of requests
60 S4_MANY_REQUESTS = 429,
61
62 // 5xx Server Error Codes
63
64 // Internal server error
65 S5_INTERNAL_ERROR = 500,
66 // Invalid upstream server response
67 S5_BAD_GATEWAY = 502,
68 // The service is temporarily unavailable
69 S5_SERVICE_UNAVAILABLE = 503,
70 // The upstream server responds too slowly
71 S5_GATEWAY_TIMEOUT = 504,
72 // The HTTP version of the request is not supported
73 S5_HTTP_VERSION_NOT_SUPPORT = 505
74};
75
76
77MSTL_INLINE17 constexpr string_view HTTP_CRLF = "\r\n";
78MSTL_INLINE17 constexpr string_view HTTP_CRLF2 = "\r\n\r\n";
79
80
81struct MSTL_API HTTP_CONTENT : istringify<HTTP_CONTENT> {
82private:
83 string content_{"UNKNOWN"};
84
85public:
86 HTTP_CONTENT() = default;
87 HTTP_CONTENT(const HTTP_CONTENT&) = default;
88 HTTP_CONTENT& operator =(const HTTP_CONTENT&) = default;
89
90 HTTP_CONTENT(HTTP_CONTENT&& content) noexcept : content_(_MSTL move(content.content_)) {}
91
92 HTTP_CONTENT& operator =(HTTP_CONTENT&& content) noexcept {
93 if (addressof(content) == this) return *this;
94 content_ = _MSTL move(content.content_);
95 return *this;
96 }
97
98 explicit HTTP_CONTENT(const string& content) : content_(content) {}
99
100 HTTP_CONTENT& operator =(const string& content) {
101 content_ = content;
102 return *this;
103 }
104
105 ~HTTP_CONTENT() = default;
106
107
108 static const HTTP_CONTENT HTML_TEXT;
109 static const HTTP_CONTENT XML_TEXT;
110 static const HTTP_CONTENT CSS_TEXT;
111 static const HTTP_CONTENT PLAIN_TEXT;
112 static const HTTP_CONTENT JSON_APP;
113 static const HTTP_CONTENT FORM_APP;
114 static const HTTP_CONTENT JPEG_IMG;
115 static const HTTP_CONTENT PNG_IMG;
116 static const HTTP_CONTENT BMP_IMG;
117 static const HTTP_CONTENT WEBP_IMG;
118 static const HTTP_CONTENT HTML_MSG;
119
120 MSTL_NODISCARD bool is_html_text() const noexcept { return content_ == HTML_TEXT.content_; }
121 MSTL_NODISCARD bool is_xml_text() const noexcept { return content_ == XML_TEXT.content_; }
122 MSTL_NODISCARD bool is_css_text() const noexcept { return content_ == CSS_TEXT.content_; }
123 MSTL_NODISCARD bool is_plain_text() const noexcept { return content_ == PLAIN_TEXT.content_; }
124 MSTL_NODISCARD bool is_json_app() const noexcept { return content_ == JSON_APP.content_; }
125 MSTL_NODISCARD bool is_form_app() const noexcept { return content_ == FORM_APP.content_; }
126 MSTL_NODISCARD bool is_jpeg_img() const noexcept { return content_ == JPEG_IMG.content_; }
127 MSTL_NODISCARD bool is_png_img() const noexcept { return content_ == PNG_IMG.content_; }
128 MSTL_NODISCARD bool is_bmp_img() const noexcept { return content_ == BMP_IMG.content_; }
129 MSTL_NODISCARD bool is_webp_img() const noexcept { return content_ == WEBP_IMG.content_; }
130 MSTL_NODISCARD bool is_html_msg() const noexcept { return content_ == HTML_MSG.content_; }
131
132 MSTL_NODISCARD static bool is_html_text(const string_view view) noexcept { return view == HTML_TEXT.content_; }
133 MSTL_NODISCARD static bool is_xml_text(const string_view view) noexcept { return view == XML_TEXT.content_; }
134 MSTL_NODISCARD static bool is_css_text(const string_view view) noexcept { return view == CSS_TEXT.content_; }
135 MSTL_NODISCARD static bool is_plain_text(const string_view view) noexcept { return view == PLAIN_TEXT.content_; }
136 MSTL_NODISCARD static bool is_json_app(const string_view view) noexcept { return view == JSON_APP.content_; }
137 MSTL_NODISCARD static bool is_form_app(const string_view view) noexcept { return view == FORM_APP.content_; }
138 MSTL_NODISCARD static bool is_jpeg_img(const string_view view) noexcept { return view == JPEG_IMG.content_; }
139 MSTL_NODISCARD static bool is_png_img(const string_view view) noexcept { return view == PNG_IMG.content_; }
140 MSTL_NODISCARD static bool is_bmp_img(const string_view view) noexcept { return view == BMP_IMG.content_; }
141 MSTL_NODISCARD static bool is_webp_img(const string_view view) noexcept { return view == WEBP_IMG.content_; }
142 MSTL_NODISCARD static bool is_html_msg(const string_view view) noexcept { return view == HTML_MSG.content_; }
143
144
145 MSTL_NODISCARD string_view content() const & noexcept { return content_.view(); }
146 MSTL_NODISCARD string content() && noexcept { return _MSTL move(content_); }
147 MSTL_NODISCARD string to_string() const { return content_; }
148};
149
150
151#ifdef DELETE
152#undef DELETE
153#endif
154
155struct MSTL_API HTTP_METHOD : istringify<HTTP_METHOD> {
156private:
157 string method_{"UNKNOWN"};
158
159public:
160 HTTP_METHOD() = default;
161 HTTP_METHOD(const HTTP_METHOD&) = default;
162 HTTP_METHOD& operator =(const HTTP_METHOD&) = default;
163
164 HTTP_METHOD(HTTP_METHOD&& method) noexcept : method_(_MSTL move(method.method_)) {}
165 HTTP_METHOD& operator =(HTTP_METHOD&& method) noexcept {
166 if (_MSTL addressof(method) == this) return *this;
167 method_ = _MSTL move(method.method_);
168 return *this;
169 }
170
171 explicit HTTP_METHOD(const string& method)
172 : method_(method) {}
173
174 HTTP_METHOD& operator =(const string& method) {
175 method_ = method;
176 return *this;
177 }
178
179 explicit HTTP_METHOD(string&& method)
180 : method_(_MSTL move(method)) {}
181
182 HTTP_METHOD& operator =(string&& method) {
183 method_ = _MSTL move(method);
184 return *this;
185 }
186
187 ~HTTP_METHOD() = default;
188
189 static const HTTP_METHOD GET;
190 static const HTTP_METHOD POST;
191 static const HTTP_METHOD HEAD;
192 static const HTTP_METHOD PUT;
193 static const HTTP_METHOD DELETE;
194 static const HTTP_METHOD OPTIONS;
195 static const HTTP_METHOD TRACE;
196 static const HTTP_METHOD CONNECT;
197 static const HTTP_METHOD DEFAULT;
198
199
200 MSTL_NODISCARD const string& method() const & noexcept { return method_; }
201 MSTL_NODISCARD string method() && noexcept { return _MSTL move(method_); }
202
203 MSTL_NODISCARD HTTP_METHOD operator &(const HTTP_METHOD& rhs) const & {
204 return HTTP_METHOD(method_ + ", " + rhs.method_);
205 }
206 MSTL_NODISCARD HTTP_METHOD operator &(HTTP_METHOD&& rhs) const & {
207 return HTTP_METHOD(method_ + ", " + _MSTL move(rhs.method_));
208 }
209 MSTL_NODISCARD HTTP_METHOD operator &(const HTTP_METHOD& rhs) && {
210 return HTTP_METHOD(_MSTL move(method_) + ", " + rhs.method_);
211 }
212 MSTL_NODISCARD HTTP_METHOD operator &(HTTP_METHOD&& rhs) && {
213 return HTTP_METHOD(_MSTL move(method_) + ", " + _MSTL move(rhs.method_));
214 }
215
216 MSTL_NODISCARD bool is_get() const noexcept { return method_ == GET.method_; }
217 MSTL_NODISCARD bool is_post() const noexcept { return method_ == POST.method_; }
218 MSTL_NODISCARD bool is_head() const noexcept { return method_ == HEAD.method_; }
219 MSTL_NODISCARD bool is_put() const noexcept { return method_ == PUT.method_; }
220 MSTL_NODISCARD bool is_delete() const noexcept { return method_ == DELETE.method_; }
221 MSTL_NODISCARD bool is_options() const noexcept { return method_ == OPTIONS.method_; }
222 MSTL_NODISCARD bool is_trace() const noexcept { return method_ == TRACE.method_; }
223 MSTL_NODISCARD bool is_connect() const noexcept { return method_ == CONNECT.method_; }
224
225 MSTL_NODISCARD string to_string() const { return method_; }
226};
227
228
229struct MSTL_API HTTP_COOKIE_NAME : istringify<HTTP_COOKIE_NAME> {
230private:
231 string cookie_{"UNKNOWN"};
232
233public:
234 HTTP_COOKIE_NAME() = default;
235 HTTP_COOKIE_NAME(const HTTP_COOKIE_NAME&) = default;
236 HTTP_COOKIE_NAME& operator =(const HTTP_COOKIE_NAME&) = default;
237
238 HTTP_COOKIE_NAME(HTTP_COOKIE_NAME&& cookie) noexcept : cookie_(_MSTL move(cookie.cookie_)) {}
239 HTTP_COOKIE_NAME& operator =(HTTP_COOKIE_NAME&& cookie) noexcept {
240 if (_MSTL addressof(cookie) == this) return *this;
241 cookie_ = _MSTL move(cookie.cookie_);
242 return *this;
243 }
244
245 explicit HTTP_COOKIE_NAME(const string& cookie) : cookie_(cookie) {}
246 HTTP_COOKIE_NAME& operator =(const string& cookie) {
247 cookie_ = cookie;
248 return *this;
249 }
250
251 ~HTTP_COOKIE_NAME() = default;
252
253 static const HTTP_COOKIE_NAME JSESSIONID;
254 static const HTTP_COOKIE_NAME SESSIONID;
255 static const HTTP_COOKIE_NAME PHPSESSID;
256 static const HTTP_COOKIE_NAME ASPSESSIONID;
257
258 MSTL_NODISCARD const string& cookie_name() const & noexcept { return cookie_; }
259 MSTL_NODISCARD string cookie_name() && noexcept { return _MSTL move(cookie_); }
260
261 MSTL_NODISCARD string to_string() const { return cookie_; }
262};
263
265#endif // MSTL_NETWORK_HTTP_CONSTANTS_HPP__
MSTL_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
unsigned int uint32_t
32位无符号整数类型
#define MSTL_ERROR_BUILD_FINAL_CLASS(THIS, BASE, INFO)
构建最终异常类宏
constexpr memory_order operator&(memory_order mo, memory_order_modifier mod) noexcept
内存顺序与修饰符的与操作符
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素
异常基类