http_client.cpp 5.1 KB

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