-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathHttpClient.cpp
More file actions
741 lines (651 loc) · 21.4 KB
/
HttpClient.cpp
File metadata and controls
741 lines (651 loc) · 21.4 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
#include "HttpClient.h"
#include <memory>
#include <mutex>
#ifdef WITH_CURL
#include "curl/curl.h"
#endif
#include "herr.h"
#include "hlog.h"
#include "htime.h"
#include "hstring.h"
#include "hsocket.h"
#include "hssl.h"
#include "HttpParser.h"
// for async
#include "AsyncHttpClient.h"
using namespace hv;
struct http_client_s {
std::string host;
int port;
int https;
int timeout; // s
http_headers headers;
// http_proxy
std::string http_proxy_host;
int http_proxy_port;
// https_proxy
std::string https_proxy_host;
int https_proxy_port;
// no_proxy
StringList no_proxy_hosts;
//private:
#ifdef WITH_CURL
CURL* curl;
#endif
// for sync
int fd;
unsigned int keepalive_requests;
hssl_t ssl;
hssl_ctx_t ssl_ctx;
bool alloced_ssl_ctx;
HttpParserPtr parser;
// for async
std::mutex mutex_;
std::shared_ptr<hv::AsyncHttpClient> async_client_;
http_client_s() {
host = LOCALHOST;
port = DEFAULT_HTTP_PORT;
https = 0;
timeout = DEFAULT_HTTP_TIMEOUT;
http_proxy_port = DEFAULT_HTTP_PORT;
https_proxy_port = DEFAULT_HTTP_PORT;
#ifdef WITH_CURL
curl = NULL;
#endif
fd = -1;
keepalive_requests = 0;
ssl = NULL;
ssl_ctx = NULL;
alloced_ssl_ctx = false;
}
~http_client_s() {
Close();
if (ssl_ctx && alloced_ssl_ctx) {
hssl_ctx_free(ssl_ctx);
ssl_ctx = NULL;
}
}
void Close() {
#ifdef WITH_CURL
if (curl) {
curl_easy_cleanup(curl);
curl = NULL;
}
#endif
if (ssl) {
hssl_free(ssl);
ssl = NULL;
}
SAFE_CLOSESOCKET(fd);
}
};
http_client_t* http_client_new(const char* host, int port, int https) {
http_client_t* cli = new http_client_t;
if (host) cli->host = host;
cli->port = port;
cli->https = https;
cli->headers["Connection"] = "keep-alive";
return cli;
}
int http_client_del(http_client_t* cli) {
if (cli == NULL) return 0;
delete cli;
return 0;
}
int http_client_set_timeout(http_client_t* cli, int timeout) {
cli->timeout = timeout;
return 0;
}
int http_client_set_ssl_ctx(http_client_t* cli, hssl_ctx_t ssl_ctx) {
cli->ssl_ctx = ssl_ctx;
return 0;
}
int http_client_new_ssl_ctx(http_client_t* cli, hssl_ctx_opt_t* opt) {
opt->endpoint = HSSL_CLIENT;
hssl_ctx_t ssl_ctx = hssl_ctx_new(opt);
if (ssl_ctx == NULL) return ERR_NEW_SSL_CTX;
cli->alloced_ssl_ctx = true;
return http_client_set_ssl_ctx(cli, ssl_ctx);
}
int http_client_clear_headers(http_client_t* cli) {
cli->headers.clear();
return 0;
}
int http_client_set_header(http_client_t* cli, const char* key, const char* value) {
cli->headers[key] = value;
return 0;
}
int http_client_del_header(http_client_t* cli, const char* key) {
auto iter = cli->headers.find(key);
if (iter != cli->headers.end()) {
cli->headers.erase(iter);
}
return 0;
}
const char* http_client_get_header(http_client_t* cli, const char* key) {
auto iter = cli->headers.find(key);
if (iter != cli->headers.end()) {
return iter->second.c_str();
}
return NULL;
}
int http_client_set_http_proxy(http_client_t* cli, const char* host, int port) {
cli->http_proxy_host = host;
cli->http_proxy_port = port;
return 0;
}
int http_client_set_https_proxy(http_client_t* cli, const char* host, int port) {
cli->https_proxy_host = host;
cli->https_proxy_port = port;
return 0;
}
int http_client_add_no_proxy(http_client_t* cli, const char* host) {
cli->no_proxy_hosts.push_back(host);
return 0;
}
static int http_client_make_request(http_client_t* cli, HttpRequest* req) {
if (req->url.empty() || *req->url.c_str() == '/') {
req->scheme = cli->https ? "https" : "http";
req->host = cli->host;
req->port = cli->port;
}
req->ParseUrl();
bool https = req->IsHttps();
bool use_proxy = https ? (!cli->https_proxy_host.empty()) : (!cli->http_proxy_host.empty());
if (use_proxy) {
if (req->host == "127.0.0.1" || req->host == "localhost") {
use_proxy = false;
}
}
if (use_proxy) {
for (const auto& host : cli->no_proxy_hosts) {
if (req->host == host) {
use_proxy = false;
break;
}
}
}
if (use_proxy) {
req->SetProxy(https ? cli->https_proxy_host.c_str() : cli->http_proxy_host.c_str(),
https ? cli->https_proxy_port : cli->http_proxy_port);
}
if (req->timeout == 0) {
req->timeout = cli->timeout;
}
for (const auto& pair : cli->headers) {
if (req->headers.find(pair.first) == req->headers.end()) {
req->headers.insert(pair);
}
}
return 0;
}
int http_client_connect(http_client_t* cli, const char* host, int port, int https, int timeout) {
cli->Close();
int blocktime = DEFAULT_CONNECT_TIMEOUT;
if (timeout > 0) {
blocktime = MIN(timeout*1000, blocktime);
}
int connfd = ConnectTimeout(host, port, blocktime);
if (connfd < 0) {
hloge("connect %s:%d failed!", host, port);
return connfd;
}
tcp_nodelay(connfd, 1);
if (https && cli->ssl == NULL) {
// cli->ssl_ctx > g_ssl_ctx > hssl_ctx_new
hssl_ctx_t ssl_ctx = NULL;
if (cli->ssl_ctx) {
ssl_ctx = cli->ssl_ctx;
} else if (g_ssl_ctx) {
ssl_ctx = g_ssl_ctx;
} else {
cli->ssl_ctx = ssl_ctx = hssl_ctx_new(NULL);
cli->alloced_ssl_ctx = true;
}
if (ssl_ctx == NULL) {
closesocket(connfd);
return NABS(ERR_NEW_SSL_CTX);
}
cli->ssl = hssl_new(ssl_ctx, connfd);
if (cli->ssl == NULL) {
closesocket(connfd);
return NABS(ERR_NEW_SSL);
}
if (!is_ipaddr(host)) {
hssl_set_sni_hostname(cli->ssl, host);
}
so_rcvtimeo(connfd, blocktime);
int ret = hssl_connect(cli->ssl);
if (ret != 0) {
fprintf(stderr, "* ssl handshake failed: %d\n", ret);
hloge("ssl handshake failed: %d", ret);
hssl_free(cli->ssl);
cli->ssl = NULL;
closesocket(connfd);
return NABS(ret);
}
}
cli->fd = connfd;
cli->keepalive_requests = 0;
return connfd;
}
int http_client_close(http_client_t* cli) {
if (cli == NULL) return 0;
cli->Close();
return 0;
}
int http_client_send_data(http_client_t* cli, const char* data, int size) {
if (!cli || !data || size <= 0) return -1;
if (cli->ssl) {
return hssl_write(cli->ssl, data, size);
}
return send(cli->fd, data, size, 0);
}
int http_client_recv_data(http_client_t* cli, char* data, int size) {
if (!cli || !data || size <= 0) return -1;
if (cli->ssl) {
return hssl_read(cli->ssl, data, size);
}
return recv(cli->fd, data, size, 0);
}
static int http_client_exec(http_client_t* cli, HttpRequest* req, HttpResponse* resp) {
// connect -> send -> recv -> http_parser
int err = 0;
int connfd = cli->fd;
bool https = req->IsHttps() && !req->IsProxy();
bool keepalive = true;
time_t connect_timeout = MIN(req->connect_timeout, req->timeout);
time_t timeout_ms = req->timeout * 1000;
time_t start_time = gettick_ms();
time_t cur_time = start_time, left_time = INFINITE;
#define CHECK_TIMEOUT \
do { \
if (timeout_ms > 0) { \
cur_time = gettick_ms(); \
if (cur_time - start_time >= timeout_ms) { \
goto timeout; \
} \
left_time = timeout_ms - (cur_time - start_time); \
} \
} while(0);
uint32_t retry_count = req->retry_count;
if (cli->keepalive_requests > 0 && retry_count == 0) {
// maybe keep-alive timeout, retry at least once
retry_count = 1;
}
if (cli->parser == NULL) {
cli->parser = HttpParserPtr(HttpParser::New(HTTP_CLIENT, (http_version)req->http_major));
if (cli->parser == NULL) {
hloge("New HttpParser failed!");
return ERR_NULL_POINTER;
}
}
if (connfd <= 0 || cli->host != req->host || cli->port != req->port) {
cli->host = req->host;
cli->port = req->port;
connect:
connfd = http_client_connect(cli, req->host.c_str(), req->port, https, connect_timeout);
if (connfd < 0) {
return connfd;
}
}
cli->parser->SubmitRequest(req);
char recvbuf[1024] = {0};
int total_nsend, nsend, nrecv;
total_nsend = nsend = nrecv = 0;
send:
char* data = NULL;
size_t len = 0;
while (cli->parser->GetSendData(&data, &len)) {
total_nsend = 0;
while (total_nsend < len) {
CHECK_TIMEOUT
if (left_time != INFINITE) {
so_sndtimeo(cli->fd, left_time);
}
if (req->cancel) goto disconnect;
nsend = http_client_send_data(cli, data + total_nsend, len - total_nsend);
if (req->cancel) goto disconnect;
if (nsend <= 0) {
CHECK_TIMEOUT
err = socket_errno();
if (err == EINTR) continue;
if (retry_count-- > 0 && left_time > req->retry_delay + connect_timeout * 1000) {
err = 0;
if (req->retry_delay > 0) hv_msleep(req->retry_delay);
goto connect;
}
goto disconnect;
}
total_nsend += nsend;
}
}
if (resp == NULL) return 0;
cli->parser->InitResponse(resp);
recv:
do {
CHECK_TIMEOUT
if (left_time != INFINITE) {
so_rcvtimeo(cli->fd, left_time);
}
if (req->cancel) goto disconnect;
nrecv = http_client_recv_data(cli, recvbuf, sizeof(recvbuf));
if (req->cancel) goto disconnect;
if (nrecv <= 0) {
CHECK_TIMEOUT
err = socket_errno();
if (err == EINTR) continue;
if (cli->parser->IsEof()) {
err = 0;
goto disconnect;
}
if (retry_count-- > 0 && left_time > req->retry_delay + connect_timeout * 1000) {
err = 0;
if (req->retry_delay > 0) hv_msleep(req->retry_delay);
goto connect;
}
goto disconnect;
}
int nparse = cli->parser->FeedRecvData(recvbuf, nrecv);
if (nparse != nrecv) {
return ERR_PARSE;
}
} while(!cli->parser->IsComplete());
keepalive = req->IsKeepAlive() && resp->IsKeepAlive();
if (keepalive) {
++cli->keepalive_requests;
} else {
cli->Close();
}
return 0;
timeout:
err = ERR_TASK_TIMEOUT;
disconnect:
cli->Close();
return err;
}
int http_client_send_header(http_client_t* cli, HttpRequest* req) {
if (!cli || !req) return ERR_NULL_POINTER;
http_client_make_request(cli, req);
return http_client_exec(cli, req, NULL);
}
int http_client_recv_response(http_client_t* cli, HttpResponse* resp) {
if (!cli || !resp) return ERR_NULL_POINTER;
if (!cli->parser) {
hloge("Call http_client_send_header first!");
return ERR_NULL_POINTER;
}
char recvbuf[1024] = {0};
cli->parser->InitResponse(resp);
do {
int nrecv = http_client_recv_data(cli, recvbuf, sizeof(recvbuf));
if (nrecv <= 0) {
int err = socket_errno();
if (err == EINTR) continue;
cli->Close();
return err;
}
int nparse = cli->parser->FeedRecvData(recvbuf, nrecv);
if (nparse != nrecv) {
return ERR_PARSE;
}
} while(!cli->parser->IsComplete());
return 0;
}
#ifdef WITH_CURL
static size_t s_header_cb(char* buf, size_t size, size_t cnt, void* userdata) {
if (buf == NULL || userdata == NULL) return 0;
size_t len = size * cnt;
std::string str(buf, len);
HttpResponse* resp = (HttpResponse*)userdata;
std::string::size_type pos = str.find_first_of(':');
if (pos == std::string::npos) {
if (strncmp(buf, "HTTP/", 5) == 0) {
// status line
//hlogd("%s", buf);
int http_major = 1, http_minor = 1, status_code = 200;
if (buf[5] == '1') {
// HTTP/1.1 200 OK\r\n
sscanf(buf, "HTTP/%d.%d %d", &http_major, &http_minor, &status_code);
}
else if (buf[5] == '2') {
// HTTP/2 200\r\n
sscanf(buf, "HTTP/%d %d", &http_major, &status_code);
http_minor = 0;
}
resp->http_major = http_major;
resp->http_minor = http_minor;
resp->status_code = (http_status)status_code;
if (resp->http_cb) {
resp->http_cb(resp, HP_MESSAGE_BEGIN, NULL, 0);
}
}
}
else {
// headers
std::string key = trim(str.substr(0, pos));
std::string value = trim(str.substr(pos+1));
resp->headers[key] = value;
}
return len;
}
static size_t s_body_cb(char* buf, size_t size, size_t cnt, void *userdata) {
if (buf == NULL || userdata == NULL) return 0;
size_t len = size * cnt;
HttpMessage* resp = (HttpMessage*)userdata;
if (resp->http_cb) {
if (resp->content == NULL && resp->content_length == 0) {
resp->content = buf;
resp->content_length = len;
resp->http_cb(resp, HP_HEADERS_COMPLETE, NULL, 0);
}
resp->http_cb(resp, HP_BODY, buf, len);
} else {
resp->body.append(buf, len);
}
return len;
}
static int http_client_exec_curl(http_client_t* cli, HttpRequest* req, HttpResponse* resp) {
if (cli->curl == NULL) {
cli->curl = curl_easy_init();
}
CURL* curl = cli->curl;
// proxy
if (req->IsProxy()) {
curl_easy_setopt(curl, CURLOPT_PROXY, req->host.c_str());
curl_easy_setopt(curl, CURLOPT_PROXYPORT, req->port);
}
// SSL
if (req->IsHttps()) {
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
}
// http2
if (req->http_major == 2) {
#if LIBCURL_VERSION_NUM < 0x073100 // 7.49.0
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2_0);
#else
// No Connection: Upgrade
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE);
#endif
}
// TCP_NODELAY
curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
// method
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, http_method_str(req->method));
// url
req->DumpUrl();
curl_easy_setopt(curl, CURLOPT_URL, req->url.c_str());
//hlogd("%s %s HTTP/%d.%d", http_method_str(req->method), req->url.c_str(), req->http_major, req->http_minor);
// headers
req->FillContentType();
struct curl_slist *headers = NULL;
for (auto& pair : req->headers) {
std::string header = pair.first;
header += ": ";
header += pair.second;
headers = curl_slist_append(headers, header.c_str());
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// body
//struct curl_httppost* httppost = NULL;
//struct curl_httppost* lastpost = NULL;
if (req->body.size() == 0) {
req->DumpBody();
/*
if (req->body.size() == 0 &&
req->content_type == MULTIPART_FORM_DATA) {
for (auto& pair : req->mp) {
if (pair.second.filename.size() != 0) {
curl_formadd(&httppost, &lastpost,
CURLFORM_COPYNAME, pair.first.c_str(),
CURLFORM_FILE, pair.second.filename.c_str(),
CURLFORM_END);
}
else if (pair.second.content.size() != 0) {
curl_formadd(&httppost, &lastpost,
CURLFORM_COPYNAME, pair.first.c_str(),
CURLFORM_COPYCONTENTS, pair.second.content.c_str(),
CURLFORM_END);
}
}
if (httppost) {
curl_easy_setopt(curl, CURLOPT_HTTPPOST, httppost);
}
}
*/
}
if (req->body.size() != 0) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, req->body.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, req->body.size());
}
if (req->connect_timeout > 0) {
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, req->connect_timeout);
}
if (req->timeout > 0) {
curl_easy_setopt(curl, CURLOPT_TIMEOUT, req->timeout);
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, s_body_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, resp);
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, s_header_cb);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, resp);
int ret = curl_easy_perform(curl);
/*
if (ret != 0) {
hloge("curl error: %d: %s", ret, curl_easy_strerror((CURLcode)ret));
}
if (resp->body.length() != 0) {
hlogd("[Response]\n%s", resp->body.c_str());
}
double total_time, name_time, conn_time, pre_time;
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME, &name_time);
curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &conn_time);
curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME, &pre_time);
hlogd("TIME_INFO: %lf,%lf,%lf,%lf", total_time, name_time, conn_time, pre_time);
*/
if (headers) {
curl_slist_free_all(headers);
}
/*
if (httppost) {
curl_formfree(httppost);
}
*/
if (resp->http_cb) {
resp->http_cb(resp, HP_MESSAGE_COMPLETE, NULL, 0);
}
return ret;
}
const char* http_client_strerror(int errcode) {
return curl_easy_strerror((CURLcode)errcode);
}
#else
const char* http_client_strerror(int errcode) {
return socket_strerror(errcode);
}
#endif
static int http_client_redirect(HttpRequest* req, HttpResponse* resp) {
std::string location = resp->headers["Location"];
if (!location.empty()) {
hlogi("redirect %s => %s", req->url.c_str(), location.c_str());
req->url = location;
req->ParseUrl();
req->headers["Host"] = req->host;
resp->Reset();
return http_client_send(req, resp);
}
return 0;
}
int http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp) {
if (!cli || !req || !resp) return ERR_NULL_POINTER;
http_client_make_request(cli, req);
if (req->http_cb) resp->http_cb = std::move(req->http_cb);
#if WITH_CURL
int ret = http_client_exec_curl(cli, req, resp);
#else
int ret = http_client_exec(cli, req, resp);
#endif
if (ret != 0) return ret;
// redirect
if (req->redirect && HTTP_STATUS_IS_REDIRECT(resp->status_code)) {
return http_client_redirect(req, resp);
}
return 0;
}
int http_client_send(HttpRequest* req, HttpResponse* resp) {
if (!req || !resp) return ERR_NULL_POINTER;
http_client_t cli;
return http_client_send(&cli, req, resp);
}
// below for async
static int http_client_exec_async(http_client_t* cli, HttpRequestPtr req, HttpResponseCallback resp_cb) {
if (cli->async_client_ == NULL) {
cli->mutex_.lock();
if (cli->async_client_ == NULL) {
cli->async_client_ = std::make_shared<hv::AsyncHttpClient>();
}
cli->mutex_.unlock();
}
return cli->async_client_->send(req, std::move(resp_cb));
}
int http_client_send_async(http_client_t* cli, HttpRequestPtr req, HttpResponseCallback resp_cb) {
if (!cli || !req) return ERR_NULL_POINTER;
http_client_make_request(cli, req.get());
return http_client_exec_async(cli, req, std::move(resp_cb));
}
static http_client_t* s_async_http_client = NULL;
static void hv_destroy_default_async_http_client() {
hlogi("destory default http async client");
if (s_async_http_client) {
http_client_del(s_async_http_client);
s_async_http_client = NULL;
}
}
static http_client_t* hv_default_async_http_client() {
static std::mutex s_mutex;
if (s_async_http_client == NULL) {
s_mutex.lock();
if (s_async_http_client == NULL) {
hlogi("create default http async client");
s_async_http_client = http_client_new();
// NOTE: I have No better idea
atexit(hv_destroy_default_async_http_client);
}
s_mutex.unlock();
}
return s_async_http_client;
}
int http_client_send_async(HttpRequestPtr req, HttpResponseCallback resp_cb) {
if (req == NULL) return ERR_NULL_POINTER;
if (req->timeout == 0) {
req->timeout = DEFAULT_HTTP_TIMEOUT;
}
return http_client_exec_async(hv_default_async_http_client(), req, std::move(resp_cb));
}
int http_client_reuse_loop(http_client_t *cli, hv::EventLoopPtr loop) {
if (!cli || !loop) return ERR_NULL_POINTER;
cli->async_client_ = std::make_shared<hv::AsyncHttpClient>(std::move(loop));
return 0;
}