NIM PC Cross Platform SDK
nim_tools_http_cpp.h
Go to the documentation of this file.
1
8#ifndef _NIM_TOOLS_SDK_CPP_HTTP_H_
9#define _NIM_TOOLS_SDK_CPP_HTTP_H_
10
11#include <functional>
12#include <map>
13#include <string>
14#include "nim_define_include.h"
15#include "nim_tools_http_def.h"
16
21namespace nim_http {
26typedef std::function<void(bool, int)> CompletedCallback;
27
33typedef std::function<void(bool, int, const std::string&)> ResponseCallback;
34
41typedef std::function<void(bool, int, const std::string&, const std::string&)> ResponseCallbackEx;
42
49typedef std::function<void(double, double, double, double)> ProgressCallback;
50
55typedef std::function<void(double, double)> SpeedCallback;
56
63typedef std::function<void(double, double, double, double)> TransferCallback;
64
65class HttpRequest;
66
72bool Init(const std::string& library_path = "");
73
78void Uninit();
79
85void InitLog(const std::string& log_file_path);
86
91bool IsInitLog();
92
102void SetGlobalProxy(NIMProxyType type, const std::string& host, short port, const std::string& user, const std::string& pass);
103
109HttpRequestID PostRequest(const HttpRequest& http_request);
110
116void RemoveRequest(HttpRequestID http_request_id);
117
123std::string GetResponseHead(HttpRequestID http_request_id);
124
129public:
140 HttpRequest(const std::string& url,
141 const std::string& download_file_path,
142 const CompletedCallback& complete_cb,
143 const ProgressCallback& progress_cb = ProgressCallback(),
144 const SpeedCallback& speed_cb = SpeedCallback(),
145 const TransferCallback& transfer_cb = TransferCallback());
146
158 HttpRequest(const std::string& url,
159 const std::string& download_file_path,
160 int64_t range_start,
161 const CompletedCallback& complete_cb,
162 const ProgressCallback& progress_cb = ProgressCallback(),
163 const SpeedCallback& speed_cb = SpeedCallback(),
164 const TransferCallback& transfer_cb = TransferCallback());
165
177 HttpRequest(const std::string& url,
178 const char* post_body,
179 size_t post_body_size,
180 const ResponseCallback& response_cb,
181 const ProgressCallback& progress_cb = ProgressCallback(),
182 const SpeedCallback& speed_cb = SpeedCallback(),
183 const TransferCallback& transfer_cb = TransferCallback());
184
196 HttpRequest(const std::string& url,
197 const char* post_body,
198 size_t post_body_size,
199 const ResponseCallbackEx& response_cb,
200 const ProgressCallback& progress_cb = ProgressCallback(),
201 const SpeedCallback& speed_cb = SpeedCallback(),
202 const TransferCallback& transfer_cb = TransferCallback());
203
210 void AddHeader(const std::string& key, const std::string& value);
211
217 void AddHeader(const std::map<std::string, std::string>& headers_map);
218
223 void SetMethodAsPost();
224
230 void SetTimeout(int timeout_ms);
231
238 void SetLowSpeed(int low_speed_limit, int low_speed_time);
239
249 void SetProxy(NIMProxyType type, const std::string& host, short port, const std::string& user, const std::string& pass);
250
251private:
252 static void CompletedCallbackWrapper(const void* user_data, bool is_ok, int response_code);
253 static void ResponseCallbackWrapper(const void* user_data, bool is_ok, int response_code, const char* content, const char* head);
254 static void ProgressCallbackWrapper(const void* user_data,
255 double upload_size,
256 double uploaded_size,
257 double download_size,
258 double downloaded_size);
259 static void SpeedCallbackWrapper(const void* user_data, double upload_speed, double download_speed);
260 static void TransferCallbackWrapper(const void* user_data,
261 double actual_upload_size,
262 double upload_speed,
263 double actual_download_size,
264 double download_speed);
265
266private:
267 friend HttpRequestID PostRequest(const HttpRequest& http_request);
268 HttpRequestHandle http_reuqest_handle_;
269};
270
271} // namespace nim_http
272
273#endif // _NIM_TOOLS_SDK_CPP_HTTP_H_
NIM HTTP提供的http传输相关接口
Definition: nim_tools_http_cpp.h:128
void SetTimeout(int timeout_ms)
Definition: nim_tools_http_cpp.cpp:406
void SetMethodAsPost()
Definition: nim_tools_http_cpp.cpp:402
void SetLowSpeed(int low_speed_limit, int low_speed_time)
Definition: nim_tools_http_cpp.cpp:410
void AddHeader(const std::string &key, const std::string &value)
Definition: nim_tools_http_cpp.cpp:392
void SetProxy(NIMProxyType type, const std::string &host, short port, const std::string &user, const std::string &pass)
Definition: nim_tools_http_cpp.cpp:414
HttpRequest(const std::string &url, const std::string &download_file_path, const CompletedCallback &complete_cb, const ProgressCallback &progress_cb=ProgressCallback(), const SpeedCallback &speed_cb=SpeedCallback(), const TransferCallback &transfer_cb=TransferCallback())
Definition: nim_tools_http_cpp.cpp:221
NIM提供的HTTP工具
Definition: nim_tools_http_cpp.cpp:16
HttpRequestID PostRequest(const HttpRequest &http_request)
Definition: nim_tools_http_cpp.cpp:208
std::function< void(double, double, double, double)> ProgressCallback
Definition: nim_tools_http_cpp.h:49
bool IsInitLog()
Definition: nim_tools_http_cpp.cpp:190
void RemoveRequest(HttpRequestID http_request_id)
Definition: nim_tools_http_cpp.cpp:212
std::function< void(bool, int, const std::string &, const std::string &)> ResponseCallbackEx
Definition: nim_tools_http_cpp.h:41
std::function< void(double, double, double, double)> TransferCallback
Definition: nim_tools_http_cpp.h:63
std::function< void(bool, int, const std::string &)> ResponseCallback
Definition: nim_tools_http_cpp.h:33
std::function< void(double, double)> SpeedCallback
Definition: nim_tools_http_cpp.h:55
void SetGlobalProxy(NIMProxyType type, const std::string &host, short port, const std::string &user, const std::string &pass)
Definition: nim_tools_http_cpp.cpp:200
void InitLog(const std::string &log_file_path)
Definition: nim_tools_http_cpp.cpp:186
void Uninit()
Definition: nim_tools_http_cpp.cpp:179
std::function< void(bool, int)> CompletedCallback
Definition: nim_tools_http_cpp.h:26
std::string GetResponseHead(HttpRequestID http_request_id)
Definition: nim_tools_http_cpp.cpp:215
NIM 公共数据类型定义总的包含文件
NIMProxyType
Definition: nim_global_def.h:186
NIM HTTP提供的http传输相关接口的定义
void * HttpRequestHandle
Definition: nim_tools_http_def.h:27
unsigned int HttpRequestID
Definition: nim_tools_http_def.h:32