@ossiana/node-libcurl 1.0.6-alpha-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/binding.gyp +89 -0
- package/dist/src/export.d.ts +3 -0
- package/dist/src/export.js +8 -0
- package/dist/src/export.js.map +1 -0
- package/dist/src/fetch.d.ts +32 -0
- package/dist/src/fetch.js +90 -0
- package/dist/src/fetch.js.map +1 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +5 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/libcurl.d.ts +31 -0
- package/dist/src/libcurl.js +149 -0
- package/dist/src/libcurl.js.map +1 -0
- package/index.js +1 -0
- package/package.json +29 -0
- package/readme.md +68 -0
- package/src/export.ts +4 -0
- package/src/fetch.ts +116 -0
- package/src/libcurl/bao_curl.cpp +263 -0
- package/src/libcurl/bao_curl_node_addon.cpp +427 -0
- package/src/libcurl/curlAsyncWorker.cpp +31 -0
- package/src/libcurl/include/bao_curl.h +82 -0
- package/src/libcurl/include/curl/Makefile.am +39 -0
- package/src/libcurl/include/curl/curl.h +3103 -0
- package/src/libcurl/include/curl/curlver.h +77 -0
- package/src/libcurl/include/curl/easy.h +123 -0
- package/src/libcurl/include/curl/mprintf.h +50 -0
- package/src/libcurl/include/curl/multi.h +457 -0
- package/src/libcurl/include/curl/options.h +68 -0
- package/src/libcurl/include/curl/stdcheaders.h +33 -0
- package/src/libcurl/include/curl/system.h +504 -0
- package/src/libcurl/include/curl/typecheck-gcc.h +707 -0
- package/src/libcurl/include/curl/urlapi.h +145 -0
- package/src/libcurl/include/curlAysncWorker.h +14 -0
- package/src/libcurl/include/request_tls_utils.h +30 -0
- package/src/libcurl/include/utils.h +13 -0
- package/src/libcurl/lib/Release/libcurl.dll +0 -0
- package/src/libcurl/lib/Release/libcurl_imp.lib +0 -0
- package/src/libcurl/utils.cpp +41 -0
- package/src/libcurl.ts +235 -0
- package/tsconfig.json +31 -0
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
#include "bao_curl.h"
|
|
2
|
+
|
|
3
|
+
#define CHECK_CURLOK(e) \
|
|
4
|
+
{ \
|
|
5
|
+
CURLcode code = (e); \
|
|
6
|
+
(code != CURLcode::CURLE_OK) && (printf("CURL Error:%s\n", curl_easy_strerror(code))); \
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
size_t write_func(void *ptr, size_t size, size_t nmemb, std::string &stream)
|
|
10
|
+
{
|
|
11
|
+
const size_t resize = size * nmemb;
|
|
12
|
+
std::string html_data(reinterpret_cast<const char *>(ptr), size * nmemb);
|
|
13
|
+
stream += html_data;
|
|
14
|
+
return resize;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
BaoCurl::BaoCurl()
|
|
18
|
+
{
|
|
19
|
+
this->init();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
BaoCurl::~BaoCurl()
|
|
23
|
+
{
|
|
24
|
+
assert(this->m_pCURL);
|
|
25
|
+
curl_easy_cleanup(this->m_pCURL);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
void BaoCurl::init()
|
|
29
|
+
{
|
|
30
|
+
this->m_pCURL = curl_easy_init();
|
|
31
|
+
assert(this->m_pCURL);
|
|
32
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_SSL_VERIFYPEER, 0L));
|
|
33
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_SSL_VERIFYHOST, 0L));
|
|
34
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_SSL_CIPHER_LIST, "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DH-RSA-AES128-GCM-SHA256:AES128-SHA:AES256-SHA"));
|
|
35
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_SSL_EC_CURVES, "X25519:P-256:P-384"));
|
|
36
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_ACCEPT_ENCODING, ""));
|
|
37
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1));
|
|
38
|
+
this->setTimeout(15, 15);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
void BaoCurl::open(std::string method, std::string url)
|
|
42
|
+
{
|
|
43
|
+
this->m_method = method;
|
|
44
|
+
this->m_stream.header = "";
|
|
45
|
+
this->m_stream.responseText = "";
|
|
46
|
+
this->m_bProxy = false;
|
|
47
|
+
this->m_bIsHttps = url.substr(0, 5) == "https";
|
|
48
|
+
if (this->m_pHeaders)
|
|
49
|
+
{
|
|
50
|
+
curl_slist_free_all(this->m_pHeaders);
|
|
51
|
+
this->m_pHeaders = NULL;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_COOKIELIST, ""));
|
|
55
|
+
|
|
56
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_URL, url.c_str()));
|
|
57
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_WRITEFUNCTION, &write_func));
|
|
58
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_WRITEDATA, &this->m_stream.responseText));
|
|
59
|
+
|
|
60
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_HEADERFUNCTION, &write_func));
|
|
61
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_HEADERDATA, &this->m_stream.header));
|
|
62
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_POST, method == "POST"));
|
|
63
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_CUSTOMREQUEST, method.c_str()));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
void BaoCurl::setRequestHeader(std::string key, std::string value)
|
|
67
|
+
{
|
|
68
|
+
this->m_pHeaders = curl_slist_append(this->m_pHeaders, (key + ": " + value).c_str());
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
void BaoCurl::setRequestHeader(std::string keyValue)
|
|
72
|
+
{
|
|
73
|
+
this->m_pHeaders = curl_slist_append(this->m_pHeaders, keyValue.c_str());
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
void BaoCurl::setRequestHeaders(std::string header)
|
|
77
|
+
{
|
|
78
|
+
auto arr = StringSplit(header, "\n");
|
|
79
|
+
for (auto arr_b = arr.begin(); arr_b != arr.end(); ++arr_b)
|
|
80
|
+
{
|
|
81
|
+
std::string arr_mem = *arr_b;
|
|
82
|
+
if (arr_mem.size() == 0)
|
|
83
|
+
{
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
setRequestHeader(arr_mem);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
void BaoCurl::setProxy(std::string proxy)
|
|
91
|
+
{
|
|
92
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_PROXY, proxy.c_str()));
|
|
93
|
+
this->m_bProxy = true;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
void BaoCurl::setProxy(std::string proxy, std::string username,
|
|
97
|
+
std::string password)
|
|
98
|
+
{
|
|
99
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_PROXY, proxy.c_str()));
|
|
100
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_PROXYUSERPWD, username + ":" + password));
|
|
101
|
+
this->m_bProxy = true;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
void BaoCurl::setTimeout(
|
|
105
|
+
int connectTime,
|
|
106
|
+
int sendTime)
|
|
107
|
+
{
|
|
108
|
+
/*
|
|
109
|
+
CURLOPT_CONNECTTIMEOUT:连接对方主机时的最长等待时间,此设置限制的是建立连接过程的时间,其它过程的时间不在控制范围
|
|
110
|
+
CURLOPT_TIMEOUT:整个cURL函数执行过程的最长等待时间,也就是说,这个时间是包含连接等待时间的
|
|
111
|
+
CURLOPT_TIMEOUT的值应比CURLOPT_CONNECTTIMEOUT大
|
|
112
|
+
*/
|
|
113
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_CONNECTTIMEOUT, connectTime));
|
|
114
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_TIMEOUT, sendTime));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
void BaoCurl::send()
|
|
118
|
+
{
|
|
119
|
+
BaoCurl::send("");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
void BaoCurl::send(std::string data)
|
|
123
|
+
{
|
|
124
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_HTTPHEADER, this->m_pHeaders));
|
|
125
|
+
if (this->m_bProxy)
|
|
126
|
+
{
|
|
127
|
+
if (this->m_bIsHttps)
|
|
128
|
+
{
|
|
129
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_HTTPPROXYTUNNEL, 1L));
|
|
130
|
+
}
|
|
131
|
+
else
|
|
132
|
+
{
|
|
133
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_PROXYTYPE, CURLPROXY_HTTP));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (this->m_method == "POST" || this->m_method == "PUT" || this->m_method == "PATCH")
|
|
137
|
+
{
|
|
138
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_POSTFIELDS, data.c_str()));
|
|
139
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_POSTFIELDSIZE, data.size()));
|
|
140
|
+
}
|
|
141
|
+
else
|
|
142
|
+
{
|
|
143
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_NOBODY, data.size()));
|
|
144
|
+
}
|
|
145
|
+
CHECK_CURLOK(curl_easy_perform(this->m_pCURL));
|
|
146
|
+
curl_slist_free_all(this->m_pHeaders);
|
|
147
|
+
this->m_pHeaders = NULL;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
void BaoCurl::sendByte(const char *data, const int len)
|
|
151
|
+
{
|
|
152
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_HTTPHEADER, this->m_pHeaders));
|
|
153
|
+
if (this->m_bProxy)
|
|
154
|
+
{
|
|
155
|
+
if (this->m_bIsHttps)
|
|
156
|
+
{
|
|
157
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_HTTPPROXYTUNNEL, 1L));
|
|
158
|
+
}
|
|
159
|
+
else
|
|
160
|
+
{
|
|
161
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_PROXYTYPE, CURLPROXY_HTTP));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (this->m_method == "POST" || this->m_method == "PUT" || this->m_method == "PATCH")
|
|
166
|
+
{
|
|
167
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_POSTFIELDS, data));
|
|
168
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_POSTFIELDSIZE, len));
|
|
169
|
+
}
|
|
170
|
+
CHECK_CURLOK(curl_easy_perform(this->m_pCURL));
|
|
171
|
+
curl_slist_free_all(this->m_pHeaders);
|
|
172
|
+
this->m_pHeaders = NULL;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
void BaoCurl::setCookie(std::string key, std::string value, std::string domain)
|
|
176
|
+
{
|
|
177
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_COOKIELIST, StringFormat("%s\tTRUE\t/\tFALSE\t3000000000\t%s\t%s", domain.c_str(), key.c_str(), value.c_str()).c_str()));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
void BaoCurl::removeCookie(std::string key, std::string domain)
|
|
181
|
+
{
|
|
182
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_COOKIELIST, StringFormat("%s\tTRUE\t/\tFALSE\t0\t%s\t%s", domain.c_str(), key.c_str(), "").c_str()));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
std::string BaoCurl::getCookies()
|
|
186
|
+
{
|
|
187
|
+
struct curl_slist *cookies = NULL;
|
|
188
|
+
std::string str;
|
|
189
|
+
CHECK_CURLOK(curl_easy_getinfo(this->m_pCURL, CURLINFO_COOKIELIST, &cookies));
|
|
190
|
+
if (!cookies)
|
|
191
|
+
{
|
|
192
|
+
return "";
|
|
193
|
+
}
|
|
194
|
+
do
|
|
195
|
+
{
|
|
196
|
+
str += cookies->data;
|
|
197
|
+
str += "\n";
|
|
198
|
+
cookies = cookies->next;
|
|
199
|
+
} while (cookies);
|
|
200
|
+
return str;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
std::string BaoCurl::getCookie(std::string key)
|
|
204
|
+
{
|
|
205
|
+
struct curl_slist *cookies = NULL;
|
|
206
|
+
std::string str = "";
|
|
207
|
+
CHECK_CURLOK(curl_easy_getinfo(this->m_pCURL, CURLINFO_COOKIELIST, &cookies));
|
|
208
|
+
if (!cookies)
|
|
209
|
+
{
|
|
210
|
+
return "";
|
|
211
|
+
}
|
|
212
|
+
do
|
|
213
|
+
{
|
|
214
|
+
str = std::string(cookies->data);
|
|
215
|
+
size_t pos = str.find(key);
|
|
216
|
+
if (pos != -1)
|
|
217
|
+
{
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
cookies = cookies->next;
|
|
221
|
+
} while (cookies);
|
|
222
|
+
curl_slist_free_all(cookies);
|
|
223
|
+
return str;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
long BaoCurl::getResponseStatus()
|
|
227
|
+
{
|
|
228
|
+
long http_code;
|
|
229
|
+
CHECK_CURLOK(curl_easy_getinfo(this->m_pCURL, CURLINFO_RESPONSE_CODE, &http_code));
|
|
230
|
+
return http_code;
|
|
231
|
+
}
|
|
232
|
+
void BaoCurl::reset()
|
|
233
|
+
{
|
|
234
|
+
curl_easy_cleanup(this->m_pCURL);
|
|
235
|
+
this->init();
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
void BaoCurl::setRedirect(bool isAllow)
|
|
239
|
+
{
|
|
240
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_FOLLOWLOCATION, isAllow));
|
|
241
|
+
}
|
|
242
|
+
void BaoCurl::printInnerLogger()
|
|
243
|
+
{
|
|
244
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_VERBOSE, 1L));
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
void BaoCurl::setHttpVersion(BaoCurl::HttpVersion version)
|
|
248
|
+
{
|
|
249
|
+
int temp = CURL_HTTP_VERSION_1_1;
|
|
250
|
+
switch (version)
|
|
251
|
+
{
|
|
252
|
+
case BaoCurl::HttpVersion::http1_1:
|
|
253
|
+
temp = CURL_HTTP_VERSION_1_1;
|
|
254
|
+
break;
|
|
255
|
+
case BaoCurl::HttpVersion::http2:
|
|
256
|
+
temp = CURL_HTTP_VERSION_2;
|
|
257
|
+
break;
|
|
258
|
+
default:
|
|
259
|
+
printf("error httpVersion!\n");
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_HTTP_VERSION, temp));
|
|
263
|
+
}
|
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
#include <napi.h>
|
|
2
|
+
#include <iostream>
|
|
3
|
+
#include "bao_curl.h"
|
|
4
|
+
#include "request_tls_utils.h"
|
|
5
|
+
#include "curlAysncWorker.h"
|
|
6
|
+
|
|
7
|
+
using namespace std;
|
|
8
|
+
|
|
9
|
+
void initLibCurl()
|
|
10
|
+
{
|
|
11
|
+
curl_global_init(CURL_GLOBAL_ALL);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
void uninitLibCurl()
|
|
15
|
+
{
|
|
16
|
+
curl_global_cleanup();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
class BaoLibCurlWarp : public Napi::ObjectWrap<BaoLibCurlWarp>
|
|
20
|
+
{
|
|
21
|
+
public:
|
|
22
|
+
static Napi::Object Init(Napi::Env env, Napi::Object exports);
|
|
23
|
+
BaoLibCurlWarp(const Napi::CallbackInfo &info);
|
|
24
|
+
~BaoLibCurlWarp();
|
|
25
|
+
// static Napi::Value CreateNewItem(const Napi::CallbackInfo& info);
|
|
26
|
+
|
|
27
|
+
private:
|
|
28
|
+
BaoCurl m_curl;
|
|
29
|
+
Napi::Value open(const Napi::CallbackInfo &info);
|
|
30
|
+
Napi::Value setRequestHeader(const Napi::CallbackInfo &info);
|
|
31
|
+
Napi::Value setRequestHeaders(const Napi::CallbackInfo &info);
|
|
32
|
+
Napi::Value setProxy(const Napi::CallbackInfo &info);
|
|
33
|
+
Napi::Value setTimeout(const Napi::CallbackInfo &info);
|
|
34
|
+
Napi::Value setCookie(const Napi::CallbackInfo &info);
|
|
35
|
+
Napi::Value removeCookie(const Napi::CallbackInfo &info);
|
|
36
|
+
Napi::Value getCookies(const Napi::CallbackInfo &info);
|
|
37
|
+
Napi::Value getCookie(const Napi::CallbackInfo &info);
|
|
38
|
+
Napi::Value getResponseStatus(const Napi::CallbackInfo &info);
|
|
39
|
+
Napi::Value reset(const Napi::CallbackInfo &info);
|
|
40
|
+
Napi::Value setRedirect(const Napi::CallbackInfo &info);
|
|
41
|
+
Napi::Value printInnerLogger(const Napi::CallbackInfo &info);
|
|
42
|
+
Napi::Value setHttpVersion(const Napi::CallbackInfo &info);
|
|
43
|
+
Napi::Value send(const Napi::CallbackInfo &info);
|
|
44
|
+
Napi::Value sendAsync(const Napi::CallbackInfo &info);
|
|
45
|
+
Napi::Value getResponseBody(const Napi::CallbackInfo &info);
|
|
46
|
+
Napi::Value getResponseString(const Napi::CallbackInfo &info);
|
|
47
|
+
Napi::Value getResponseHeaders(const Napi::CallbackInfo &info);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
Napi::Object BaoLibCurlWarp::Init(Napi::Env env, Napi::Object exports)
|
|
51
|
+
{
|
|
52
|
+
// This method is used to hook the accessor and method callbacks
|
|
53
|
+
std::vector<Napi::ClassPropertyDescriptor<BaoLibCurlWarp>> methodList = {
|
|
54
|
+
InstanceMethod<&BaoLibCurlWarp::open>("open", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::setRequestHeader>("setRequestHeader", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::setRequestHeaders>("setRequestHeaders", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::setProxy>("setProxy", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::setTimeout>("setTimeout", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::setCookie>("setCookie", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::removeCookie>("removeCookie", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::getCookies>("getCookies", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::getCookie>("getCookie", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::getResponseStatus>("getResponseStatus", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::reset>("reset", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::setRedirect>("setRedirect", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::printInnerLogger>("printInnerLogger", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::setHttpVersion>("setHttpVersion", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::send>("send", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
|
|
55
|
+
InstanceMethod<&BaoLibCurlWarp::getResponseBody>("getResponseBody", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
|
|
56
|
+
InstanceMethod<&BaoLibCurlWarp::getResponseString>("getResponseString", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
|
|
57
|
+
InstanceMethod<&BaoLibCurlWarp::sendAsync>("sendAsync", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
|
|
58
|
+
InstanceMethod<&BaoLibCurlWarp::getResponseHeaders>("getResponseHeaders", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
|
|
59
|
+
// StaticMethod<&BaoLibCurlWarp::CreateNewItem>("CreateNewItem", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
|
|
60
|
+
};
|
|
61
|
+
Napi::Function func = DefineClass(env, "BaoLibCurl", methodList);
|
|
62
|
+
auto constructor = Napi::Persistent(func);
|
|
63
|
+
constructor.SuppressDestruct();
|
|
64
|
+
exports.Set("BaoLibCurl", func);
|
|
65
|
+
return exports;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
BaoLibCurlWarp::BaoLibCurlWarp(const Napi::CallbackInfo &info)
|
|
69
|
+
: Napi::ObjectWrap<BaoLibCurlWarp>(info)
|
|
70
|
+
{
|
|
71
|
+
// cout << "BaoLibCurlWarp Initilaze" << endl;
|
|
72
|
+
// this->m_curl.printInnerLogger();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
BaoLibCurlWarp::~BaoLibCurlWarp()
|
|
76
|
+
{
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/*
|
|
80
|
+
open(method, url)
|
|
81
|
+
*/
|
|
82
|
+
Napi::Value BaoLibCurlWarp::open(const Napi::CallbackInfo &info)
|
|
83
|
+
{
|
|
84
|
+
Napi::Env env = info.Env();
|
|
85
|
+
size_t argsLen = info.Length();
|
|
86
|
+
REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "open", 2, argsLen)
|
|
87
|
+
REQUEST_TLS_METHOD_CHECK(env, info[0].IsString(), "argument 0 is not a string")
|
|
88
|
+
REQUEST_TLS_METHOD_CHECK(env, info[1].IsString(), "argument 1 is not a string")
|
|
89
|
+
string method = info[0].As<Napi::String>().Utf8Value();
|
|
90
|
+
string url = info[1].As<Napi::String>().Utf8Value();
|
|
91
|
+
this->m_curl.open(method, url);
|
|
92
|
+
return env.Undefined();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/*
|
|
96
|
+
setRequestHeader(key,value)
|
|
97
|
+
*/
|
|
98
|
+
Napi::Value BaoLibCurlWarp::setRequestHeader(const Napi::CallbackInfo &info)
|
|
99
|
+
{
|
|
100
|
+
Napi::Env env = info.Env();
|
|
101
|
+
size_t argsLen = info.Length();
|
|
102
|
+
REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "setRequestHeader", 2, argsLen)
|
|
103
|
+
REQUEST_TLS_METHOD_CHECK(env, info[0].IsString(), "argument 0 is not a string")
|
|
104
|
+
REQUEST_TLS_METHOD_CHECK(env, info[1].IsString(), "argument 1 is not a string")
|
|
105
|
+
|
|
106
|
+
string key = info[0].As<Napi::String>().Utf8Value();
|
|
107
|
+
string value = info[1].As<Napi::String>().Utf8Value();
|
|
108
|
+
if (value == "")
|
|
109
|
+
{
|
|
110
|
+
REQUEST_TLS_METHOD_CHECK(env, key != "", "key and value are empty")
|
|
111
|
+
// if the value is empty then only set key;
|
|
112
|
+
this->m_curl.setRequestHeaders(key + ";");
|
|
113
|
+
return env.Undefined();
|
|
114
|
+
}
|
|
115
|
+
this->m_curl.setRequestHeader(key, value);
|
|
116
|
+
return env.Undefined();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/*
|
|
120
|
+
setRequestHeaders(headers)
|
|
121
|
+
*/
|
|
122
|
+
Napi::Value BaoLibCurlWarp::setRequestHeaders(const Napi::CallbackInfo &info)
|
|
123
|
+
{
|
|
124
|
+
Napi::Env env = info.Env();
|
|
125
|
+
size_t argsLen = info.Length();
|
|
126
|
+
REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "setRequestHeaders", 1, argsLen)
|
|
127
|
+
REQUEST_TLS_METHOD_CHECK(env, info[0].IsString(), "argument 0 is not a string")
|
|
128
|
+
string headers = info[0].As<Napi::String>().Utf8Value();
|
|
129
|
+
this->m_curl.setRequestHeaders(headers);
|
|
130
|
+
return env.Undefined();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/*
|
|
134
|
+
setProxy(proxy)
|
|
135
|
+
setProxy(proxy,username,password)
|
|
136
|
+
*/
|
|
137
|
+
Napi::Value BaoLibCurlWarp::setProxy(const Napi::CallbackInfo &info)
|
|
138
|
+
{
|
|
139
|
+
Napi::Env env = info.Env();
|
|
140
|
+
size_t argsLen = info.Length();
|
|
141
|
+
REQUEST_TLS_METHOD_ARGS_TOO_MUCH_CHECK(env, "BaoCurl", "setProxy", 3, argsLen)
|
|
142
|
+
REQUEST_TLS_METHOD_CHECK(env, info[0].IsString(), "argument 0 is not a string")
|
|
143
|
+
string proxy = info[0].As<Napi::String>().Utf8Value();
|
|
144
|
+
if (argsLen == 1)
|
|
145
|
+
{
|
|
146
|
+
this->m_curl.setProxy(proxy);
|
|
147
|
+
return env.Undefined();
|
|
148
|
+
}
|
|
149
|
+
else if (argsLen == 3)
|
|
150
|
+
{
|
|
151
|
+
REQUEST_TLS_METHOD_CHECK(env, info[1].IsString(), "argument 1 is not a string")
|
|
152
|
+
REQUEST_TLS_METHOD_CHECK(env, info[2].IsString(), "argument 2 is not a string")
|
|
153
|
+
// if take the username and password
|
|
154
|
+
string username = info[1].As<Napi::String>().Utf8Value();
|
|
155
|
+
string password = info[2].As<Napi::String>().Utf8Value();
|
|
156
|
+
this->m_curl.setProxy(proxy, username, password);
|
|
157
|
+
return env.Undefined();
|
|
158
|
+
}
|
|
159
|
+
REQUEST_TLS_METHOD_ARGS_NO_CONFIG(env, "BaoCurl", "setProxy", "1 or 2", argsLen)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/*
|
|
163
|
+
setTimeout(connectTime, sendTime)
|
|
164
|
+
*/
|
|
165
|
+
Napi::Value BaoLibCurlWarp::setTimeout(const Napi::CallbackInfo &info)
|
|
166
|
+
{
|
|
167
|
+
Napi::Env env = info.Env();
|
|
168
|
+
size_t argsLen = info.Length();
|
|
169
|
+
REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "setTimeout", 2, argsLen)
|
|
170
|
+
REQUEST_TLS_METHOD_CHECK(env, info[0].IsNumber(), "argument 0 is not a number")
|
|
171
|
+
REQUEST_TLS_METHOD_CHECK(env, info[1].IsNumber(), "argument 1 is not a number")
|
|
172
|
+
int32_t connectTime = info[0].As<Napi::Number>().Int32Value();
|
|
173
|
+
int32_t sendTime = info[1].As<Napi::Number>().Int32Value();
|
|
174
|
+
this->m_curl.setTimeout(connectTime, sendTime);
|
|
175
|
+
return env.Undefined();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/*
|
|
179
|
+
setCookie(key, value, domain)
|
|
180
|
+
*/
|
|
181
|
+
Napi::Value BaoLibCurlWarp::setCookie(const Napi::CallbackInfo &info)
|
|
182
|
+
{
|
|
183
|
+
Napi::Env env = info.Env();
|
|
184
|
+
size_t argsLen = info.Length();
|
|
185
|
+
REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "setCookie", 3, argsLen)
|
|
186
|
+
REQUEST_TLS_METHOD_CHECK(env, info[0].IsString(), "argument 0 is not a string")
|
|
187
|
+
REQUEST_TLS_METHOD_CHECK(env, info[1].IsString(), "argument 1 is not a string")
|
|
188
|
+
REQUEST_TLS_METHOD_CHECK(env, info[2].IsString(), "argument 2 is not a string")
|
|
189
|
+
string key = info[0].As<Napi::String>().Utf8Value();
|
|
190
|
+
string value = info[1].As<Napi::String>().Utf8Value();
|
|
191
|
+
string domain = info[2].As<Napi::String>().Utf8Value();
|
|
192
|
+
this->m_curl.setCookie(key, value, domain);
|
|
193
|
+
return env.Undefined();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/*
|
|
197
|
+
removeCookie(key, domain)
|
|
198
|
+
*/
|
|
199
|
+
Napi::Value BaoLibCurlWarp::removeCookie(const Napi::CallbackInfo &info)
|
|
200
|
+
{
|
|
201
|
+
Napi::Env env = info.Env();
|
|
202
|
+
size_t argsLen = info.Length();
|
|
203
|
+
REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "removeCookie", 2, argsLen)
|
|
204
|
+
string key = info[0].As<Napi::String>().Utf8Value();
|
|
205
|
+
string domain = info[1].As<Napi::String>().Utf8Value();
|
|
206
|
+
this->m_curl.removeCookie(key, domain);
|
|
207
|
+
return env.Undefined();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/*
|
|
211
|
+
getCookies()
|
|
212
|
+
*/
|
|
213
|
+
Napi::Value BaoLibCurlWarp::getCookies(const Napi::CallbackInfo &info)
|
|
214
|
+
{
|
|
215
|
+
Napi::Env env = info.Env();
|
|
216
|
+
size_t argsLen = info.Length();
|
|
217
|
+
|
|
218
|
+
return Napi::String::New(env, this->m_curl.getCookies());
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/*
|
|
222
|
+
getCookie(key)
|
|
223
|
+
*/
|
|
224
|
+
Napi::Value BaoLibCurlWarp::getCookie(const Napi::CallbackInfo &info)
|
|
225
|
+
{
|
|
226
|
+
Napi::Env env = info.Env();
|
|
227
|
+
size_t argsLen = info.Length();
|
|
228
|
+
REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "getCookie", 1, argsLen);
|
|
229
|
+
REQUEST_TLS_METHOD_CHECK(env, info[0].IsString(), "argument 0 is not a string")
|
|
230
|
+
return Napi::String::New(env, this->m_curl.getCookie(
|
|
231
|
+
info[0].As<Napi::String>().Utf8Value()));
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/*
|
|
235
|
+
getResponseStatus()
|
|
236
|
+
*/
|
|
237
|
+
Napi::Value BaoLibCurlWarp::getResponseStatus(const Napi::CallbackInfo &info)
|
|
238
|
+
{
|
|
239
|
+
Napi::Env env = info.Env();
|
|
240
|
+
size_t argsLen = info.Length();
|
|
241
|
+
|
|
242
|
+
return Napi::Number::New(env, this->m_curl.getResponseStatus());
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/*
|
|
246
|
+
reset()
|
|
247
|
+
*/
|
|
248
|
+
Napi::Value BaoLibCurlWarp::reset(const Napi::CallbackInfo &info)
|
|
249
|
+
{
|
|
250
|
+
Napi::Env env = info.Env();
|
|
251
|
+
size_t argsLen = info.Length();
|
|
252
|
+
|
|
253
|
+
this->m_curl.reset();
|
|
254
|
+
return env.Undefined();
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/*
|
|
258
|
+
setRedirect(isAllow)
|
|
259
|
+
*/
|
|
260
|
+
Napi::Value BaoLibCurlWarp::setRedirect(const Napi::CallbackInfo &info)
|
|
261
|
+
{
|
|
262
|
+
Napi::Env env = info.Env();
|
|
263
|
+
size_t argsLen = info.Length();
|
|
264
|
+
REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "setRedirect", 1, argsLen)
|
|
265
|
+
REQUEST_TLS_METHOD_CHECK(env, info[0].IsBoolean(), "argument 0 is not a boolean")
|
|
266
|
+
this->m_curl.setRedirect(
|
|
267
|
+
info[0].As<Napi::Boolean>().Value());
|
|
268
|
+
return env.Undefined();
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/*
|
|
272
|
+
printInnerLogger()
|
|
273
|
+
*/
|
|
274
|
+
Napi::Value BaoLibCurlWarp::printInnerLogger(const Napi::CallbackInfo &info)
|
|
275
|
+
{
|
|
276
|
+
Napi::Env env = info.Env();
|
|
277
|
+
size_t argsLen = info.Length();
|
|
278
|
+
this->m_curl.printInnerLogger();
|
|
279
|
+
return env.Undefined();
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/*
|
|
283
|
+
setHttpVersion(double)
|
|
284
|
+
*/
|
|
285
|
+
Napi::Value BaoLibCurlWarp::setHttpVersion(const Napi::CallbackInfo &info)
|
|
286
|
+
{
|
|
287
|
+
Napi::Env env = info.Env();
|
|
288
|
+
size_t argsLen = info.Length();
|
|
289
|
+
REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "setHttpVersion", 1, argsLen);
|
|
290
|
+
REQUEST_TLS_METHOD_CHECK(env, info[0].IsNumber(), "argument 0 is not a number")
|
|
291
|
+
int32_t ver = info[0].As<Napi::Number>().Int32Value();
|
|
292
|
+
if (ver == 0)
|
|
293
|
+
{
|
|
294
|
+
this->m_curl.setHttpVersion(BaoCurl::HttpVersion::http1_1);
|
|
295
|
+
}
|
|
296
|
+
else if (ver == 1)
|
|
297
|
+
{
|
|
298
|
+
this->m_curl.setHttpVersion(BaoCurl::HttpVersion::http2);
|
|
299
|
+
}
|
|
300
|
+
else
|
|
301
|
+
{
|
|
302
|
+
REQUEST_TLS_METHOD_THROW(env, "BaoCurl", "setHttpVersion", "Version Not Support.")
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return env.Undefined();
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/*
|
|
309
|
+
send()
|
|
310
|
+
*/
|
|
311
|
+
Napi::Value BaoLibCurlWarp::send(const Napi::CallbackInfo &info)
|
|
312
|
+
{
|
|
313
|
+
Napi::Env env = info.Env();
|
|
314
|
+
size_t argsLen = info.Length();
|
|
315
|
+
if (argsLen == 0 || info[0].IsNull())
|
|
316
|
+
{
|
|
317
|
+
this->m_curl.send();
|
|
318
|
+
return env.Undefined();
|
|
319
|
+
}
|
|
320
|
+
REQUEST_TLS_METHOD_ARGS_TOO_MUCH_CHECK(env, "BaoCurl", "send", 1, argsLen)
|
|
321
|
+
if (info[0].IsTypedArray())
|
|
322
|
+
{
|
|
323
|
+
Napi::Uint8Array u8Arr = info[0].As<Napi::Uint8Array>();
|
|
324
|
+
uint8_t *utf8Buffer = u8Arr.Data();
|
|
325
|
+
this->m_curl.sendByte(reinterpret_cast<const char *>(utf8Buffer), u8Arr.ByteLength());
|
|
326
|
+
return env.Undefined();
|
|
327
|
+
}
|
|
328
|
+
if (info[0].IsString())
|
|
329
|
+
{
|
|
330
|
+
string str = info[0].As<Napi::String>().Utf8Value();
|
|
331
|
+
this->m_curl.send(str);
|
|
332
|
+
return env.Undefined();
|
|
333
|
+
}
|
|
334
|
+
vector<Napi::Value> argsList{info[0].As<Napi::Value>()};
|
|
335
|
+
string jsonStr = env.Global().Get("JSON").As<Napi::Object>().Get("stringify").As<Napi::Function>().Call(argsList).As<Napi::String>().Utf8Value();
|
|
336
|
+
this->m_curl.send(jsonStr);
|
|
337
|
+
return env.Undefined();
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/*
|
|
341
|
+
sendAsync()
|
|
342
|
+
*/
|
|
343
|
+
Napi::Value BaoLibCurlWarp::sendAsync(const Napi::CallbackInfo &info)
|
|
344
|
+
{
|
|
345
|
+
Napi::Env env = info.Env();
|
|
346
|
+
size_t argsLen = info.Length();
|
|
347
|
+
curlAsyncWorker *workerPtr;
|
|
348
|
+
if (argsLen == 1 || info[0].IsFunction())
|
|
349
|
+
{
|
|
350
|
+
workerPtr = new curlAsyncWorker(info[0].As<Napi::Function>(), this->m_curl, Napi::Uint8Array::New(env, 1));
|
|
351
|
+
workerPtr->Queue();
|
|
352
|
+
return env.Undefined();
|
|
353
|
+
}
|
|
354
|
+
if (argsLen > 2)
|
|
355
|
+
{
|
|
356
|
+
REQUEST_TLS_METHOD_ARGS_TOO_MUCH_CHECK(env, "BaoCurl", "send", 2, argsLen)
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (info[0].IsTypedArray())
|
|
360
|
+
{
|
|
361
|
+
Napi::Uint8Array u8Arr = info[0].As<Napi::Uint8Array>();
|
|
362
|
+
workerPtr = new curlAsyncWorker(info[1].As<Napi::Function>(), this->m_curl, u8Arr);
|
|
363
|
+
workerPtr->Queue();
|
|
364
|
+
// this->m_curl.sendByte(reinterpret_cast<const char *>(utf8Buffer), u8Arr.ByteLength());
|
|
365
|
+
return env.Undefined();
|
|
366
|
+
}
|
|
367
|
+
if (info[0].IsString())
|
|
368
|
+
{
|
|
369
|
+
string str = info[0].As<Napi::String>().Utf8Value();
|
|
370
|
+
size_t strLen = str.size();
|
|
371
|
+
Napi::Uint8Array u8buffer = Napi::Uint8Array::New(env, strLen);
|
|
372
|
+
memcpy_s(u8buffer.Data(), strLen, str.c_str(), strLen);
|
|
373
|
+
workerPtr = new curlAsyncWorker(info[1].As<Napi::Function>(), this->m_curl, u8buffer);
|
|
374
|
+
workerPtr->Queue();
|
|
375
|
+
return env.Undefined();
|
|
376
|
+
}
|
|
377
|
+
vector<Napi::Value> argsList{info[0].As<Napi::Value>()};
|
|
378
|
+
string jsonStr = env.Global().Get("JSON").As<Napi::Object>().Get("stringify").As<Napi::Function>().Call(argsList).As<Napi::String>().Utf8Value();
|
|
379
|
+
size_t strLen = jsonStr.size();
|
|
380
|
+
Napi::Uint8Array u8buffer = Napi::Uint8Array::New(env, strLen);
|
|
381
|
+
memcpy_s(u8buffer.Data(), strLen, jsonStr.c_str(), strLen);
|
|
382
|
+
workerPtr = new curlAsyncWorker(info[1].As<Napi::Function>(), this->m_curl, u8buffer);
|
|
383
|
+
workerPtr->Queue();
|
|
384
|
+
return env.Undefined();
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/*
|
|
388
|
+
getResponseHeaders()
|
|
389
|
+
*/
|
|
390
|
+
Napi::Value BaoLibCurlWarp::getResponseHeaders(const Napi::CallbackInfo &info)
|
|
391
|
+
{
|
|
392
|
+
Napi::Env env = info.Env();
|
|
393
|
+
size_t argsLen = info.Length();
|
|
394
|
+
return Napi::String::New(env, this->m_curl.getResponseHeaders());
|
|
395
|
+
}
|
|
396
|
+
/*
|
|
397
|
+
getResponseBody()
|
|
398
|
+
*/
|
|
399
|
+
Napi::Value BaoLibCurlWarp::getResponseBody(const Napi::CallbackInfo &info)
|
|
400
|
+
{
|
|
401
|
+
Napi::Env env = info.Env();
|
|
402
|
+
size_t argsLen = info.Length();
|
|
403
|
+
string utf8Str = this->m_curl.getResponseBody();
|
|
404
|
+
size_t utf8Size = utf8Str.size();
|
|
405
|
+
Napi::Uint8Array uint8buffer = Napi::Uint8Array::New(env, utf8Size);
|
|
406
|
+
memcpy_s((void *)uint8buffer.Data(), utf8Size, utf8Str.c_str(), utf8Size);
|
|
407
|
+
return uint8buffer;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/*
|
|
411
|
+
getResponseString()
|
|
412
|
+
*/
|
|
413
|
+
Napi::Value BaoLibCurlWarp::getResponseString(const Napi::CallbackInfo &info)
|
|
414
|
+
{
|
|
415
|
+
Napi::Env env = info.Env();
|
|
416
|
+
size_t argsLen = info.Length();
|
|
417
|
+
return Napi::String::New(env, this->m_curl.getResponseBody());
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// Initialize native add-on
|
|
421
|
+
Napi::Object Init(Napi::Env env, Napi::Object exports)
|
|
422
|
+
{
|
|
423
|
+
initLibCurl();
|
|
424
|
+
BaoLibCurlWarp::Init(env, exports);
|
|
425
|
+
return exports;
|
|
426
|
+
}
|
|
427
|
+
NODE_API_MODULE(requests_tls, Init)
|