http_client_plus.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #pragma once
  2. #include "http_define.h"
  3. #if USE_NET_HTTP_CLIENT
  4. #include <functional>
  5. #include "http_header.h"
  6. #include "http_cookie.h"
  7. #include "make_form.h"
  8. class http_client_listener;
  9. namespace ylib
  10. {
  11. namespace network
  12. {
  13. namespace http
  14. {
  15. class client_plus :public ylib::error_base
  16. {
  17. public:
  18. static constexpr uint64 kMaxResponseBytes = 1024ULL * 1024ULL * 1024ULL; // 1GB
  19. client_plus();
  20. ~client_plus();
  21. void close();
  22. void set_timeout(uint32 connect_msec = 3000, uint32 recv_msec = 8000);
  23. bool del(const std::string& url, const std::map<std::string, std::string>& value = std::map<std::string, std::string>());
  24. bool get(const std::string& url, const std::map<std::string, std::string>& value = std::map<std::string, std::string>(), bool wait = true);
  25. bool post(const std::string& url, const std::map<std::string, std::string>& value, bool to_utf8 = false);
  26. bool post(const std::string& url, const ylib::json& value, bool to_utf8 = false);
  27. bool post(const std::string& url, const ylib::buffer& value);
  28. //bool post(const std::string& url, const http::make_form& value);
  29. bool head(const std::string& url);
  30. /// <summary>
  31. /// 连接代理服务器
  32. /// </summary>
  33. /// <param name="address"></param>
  34. /// <param name="port"></param>
  35. /// <returns></returns>
  36. void setproxy(const std::string& address,ushort port);
  37. network::http::header_list& headers_request();
  38. network::http::header_list& headers_response();
  39. uint32 status();
  40. ylib::buffer& response();
  41. /*请求路径*/
  42. inline const std::string& url() { return m_url; }
  43. /*cookie*/
  44. inline network::http::cookie& cookie() { return m_cookie; }
  45. inline void cookie(const network::http::cookie& ck) { m_cookie = ck; }
  46. /*[回调] 正在下载*/
  47. void on_down_ing(const std::function<bool(void* data, uint32 downsize, uint64 alldownsize, uint64 allsize, network::http::client_plus& client)>& callback);
  48. /*[回调] 下载结束*/
  49. void on_down_end(const std::function<void(network::http::client_plus& client)>& callback);
  50. /*[回调] 下载失败*/
  51. void on_down_failed(const std::function<void(network::http::client_plus& client)>& callback);
  52. // 清理所有Cookie缓存
  53. static void clear_all_cookies();
  54. friend class ::http_client_listener;
  55. public:
  56. uint64 m_temp[10];
  57. private:
  58. bool parseurl(std::string url);
  59. bool connect();
  60. bool init();
  61. bool request();
  62. bool post(const std::string& url);
  63. void* client();
  64. bool init_proxy();
  65. bool ensure_ssl_context();
  66. void stop_transport();
  67. void on_transport_closed();
  68. bool wait_recv(const char* timeout_msg);
  69. static std::string build_query(const std::map<std::string, std::string>& value);
  70. private:
  71. // HP客户端
  72. void* m_client;
  73. // HP客户端-SSL
  74. void* m_client_ssl;
  75. // HP监听器
  76. http_client_listener* m_listener;
  77. // 是否已初始化
  78. bool m_init;
  79. // SSL context 是否已 Setup
  80. bool m_ssl_context_ready;
  81. // 当前已连接目标(用于复用 keep-alive)
  82. std::string m_connected_host;
  83. ushort m_connected_port;
  84. bool m_connected_ssl;
  85. std::string m_connected_proxy;
  86. ushort m_connected_proxy_port;
  87. // 连接IP
  88. std::string m_ipaddress;
  89. // 连接端口
  90. ushort m_port;
  91. // HTTPS
  92. bool m_ssl;
  93. // 请求链接
  94. std::string m_url;
  95. // 请求路径
  96. std::string m_path;
  97. // 请求方式
  98. std::string m_method;
  99. // 请求数据
  100. ylib::buffer m_request_body;
  101. // [header] 请求
  102. network::http::header_list m_headers_request;
  103. // 超时时间 连接
  104. timestamp m_timeout_connect_msec;
  105. // 超时时间 接收
  106. timestamp m_timeout_recv_msec;
  107. // cookie
  108. network::http::cookie m_cookie;
  109. // 请求等待
  110. bool m_request_wait = true;
  111. #ifndef _WIN32
  112. public:
  113. #endif
  114. // 关闭
  115. bool m_close;
  116. // 代理服务器
  117. ylib::AddressPort m_proxy;
  118. };
  119. }
  120. }
  121. }
  122. #endif