RestfulFuncImpl.cpp 10 KB

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