http_client.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "http_client.h"
  2. #include "hstring.h"
  3. /***************************************************************
  4. HttpClient based libcurl
  5. ***************************************************************/
  6. #include "curl/curl.h"
  7. #ifdef _MSC_VER
  8. #pragma comment(lib, "ws2_32.lib")
  9. #pragma comment(lib, "wldap32.lib")
  10. #pragma comment(lib, "advapi32.lib")
  11. #pragma comment(lib, "crypt32.lib")
  12. #endif
  13. //#include "hlog.h"
  14. static size_t s_formget_cb(void *arg, const char *buf, size_t len) {
  15. return len;
  16. }
  17. static size_t s_header_cb(char* buf, size_t size, size_t cnt, void* userdata) {
  18. if (buf == NULL || userdata == NULL) return 0;
  19. HttpResponse* res = (HttpResponse*)userdata;
  20. string str(buf);
  21. string::size_type pos = str.find_first_of(':');
  22. if (pos == string::npos) {
  23. if (strncmp(buf, "HTTP/", 5) == 0) {
  24. // status line
  25. // HTTP/1.1 200 OK\r\n
  26. //hlogd("%s", buf);
  27. int http_major,http_minor,status_code;
  28. sscanf(buf, "HTTP/%d.%d %d", &http_major, &http_minor, &status_code);
  29. res->http_major = http_major;
  30. res->http_minor = http_minor;
  31. res->status_code = (http_status)status_code;
  32. }
  33. }
  34. else {
  35. // headers
  36. string key = trim(str.substr(0, pos));
  37. string value = trim(str.substr(pos+1));
  38. res->headers[key] = value;
  39. }
  40. return size*cnt;
  41. }
  42. static size_t s_body_cb(char *buf, size_t size, size_t cnt, void *userdata) {
  43. if (buf == NULL || userdata == NULL) return 0;
  44. HttpResponse* res = (HttpResponse*)userdata;
  45. res->body.append(buf, size*cnt);
  46. return size*cnt;
  47. }
  48. #include <atomic>
  49. static std::atomic_flag s_curl_global_init = ATOMIC_FLAG_INIT;
  50. int http_client_send(HttpRequest* req, HttpResponse* res, int timeout) {
  51. if (req == NULL || res == NULL) {
  52. return -1;
  53. }
  54. if (!s_curl_global_init.test_and_set()) {
  55. curl_global_init(CURL_GLOBAL_ALL);
  56. }
  57. CURL* handle = curl_easy_init();
  58. // SSL
  59. curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
  60. curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0);
  61. // method
  62. curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, http_method_str(req->method));
  63. // url
  64. std::string url = req->dump_url();
  65. curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
  66. //hlogd("%s %s HTTP/%d.%d", http_method_str(req->method), url.c_str(), req->http_major, req->http_minor);
  67. // header
  68. req->fill_content_type();
  69. struct curl_slist *headers = NULL;
  70. for (auto& pair : req->headers) {
  71. string header = pair.first;
  72. header += ": ";
  73. header += pair.second;
  74. headers = curl_slist_append(headers, header.c_str());
  75. }
  76. curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
  77. // body
  78. struct curl_httppost* httppost = NULL;
  79. struct curl_httppost* lastpost = NULL;
  80. if (req->body.size() == 0) {
  81. req->dump_body();
  82. if (req->body.size() == 0 &&
  83. req->content_type == MULTIPART_FORM_DATA) {
  84. for (auto& pair : req->mp) {
  85. if (pair.second.filename.size() != 0) {
  86. curl_formadd(&httppost, &lastpost,
  87. CURLFORM_COPYNAME, pair.first.c_str(),
  88. CURLFORM_FILE, pair.second.filename.c_str(),
  89. CURLFORM_END);
  90. }
  91. else if (pair.second.content.size() != 0) {
  92. curl_formadd(&httppost, &lastpost,
  93. CURLFORM_COPYNAME, pair.first.c_str(),
  94. CURLFORM_COPYCONTENTS, pair.second.content.c_str(),
  95. CURLFORM_END);
  96. }
  97. }
  98. if (httppost) {
  99. curl_easy_setopt(handle, CURLOPT_HTTPPOST, httppost);
  100. curl_formget(httppost, NULL, s_formget_cb);
  101. }
  102. }
  103. }
  104. if (req->body.size() != 0) {
  105. curl_easy_setopt(handle, CURLOPT_POSTFIELDS, req->body.c_str());
  106. curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, req->body.size());
  107. }
  108. if (timeout > 0) {
  109. curl_easy_setopt(handle, CURLOPT_TIMEOUT, timeout);
  110. }
  111. curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, s_body_cb);
  112. curl_easy_setopt(handle, CURLOPT_WRITEDATA, res);
  113. curl_easy_setopt(handle, CURLOPT_HEADER, 0);
  114. curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, s_header_cb);
  115. curl_easy_setopt(handle, CURLOPT_HEADERDATA, res);
  116. int ret = curl_easy_perform(handle);
  117. /*
  118. if (ret != 0) {
  119. hloge("curl error: %d: %s", ret, curl_easy_strerror((CURLcode)ret));
  120. }
  121. if (res->body.length() != 0) {
  122. hlogd("[Response]\n%s", res->body.c_str());
  123. }
  124. double total_time, name_time, conn_time, pre_time;
  125. curl_easy_getinfo(handle, CURLINFO_TOTAL_TIME, &total_time);
  126. curl_easy_getinfo(handle, CURLINFO_NAMELOOKUP_TIME, &name_time);
  127. curl_easy_getinfo(handle, CURLINFO_CONNECT_TIME, &conn_time);
  128. curl_easy_getinfo(handle, CURLINFO_PRETRANSFER_TIME, &pre_time);
  129. hlogd("TIME_INFO: %lf,%lf,%lf,%lf", total_time, name_time, conn_time, pre_time);
  130. */
  131. if (headers) {
  132. curl_slist_free_all(headers);
  133. }
  134. if (httppost) {
  135. curl_formfree(httppost);
  136. }
  137. curl_easy_cleanup(handle);
  138. return ret;
  139. }
  140. const char* http_client_strerror(int errcode) {
  141. return curl_easy_strerror((CURLcode)errcode);
  142. }