CHttpInterceptor.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #include "CHttpInterceptor.h"
  2. //#include "logproc.h"
  3. #include "CCommonTools.h"
  4. CHttpInterceptor::CHttpInterceptor()
  5. {
  6. m_bIsRun = true;
  7. m_thread = std::thread(std::bind(&CHttpInterceptor::threadProc, this));
  8. }
  9. CHttpInterceptor::~CHttpInterceptor()
  10. {
  11. m_bIsRun = false;
  12. m_thread.join();
  13. }
  14. void CHttpInterceptor::init(QString sIp, QString sPort, bool bUseHttps)
  15. {
  16. if(bUseHttps)
  17. {
  18. m_sUrl = QString("https://%1:%2").arg(sIp).arg(sPort);
  19. }
  20. else
  21. {
  22. m_sUrl = QString("http://%1:%2").arg(sIp).arg(sPort);
  23. }
  24. }
  25. void CHttpInterceptor::threadProc()
  26. {
  27. while(m_bIsRun)
  28. {
  29. if(m_mReqPkgList.size() > 0)
  30. {
  31. CHttpRequestPackage http_req_pkg;
  32. {
  33. std::scoped_lock lock(m_mReqPkgMutex);
  34. http_req_pkg = m_mReqPkgList.front();
  35. m_mReqPkgList.pop_front();
  36. }
  37. requestProc(http_req_pkg);
  38. }
  39. else
  40. {
  41. Sleep(100);
  42. }
  43. }
  44. }
  45. void CHttpInterceptor::post(CHttpRequestPackage requestPkg)
  46. {
  47. std::scoped_lock lock(m_mReqPkgMutex);
  48. requestPkg.nHttpType = HttpType::htPost;
  49. m_mReqPkgList.push_back(std::move(requestPkg));
  50. }
  51. void CHttpInterceptor::get(CHttpRequestPackage requestPkg)
  52. {
  53. std::scoped_lock lock(m_mReqPkgMutex);
  54. requestPkg.nHttpType = HttpType::htGet;
  55. m_mReqPkgList.push_back(std::move(requestPkg));
  56. }
  57. QString CHttpInterceptor::getHttpUrl() const
  58. {
  59. return m_sUrl;
  60. }
  61. void CHttpInterceptor::getUrl(CHttpRequestPackage requestPkg)
  62. {
  63. std::scoped_lock lock(m_mReqPkgMutex);
  64. requestPkg.nHttpType = HttpType::htGetUrl;
  65. m_mReqPkgList.push_back(std::move(requestPkg));
  66. }
  67. void CHttpInterceptor::put(CHttpRequestPackage requestPkg)
  68. {
  69. std::scoped_lock lock(m_mReqPkgMutex);
  70. requestPkg.nHttpType = HttpType::htPut;
  71. m_mReqPkgList.push_back(std::move(requestPkg));
  72. }
  73. void CHttpInterceptor::downLoad(CHttpRequestPackage requestPkg)
  74. {
  75. std::scoped_lock lock(m_mReqPkgMutex);
  76. requestPkg.nHttpType = HttpType::htDownload;
  77. m_mReqPkgList.push_back(std::move(requestPkg));
  78. }
  79. void CHttpInterceptor::initHeads(const CHttpRequestPackage &requestPkg)
  80. {
  81. if(!requestPkg.sHeadList.isEmpty())
  82. {
  83. for(QString sheads : requestPkg.sHeadList)
  84. {
  85. QString sKey = sheads.left(sheads.indexOf(","));
  86. QString sValue = sheads.right(sheads.length() - sheads.indexOf(",") - 1);
  87. m_httpClient.addHeader(sKey.toStdString(), sValue.toStdString());
  88. }
  89. }
  90. }
  91. bool CHttpInterceptor::doPost(const CHttpRequestPackage &requestPkg, std::string &sResponseStr, int &nCode)
  92. {
  93. if(requestPkg.eParamType == HttpParamType::hptBody)
  94. {
  95. QString sPostStr = "";
  96. Json::Value jPost = Json::Value::null;
  97. if(!requestPkg.sParamList.isEmpty())
  98. {
  99. for(QString sParams : requestPkg.sParamList)
  100. {
  101. QString sKey = sParams.left(sParams.indexOf(","));
  102. QString sValue = sParams.right(sParams.length() - sParams.indexOf(",") - 1);
  103. jPost[sKey.toStdString()] = sValue.toStdString();
  104. // myDebug() << sParams;
  105. }
  106. sPostStr = jPost.toStyledString().c_str();
  107. }
  108. else
  109. {
  110. sPostStr = "{}";
  111. }
  112. std::string sUrl = (m_sUrl + requestPkg.sUri).toStdString();
  113. if(m_httpClient.post(sUrl, sPostStr.toStdString(), sResponseStr, nCode))
  114. {
  115. return true;
  116. }
  117. else
  118. {
  119. return false;
  120. }
  121. }
  122. else if (requestPkg.eParamType == HttpParamType::hptCustomBody)
  123. {
  124. QString sPostStr = "";
  125. if (requestPkg.sParamList.count() == 1)
  126. {
  127. QString sParams = requestPkg.sParamList[0];
  128. sPostStr = sParams.right(sParams.length() - sParams.indexOf(",") - 1);
  129. }
  130. std::string sUrl = (m_sUrl + requestPkg.sUri).toStdString();
  131. if (m_httpClient.post(sUrl, sPostStr.toStdString(), sResponseStr, nCode))
  132. {
  133. return true;
  134. }
  135. else
  136. {
  137. return false;
  138. }
  139. }
  140. else if (requestPkg.eParamType == HttpParamType::hptFormdata)
  141. {
  142. for (QString sParams : requestPkg.sParamList)
  143. {
  144. QString sKey = sParams.left(sParams.indexOf(","));
  145. QString sValue = sParams.right(sParams.length() - sParams.indexOf(",") - 1);
  146. if (sKey == "formdataFileType")
  147. {
  148. sKey = sValue.left(sValue.indexOf(","));
  149. sValue = sValue.right(sValue.length() - sValue.indexOf(",") - 1);
  150. m_httpClient.addFormData(fdt_file, sKey.toStdString(), sValue.toLocal8Bit().data());
  151. }
  152. else
  153. {
  154. m_httpClient.addFormData(fdt_content, sKey.toStdString(), sValue.toStdString());
  155. }
  156. }
  157. std::string sUrl = (m_sUrl + requestPkg.sUri).toStdString();
  158. if (m_httpClient.postFormData(sUrl, sResponseStr, nCode))
  159. {
  160. return true;
  161. }
  162. else
  163. {
  164. return false;
  165. }
  166. }
  167. else
  168. {
  169. if(!requestPkg.sParamList.isEmpty())
  170. {
  171. for(QString sParams : requestPkg.sParamList)
  172. {
  173. QString sKey = sParams.left(sParams.indexOf(","));
  174. QString sValue = sParams.right(sParams.length() - sParams.indexOf(",") - 1);
  175. m_httpClient.addParam(sKey.toStdString(), sValue.toStdString());
  176. // myDebug() << sParams;
  177. }
  178. }
  179. std::string sUrl = (m_sUrl + requestPkg.sUri).toStdString();
  180. if(m_httpClient.post(sUrl, sResponseStr, nCode))
  181. {
  182. return true;
  183. }
  184. else
  185. {
  186. return false;
  187. }
  188. }
  189. }
  190. bool CHttpInterceptor::doGet(const CHttpRequestPackage &requestPkg, std::string &sResponseStr, int &nCode)
  191. {
  192. QString sParamStr = "";
  193. for(QString sParams : requestPkg.sParamList)
  194. {
  195. QString sKey = sParams.left(sParams.indexOf(","));
  196. QString sValue = sParams.right(sParams.length() - sParams.indexOf(",") - 1);
  197. if(sParamStr.isEmpty())
  198. {
  199. sParamStr = sKey + "=" + sValue;
  200. }
  201. else
  202. {
  203. sParamStr = sParamStr + "&" + sKey + "=" + sValue;
  204. }
  205. }
  206. std::string sUrl = "";
  207. if(requestPkg.nHttpType == HttpType::htGetUrl)
  208. {
  209. sUrl = requestPkg.sUri.toStdString();
  210. }
  211. else
  212. {
  213. if(sParamStr == "")
  214. {
  215. sUrl = (m_sUrl + requestPkg.sUri).toStdString();
  216. }
  217. else
  218. {
  219. sUrl = (m_sUrl + requestPkg.sUri + "?" + sParamStr).toStdString();
  220. }
  221. }
  222. if(m_httpClient.get(sUrl, sResponseStr, nCode))
  223. {
  224. return true;
  225. }
  226. else
  227. {
  228. return false;
  229. }
  230. }
  231. bool CHttpInterceptor::doPut(const CHttpRequestPackage &requestPkg, std::string &sResponseStr, int &nCode)
  232. {
  233. QString sParamStr = "";
  234. for (QString sParams : requestPkg.sParamList)
  235. {
  236. QString sKey = sParams.left(sParams.indexOf(","));
  237. QString sValue = sParams.right(sParams.length() - sParams.indexOf(",") - 1);
  238. if (sParamStr.isEmpty())
  239. {
  240. sParamStr = sKey + "=" + sValue;
  241. }
  242. else
  243. {
  244. sParamStr = sParamStr + "&" + sKey + "=" + sValue;
  245. }
  246. }
  247. std::string sUrl = "";
  248. if (sParamStr == "")
  249. {
  250. sUrl = (m_sUrl + requestPkg.sUri).toStdString();
  251. }
  252. else
  253. {
  254. sUrl = (m_sUrl + requestPkg.sUri + "?" + sParamStr).toStdString();
  255. }
  256. if (m_httpClient.put(sUrl, sResponseStr, nCode))
  257. {
  258. return true;
  259. }
  260. else
  261. {
  262. return false;
  263. }
  264. }
  265. //下载文件
  266. bool CHttpInterceptor::downLoadFile(std::string uri, std::string sFileName, int &nResCode)
  267. {
  268. if(m_httpClient.download(uri, sFileName, nResCode))
  269. {
  270. return true;
  271. }
  272. else
  273. {
  274. return false;
  275. }
  276. }