-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpRequest.hpp
238 lines (201 loc) · 6.1 KB
/
HttpRequest.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#ifndef HTTP_REQUEST_HPP
#define HTTP_REQUEST_HPP
#include <string>
#include <unordered_map>
#include <sstream>
#include <curl/curl.h>
class HttpClient {
public:
enum HttpRequestMethod { Get = 0, Post, Delete, Put, Head, Patch };
HttpClient(const std::string& url) : m_url(url), curl(curl_easy_init()) {}
~HttpClient() {
if (curl) {
curl_easy_cleanup(curl);
curl = nullptr;
}
}
class Response {
public:
std::string error;
std::string body;
long response_code = 0;
bool success = false;
std::vector<std::string> receivedCookies;
std::string header;
};
HttpClient* initCurl() {
if (!curl) {
curl = curl_easy_init();
}
return this;
}
HttpClient* setUrl(const std::string& url) {
m_url = url;
return this;
}
HttpClient* setProxy(const std::string& proxyInfo) {
if (!proxyInfo.empty()) {
m_proxyInfo = proxyInfo;
}
return this;
}
HttpClient* setMethod(HttpRequestMethod method) {
if (curl) {
switch (method) {
case Get: curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); break;
case Post: curl_easy_setopt(curl, CURLOPT_POST, 1L); break;
case Delete: curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); break;
case Put: curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); break;
case Head: curl_easy_setopt(curl, CURLOPT_NOBODY, 1L); break;
case Patch: curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH"); break;
}
}
return this;
}
HttpClient* setData(const std::string& postData) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
return this;
}
HttpClient* addJson(const std::string& jsonData) {
addHeader("Content-Type", "application/json");
m_jsonData = jsonData;
return this;
}
HttpClient* addParam(const std::string& key, const std::string& value) {
param[key] = value;
return this;
}
HttpClient* setAutoRedirect(bool status) {
m_autoRedirect = status;
return this;
}
HttpClient* setVerifySSL(bool status) {
m_verifySSL = status;
return this;
}
HttpClient* setTimeout(int ms) {
m_connectTimeout = ms;
return this;
}
HttpClient* addHeader(const std::string& key, const std::string& value) {
if (!key.empty() && !value.empty()) {
header[key] = value;
}
return this;
}
HttpClient* setUserAgent(const std::string& userAgent) {
addHeader("User-Agent", userAgent);
return this;
}
HttpClient* setCookie(const std::string& key, const std::string& value) {
m_cookies[key] = value;
return this;
}
Response execute() {
Response response;
if (!curl) return response;
curl_easy_setopt(curl, CURLOPT_URL, m_url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response.body);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, writeHeaderCallback);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &response.header);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, m_connectTimeout);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.79 Safari/537.36");
if (m_autoRedirect) {
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 5L);
curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1L);
}
if (!m_proxyInfo.empty()) {
curl_easy_setopt(curl, CURLOPT_PROXY, m_proxyInfo.c_str());
}
if (!m_verifySSL) {
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
}
std::string targetData;
if (!m_jsonData.empty() || !param.empty()) {
if (!m_jsonData.empty()) {
targetData = m_jsonData;
}
else {
for (auto it = param.begin(); it != param.end(); ++it) {
targetData += it->first + "=" + it->second;
if (std::next(it) != param.end()) {
targetData += "&";
}
}
}
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, targetData.c_str());
}
struct curl_slist* chunk = nullptr;
if (!header.empty()) {
for (const auto& h : header) {
chunk = curl_slist_append(chunk, (h.first + ": " + h.second).c_str());
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
}
if (!m_cookies.empty()) {
std::string cookie_header = "Cookie: ";
for (const auto& cookie : m_cookies) {
cookie_header += cookie.first + "=" + cookie.second + "; ";
}
chunk = curl_slist_append(chunk, cookie_header.c_str());
}
CURLcode res = curl_easy_perform(curl);
if (chunk)
curl_slist_free_all(chunk);
if (res != CURLE_OK) {
response.error = curl_easy_strerror(res);
response.success = false;
}
else {
response.success = true;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response.response_code);
}
parseCookies(response);
return response;
}
private:
std::string m_url;
std::string m_jsonData;
std::string m_proxyInfo;
bool m_autoRedirect = true;
bool m_verifySSL = false;
int m_connectTimeout = 5000;
CURL* curl = nullptr;
std::unordered_map<std::string, std::string> header;
std::unordered_map<std::string, std::string> m_cookies;
std::unordered_map<std::string, std::string> param;
static size_t writeCallback(char* ptr, size_t size, size_t nmemb, std::string* data) {
data->append(ptr, size * nmemb);
return size * nmemb;
}
static void parseCookies(Response& response) {
auto headerLines = split(response.header, '\n');
for (const auto& line : headerLines) {
if (line.find("Set-Cookie:") != std::string::npos) {
auto cookie = split(line.substr(line.find(":") + 1), ';')[0];
response.receivedCookies.push_back(cookie);
}
}
}
static size_t writeHeaderCallback(void* contents, size_t size, size_t nmemb, void* userdata) {
size_t totalSize = size * nmemb;
std::string* headerData = static_cast<std::string*>(userdata);
headerData->append(static_cast<char*>(contents), totalSize);
return totalSize;
}
static std::vector<std::string> split(const std::string& s, char delim) {
std::vector<std::string> elems;
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
};
#endif