@ossiana/node-libcurl 1.0.7-alpha-3 → 1.0.8-alpha-1
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 +15 -40
- package/dist/export.d.ts +2 -1
- package/dist/export.js +3 -1
- package/dist/export.js.map +1 -1
- package/dist/fetch.d.ts +4 -13
- package/dist/fetch.js +33 -68
- package/dist/fetch.js.map +1 -1
- package/dist/libcurl.d.ts +64 -11
- package/dist/libcurl.js +87 -47
- package/dist/libcurl.js.map +1 -1
- package/dist/requests.d.ts +66 -0
- package/dist/requests.js +186 -0
- package/dist/requests.js.map +1 -0
- package/dist/utils.d.ts +4 -0
- package/dist/utils.js +69 -0
- package/dist/utils.js.map +1 -0
- package/package.json +14 -5
- package/readme.md +28 -13
- package/src/export.ts +3 -3
- package/src/fetch.ts +38 -86
- package/src/libcurl/bao_curl.cpp +58 -22
- package/src/libcurl/bao_curl_node_addon.cpp +20 -13
- package/src/libcurl/curlAsyncWorker.cpp +6 -2
- package/src/libcurl/include/bao_curl.h +41 -38
- package/src/libcurl/include/curlAysncWorker.h +2 -0
- package/src/libcurl.ts +149 -58
- package/src/requests.ts +211 -0
- package/src/utils.ts +71 -0
- package/tsconfig.json +1 -0
- package/dist/src/export.d.ts +0 -3
- package/dist/src/export.js +0 -8
- package/dist/src/export.js.map +0 -1
- package/dist/src/fetch.d.ts +0 -32
- package/dist/src/fetch.js +0 -90
- package/dist/src/fetch.js.map +0 -1
- package/dist/src/index.d.ts +0 -1
- package/dist/src/index.js +0 -18
- package/dist/src/index.js.map +0 -1
- package/dist/src/libcurl.d.ts +0 -31
- package/dist/src/libcurl.js +0 -154
- package/dist/src/libcurl.js.map +0 -1
- package/index.js +0 -1
- package/src/libcurl/lib/Release/darwin/libcrypto.a +0 -0
- package/src/libcurl/lib/Release/darwin/libcurl.a +0 -0
- package/src/libcurl/lib/Release/darwin/libssl.a +0 -0
- package/src/libcurl/lib/Release/linux/libcrypto.a +0 -0
- package/src/libcurl/lib/Release/linux/libcurl.a +0 -0
- package/src/libcurl/lib/Release/linux/libssl.a +0 -0
- package/src/libcurl/lib/Release/windows/libcurl.dll +0 -0
- package/src/libcurl/lib/Release/windows/libcurl_imp.lib +0 -0
package/src/libcurl/bao_curl.cpp
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
#include "bao_curl.h"
|
|
2
2
|
|
|
3
|
-
#define CHECK_CURLOK(e)
|
|
4
|
-
{
|
|
5
|
-
CURLcode code = (e);
|
|
6
|
-
|
|
3
|
+
#define CHECK_CURLOK(e) \
|
|
4
|
+
{ \
|
|
5
|
+
CURLcode code = (e); \
|
|
6
|
+
this->m_lastCode = code; \
|
|
7
|
+
if (this->m_verbose && code != CURLcode::CURLE_OK) \
|
|
8
|
+
{ \
|
|
9
|
+
printf("CURL Error:%s\n", curl_easy_strerror(code)); \
|
|
10
|
+
} \
|
|
7
11
|
}
|
|
8
12
|
|
|
9
13
|
size_t write_func(void *ptr, size_t size, size_t nmemb, std::string &stream)
|
|
@@ -33,12 +37,14 @@ void BaoCurl::init()
|
|
|
33
37
|
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_SSL_VERIFYHOST, 0L));
|
|
34
38
|
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
39
|
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_SSL_EC_CURVES, "X25519:P-256:P-384"));
|
|
40
|
+
// CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_SSL_, "X25519:P-256:P-384"));
|
|
36
41
|
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_ACCEPT_ENCODING, ""));
|
|
37
42
|
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1));
|
|
43
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_COOKIEFILE, NULL));
|
|
38
44
|
this->setTimeout(15, 15);
|
|
39
45
|
}
|
|
40
46
|
|
|
41
|
-
void BaoCurl::open(std::string method, std::string url)
|
|
47
|
+
void BaoCurl::open(std::string &method, std::string &url)
|
|
42
48
|
{
|
|
43
49
|
this->m_method = method;
|
|
44
50
|
this->m_stream.header = "";
|
|
@@ -63,17 +69,17 @@ void BaoCurl::open(std::string method, std::string url)
|
|
|
63
69
|
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_CUSTOMREQUEST, method.c_str()));
|
|
64
70
|
}
|
|
65
71
|
|
|
66
|
-
void BaoCurl::setRequestHeader(std::string key, std::string value)
|
|
72
|
+
void BaoCurl::setRequestHeader(std::string &key, std::string &value)
|
|
67
73
|
{
|
|
68
74
|
this->m_pHeaders = curl_slist_append(this->m_pHeaders, (key + ": " + value).c_str());
|
|
69
75
|
}
|
|
70
76
|
|
|
71
|
-
void BaoCurl::setRequestHeader(std::string keyValue)
|
|
77
|
+
void BaoCurl::setRequestHeader(std::string &keyValue)
|
|
72
78
|
{
|
|
73
79
|
this->m_pHeaders = curl_slist_append(this->m_pHeaders, keyValue.c_str());
|
|
74
80
|
}
|
|
75
81
|
|
|
76
|
-
void BaoCurl::setRequestHeaders(std::string header)
|
|
82
|
+
void BaoCurl::setRequestHeaders(std::string &header)
|
|
77
83
|
{
|
|
78
84
|
auto arr = StringSplit(header, "\n");
|
|
79
85
|
for (auto arr_b = arr.begin(); arr_b != arr.end(); ++arr_b)
|
|
@@ -87,14 +93,14 @@ void BaoCurl::setRequestHeaders(std::string header)
|
|
|
87
93
|
}
|
|
88
94
|
}
|
|
89
95
|
|
|
90
|
-
void BaoCurl::setProxy(std::string proxy)
|
|
96
|
+
void BaoCurl::setProxy(std::string &proxy)
|
|
91
97
|
{
|
|
92
98
|
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_PROXY, proxy.c_str()));
|
|
93
99
|
this->m_bProxy = true;
|
|
94
100
|
}
|
|
95
101
|
|
|
96
|
-
void BaoCurl::setProxy(std::string proxy, std::string username,
|
|
97
|
-
std::string password)
|
|
102
|
+
void BaoCurl::setProxy(std::string &proxy, std::string &username,
|
|
103
|
+
std::string &password)
|
|
98
104
|
{
|
|
99
105
|
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_PROXY, proxy.c_str()));
|
|
100
106
|
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_PROXYUSERPWD, (username + ":" + password).c_str()));
|
|
@@ -116,10 +122,10 @@ void BaoCurl::setTimeout(
|
|
|
116
122
|
|
|
117
123
|
void BaoCurl::send()
|
|
118
124
|
{
|
|
119
|
-
BaoCurl::send("");
|
|
125
|
+
BaoCurl::send(std::string(""));
|
|
120
126
|
}
|
|
121
127
|
|
|
122
|
-
void BaoCurl::send(std::string data)
|
|
128
|
+
void BaoCurl::send(std::string &data)
|
|
123
129
|
{
|
|
124
130
|
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_HTTPHEADER, this->m_pHeaders));
|
|
125
131
|
if (this->m_bProxy)
|
|
@@ -172,14 +178,14 @@ void BaoCurl::sendByte(const char *data, const int len)
|
|
|
172
178
|
this->m_pHeaders = NULL;
|
|
173
179
|
}
|
|
174
180
|
|
|
175
|
-
void BaoCurl::setCookie(std::string key, std::string value, std::string domain)
|
|
181
|
+
void BaoCurl::setCookie(std::string &key, std::string &value, std::string &domain, std::string &path)
|
|
176
182
|
{
|
|
177
|
-
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_COOKIELIST, StringFormat("%s\
|
|
183
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_COOKIELIST, StringFormat("%s\tFALSE\t%s\tFALSE\t3000000000\t%s\t%s", domain.c_str(), path.c_str(), key.c_str(), value.c_str()).c_str()));
|
|
178
184
|
}
|
|
179
185
|
|
|
180
|
-
void BaoCurl::
|
|
186
|
+
void BaoCurl::deleteCookie(std::string &key, std::string &domain, std::string &path)
|
|
181
187
|
{
|
|
182
|
-
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_COOKIELIST, StringFormat("%s\
|
|
188
|
+
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_COOKIELIST, StringFormat("%s\tFALSE\t%s\tFALSE\t1600000000\t%s\t%s", domain.c_str(), path.c_str(), key.c_str(), "").c_str()));
|
|
183
189
|
}
|
|
184
190
|
|
|
185
191
|
std::string BaoCurl::getCookies()
|
|
@@ -187,6 +193,8 @@ std::string BaoCurl::getCookies()
|
|
|
187
193
|
struct curl_slist *cookies = NULL;
|
|
188
194
|
std::string str;
|
|
189
195
|
CHECK_CURLOK(curl_easy_getinfo(this->m_pCURL, CURLINFO_COOKIELIST, &cookies));
|
|
196
|
+
struct curl_slist *cookiesOrign = cookies;
|
|
197
|
+
|
|
190
198
|
if (!cookies)
|
|
191
199
|
{
|
|
192
200
|
return "";
|
|
@@ -197,29 +205,47 @@ std::string BaoCurl::getCookies()
|
|
|
197
205
|
str += "\n";
|
|
198
206
|
cookies = cookies->next;
|
|
199
207
|
} while (cookies);
|
|
208
|
+
curl_slist_free_all(cookiesOrign);
|
|
200
209
|
return str;
|
|
201
210
|
}
|
|
202
211
|
|
|
203
|
-
std::string BaoCurl::getCookie(std::string key)
|
|
212
|
+
std::string BaoCurl::getCookie(std::string &key, std::string &domain, std::string &path)
|
|
204
213
|
{
|
|
205
214
|
struct curl_slist *cookies = NULL;
|
|
206
215
|
std::string str = "";
|
|
207
216
|
CHECK_CURLOK(curl_easy_getinfo(this->m_pCURL, CURLINFO_COOKIELIST, &cookies));
|
|
217
|
+
struct curl_slist *cookiesOrign = cookies;
|
|
208
218
|
if (!cookies)
|
|
209
219
|
{
|
|
210
220
|
return "";
|
|
211
221
|
}
|
|
212
222
|
do
|
|
213
223
|
{
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
224
|
+
std::vector<std::string> vt = StringSplit(std::string(cookies->data), "\t");
|
|
225
|
+
if (domain.size() != 0)
|
|
226
|
+
{
|
|
227
|
+
if (domain != vt.at(0))
|
|
228
|
+
{
|
|
229
|
+
cookies = cookies->next;
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (path.size() != 0)
|
|
217
234
|
{
|
|
235
|
+
if (path != vt.at(2))
|
|
236
|
+
{
|
|
237
|
+
cookies = cookies->next;
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (vt.at(5) == key)
|
|
242
|
+
{
|
|
243
|
+
str = vt.at(6);
|
|
218
244
|
break;
|
|
219
245
|
}
|
|
220
246
|
cookies = cookies->next;
|
|
221
247
|
} while (cookies);
|
|
222
|
-
curl_slist_free_all(
|
|
248
|
+
curl_slist_free_all(cookiesOrign);
|
|
223
249
|
return str;
|
|
224
250
|
}
|
|
225
251
|
|
|
@@ -260,4 +286,14 @@ void BaoCurl::setHttpVersion(BaoCurl::HttpVersion version)
|
|
|
260
286
|
return;
|
|
261
287
|
}
|
|
262
288
|
CHECK_CURLOK(curl_easy_setopt(this->m_pCURL, CURLOPT_HTTP_VERSION, temp));
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
unsigned int BaoCurl::getLastCurlCode()
|
|
292
|
+
{
|
|
293
|
+
return this->m_lastCode;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const char* BaoCurl::getLastCurlCodeError()
|
|
297
|
+
{
|
|
298
|
+
return curl_easy_strerror(this->m_lastCode);
|
|
263
299
|
}
|
|
@@ -32,7 +32,7 @@ private:
|
|
|
32
32
|
Napi::Value setProxy(const Napi::CallbackInfo &info);
|
|
33
33
|
Napi::Value setTimeout(const Napi::CallbackInfo &info);
|
|
34
34
|
Napi::Value setCookie(const Napi::CallbackInfo &info);
|
|
35
|
-
Napi::Value
|
|
35
|
+
Napi::Value deleteCookie(const Napi::CallbackInfo &info);
|
|
36
36
|
Napi::Value getCookies(const Napi::CallbackInfo &info);
|
|
37
37
|
Napi::Value getCookie(const Napi::CallbackInfo &info);
|
|
38
38
|
Napi::Value getResponseStatus(const Napi::CallbackInfo &info);
|
|
@@ -51,7 +51,7 @@ Napi::Object BaoLibCurlWarp::Init(Napi::Env env, Napi::Object exports)
|
|
|
51
51
|
{
|
|
52
52
|
// This method is used to hook the accessor and method callbacks
|
|
53
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::
|
|
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::deleteCookie>("deleteCookie", 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
55
|
InstanceMethod<&BaoLibCurlWarp::getResponseBody>("getResponseBody", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
|
|
56
56
|
InstanceMethod<&BaoLibCurlWarp::getResponseString>("getResponseString", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
|
|
57
57
|
InstanceMethod<&BaoLibCurlWarp::sendAsync>("sendAsync", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
|
|
@@ -182,28 +182,31 @@ Napi::Value BaoLibCurlWarp::setCookie(const Napi::CallbackInfo &info)
|
|
|
182
182
|
{
|
|
183
183
|
Napi::Env env = info.Env();
|
|
184
184
|
size_t argsLen = info.Length();
|
|
185
|
-
REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "setCookie",
|
|
185
|
+
REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "setCookie", 4, argsLen)
|
|
186
186
|
REQUEST_TLS_METHOD_CHECK(env, info[0].IsString(), "argument 0 is not a string")
|
|
187
187
|
REQUEST_TLS_METHOD_CHECK(env, info[1].IsString(), "argument 1 is not a string")
|
|
188
188
|
REQUEST_TLS_METHOD_CHECK(env, info[2].IsString(), "argument 2 is not a string")
|
|
189
|
+
REQUEST_TLS_METHOD_CHECK(env, info[3].IsString(), "argument 3 is not a string")
|
|
189
190
|
string key = info[0].As<Napi::String>().Utf8Value();
|
|
190
191
|
string value = info[1].As<Napi::String>().Utf8Value();
|
|
191
192
|
string domain = info[2].As<Napi::String>().Utf8Value();
|
|
192
|
-
|
|
193
|
+
string path = info[3].As<Napi::String>().Utf8Value();
|
|
194
|
+
this->m_curl.setCookie(key, value, domain,path);
|
|
193
195
|
return env.Undefined();
|
|
194
196
|
}
|
|
195
197
|
|
|
196
198
|
/*
|
|
197
|
-
|
|
199
|
+
deleteCookie(key, domain)
|
|
198
200
|
*/
|
|
199
|
-
Napi::Value BaoLibCurlWarp::
|
|
201
|
+
Napi::Value BaoLibCurlWarp::deleteCookie(const Napi::CallbackInfo &info)
|
|
200
202
|
{
|
|
201
203
|
Napi::Env env = info.Env();
|
|
202
204
|
size_t argsLen = info.Length();
|
|
203
|
-
REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "
|
|
205
|
+
REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "deleteCookie", 3, argsLen)
|
|
204
206
|
string key = info[0].As<Napi::String>().Utf8Value();
|
|
205
207
|
string domain = info[1].As<Napi::String>().Utf8Value();
|
|
206
|
-
|
|
208
|
+
string path = info[2].As<Napi::String>().Utf8Value();
|
|
209
|
+
this->m_curl.deleteCookie(key, domain, path);
|
|
207
210
|
return env.Undefined();
|
|
208
211
|
}
|
|
209
212
|
|
|
@@ -225,10 +228,14 @@ Napi::Value BaoLibCurlWarp::getCookie(const Napi::CallbackInfo &info)
|
|
|
225
228
|
{
|
|
226
229
|
Napi::Env env = info.Env();
|
|
227
230
|
size_t argsLen = info.Length();
|
|
228
|
-
REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "getCookie",
|
|
231
|
+
REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "getCookie", 3, argsLen)
|
|
229
232
|
REQUEST_TLS_METHOD_CHECK(env, info[0].IsString(), "argument 0 is not a string")
|
|
230
|
-
|
|
231
|
-
|
|
233
|
+
REQUEST_TLS_METHOD_CHECK(env, info[1].IsString(), "argument 1 is not a string")
|
|
234
|
+
REQUEST_TLS_METHOD_CHECK(env, info[2].IsString(), "argument 2 is not a string")
|
|
235
|
+
std::string key = info[0].As<Napi::String>().Utf8Value();
|
|
236
|
+
std::string domain = info[1].As<Napi::String>().Utf8Value();
|
|
237
|
+
std::string path = info[2].As<Napi::String>().Utf8Value();
|
|
238
|
+
return Napi::String::New(env, this->m_curl.getCookie(key, domain, path));
|
|
232
239
|
}
|
|
233
240
|
|
|
234
241
|
/*
|
|
@@ -370,7 +377,7 @@ Napi::Value BaoLibCurlWarp::sendAsync(const Napi::CallbackInfo &info)
|
|
|
370
377
|
size_t strLen = str.size();
|
|
371
378
|
Napi::Uint8Array u8buffer = Napi::Uint8Array::New(env, strLen);
|
|
372
379
|
memcpy(u8buffer.Data(), str.c_str(), strLen);
|
|
373
|
-
workerPtr = new curlAsyncWorker(info[1].As<Napi::Function>(), this->m_curl, (const char *)u8buffer.Data(),u8buffer.ElementLength());
|
|
380
|
+
workerPtr = new curlAsyncWorker(info[1].As<Napi::Function>(), this->m_curl, (const char *)u8buffer.Data(), u8buffer.ElementLength());
|
|
374
381
|
workerPtr->Queue();
|
|
375
382
|
return env.Undefined();
|
|
376
383
|
}
|
|
@@ -379,7 +386,7 @@ Napi::Value BaoLibCurlWarp::sendAsync(const Napi::CallbackInfo &info)
|
|
|
379
386
|
size_t strLen = jsonStr.size();
|
|
380
387
|
Napi::Uint8Array u8buffer = Napi::Uint8Array::New(env, strLen);
|
|
381
388
|
memcpy(u8buffer.Data(), jsonStr.c_str(), strLen);
|
|
382
|
-
workerPtr = new curlAsyncWorker(info[1].As<Napi::Function>(), this->m_curl, (const char *)u8buffer.Data(),u8buffer.ElementLength());
|
|
389
|
+
workerPtr = new curlAsyncWorker(info[1].As<Napi::Function>(), this->m_curl, (const char *)u8buffer.Data(), u8buffer.ElementLength());
|
|
383
390
|
workerPtr->Queue();
|
|
384
391
|
return env.Undefined();
|
|
385
392
|
}
|
|
@@ -20,6 +20,8 @@ curlAsyncWorker::~curlAsyncWorker()
|
|
|
20
20
|
void curlAsyncWorker::Execute()
|
|
21
21
|
{
|
|
22
22
|
this->m_baoCurlInstance.sendByte(this->m_sendBody, this->m_bodySize);
|
|
23
|
+
this->m_lastCurlCode = this->m_baoCurlInstance.getLastCurlCode();
|
|
24
|
+
this->m_lastCurlCodeError = this->m_baoCurlInstance.getLastCurlCodeError();
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
// Executed when the async work is complete
|
|
@@ -27,12 +29,14 @@ void curlAsyncWorker::Execute()
|
|
|
27
29
|
// so it is safe to use JS engine data again
|
|
28
30
|
void curlAsyncWorker::OnOK()
|
|
29
31
|
{
|
|
30
|
-
Napi::
|
|
32
|
+
Napi::Env env = Env();
|
|
33
|
+
Napi::HandleScope scope(env);
|
|
31
34
|
/* std::string str = this->m_baoCurlInstance.getResponseBody();
|
|
32
35
|
size_t strLen = str.size();
|
|
33
36
|
Napi::Uint8Array u8buffer = Napi::Uint8Array::New(Env(), strLen);
|
|
34
37
|
memcpy(u8buffer.Data(), strLen, str.c_str(), strLen); */
|
|
35
38
|
Callback().Call({
|
|
36
|
-
|
|
39
|
+
Napi::Number::New(env,this->m_lastCurlCode),
|
|
40
|
+
Napi::String::New(env,this->m_lastCurlCodeError),
|
|
37
41
|
});
|
|
38
42
|
}
|
|
@@ -2,79 +2,82 @@
|
|
|
2
2
|
#ifndef _CURL_H
|
|
3
3
|
#define _CURL_H
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
5
|
#include "curl/curl.h"
|
|
8
6
|
#include "utils.h"
|
|
9
7
|
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
struct Stream_st
|
|
9
|
+
{
|
|
12
10
|
std::string header;
|
|
13
11
|
std::string responseText;
|
|
14
12
|
};
|
|
15
13
|
|
|
16
|
-
class BaoCurl
|
|
14
|
+
class BaoCurl
|
|
15
|
+
{
|
|
17
16
|
public:
|
|
18
17
|
BaoCurl();
|
|
19
18
|
~BaoCurl();
|
|
20
|
-
void open(std::string
|
|
21
|
-
void setRequestHeader(std::string
|
|
22
|
-
void setRequestHeader(std::string);
|
|
23
|
-
void setRequestHeaders(std::string);
|
|
19
|
+
void open(std::string &, std::string &);
|
|
20
|
+
void setRequestHeader(std::string &, std::string &);
|
|
21
|
+
void setRequestHeader(std::string &);
|
|
22
|
+
void setRequestHeaders(std::string &);
|
|
24
23
|
/*
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
void setProxy(std::string);
|
|
29
|
-
void setProxy(std::string
|
|
30
|
-
|
|
31
|
-
);
|
|
24
|
+
* setProxy必须在open后 send前设置
|
|
25
|
+
* 在send后失效 需重新设置
|
|
26
|
+
*/
|
|
27
|
+
void setProxy(std::string &);
|
|
28
|
+
void setProxy(std::string &, std::string &,
|
|
29
|
+
std::string &);
|
|
32
30
|
/*
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
* setTimeout必须在open后 send前设置
|
|
32
|
+
* 在send后失效 需重新设置
|
|
33
|
+
* 单位:秒 例子:setTimeout(10,10);
|
|
34
|
+
*/
|
|
37
35
|
void setTimeout(
|
|
38
36
|
int connectTime,
|
|
39
|
-
int sendTime
|
|
40
|
-
);
|
|
37
|
+
int sendTime);
|
|
41
38
|
/*
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
void setCookie(std::string key, std::string value, std::string domain);
|
|
39
|
+
* 设置单条Cookie
|
|
40
|
+
*/
|
|
41
|
+
void setCookie(std::string &key, std::string &value, std::string &domain, std::string &path);
|
|
45
42
|
/*
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
void
|
|
43
|
+
* 移除单条Cookie
|
|
44
|
+
*/
|
|
45
|
+
void deleteCookie(std::string &key, std::string &domain, std::string &path);
|
|
49
46
|
void send();
|
|
50
|
-
void send(std::string);
|
|
51
|
-
void sendByte(const char*,const int);
|
|
47
|
+
void send(std::string &);
|
|
48
|
+
void sendByte(const char *, const int);
|
|
52
49
|
/*
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
* 重置Curl 此前的操作全部失效
|
|
51
|
+
*/
|
|
55
52
|
void reset();
|
|
56
53
|
|
|
57
54
|
std::string getCookies();
|
|
58
|
-
std::string getCookie(std::string key);
|
|
55
|
+
std::string getCookie(std::string &key, std::string &domain, std::string &path);
|
|
59
56
|
std::string getResponseHeaders() { return this->m_stream.header; };
|
|
60
57
|
std::string getResponseBody() { return this->m_stream.responseText; };
|
|
61
58
|
long getResponseStatus();
|
|
62
|
-
void setRedirect(bool isAllow)
|
|
63
|
-
void printInnerLogger()
|
|
59
|
+
void setRedirect(bool isAllow); // 重定向
|
|
60
|
+
void printInnerLogger(); // 打印内部日志
|
|
64
61
|
|
|
65
|
-
enum HttpVersion
|
|
62
|
+
enum HttpVersion
|
|
63
|
+
{
|
|
66
64
|
http1_1,
|
|
67
65
|
http2,
|
|
68
66
|
};
|
|
69
67
|
void setHttpVersion(HttpVersion);
|
|
68
|
+
unsigned int getLastCurlCode();
|
|
69
|
+
const char *getLastCurlCodeError();
|
|
70
|
+
|
|
70
71
|
private:
|
|
71
|
-
CURL*
|
|
72
|
-
struct curl_slist*
|
|
72
|
+
CURL *m_pCURL = NULL;
|
|
73
|
+
struct curl_slist *m_pHeaders = NULL;
|
|
73
74
|
std::string m_method = "GET";
|
|
74
75
|
struct Stream_st m_stream;
|
|
75
76
|
// struct curl_slist* m_cookies = NULL;
|
|
76
77
|
bool m_bProxy = false;
|
|
77
78
|
bool m_bIsHttps = false;
|
|
79
|
+
bool m_verbose = false;
|
|
80
|
+
CURLcode m_lastCode;
|
|
78
81
|
|
|
79
82
|
void init();
|
|
80
83
|
};
|