NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
http_constants.hpp
浏览该文件的文档.
1#ifndef NEFORCE_NETWORK_HTTP_HTTP_CONSTANTS_HPP__
2#define NEFORCE_NETWORK_HTTP_HTTP_CONSTANTS_HPP__
3
15
16#include "NeForce/core/exception/system_exception.hpp"
18NEFORCE_BEGIN_NAMESPACE__
19
25
31NEFORCE_ERROR_BUILD_FINAL_CLASS(http_exception, network_exception, "Http Actions Failed");
32 // Exceptions
34
35NEFORCE_BEGIN_HTTP__
36
226
481
482NEFORCE_API string http_status_message(http_status status);
483
484NEFORCE_API http_status http_status_from_code(uint16_t code) noexcept;
485
486
493struct NEFORCE_API http_content : istringify<http_content> {
494private:
495 string content_{"UNKNOWN"};
496
497public:
498 http_content() = default;
499 http_content(const http_content&) = default;
500 http_content& operator=(const http_content&) = default;
501
506 http_content(http_content&& other) noexcept :
507 content_(_NEFORCE move(other.content_)) {}
508
514 http_content& operator=(http_content&& other) noexcept {
515 if (addressof(other) == this) {
516 return *this;
517 }
518 content_ = _NEFORCE move(other.content_);
519 return *this;
520 }
521
526 explicit http_content(string content) :
527 content_(move(content)) {}
528
534 http_content& operator=(const string& content) {
535 content_ = content;
536 return *this;
537 }
538
539 ~http_content() = default;
540
541 static const http_content& HTML_TEXT();
542 static const http_content& XML_TEXT();
543 static const http_content& CSS_TEXT();
544 static const http_content& PLAIN_TEXT();
545 static const http_content& JSON_APP();
546 static const http_content& FORM_APP();
547 static const http_content& JPEG_IMG();
548 static const http_content& PNG_IMG();
549 static const http_content& BMP_IMG();
550 static const http_content& WEBP_IMG();
551 static const http_content& HTML_MSG();
552
553 // TODO: application/problem+json — RFC 9457 Problem Details MIME type for standardized API error responses
554 // TODO: application/octet-stream — generic binary data MIME type for file downloads
555 // TODO: multipart/form-data — MIME type constant for form upload content negotiation
556
557 NEFORCE_NODISCARD bool is_html_text() const { return content_ == HTML_TEXT().content_; }
558 NEFORCE_NODISCARD bool is_xml_text() const { return content_ == XML_TEXT().content_; }
559 NEFORCE_NODISCARD bool is_css_text() const { return content_ == CSS_TEXT().content_; }
560 NEFORCE_NODISCARD bool is_plain_text() const { return content_ == PLAIN_TEXT().content_; }
561 NEFORCE_NODISCARD bool is_json_app() const { return content_ == JSON_APP().content_; }
562 NEFORCE_NODISCARD bool is_form_app() const { return content_ == FORM_APP().content_; }
563 NEFORCE_NODISCARD bool is_jpeg_img() const { return content_ == JPEG_IMG().content_; }
564 NEFORCE_NODISCARD bool is_png_img() const { return content_ == PNG_IMG().content_; }
565 NEFORCE_NODISCARD bool is_bmp_img() const { return content_ == BMP_IMG().content_; }
566 NEFORCE_NODISCARD bool is_webp_img() const { return content_ == WEBP_IMG().content_; }
567 NEFORCE_NODISCARD bool is_html_msg() const { return content_ == HTML_MSG().content_; }
568
569 NEFORCE_NODISCARD static bool is_html_text(const string_view view) { return view == HTML_TEXT().content_; }
570 NEFORCE_NODISCARD static bool is_xml_text(const string_view view) { return view == XML_TEXT().content_; }
571 NEFORCE_NODISCARD static bool is_css_text(const string_view view) { return view == CSS_TEXT().content_; }
572 NEFORCE_NODISCARD static bool is_plain_text(const string_view view) { return view == PLAIN_TEXT().content_; }
573 NEFORCE_NODISCARD static bool is_json_app(const string_view view) { return view == JSON_APP().content_; }
574 NEFORCE_NODISCARD static bool is_form_app(const string_view view) { return view == FORM_APP().content_; }
575 NEFORCE_NODISCARD static bool is_jpeg_img(const string_view view) { return view == JPEG_IMG().content_; }
576 NEFORCE_NODISCARD static bool is_png_img(const string_view view) { return view == PNG_IMG().content_; }
577 NEFORCE_NODISCARD static bool is_bmp_img(const string_view view) { return view == BMP_IMG().content_; }
578 NEFORCE_NODISCARD static bool is_webp_img(const string_view view) { return view == WEBP_IMG().content_; }
579 NEFORCE_NODISCARD static bool is_html_msg(const string_view view) { return view == HTML_MSG().content_; }
580
585 NEFORCE_NODISCARD const string& content() const& noexcept { return content_; }
586
591 NEFORCE_NODISCARD string content() && noexcept { return _NEFORCE move(content_); }
592
597 NEFORCE_NODISCARD string to_string() const { return content_; }
598
604 NEFORCE_NODISCARD http_content with_charset(string charset) const {
605 return http_content(content_ + "; charset=" + move(charset));
606 }
607};
608
609
610#ifdef DELETE
611# undef DELETE
612#endif
613
620struct NEFORCE_API http_method : istringify<http_method> {
621private:
622 string method_{"UNKNOWN"};
623
624public:
625 http_method() = default;
626 http_method(const http_method&) = default;
627 http_method& operator=(const http_method&) = default;
628
633 http_method(http_method&& other) noexcept :
634 method_(_NEFORCE move(other.method_)) {}
635
641 http_method& operator=(http_method&& other) noexcept {
642 if (_NEFORCE addressof(other) == this) {
643 return *this;
644 }
645 method_ = _NEFORCE move(other.method_);
646 return *this;
647 }
648
653 explicit http_method(const string& method) :
654 method_(method) {}
655
661 http_method& operator=(const string& method) {
662 method_ = method;
663 return *this;
664 }
665
670 explicit http_method(string&& method) :
671 method_(_NEFORCE move(method)) {}
672
678 http_method& operator=(string&& method) {
679 method_ = _NEFORCE move(method);
680 return *this;
681 }
682
683 ~http_method() = default;
684
685 static const http_method& GET();
686 static const http_method& POST();
687 static const http_method& HEAD();
688 static const http_method& PUT();
689 static const http_method& DELETE();
690 static const http_method& OPTIONS();
691 static const http_method& TRACE();
692 static const http_method& CONNECT();
693 static const http_method& PATCH();
694 static const http_method& DEFAULT();
695
700 NEFORCE_NODISCARD const string& method() const& noexcept { return method_; }
701
706 NEFORCE_NODISCARD string method() && noexcept { return _NEFORCE move(method_); }
707
715 NEFORCE_NODISCARD http_method operator&(const http_method& rhs) const& {
716 return http_method(method_ + ", " + rhs.method_);
717 }
718
719 NEFORCE_NODISCARD http_method operator&(http_method&& rhs) const& {
720 return http_method(method_ + ", " + _NEFORCE move(rhs.method_));
721 }
722
723 NEFORCE_NODISCARD http_method operator&(const http_method& rhs) && {
724 return http_method(_NEFORCE move(method_) + ", " + rhs.method_);
725 }
726
727 NEFORCE_NODISCARD http_method operator&(http_method&& rhs) && {
728 return http_method(_NEFORCE move(method_) + ", " + _NEFORCE move(rhs.method_));
729 }
730
731 NEFORCE_NODISCARD bool is_get() const { return method_ == GET().method_; }
732 NEFORCE_NODISCARD bool is_post() const { return method_ == POST().method_; }
733 NEFORCE_NODISCARD bool is_head() const { return method_ == HEAD().method_; }
734 NEFORCE_NODISCARD bool is_put() const { return method_ == PUT().method_; }
735 NEFORCE_NODISCARD bool is_delete() const { return method_ == DELETE().method_; }
736 NEFORCE_NODISCARD bool is_options() const { return method_ == OPTIONS().method_; }
737 NEFORCE_NODISCARD bool is_trace() const { return method_ == TRACE().method_; }
738 NEFORCE_NODISCARD bool is_connect() const { return method_ == CONNECT().method_; }
739 NEFORCE_NODISCARD bool is_patch() const { return method_ == PATCH().method_; }
740
745 NEFORCE_NODISCARD string to_string() const { return method_; }
746};
747
748
755struct NEFORCE_API http_cookie_name : istringify<http_cookie_name> {
756private:
757 string cookie_{"UNKNOWN"};
758
759public:
760 http_cookie_name() = default;
761 http_cookie_name(const http_cookie_name&) = default;
762 http_cookie_name& operator=(const http_cookie_name&) = default;
763
768 http_cookie_name(http_cookie_name&& other) noexcept :
769 cookie_(_NEFORCE move(other.cookie_)) {}
770
776 http_cookie_name& operator=(http_cookie_name&& other) noexcept {
777 if (_NEFORCE addressof(other) == this) {
778 return *this;
779 }
780 cookie_ = _NEFORCE move(other.cookie_);
781 return *this;
782 }
783
788 explicit http_cookie_name(string cookie) :
789 cookie_(move(cookie)) {}
790
796 http_cookie_name& operator=(const string& cookie) {
797 cookie_ = cookie;
798 return *this;
799 }
800
801 ~http_cookie_name() = default;
802
803 static const http_cookie_name& JSESSIONID();
804 static const http_cookie_name& SESSIONID();
805 static const http_cookie_name& PHPSESSID();
806 static const http_cookie_name& ASPSESSIONID();
807
812 NEFORCE_NODISCARD const string& cookie_name() const& noexcept { return cookie_; }
813
818 NEFORCE_NODISCARD string cookie_name() && noexcept { return _NEFORCE move(cookie_); }
819
824 NEFORCE_NODISCARD string to_string() const { return cookie_; }
825};
826
827
828struct NEFORCE_API http_key {
829 static const string& Access_Control_Allow_Credentials();
830 static const string& Access_Control_Allow_Headers();
831 static const string& Access_Control_Allow_Methods();
832 static const string& Access_Control_Allow_Origin();
833 static const string& Access_Control_Max_Age();
834 static const string& Connection();
835 static const string& Content_Length();
836 static const string& Content_Type();
837 static const string& Lax();
838 static const string& Strict();
839 static const string& X_Forwarded_Proto();
840
841 static const string& Strict_Transport_Security();
842 static const string& X_Frame_Options();
843 static const string& X_Content_Type_Options();
844 static const string& Content_Security_Policy();
845 static const string& X_XSS_Protection();
846 static const string& Referrer_Policy();
847 static const string& Permissions_Policy();
848};
849 // Http
851
852NEFORCE_END_HTTP__
853NEFORCE_END_NAMESPACE__
854#endif // NEFORCE_NETWORK_HTTP_HTTP_CONSTANTS_HPP__
constexpr T * addressof(T &x) noexcept
获取对象的地址
unsigned short uint16_t
16位无符号整数类型
#define NEFORCE_ERROR_BUILD_FINAL_CLASS(THIS, BASE, INFO)
构建最终异常类宏
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
http_status
HTTP状态码枚举
@ S5_HTTP_VERSION_NOT_SUPPORTED
505 HTTP Version Not Supported 不支持的HTTP版本
@ S5_GATEWAY_TIMEOUT
504 Gateway Timeout 上游服务器响应超时
@ S5_BAD_GATEWAY
502 Bad Gateway 上游服务器响应无效
@ S5_INTERNAL_SERVER_ERROR
500 Internal Server Error 服务器内部错误
@ S1_SWITCHING_PROTOCOLS
101 Switching Protocols 同意切换协议
@ S4_UNAUTHORIZED
401 Unauthorized 需要身份验证
@ S4_METHOD_NOT_ALLOWED
405 Method Not Allowed 请求方法不允许
@ S4_FORBIDDEN
403 Forbidden 服务器拒绝请求
@ S4_UNAVAILABLE_FOR_LEGAL_REASONS
451 Unavailable For Legal Reasons 因法律原因不可用
@ S1_CONTINUE
100 Continue 服务器收到请求头,客户端可以继续发送请求体
@ S4_EXPECTATION_FAILED
417 Expectation Failed 无法满足Expect请求头
@ S3_NOT_MODIFIED
304 Not Modified 资源未修改,可使用本地缓存
@ S2_CREATED
201 Created 请求成功并创建了新资源
@ S4_URI_TOO_LONG
414 URI Too Long 请求URL过长
@ S4_UNSUPPORTED_MEDIA_TYPE
415 Unsupported Media Type 不支持的媒体格式
@ S5_NOT_IMPLEMENTED
501 Not Implemented 服务器不支持该请求功能
@ S2_PARTIAL_CONTENT
206 Partial Content 部分请求成功(范围请求)
@ S3_MOVED_PERMANENT
301 Moved Permanently 资源已永久迁移到新URL
@ S4_CONFLICT
409 Conflict 请求与资源当前状态冲突
@ S4_TOO_MANY_REQUESTS
429 Too Many Requests 请求次数过多
@ S4_LENGTH_REQUIRED
411 Length Required 请求未指定Content-Length
@ S4_PRECONDITION_FAILED
412 Precondition Failed 请求先决条件未满足
@ S5_SERVICE_UNAVAILABLE
503 Service Unavailable 服务暂时不可用
@ S2_NO_CONTENT
204 No Content 请求成功但无内容返回
@ S4_UNPROCESSABLE_CONTENT
422 Unprocessable Content 请求格式正确但语义有误
@ S4_UPGRADE_REQUIRED
426 Upgrade Required 需要升级协议
@ S4_HEADER_FIELDS_TOO_LARGE
431 Request Header Fields Too Large 请求头字段过大
@ S3_FOUND
302 Found 资源临时迁移到新URL
@ S4_NOT_FOUND
404 Not Found 请求的资源不存在
@ S2_ACCEPTED
202 Accepted 请求已接受但尚未处理
@ S4_RANGE_NOT_SATISFIABLE
416 Range Not Satisfiable 无法满足Range请求
@ S4_PAYLOAD_TOO_LARGE
413 Payload Too Large 请求体过大
@ S4_BAD_REQUEST
400 Bad Request 请求格式错误
@ S3_TEMPORARY_REDIRECT
307 Temporary Redirect 临时重定向
@ S4_PROXY_AUTH_REQUIRED
407 Proxy Authentication Required 需要通过代理认证
@ S3_PERMANENT_REDIRECT
308 Permanent Redirect 永久重定向
@ S4_NOT_ACCEPTABLE
406 Not Acceptable 无法生成匹配Accept头的内容
@ S4_GONE
410 Gone 资源已永久删除
@ S4_REQUEST_TIMEOUT
408 Request Timeout 客户端请求超时
@ S3_SEE_OTHER
303 See Other 重定向到另一个URI获取响应
可字符串化接口
HTTP操作异常
http_content(http_content &&other) noexcept
移动构造函数
const string & content() const &noexcept
获取左值内容
http_content & operator=(const string &content)
字符串赋值运算符
static const http_content & JPEG_IMG()
image/jpeg
static const http_content & WEBP_IMG()
image/webp
http_content with_charset(string charset) const
创建带 charset 的新 http_content
static const http_content & CSS_TEXT()
text/css
static const http_content & JSON_APP()
application/json
http_content(string content)
字符串构造函数
static const http_content & PNG_IMG()
image/png
string to_string() const
转换为字符串
static const http_content & FORM_APP()
application/x-www-form-urlencoded
static const http_content & HTML_MSG()
message/html
static const http_content & XML_TEXT()
text/xml
http_content & operator=(http_content &&other) noexcept
移动赋值运算符
string content() &&noexcept
获取右值内容
static const http_content & BMP_IMG()
image/bmp
static const http_content & PLAIN_TEXT()
text/plain
static const http_content & HTML_TEXT()
text/html
static const http_method & HEAD()
HEAD方法
http_method & operator=(string &&method)
右值字符串赋值运算符
string method() &&noexcept
获取右值方法
http_method & operator=(http_method &&other) noexcept
移动赋值运算符
static const http_method & OPTIONS()
OPTIONS方法
static const http_method & POST()
POST方法
static const http_method & PATCH()
PATCH方法
static const http_method & TRACE()
TRACE方法
static const http_method & DEFAULT()
默认方法
static const http_method & GET()
GET方法
string to_string() const
转换为字符串
http_method(const string &method)
左值字符串构造函数
static const http_method & PUT()
PUT方法
http_method & operator=(const string &method)
左值字符串赋值运算符
http_method operator&(const http_method &rhs) const &
方法组合操作符
http_method(string &&method)
右值字符串构造函数
const string & method() const &noexcept
获取左值方法
static const http_method & DELETE()
DELETE方法
static const http_method & CONNECT()
CONNECT方法
http_method(http_method &&other) noexcept
移动构造函数
可字符串化接口