MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
http_client_message.hpp
1#ifndef MSTL_NETWORK_HTTP_HTTP_CLIENT_MESSAGE_HPP__
2#define MSTL_NETWORK_HTTP_HTTP_CLIENT_MESSAGE_HPP__
3#include "MSTL/core/container/unordered_map.hpp"
4#include "MSTL/core/container/vector.hpp"
5#include "session.hpp"
7
8struct http_client_response {
9private:
10 string version_;
11 HTTP_STATUS status_;
12 string status_msg_;
13 unordered_map<string, vector<string>> headers_;
14 string body_;
15 vector<cookie> cookies_;
16
17public:
18 http_client_response() = default;
19 http_client_response(const http_client_response &) = delete;
20 http_client_response &operator =(const http_client_response &) = delete;
21 http_client_response(http_client_response &&) noexcept = default;
22 http_client_response &operator =(http_client_response &&) noexcept = default;
23
24 void set_version(string v) noexcept { version_ = _MSTL move(v); }
25 void set_status(const HTTP_STATUS s) noexcept { status_ = s; }
26 void set_status_msg(string msg) noexcept { status_msg_ = _MSTL move(msg); }
27
28 void add_header(const string& key, string value) {
29 headers_[key].push_back(_MSTL move(value));
30 }
31 MSTL_NODISCARD const string& header(const string &key) const noexcept {
32 static const string empty;
33 const auto it = headers_.find(key);
34 if (it == headers_.end() || it->second.empty()) return empty;
35 return it->second[0];
36 }
37 MSTL_NODISCARD const vector<string>& headers(const string &key) const noexcept {
38 static vector<string> empty;
39 const auto it = headers_.find(key);
40 return it != headers_.end() ? it->second : empty;
41 }
42 MSTL_NODISCARD const unordered_map<string, vector<string>>&
43 all_headers() const noexcept {
44 return headers_;
45 }
46
47 void append_body(string b) { body_ += _MSTL move(b); }
48 MSTL_NODISCARD const string& body() const noexcept { return body_; }
49
50 void append_cookie(cookie c) { cookies_.push_back(_MSTL move(c)); }
51 MSTL_NODISCARD const vector<cookie>& cookies() const noexcept { return cookies_; }
52
53 MSTL_NODISCARD const string& version() const noexcept { return version_; }
54 MSTL_NODISCARD HTTP_STATUS status() const noexcept { return status_; }
55 MSTL_NODISCARD const string& status_msg() const noexcept { return status_msg_; }
56};
57
58struct http_client_request {
59 HTTP_METHOD method = HTTP_METHOD::GET;
60 string host;
61 uint16_t port = 80;
62 string path = "/";
63 string version = "HTTP/1.1";
64 unordered_map<string, string> headers;
65 string body;
66
67 explicit http_client_request(string h, const uint16_t p = 80)
68 : host(_MSTL move(h)), port(p) {}
69
70 void set_header(const string& key, string value) { headers[key] = _MSTL move(value); }
71 void set_body(string b) { body = _MSTL move(b); }
72 void set_method(HTTP_METHOD m) { method = _MSTL move(m); }
73};
74
76#endif // MSTL_NETWORK_HTTP_HTTP_CLIENT_MESSAGE_HPP__
unsigned short uint16_t
16位无符号整数类型
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素
MSTL_NODISCARD MSTL_ALWAYS_INLINE constexpr bool empty(const Container &cont) noexcept(noexcept(cont.empty()))
检查容器是否为空