RestfulFuncImpl.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #include "RestfulFunc.h"
  2. #include <string>
  3. #include <cpprest/http_client.h>
  4. #include <mutex>
  5. #if defined(_MSC_VER)
  6. #include <Windows.h>
  7. #endif //_MSC_VER
  8. using namespace utility; // Common utilities like string conversions
  9. using namespace web; // Common features like URIs.
  10. using namespace web::http; // Common HTTP functionality
  11. using namespace web::http::client; // HTTP client features
  12. volatile GetRestfulTokenCallBack g_tokenCall = nullptr;
  13. RVCRESTFULSDK_API void SetRestfulTokenCallBack(GetRestfulTokenCallBack t_callBack)
  14. {
  15. g_tokenCall = t_callBack;
  16. }
  17. RestfulClient& RestfulClient::getInstance()
  18. {
  19. static RestfulClient c;
  20. return c;
  21. }
  22. RestfulClient::RestfulClient()
  23. {
  24. }
  25. RestfulClient::~RestfulClient()
  26. {
  27. }
  28. bool HttpClientRequestConfig::getToken(std::string& t_channelId, std::string& t_token, std::string& terminalNo, std::string& reserve1) const {
  29. if (g_tokenCall == nullptr)
  30. return false;
  31. if (m_withToken)
  32. {
  33. g_tokenCall(t_channelId, t_token, terminalNo, reserve1);
  34. if (t_channelId.length() > 0 && t_token.length() > 0)
  35. return true;
  36. }
  37. return false;
  38. }
  39. namespace
  40. {
  41. const http::method MappingHttpRequestMethod(HttpRequestMethod provideMethod)
  42. {
  43. switch (provideMethod) {
  44. case GET:
  45. return http::methods::GET;
  46. break;
  47. case POST:
  48. return http::methods::POST;
  49. break;
  50. case PUT:
  51. return http::methods::PUT;
  52. break;
  53. case DEL:
  54. return http::methods::DEL;
  55. break;
  56. case HEAD:
  57. return http::methods::HEAD;
  58. break;
  59. case OPTIONS:
  60. return http::methods::OPTIONS;
  61. break;
  62. case TRCE:
  63. return http::methods::TRCE;
  64. break;
  65. case CONNECT:
  66. return http::methods::CONNECT;
  67. break;
  68. case MERGE:
  69. return http::methods::MERGE;
  70. break;
  71. case PATCH:
  72. return http::methods::PATCH;
  73. break;
  74. case UPLOAD:
  75. return http::methods::POST;
  76. break;
  77. case DOWNLOAD:
  78. return http::methods::POST;
  79. break;
  80. default:
  81. break;
  82. }
  83. return http::methods::GET;
  84. }
  85. void GetFileNameAndType(const std::string& absoluteFilePath, std::string& fileName, std::string& contentType)
  86. {
  87. size_t pos = absoluteFilePath.find_last_of("/\\");
  88. fileName = absoluteFilePath.substr(pos + 1);
  89. contentType = "application/octet-stream";
  90. pos = fileName.find_last_of(".");
  91. if (pos != std::string::npos) {
  92. std::string ext = fileName.substr(pos + 1);
  93. std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
  94. if (ext == "jpg" || ext == "jpeg") {
  95. contentType = "image/jpeg";
  96. } else if (ext == "txt" /*|| ext == "log"*/) {
  97. contentType = "text/plain";
  98. }
  99. }
  100. }
  101. #if defined(_MSC_VER)
  102. static std::wstring s2w(const std::string str)
  103. {
  104. wchar_t* wstr = NULL;
  105. int n = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
  106. if (n > 0) {
  107. wstr = new wchar_t[n + 1];
  108. if (wstr == NULL) {
  109. return std::wstring();
  110. }
  111. std::memset(wstr, 0, (n + 1) * sizeof(wchar_t));
  112. ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, &wstr[0], n);
  113. std::wstring strr(wstr);
  114. delete wstr;
  115. return strr;
  116. }
  117. return std::wstring();
  118. }
  119. #endif //_MSC_VER
  120. std::string w2s(const std::wstring wstr)
  121. {
  122. #if defined(_MSC_VER)
  123. char* str = NULL;
  124. int n = ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
  125. if (n > 0) {
  126. str = new char[n + 1];
  127. if (str == NULL) {
  128. return std::string();
  129. }
  130. std::memset(str, 0, sizeof(char) * (n + 1));
  131. ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, &str[0], n, NULL, NULL);
  132. std::string strr(str);
  133. delete str;
  134. return strr;
  135. }
  136. return std::string();
  137. #else
  138. if (wstr.empty()) {
  139. return "";
  140. }
  141. unsigned len = wcstombs(NULL, wstr.c_str(), 0) + 1;
  142. if (NULL == setlocale(LC_ALL, "zh_CN.gbk")) {
  143. }
  144. char* p = new char[len];
  145. wcstombs(p, wstr.c_str(), len);
  146. p[len - 1] = '\0';
  147. std::string str(p);
  148. delete[] p;
  149. return str;
  150. #endif // _MSC_VER
  151. }
  152. }
  153. #if defined(_MSC_VER)
  154. #define STRW(str) s2w(str)
  155. #define WSTR(str) w2s(str)
  156. #else
  157. #define STRW(str) (str)
  158. #define WSTR(str) (str)
  159. #endif //_MSC_VER
  160. void RestfulClient::Do(const HttpClientRequestConfig* const pRequestConfig, HttpClientResponseResult* pResponse, HttpClientTraceLink* pTrace) const
  161. {
  162. static std::mutex do_mtx;
  163. std::lock_guard<std::mutex> lock(do_mtx);
  164. http_client_config config;
  165. config.set_validate_certificates(pRequestConfig->NeedValidCert());
  166. const uint32_t timeoutVal = pRequestConfig->GetTimeout();
  167. if (timeoutVal != 0) {
  168. config.set_timeout(utility::seconds(timeoutVal));
  169. }
  170. web::http::client::http_client client(STRW(pRequestConfig->GetBaseUri()), config);
  171. http_request request(MappingHttpRequestMethod(pRequestConfig->GetRequestType()));
  172. uri_builder urib(STRW(pRequestConfig->GetSubUri()));
  173. if (pRequestConfig->GetQueryPairs().size() > 0) {
  174. const auto& queries = pRequestConfig->GetQueryPairs();
  175. for (auto it = queries.cbegin(); it != queries.cend(); ++it) {
  176. urib.append_query(STRW(it->first), STRW(it->second));
  177. }
  178. }
  179. request.set_request_uri(urib.to_string());
  180. request.headers().add(header_names::accept, STRW(pRequestConfig->GetAcceptType()));
  181. std::string token, channelId, terminalno, reserve1;
  182. if (pRequestConfig->getToken(channelId, token, terminalno, reserve1))
  183. {
  184. request.headers().add(STRW("channelId"), STRW(channelId));
  185. request.headers().add(STRW("token"), STRW(token));
  186. request.headers().add(STRW("terminalno"), STRW(terminalno));
  187. }
  188. if (pTrace != NULL
  189. && strlen(pTrace->X_B3_BusinessId) > 0
  190. && strlen(pTrace->X_B3_TraceId) > 0
  191. && strlen(pTrace->X_B3_SpanId) > 0
  192. && strlen(pTrace->X_B3_ParentSpanId) > 0
  193. && strlen(pTrace->X_B3_Timestamp) > 0)
  194. {
  195. request.headers().add(STRW("X_B3_BusinessId"), STRW(pTrace->X_B3_BusinessId));
  196. request.headers().add(STRW("X_B3_TraceId"), STRW(pTrace->X_B3_BusinessId));
  197. request.headers().add(STRW("X_B3_SpanId"), STRW(pTrace->X_B3_BusinessId));
  198. request.headers().add(STRW("X_B3_ParentSpanId"), STRW(pTrace->X_B3_BusinessId));
  199. request.headers().add(STRW("X_B3_Timestamp"), STRW(pTrace->X_B3_BusinessId));
  200. }
  201. if (pRequestConfig->GetRequestType() != HttpRequestMethod::GET) {
  202. request.set_body(STRW(pRequestConfig->GetBodyContent()), STRW(pRequestConfig->GetContentType()));
  203. }
  204. if (pRequestConfig->GetRequestType() == HttpRequestMethod::DOWNLOAD) {
  205. pplx::task<void> requestTask = client.request(request)
  206. .then([pResponse](http_response response) ->pplx::task<utility::string_t> {
  207. pResponse->statusCode = response.status_code();
  208. if (pResponse->ResponseOK()) {
  209. return response.extract_string();
  210. } else {
  211. std::cout << "response status result: " << response.status_code() << std::endl;
  212. return pplx::task_from_result(utility::string_t());
  213. }
  214. })
  215. .then([pResponse](pplx::task<utility::string_t> vt) {
  216. try {
  217. pResponse->content = WSTR(vt.get());
  218. } catch (const http_exception& ex) {
  219. pResponse->statusCode = -1;
  220. pResponse->content = ex.what();
  221. }
  222. });
  223. try {
  224. requestTask.wait();
  225. } catch (const std::exception& ex) {
  226. pResponse->statusCode = -1;
  227. pResponse->content = ex.what();
  228. }
  229. } else {
  230. pplx::task<void> requestTask = client.request(request)
  231. .then([pResponse](http_response response) ->pplx::task<json::value> {
  232. pResponse->statusCode = response.status_code();
  233. if (pResponse->ResponseOK()) {
  234. return response.extract_json();
  235. } else {
  236. std::cout << "response status result: " << response.status_code() << std::endl;
  237. return pplx::task_from_result(json::value());
  238. }
  239. })
  240. .then([pResponse](pplx::task<json::value> vt) {
  241. try {
  242. json::value const& v = vt.get();
  243. pResponse->content = WSTR(v.to_string());
  244. } catch (const http_exception& ex) {
  245. pResponse->statusCode = -1;
  246. pResponse->content = ex.what();
  247. }
  248. });
  249. try {
  250. requestTask.wait();
  251. } catch (const std::exception& ex) {
  252. pResponse->statusCode = -1;
  253. pResponse->content = ex.what();
  254. }
  255. }
  256. }
  257. std::pair<std::string, std::string> HttpClientUploadRequest::BuildBodyContent() const
  258. {
  259. std::stringstream data;
  260. std::string boundary{};
  261. for (int i = 0; i < 50; i++) {
  262. boundary += (rand() % 26) + 'A';
  263. }
  264. for (auto& param : mParams) {
  265. data << "\r\n--";
  266. data << boundary;
  267. data << "\r\nContent-Disposition: form-data; name=\"";
  268. data << param.first;
  269. data << "\"\r\n\r\n";
  270. data << param.second;
  271. }
  272. for (auto& file : mFiles) {
  273. std::string inputFileName;
  274. std::string contentType;
  275. GetFileNameAndType(file.second, inputFileName, contentType);
  276. std::ifstream inputFile;
  277. inputFile.open(file.second, std::ios::binary | std::ios::in);
  278. std::string inputFileContent = std::string((std::istreambuf_iterator<char>(inputFile)), (std::istreambuf_iterator<char>()));
  279. data << "\r\n--";
  280. data << boundary;
  281. data << "\r\nContent-Disposition: form-data; name=\"";
  282. data << file.first;
  283. data << "\"; filename=\"";
  284. data << inputFileName;
  285. data << "\"\r\nContent-Type: ";
  286. data << contentType;
  287. data << "\r\n\r\n";
  288. data << inputFileContent;
  289. }
  290. data << "\r\n--";
  291. data << boundary;
  292. data << "--\r\n";
  293. return { boundary, data.str() };
  294. }