http_client.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. #include "http_client.h"
  2. #include <mutex>
  3. #ifdef WITH_CURL
  4. #include "curl/curl.h"
  5. #endif
  6. #include "herr.h"
  7. #include "hlog.h"
  8. #include "hstring.h"
  9. #include "hsocket.h"
  10. #include "hssl.h"
  11. #include "HttpParser.h"
  12. // for async
  13. #include "AsyncHttpClient.h"
  14. using namespace hv;
  15. struct http_client_s {
  16. std::string host;
  17. int port;
  18. int https;
  19. int timeout; // s
  20. http_headers headers;
  21. // http_proxy
  22. std::string http_proxy_host;
  23. int http_proxy_port;
  24. // https_proxy
  25. std::string https_proxy_host;
  26. int https_proxy_port;
  27. // no_proxy
  28. StringList no_proxy_hosts;
  29. //private:
  30. #ifdef WITH_CURL
  31. CURL* curl;
  32. #endif
  33. // for sync
  34. int fd;
  35. hssl_t ssl;
  36. hssl_ctx_t ssl_ctx;
  37. bool alloced_ssl_ctx;
  38. HttpParserPtr parser;
  39. // for async
  40. std::mutex mutex_;
  41. std::shared_ptr<hv::AsyncHttpClient> async_client_;
  42. http_client_s() {
  43. host = LOCALHOST;
  44. port = DEFAULT_HTTP_PORT;
  45. https = 0;
  46. timeout = DEFAULT_HTTP_TIMEOUT;
  47. http_proxy_port = DEFAULT_HTTP_PORT;
  48. https_proxy_port = DEFAULT_HTTP_PORT;
  49. #ifdef WITH_CURL
  50. curl = NULL;
  51. #endif
  52. fd = -1;
  53. ssl = NULL;
  54. ssl_ctx = NULL;
  55. alloced_ssl_ctx = false;
  56. }
  57. ~http_client_s() {
  58. Close();
  59. if (ssl_ctx && alloced_ssl_ctx) {
  60. hssl_ctx_free(ssl_ctx);
  61. ssl_ctx = NULL;
  62. }
  63. }
  64. void Close() {
  65. #ifdef WITH_CURL
  66. if (curl) {
  67. curl_easy_cleanup(curl);
  68. curl = NULL;
  69. }
  70. #endif
  71. if (ssl) {
  72. hssl_free(ssl);
  73. ssl = NULL;
  74. }
  75. SAFE_CLOSESOCKET(fd);
  76. }
  77. };
  78. static int __http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp);
  79. http_client_t* http_client_new(const char* host, int port, int https) {
  80. http_client_t* cli = new http_client_t;
  81. if (host) cli->host = host;
  82. cli->port = port;
  83. cli->https = https;
  84. cli->headers["Connection"] = "keep-alive";
  85. return cli;
  86. }
  87. int http_client_del(http_client_t* cli) {
  88. if (cli == NULL) return 0;
  89. delete cli;
  90. return 0;
  91. }
  92. int http_client_close(http_client_t* cli) {
  93. if (cli == NULL) return 0;
  94. cli->Close();
  95. return 0;
  96. }
  97. int http_client_set_timeout(http_client_t* cli, int timeout) {
  98. cli->timeout = timeout;
  99. return 0;
  100. }
  101. int http_client_set_ssl_ctx(http_client_t* cli, hssl_ctx_t ssl_ctx) {
  102. cli->ssl_ctx = ssl_ctx;
  103. return 0;
  104. }
  105. int http_client_new_ssl_ctx(http_client_t* cli, hssl_ctx_opt_t* opt) {
  106. opt->endpoint = HSSL_CLIENT;
  107. hssl_ctx_t ssl_ctx = hssl_ctx_new(opt);
  108. if (ssl_ctx == NULL) return ERR_NEW_SSL_CTX;
  109. cli->alloced_ssl_ctx = true;
  110. return http_client_set_ssl_ctx(cli, ssl_ctx);
  111. }
  112. int http_client_clear_headers(http_client_t* cli) {
  113. cli->headers.clear();
  114. return 0;
  115. }
  116. int http_client_set_header(http_client_t* cli, const char* key, const char* value) {
  117. cli->headers[key] = value;
  118. return 0;
  119. }
  120. int http_client_del_header(http_client_t* cli, const char* key) {
  121. auto iter = cli->headers.find(key);
  122. if (iter != cli->headers.end()) {
  123. cli->headers.erase(iter);
  124. }
  125. return 0;
  126. }
  127. const char* http_client_get_header(http_client_t* cli, const char* key) {
  128. auto iter = cli->headers.find(key);
  129. if (iter != cli->headers.end()) {
  130. return iter->second.c_str();
  131. }
  132. return NULL;
  133. }
  134. int http_client_set_http_proxy(http_client_t* cli, const char* host, int port) {
  135. cli->http_proxy_host = host;
  136. cli->http_proxy_port = port;
  137. return 0;
  138. }
  139. int http_client_set_https_proxy(http_client_t* cli, const char* host, int port) {
  140. cli->https_proxy_host = host;
  141. cli->https_proxy_port = port;
  142. return 0;
  143. }
  144. int http_client_add_no_proxy(http_client_t* cli, const char* host) {
  145. cli->no_proxy_hosts.push_back(host);
  146. return 0;
  147. }
  148. static int http_client_redirect(HttpRequest* req, HttpResponse* resp) {
  149. std::string location = resp->headers["Location"];
  150. if (!location.empty()) {
  151. hlogi("redirect %s => %s", req->url.c_str(), location.c_str());
  152. req->url = location;
  153. req->ParseUrl();
  154. req->headers["Host"] = req->host;
  155. resp->Reset();
  156. return http_client_send(req, resp);
  157. }
  158. return 0;
  159. }
  160. static int http_client_make_request(http_client_t* cli, HttpRequest* req) {
  161. if (req->url.empty() || *req->url.c_str() == '/') {
  162. req->scheme = cli->https ? "https" : "http";
  163. req->host = cli->host;
  164. req->port = cli->port;
  165. }
  166. req->ParseUrl();
  167. bool https = req->IsHttps();
  168. bool use_proxy = https ? (!cli->https_proxy_host.empty()) : (!cli->http_proxy_host.empty());
  169. if (use_proxy) {
  170. if (req->host == "127.0.0.1" || req->host == "localhost") {
  171. use_proxy = false;
  172. }
  173. }
  174. if (use_proxy) {
  175. for (const auto& host : cli->no_proxy_hosts) {
  176. if (req->host == host) {
  177. use_proxy = false;
  178. break;
  179. }
  180. }
  181. }
  182. if (use_proxy) {
  183. req->SetProxy(https ? cli->https_proxy_host.c_str() : cli->http_proxy_host.c_str(),
  184. https ? cli->https_proxy_port : cli->http_proxy_port);
  185. }
  186. if (req->timeout == 0) {
  187. req->timeout = cli->timeout;
  188. }
  189. for (const auto& pair : cli->headers) {
  190. if (req->headers.find(pair.first) == req->headers.end()) {
  191. req->headers.insert(pair);
  192. }
  193. }
  194. return 0;
  195. }
  196. int http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp) {
  197. if (!cli || !req || !resp) return ERR_NULL_POINTER;
  198. http_client_make_request(cli, req);
  199. if (req->http_cb) resp->http_cb = std::move(req->http_cb);
  200. int ret = __http_client_send(cli, req, resp);
  201. if (ret != 0) return ret;
  202. // redirect
  203. if (req->redirect && HTTP_STATUS_IS_REDIRECT(resp->status_code)) {
  204. return http_client_redirect(req, resp);
  205. }
  206. return 0;
  207. }
  208. int http_client_send(HttpRequest* req, HttpResponse* resp) {
  209. if (!req || !resp) return ERR_NULL_POINTER;
  210. http_client_t cli;
  211. return http_client_send(&cli, req, resp);
  212. }
  213. #ifdef WITH_CURL
  214. static size_t s_header_cb(char* buf, size_t size, size_t cnt, void* userdata) {
  215. if (buf == NULL || userdata == NULL) return 0;
  216. size_t len = size * cnt;
  217. std::string str(buf, len);
  218. HttpResponse* resp = (HttpResponse*)userdata;
  219. std::string::size_type pos = str.find_first_of(':');
  220. if (pos == std::string::npos) {
  221. if (strncmp(buf, "HTTP/", 5) == 0) {
  222. // status line
  223. //hlogd("%s", buf);
  224. int http_major = 1, http_minor = 1, status_code = 200;
  225. if (buf[5] == '1') {
  226. // HTTP/1.1 200 OK\r\n
  227. sscanf(buf, "HTTP/%d.%d %d", &http_major, &http_minor, &status_code);
  228. }
  229. else if (buf[5] == '2') {
  230. // HTTP/2 200\r\n
  231. sscanf(buf, "HTTP/%d %d", &http_major, &status_code);
  232. http_minor = 0;
  233. }
  234. resp->http_major = http_major;
  235. resp->http_minor = http_minor;
  236. resp->status_code = (http_status)status_code;
  237. if (resp->http_cb) {
  238. resp->http_cb(resp, HP_MESSAGE_BEGIN, NULL, 0);
  239. }
  240. }
  241. }
  242. else {
  243. // headers
  244. std::string key = trim(str.substr(0, pos));
  245. std::string value = trim(str.substr(pos+1));
  246. resp->headers[key] = value;
  247. }
  248. return len;
  249. }
  250. static size_t s_body_cb(char* buf, size_t size, size_t cnt, void *userdata) {
  251. if (buf == NULL || userdata == NULL) return 0;
  252. size_t len = size * cnt;
  253. HttpMessage* resp = (HttpMessage*)userdata;
  254. if (resp->http_cb) {
  255. if (resp->content == NULL && resp->content_length == 0) {
  256. resp->content = buf;
  257. resp->content_length = len;
  258. resp->http_cb(resp, HP_HEADERS_COMPLETE, NULL, 0);
  259. }
  260. resp->http_cb(resp, HP_BODY, buf, len);
  261. } else {
  262. resp->body.append(buf, len);
  263. }
  264. return len;
  265. }
  266. int __http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp) {
  267. if (cli->curl == NULL) {
  268. cli->curl = curl_easy_init();
  269. }
  270. CURL* curl = cli->curl;
  271. // proxy
  272. if (req->IsProxy()) {
  273. curl_easy_setopt(curl, CURLOPT_PROXY, req->host.c_str());
  274. curl_easy_setopt(curl, CURLOPT_PROXYPORT, req->port);
  275. }
  276. // SSL
  277. if (req->IsHttps()) {
  278. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  279. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
  280. }
  281. // http2
  282. if (req->http_major == 2) {
  283. #if LIBCURL_VERSION_NUM < 0x073100 // 7.49.0
  284. curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2_0);
  285. #else
  286. // No Connection: Upgrade
  287. curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE);
  288. #endif
  289. }
  290. // TCP_NODELAY
  291. curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
  292. // method
  293. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, http_method_str(req->method));
  294. // url
  295. req->DumpUrl();
  296. curl_easy_setopt(curl, CURLOPT_URL, req->url.c_str());
  297. //hlogd("%s %s HTTP/%d.%d", http_method_str(req->method), req->url.c_str(), req->http_major, req->http_minor);
  298. // headers
  299. req->FillContentType();
  300. struct curl_slist *headers = NULL;
  301. for (auto& pair : req->headers) {
  302. std::string header = pair.first;
  303. header += ": ";
  304. header += pair.second;
  305. headers = curl_slist_append(headers, header.c_str());
  306. }
  307. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  308. // body
  309. //struct curl_httppost* httppost = NULL;
  310. //struct curl_httppost* lastpost = NULL;
  311. if (req->body.size() == 0) {
  312. req->DumpBody();
  313. /*
  314. if (req->body.size() == 0 &&
  315. req->content_type == MULTIPART_FORM_DATA) {
  316. for (auto& pair : req->mp) {
  317. if (pair.second.filename.size() != 0) {
  318. curl_formadd(&httppost, &lastpost,
  319. CURLFORM_COPYNAME, pair.first.c_str(),
  320. CURLFORM_FILE, pair.second.filename.c_str(),
  321. CURLFORM_END);
  322. }
  323. else if (pair.second.content.size() != 0) {
  324. curl_formadd(&httppost, &lastpost,
  325. CURLFORM_COPYNAME, pair.first.c_str(),
  326. CURLFORM_COPYCONTENTS, pair.second.content.c_str(),
  327. CURLFORM_END);
  328. }
  329. }
  330. if (httppost) {
  331. curl_easy_setopt(curl, CURLOPT_HTTPPOST, httppost);
  332. }
  333. }
  334. */
  335. }
  336. if (req->body.size() != 0) {
  337. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, req->body.c_str());
  338. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, req->body.size());
  339. }
  340. if (req->connect_timeout > 0) {
  341. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, req->connect_timeout);
  342. }
  343. if (req->timeout > 0) {
  344. curl_easy_setopt(curl, CURLOPT_TIMEOUT, req->timeout);
  345. }
  346. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, s_body_cb);
  347. curl_easy_setopt(curl, CURLOPT_WRITEDATA, resp);
  348. curl_easy_setopt(curl, CURLOPT_HEADER, 0);
  349. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, s_header_cb);
  350. curl_easy_setopt(curl, CURLOPT_HEADERDATA, resp);
  351. int ret = curl_easy_perform(curl);
  352. /*
  353. if (ret != 0) {
  354. hloge("curl error: %d: %s", ret, curl_easy_strerror((CURLcode)ret));
  355. }
  356. if (resp->body.length() != 0) {
  357. hlogd("[Response]\n%s", resp->body.c_str());
  358. }
  359. double total_time, name_time, conn_time, pre_time;
  360. curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
  361. curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME, &name_time);
  362. curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &conn_time);
  363. curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME, &pre_time);
  364. hlogd("TIME_INFO: %lf,%lf,%lf,%lf", total_time, name_time, conn_time, pre_time);
  365. */
  366. if (headers) {
  367. curl_slist_free_all(headers);
  368. }
  369. /*
  370. if (httppost) {
  371. curl_formfree(httppost);
  372. }
  373. */
  374. if (resp->http_cb) {
  375. resp->http_cb(resp, HP_MESSAGE_COMPLETE, NULL, 0);
  376. }
  377. return ret;
  378. }
  379. const char* http_client_strerror(int errcode) {
  380. return curl_easy_strerror((CURLcode)errcode);
  381. }
  382. #else
  383. static int http_client_connect(http_client_t* cli, const char* host, int port, int https, int timeout) {
  384. int blocktime = DEFAULT_CONNECT_TIMEOUT;
  385. if (timeout > 0) {
  386. blocktime = MIN(timeout*1000, blocktime);
  387. }
  388. int connfd = ConnectTimeout(host, port, blocktime);
  389. if (connfd < 0) {
  390. fprintf(stderr, "* connect %s:%d failed!\n", host, port);
  391. hloge("connect %s:%d failed!", host, port);
  392. return connfd;
  393. }
  394. tcp_nodelay(connfd, 1);
  395. if (https && cli->ssl == NULL) {
  396. // cli->ssl_ctx > g_ssl_ctx > hssl_ctx_new
  397. hssl_ctx_t ssl_ctx = NULL;
  398. if (cli->ssl_ctx) {
  399. ssl_ctx = cli->ssl_ctx;
  400. } else if (g_ssl_ctx) {
  401. ssl_ctx = g_ssl_ctx;
  402. } else {
  403. cli->ssl_ctx = ssl_ctx = hssl_ctx_new(NULL);
  404. cli->alloced_ssl_ctx = true;
  405. }
  406. if (ssl_ctx == NULL) {
  407. closesocket(connfd);
  408. return NABS(ERR_NEW_SSL_CTX);
  409. }
  410. cli->ssl = hssl_new(ssl_ctx, connfd);
  411. if (cli->ssl == NULL) {
  412. closesocket(connfd);
  413. return NABS(ERR_NEW_SSL);
  414. }
  415. if (!is_ipaddr(host)) {
  416. hssl_set_sni_hostname(cli->ssl, host);
  417. }
  418. int ret = hssl_connect(cli->ssl);
  419. if (ret != 0) {
  420. fprintf(stderr, "* ssl handshake failed: %d\n", ret);
  421. hloge("ssl handshake failed: %d", ret);
  422. hssl_free(cli->ssl);
  423. cli->ssl = NULL;
  424. closesocket(connfd);
  425. return NABS(ret);
  426. }
  427. }
  428. cli->fd = connfd;
  429. return connfd;
  430. }
  431. int __http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp) {
  432. // connect -> send -> recv -> http_parser
  433. int err = 0;
  434. int timeout = req->timeout;
  435. int connfd = cli->fd;
  436. bool https = req->IsHttps() && !req->IsProxy();
  437. bool keepalive = true;
  438. time_t start_time = time(NULL);
  439. time_t cur_time;
  440. int fail_cnt = 0;
  441. if (connfd <= 0) {
  442. connect:
  443. connfd = http_client_connect(cli, req->host.c_str(), req->port, https, MIN(req->connect_timeout, req->timeout));
  444. if (connfd < 0) {
  445. return connfd;
  446. }
  447. }
  448. if (cli->parser == NULL) {
  449. cli->parser = HttpParserPtr(HttpParser::New(HTTP_CLIENT, (http_version)req->http_major));
  450. }
  451. cli->parser->SubmitRequest(req);
  452. char recvbuf[1024] = {0};
  453. int total_nsend, nsend, nrecv;
  454. total_nsend = nsend = nrecv = 0;
  455. send:
  456. char* data = NULL;
  457. size_t len = 0;
  458. while (cli->parser->GetSendData(&data, &len)) {
  459. total_nsend = 0;
  460. while (total_nsend < len) {
  461. if (timeout > 0) {
  462. cur_time = time(NULL);
  463. if (cur_time - start_time >= timeout) {
  464. return ERR_TASK_TIMEOUT;
  465. }
  466. so_sndtimeo(connfd, (timeout-(cur_time-start_time)) * 1000);
  467. }
  468. if (https) {
  469. nsend = hssl_write(cli->ssl, data+total_nsend, len-total_nsend);
  470. }
  471. else {
  472. nsend = send(connfd, data+total_nsend, len-total_nsend, 0);
  473. }
  474. if (nsend <= 0) {
  475. if (++fail_cnt == 1) {
  476. // maybe keep-alive timeout, try again
  477. cli->Close();
  478. goto connect;
  479. }
  480. else {
  481. err = socket_errno();
  482. goto disconnect;
  483. }
  484. }
  485. total_nsend += nsend;
  486. }
  487. }
  488. cli->parser->InitResponse(resp);
  489. recv:
  490. do {
  491. if (timeout > 0) {
  492. cur_time = time(NULL);
  493. if (cur_time - start_time >= timeout) {
  494. return ERR_TASK_TIMEOUT;
  495. }
  496. so_rcvtimeo(connfd, (timeout-(cur_time-start_time)) * 1000);
  497. }
  498. if (https) {
  499. nrecv = hssl_read(cli->ssl, recvbuf, sizeof(recvbuf));
  500. }
  501. else {
  502. nrecv = recv(connfd, recvbuf, sizeof(recvbuf), 0);
  503. }
  504. if (nrecv <= 0) {
  505. if (resp->content_length == 0 && resp->http_major == 1 && resp->http_minor == 0) {
  506. // HTTP/1.0, assume close after body
  507. goto disconnect;
  508. }
  509. if (++fail_cnt == 1) {
  510. // maybe keep-alive timeout, try again
  511. cli->Close();
  512. goto connect;
  513. }
  514. else {
  515. err = socket_errno();
  516. goto disconnect;
  517. }
  518. }
  519. int nparse = cli->parser->FeedRecvData(recvbuf, nrecv);
  520. if (nparse != nrecv) {
  521. return ERR_PARSE;
  522. }
  523. } while(!cli->parser->IsComplete());
  524. keepalive = req->IsKeepAlive() && resp->IsKeepAlive();
  525. if (!keepalive) {
  526. cli->Close();
  527. }
  528. return 0;
  529. disconnect:
  530. cli->Close();
  531. return err;
  532. }
  533. const char* http_client_strerror(int errcode) {
  534. return socket_strerror(errcode);
  535. }
  536. #endif
  537. static int __http_client_send_async(http_client_t* cli, HttpRequestPtr req, HttpResponseCallback resp_cb) {
  538. if (cli->async_client_ == NULL) {
  539. cli->mutex_.lock();
  540. if (cli->async_client_ == NULL) {
  541. cli->async_client_.reset(new hv::AsyncHttpClient);
  542. }
  543. cli->mutex_.unlock();
  544. }
  545. return cli->async_client_->send(req, std::move(resp_cb));
  546. }
  547. int http_client_send_async(http_client_t* cli, HttpRequestPtr req, HttpResponseCallback resp_cb) {
  548. if (!cli || !req) return ERR_NULL_POINTER;
  549. http_client_make_request(cli, req.get());
  550. return __http_client_send_async(cli, req, std::move(resp_cb));
  551. }
  552. static http_client_t* __get_default_async_client();
  553. static void __del_default_async_client();
  554. http_client_t* __get_default_async_client() {
  555. static http_client_t* s_default_async_client = NULL;
  556. static std::mutex s_mutex;
  557. if (s_default_async_client == NULL) {
  558. s_mutex.lock();
  559. if (s_default_async_client == NULL) {
  560. hlogi("create default http async client");
  561. s_default_async_client = http_client_new();
  562. // NOTE: I have No better idea
  563. atexit(__del_default_async_client);
  564. }
  565. s_mutex.unlock();
  566. }
  567. return s_default_async_client;
  568. }
  569. void __del_default_async_client() {
  570. hlogi("destory default http async client");
  571. http_client_del(__get_default_async_client());
  572. }
  573. int http_client_send_async(HttpRequestPtr req, HttpResponseCallback resp_cb) {
  574. if (req == NULL) return ERR_NULL_POINTER;
  575. if (req->timeout == 0) {
  576. req->timeout = DEFAULT_HTTP_TIMEOUT;
  577. }
  578. return __http_client_send_async(__get_default_async_client(), req, std::move(resp_cb));
  579. }