MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
http_client.hpp
1#ifndef MSTL_NETWORK_HTTP_HTTP_CLIENT_HPP__
2#define MSTL_NETWORK_HTTP_HTTP_CLIENT_HPP__
3#include "../dns/dns_client.hpp"
4#include "../tcp_client.hpp"
5#include "../url.hpp"
6#include "http_client_message.hpp"
8
9class MSTL_API http_client {
10private:
11 tcp_client client_;
12 unordered_map<string, cookie> cookie_jar_; // key: cookie_name@domain_path
13 uint16_t max_redirect_ = 5;
14
15 string build_request_str(const http_client_request& req, const url& req_url) const;
16 bool send_request(string_view request_str);
17 bool read_response(string& out_data) const;
18 static bool parse_response(string_view resp_str, http_client_response& resp);
19
20 static cookie parse_set_cookie(string_view str, string default_domain, string default_path) ;
21 void update_cookies(const vector<cookie>& resp_cookies, const url& request_url);
22 string build_cookie_header(const url& request_url) const;
23
24 static bool parse_chunked_body(string_view chunked, string& decoded);
25
26public:
27 explicit http_client() = default;
28 explicit http_client(dns_client dns) : client_(_MSTL move(dns)) {}
29#ifdef MSTL_SUPPORT_OPENSSL__
30 explicit http_client(ssl_context ctx) : client_(_MSTL move(ctx)) {}
31#endif
32 ~http_client() = default;
33
34 void set_max_redirect(const uint16_t max) { max_redirect_ = max; }
35#ifdef MSTL_SUPPORT_OPENSSL__
36 void set_ssl_context(ssl_context ctx) { client_.set_ssl_context(_MSTL move(ctx)); }
37#endif
38
39 MSTL_NODISCARD http_client_response request(http_client_request req);
40
41 void close() noexcept { client_.close(); }
42};
43
45#endif // MSTL_NETWORK_HTTP_HTTP_CLIENT_HPP__
constexpr const T & max(const T &a, const T &b, Compare comp) noexcept(noexcept(comp(a, b)))
返回两个值中的较大者
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)
移动范围元素