123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- #include "CHttpInterceptor.h"
- //#include "logproc.h"
- #include "CCommonTools.h"
- CHttpInterceptor::CHttpInterceptor()
- {
- m_bIsRun = true;
- m_thread = std::thread(std::bind(&CHttpInterceptor::threadProc, this));
- }
- CHttpInterceptor::~CHttpInterceptor()
- {
- m_bIsRun = false;
- m_thread.join();
- }
- void CHttpInterceptor::init(QString sIp, QString sPort, bool bUseHttps)
- {
- if(bUseHttps)
- {
- m_sUrl = QString("https://%1:%2").arg(sIp).arg(sPort);
- }
- else
- {
- m_sUrl = QString("http://%1:%2").arg(sIp).arg(sPort);
- }
- }
- void CHttpInterceptor::threadProc()
- {
- while(m_bIsRun)
- {
- if(m_mReqPkgList.size() > 0)
- {
- CHttpRequestPackage http_req_pkg;
- {
- std::scoped_lock lock(m_mReqPkgMutex);
- http_req_pkg = m_mReqPkgList.front();
- m_mReqPkgList.pop_front();
- }
- requestProc(http_req_pkg);
- }
- else
- {
- Sleep(100);
- }
- }
- }
- void CHttpInterceptor::post(CHttpRequestPackage requestPkg)
- {
- std::scoped_lock lock(m_mReqPkgMutex);
- requestPkg.nHttpType = HttpType::htPost;
- m_mReqPkgList.push_back(std::move(requestPkg));
- }
- void CHttpInterceptor::get(CHttpRequestPackage requestPkg)
- {
- std::scoped_lock lock(m_mReqPkgMutex);
- requestPkg.nHttpType = HttpType::htGet;
- m_mReqPkgList.push_back(std::move(requestPkg));
- }
- QString CHttpInterceptor::getHttpUrl() const
- {
- return m_sUrl;
- }
- void CHttpInterceptor::getUrl(CHttpRequestPackage requestPkg)
- {
- std::scoped_lock lock(m_mReqPkgMutex);
- requestPkg.nHttpType = HttpType::htGetUrl;
- m_mReqPkgList.push_back(std::move(requestPkg));
- }
- void CHttpInterceptor::put(CHttpRequestPackage requestPkg)
- {
- std::scoped_lock lock(m_mReqPkgMutex);
- requestPkg.nHttpType = HttpType::htPut;
- m_mReqPkgList.push_back(std::move(requestPkg));
- }
- void CHttpInterceptor::downLoad(CHttpRequestPackage requestPkg)
- {
- std::scoped_lock lock(m_mReqPkgMutex);
- requestPkg.nHttpType = HttpType::htDownload;
- m_mReqPkgList.push_back(std::move(requestPkg));
- }
- void CHttpInterceptor::initHeads(const CHttpRequestPackage &requestPkg)
- {
- if(!requestPkg.sHeadList.isEmpty())
- {
- for(QString sheads : requestPkg.sHeadList)
- {
- QString sKey = sheads.left(sheads.indexOf(","));
- QString sValue = sheads.right(sheads.length() - sheads.indexOf(",") - 1);
- m_httpClient.addHeader(sKey.toStdString(), sValue.toStdString());
- }
- }
- }
- bool CHttpInterceptor::doPost(const CHttpRequestPackage &requestPkg, std::string &sResponseStr, int &nCode)
- {
- if(requestPkg.eParamType == HttpParamType::hptBody)
- {
- QString sPostStr = "";
- Json::Value jPost = Json::Value::null;
- if(!requestPkg.sParamList.isEmpty())
- {
- for(QString sParams : requestPkg.sParamList)
- {
- QString sKey = sParams.left(sParams.indexOf(","));
- QString sValue = sParams.right(sParams.length() - sParams.indexOf(",") - 1);
- jPost[sKey.toStdString()] = sValue.toStdString();
- // myDebug() << sParams;
- }
- sPostStr = jPost.toStyledString().c_str();
- }
- else
- {
- sPostStr = "{}";
- }
- std::string sUrl = (m_sUrl + requestPkg.sUri).toStdString();
- if(m_httpClient.post(sUrl, sPostStr.toStdString(), sResponseStr, nCode))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else if (requestPkg.eParamType == HttpParamType::hptCustomBody)
- {
- QString sPostStr = "";
- if (requestPkg.sParamList.count() == 1)
- {
- QString sParams = requestPkg.sParamList[0];
- sPostStr = sParams.right(sParams.length() - sParams.indexOf(",") - 1);
- }
- std::string sUrl = (m_sUrl + requestPkg.sUri).toStdString();
- if (m_httpClient.post(sUrl, sPostStr.toStdString(), sResponseStr, nCode))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else if (requestPkg.eParamType == HttpParamType::hptFormdata)
- {
- for (QString sParams : requestPkg.sParamList)
- {
- QString sKey = sParams.left(sParams.indexOf(","));
- QString sValue = sParams.right(sParams.length() - sParams.indexOf(",") - 1);
- if (sKey == "formdataFileType")
- {
- sKey = sValue.left(sValue.indexOf(","));
- sValue = sValue.right(sValue.length() - sValue.indexOf(",") - 1);
- m_httpClient.addFormData(fdt_file, sKey.toStdString(), sValue.toLocal8Bit().data());
-
- }
- else
- {
- m_httpClient.addFormData(fdt_content, sKey.toStdString(), sValue.toStdString());
- }
- }
- std::string sUrl = (m_sUrl + requestPkg.sUri).toStdString();
- if (m_httpClient.postFormData(sUrl, sResponseStr, nCode))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- if(!requestPkg.sParamList.isEmpty())
- {
- for(QString sParams : requestPkg.sParamList)
- {
- QString sKey = sParams.left(sParams.indexOf(","));
- QString sValue = sParams.right(sParams.length() - sParams.indexOf(",") - 1);
- m_httpClient.addParam(sKey.toStdString(), sValue.toStdString());
- // myDebug() << sParams;
- }
- }
- std::string sUrl = (m_sUrl + requestPkg.sUri).toStdString();
- if(m_httpClient.post(sUrl, sResponseStr, nCode))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- bool CHttpInterceptor::doGet(const CHttpRequestPackage &requestPkg, std::string &sResponseStr, int &nCode)
- {
- QString sParamStr = "";
- for(QString sParams : requestPkg.sParamList)
- {
- QString sKey = sParams.left(sParams.indexOf(","));
- QString sValue = sParams.right(sParams.length() - sParams.indexOf(",") - 1);
- if(sParamStr.isEmpty())
- {
- sParamStr = sKey + "=" + sValue;
- }
- else
- {
- sParamStr = sParamStr + "&" + sKey + "=" + sValue;
- }
- }
- std::string sUrl = "";
- if(requestPkg.nHttpType == HttpType::htGetUrl)
- {
- sUrl = requestPkg.sUri.toStdString();
- }
- else
- {
- if(sParamStr == "")
- {
- sUrl = (m_sUrl + requestPkg.sUri).toStdString();
- }
- else
- {
- sUrl = (m_sUrl + requestPkg.sUri + "?" + sParamStr).toStdString();
- }
- }
- if(m_httpClient.get(sUrl, sResponseStr, nCode))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- bool CHttpInterceptor::doPut(const CHttpRequestPackage &requestPkg, std::string &sResponseStr, int &nCode)
- {
- QString sParamStr = "";
- for (QString sParams : requestPkg.sParamList)
- {
- QString sKey = sParams.left(sParams.indexOf(","));
- QString sValue = sParams.right(sParams.length() - sParams.indexOf(",") - 1);
- if (sParamStr.isEmpty())
- {
- sParamStr = sKey + "=" + sValue;
- }
- else
- {
- sParamStr = sParamStr + "&" + sKey + "=" + sValue;
- }
- }
- std::string sUrl = "";
- if (sParamStr == "")
- {
- sUrl = (m_sUrl + requestPkg.sUri).toStdString();
- }
- else
- {
- sUrl = (m_sUrl + requestPkg.sUri + "?" + sParamStr).toStdString();
- }
-
- if (m_httpClient.put(sUrl, sResponseStr, nCode))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //下载文件
- bool CHttpInterceptor::downLoadFile(std::string uri, std::string sFileName, int &nResCode)
- {
- if(m_httpClient.download(uri, sFileName, nResCode))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
|