linux版本中间件

http.h 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  5. * the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. *
  13. * @author baidu aip
  14. */
  15. #ifndef __AIP_HTTP_H__
  16. #define __AIP_HTTP_H__
  17. #include "curl/curl.h"
  18. #include <sstream>
  19. #include <string>
  20. #include <map>
  21. #include "json/json.h"
  22. namespace aip {
  23. inline size_t onWriteData(void * buffer, size_t size, size_t nmemb, void * userp)
  24. {
  25. std::string * str = dynamic_cast<std::string *>((std::string *)userp);
  26. str->append((char *)buffer, size * nmemb);
  27. return nmemb;
  28. }
  29. class HttpClient
  30. {
  31. private:
  32. bool debug = false;
  33. int connect_timeout = 10000;
  34. int socket_timeout = 10000;
  35. void makeUrlencodedForm(std::map<std::string, std::string> const & params, std::string * content) const
  36. {
  37. content->clear();
  38. std::map<std::string, std::string>::const_iterator it;
  39. for(it=params.begin(); it!=params.end(); it++)
  40. {
  41. char * key = curl_escape(it->first.c_str(), (int) it->first.size());
  42. char * value = curl_escape(it->second.c_str(),(int) it->second.size());
  43. *content += key;
  44. *content += '=';
  45. *content += value;
  46. *content += '&';
  47. curl_free(key);
  48. curl_free(value);
  49. }
  50. }
  51. void appendUrlParams(std::map<std::string, std::string> const & params, std::string* url) const
  52. {
  53. if(params.empty()) {
  54. return;
  55. }
  56. std::string content;
  57. this->makeUrlencodedForm(params, &content);
  58. bool url_has_param = false;
  59. for (const auto& ch : *url) {
  60. if (ch == '?') {
  61. url_has_param = true;
  62. break;
  63. }
  64. }
  65. if (url_has_param) {
  66. url->append("&");
  67. } else {
  68. url->append("?");
  69. }
  70. url->append(content);
  71. }
  72. void appendHeaders(std::map<std::string, std::string> const & headers, curl_slist ** slist) const
  73. {
  74. std::ostringstream ostr;
  75. std::map<std::string, std::string>::const_iterator it;
  76. for(it=headers.begin(); it!=headers.end(); it++)
  77. {
  78. ostr << it->first << ":" << it->second;
  79. *slist = curl_slist_append(*slist, ostr.str().c_str());
  80. ostr.str("");
  81. }
  82. }
  83. public:
  84. HttpClient() = default;
  85. HttpClient(const HttpClient &) = delete;
  86. HttpClient & operator=(const HttpClient &) = delete;
  87. void setConnectTimeout(int connect_timeout)
  88. {
  89. this->connect_timeout = connect_timeout;
  90. }
  91. void setSocketTimeout(int socket_timeout)
  92. {
  93. this->socket_timeout = socket_timeout;
  94. }
  95. void setDebug(bool debug)
  96. {
  97. this->debug = debug;
  98. }
  99. int get(
  100. std::string url,
  101. std::map<std::string, std::string> const * params,
  102. std::map<std::string, std::string> const * headers,
  103. std::string * response) const
  104. {
  105. CURL * curl = curl_easy_init();
  106. struct curl_slist * slist = NULL;
  107. if (headers) {
  108. this->appendHeaders(*headers, &slist);
  109. }
  110. if (params) {
  111. this->appendUrlParams(*params, &url);
  112. }
  113. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  114. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
  115. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, onWriteData);
  116. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) response);
  117. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, true);
  118. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, this->connect_timeout);
  119. curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, this->socket_timeout);
  120. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
  121. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
  122. curl_easy_setopt(curl, CURLOPT_VERBOSE, this->debug);
  123. int status_code = curl_easy_perform(curl);
  124. curl_easy_cleanup(curl);
  125. curl_slist_free_all(slist);
  126. return status_code;
  127. }
  128. int post(
  129. std::string url,
  130. std::map<std::string, std::string> const * params,
  131. const std::string & body,
  132. std::map<std::string, std::string> const * headers,
  133. std::string * response) const
  134. {
  135. struct curl_slist * slist = NULL;
  136. CURL * curl = curl_easy_init();
  137. if (headers) {
  138. this->appendHeaders(*headers, &slist);
  139. }
  140. if (params) {
  141. this->appendUrlParams(*params, &url);
  142. }
  143. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  144. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
  145. curl_easy_setopt(curl, CURLOPT_POST, true);
  146. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
  147. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, body.size());
  148. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, onWriteData);
  149. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) response);
  150. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, true);
  151. curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, this->connect_timeout);
  152. curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, this->socket_timeout);
  153. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
  154. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
  155. curl_easy_setopt(curl, CURLOPT_VERBOSE, this->debug);
  156. int status_code = curl_easy_perform(curl);
  157. curl_easy_cleanup(curl);
  158. curl_slist_free_all(slist);
  159. return status_code;
  160. }
  161. int post(
  162. std::string url,
  163. std::map<std::string, std::string> const * params,
  164. std::map<std::string, std::string> const & data,
  165. std::map<std::string, std::string> const * headers,
  166. std::string * response) const
  167. {
  168. std::string body;
  169. this->makeUrlencodedForm(data, &body);
  170. return this->post(std::move(url), params, body, headers, response);
  171. }
  172. int post(
  173. std::string url,
  174. std::map<std::string, std::string> const * params,
  175. Json::Value const & data,
  176. std::map<std::string, std::string> const * headers,
  177. std::string * response) const
  178. {
  179. std::string body;
  180. Json::StreamWriterBuilder swb;
  181. std::unique_ptr<Json::StreamWriter> writer(swb.newStreamWriter());
  182. std::ostringstream os;
  183. writer->write(data, &os);
  184. body = os.str();
  185. std::map<std::string, std::string> temp_headers;
  186. if (headers) {
  187. std::map<std::string, std::string> temp_headers(*headers);
  188. }
  189. temp_headers["Content-Type"] = "application/json";
  190. return this->post(url.c_str(), params, body, &temp_headers, response);
  191. }
  192. int post(
  193. std::string url,
  194. std::map<std::string, std::string> const * params,
  195. std::map<std::string, std::string> const * headers,
  196. std::string * response) const
  197. {
  198. const static std::string EMPTY_STRING;
  199. return this->post(std::move(url), params, EMPTY_STRING, headers, response);
  200. }
  201. };
  202. }
  203. #endif