CHttpInterceptor.cpp 7.9 KB

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