Remove Req lib, use code instead
This commit is contained in:
@@ -50,10 +50,6 @@ endif()
|
||||
find_package(Qt5 COMPONENTS Core Widgets Gui Sql Network Multimedia MultimediaWidgets WebEngineWidgets REQUIRED)
|
||||
find_package(DCMTK REQUIRED)
|
||||
|
||||
set(Req_DIR "${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/Req/pub/cmake")
|
||||
find_package(Req REQUIRED)
|
||||
message(${Req_INCLUDES_DIRS})
|
||||
|
||||
include_directories(${DCMTK_INCLUDE_DIRS})
|
||||
|
||||
|
||||
@@ -83,11 +79,6 @@ if(${DCMTK_FOUND})
|
||||
target_link_libraries(${PROJECT_NAME} dcmnet)
|
||||
endif()
|
||||
|
||||
if(${Req_FOUND})
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC ${Req_INCLUDES_DIRS})
|
||||
target_link_libraries(${PROJECT_NAME} Req)
|
||||
endif()
|
||||
|
||||
target_compile_definitions(${PROJECT_NAME}
|
||||
PRIVATE
|
||||
# If the debug configuration pass the DEBUG define to the compiler
|
||||
@@ -114,9 +105,12 @@ if(UNIX )
|
||||
message("Build With System QT" )
|
||||
link_directories( "/usr/lib64")
|
||||
target_link_libraries(${PROJECT_NAME} Qt5Core.so Qt5Widgets.so Qt5Gui.so Qt5Sql.so Qt5Network.so Qt5Multimedia.so Qt5MultimediaWidgets.so Qt5WebEngineWidgets.so pthread)
|
||||
target_link_libraries(${PROJECT_NAME} curl)
|
||||
|
||||
else()
|
||||
message("Build With packaged QT" )
|
||||
target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Widgets Qt5::Gui Qt5::Sql Qt5::Network Qt5::Multimedia Qt5::MultimediaWidgets Qt5::WebEngineWidgets pthread)
|
||||
target_link_libraries(${PROJECT_NAME} curl)
|
||||
endif(Build_With_System_QT)
|
||||
else()
|
||||
target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Widgets Qt5::Gui Qt5::Sql Qt5::Network Qt5::Multimedia Qt5::MultimediaWidgets Qt5::WebEngineWidgets)
|
||||
|
||||
@@ -4,195 +4,215 @@
|
||||
#include <cstddef>
|
||||
#include <malloc.h>
|
||||
#include <string>
|
||||
namespace {
|
||||
const char* CREATE_URL = "/Scan/Create/";
|
||||
const char* CHECK_URL = "/v/";
|
||||
const char* QUERY_SCAN_URL = "/Scan/Query/";
|
||||
const char* QUERY_VERSION_URL = "/Version/";
|
||||
const char* QUERY_RECON_URL = "/Task/Query/";
|
||||
const char* QUERY_TRANSFER_URL = "/Transfer/Query/";
|
||||
const char* SET_PACS_URL = "/Transfer/Setting/";
|
||||
namespace
|
||||
{
|
||||
const char *CREATE_URL = "/Scan/Create/";
|
||||
const char *CHECK_URL = "/v/";
|
||||
const char *QUERY_SCAN_URL = "/Scan/Query/";
|
||||
const char *QUERY_VERSION_URL = "/Version/";
|
||||
const char *QUERY_RECON_URL = "/Task/Query/";
|
||||
const char *QUERY_TRANSFER_URL = "/Transfer/Query/";
|
||||
const char *SET_PACS_URL = "/Transfer/Setting/";
|
||||
}
|
||||
namespace Recon {
|
||||
|
||||
ReconClient::ReconClient(std::string aCerPath, std::string aKeyPath)
|
||||
ReconClient::ReconClient(std::string aCerPath, std::string aKeyPath)
|
||||
{
|
||||
Request::Init();
|
||||
mHeaders["Content-Type"] = "application/json";
|
||||
mRequest.setClientCertificate(aCerPath, aKeyPath);
|
||||
}
|
||||
|
||||
ReconClient::~ReconClient()
|
||||
{
|
||||
Request::Dispose();
|
||||
}
|
||||
|
||||
void ReconClient::SetHost(const std::string &aHost)
|
||||
{
|
||||
mHost = aHost;
|
||||
}
|
||||
|
||||
RequestResult ReconClient::CheckActive()
|
||||
{
|
||||
std::string content;
|
||||
content.append("1");
|
||||
auto resp = mRequest.post(mHost + CHECK_URL, content, mHeaders);
|
||||
if (resp.httpCode() == 200)
|
||||
{
|
||||
Req::Request::Init();
|
||||
mHeaders["Content-Type"] = "application/json";
|
||||
mRequest.setClientCertificate(aCerPath,aKeyPath);
|
||||
return RequestResult::Success();
|
||||
}
|
||||
|
||||
ReconClient::~ReconClient()
|
||||
else
|
||||
{
|
||||
Req::Request::Dispose();
|
||||
mErrorMessage = resp.getContent();
|
||||
return RequestResult::Fail(mErrorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
void ReconClient::SetHost(const std::string& aHost)
|
||||
RequestResult ReconClient::Create(const Scan &aScan)
|
||||
{
|
||||
cJSON *obj = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(obj, "ScanID", aScan.ScanID.c_str());
|
||||
cJSON_AddStringToObject(obj, "PatientID", aScan.PatientID.c_str());
|
||||
cJSON_AddStringToObject(obj, "ReferencePath", aScan.ReferencePath.c_str());
|
||||
if (!aScan.ReferencePath.empty())
|
||||
{
|
||||
mHost = aHost;
|
||||
cJSON_AddStringToObject(obj, "ReferenceID", aScan.ReferenceID.c_str());
|
||||
}
|
||||
|
||||
RequestResult ReconClient::CheckActive(){
|
||||
std::string content;
|
||||
content.append("1");
|
||||
auto resp = mRequest.post(mHost+CHECK_URL,content,mHeaders);
|
||||
if (resp.httpCode() == 200){
|
||||
return RequestResult::Success();
|
||||
}
|
||||
else{
|
||||
mErrorMessage = resp.getContent();
|
||||
return RequestResult::Fail(mErrorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
RequestResult ReconClient::Create( const Scan& aScan)
|
||||
cJSON_AddNumberToObject(obj, "Type", aScan.Type);
|
||||
char *str = cJSON_Print(obj);
|
||||
cJSON_Delete(obj);
|
||||
std::string content;
|
||||
content.append(str);
|
||||
free(str);
|
||||
auto resp = mRequest.post(mHost + CREATE_URL, content, mHeaders);
|
||||
if (resp.httpCode() == 201)
|
||||
{
|
||||
cJSON * obj = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(obj, "ScanID", aScan.ScanID.c_str());
|
||||
cJSON_AddStringToObject(obj, "PatientID", aScan.PatientID.c_str());
|
||||
cJSON_AddStringToObject(obj, "ReferencePath", aScan.ReferencePath.c_str());
|
||||
if (!aScan.ReferencePath.empty()){
|
||||
cJSON_AddStringToObject(obj, "ReferenceID", aScan.ReferenceID.c_str());
|
||||
}
|
||||
cJSON_AddNumberToObject(obj, "Type", aScan.Type);
|
||||
char* str = cJSON_Print(obj);
|
||||
cJSON_Delete(obj);
|
||||
std::string content;
|
||||
content.append(str);
|
||||
free(str);
|
||||
auto resp = mRequest.post(mHost+CREATE_URL,content,mHeaders);
|
||||
if (resp.httpCode() == 201){
|
||||
return RequestResult::Success();
|
||||
}
|
||||
else{
|
||||
mErrorMessage = resp.getContent();
|
||||
return RequestResult::Fail(mErrorMessage);
|
||||
}
|
||||
return RequestResult::Success();
|
||||
}
|
||||
|
||||
RequestResult ReconClient::QueryScan(const std::string& aScanID, int& state)
|
||||
else
|
||||
{
|
||||
cJSON * obj = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(obj, "ScanID", aScanID.data());
|
||||
char* str = cJSON_Print(obj);
|
||||
cJSON_Delete(obj);
|
||||
std::string content;
|
||||
content.append(str);
|
||||
free(str);
|
||||
auto resp = mRequest.post(mHost+QUERY_SCAN_URL,content,mHeaders);
|
||||
if (resp.httpCode() == 200){
|
||||
cJSON * scans = cJSON_Parse(resp.getContent().data());
|
||||
int size = cJSON_GetArraySize(scans);
|
||||
if (size == 1){
|
||||
cJSON * scaninf = cJSON_GetArrayItem(scans, 0);
|
||||
if (scaninf!=NULL){
|
||||
cJSON * value = cJSON_GetObjectItem(scaninf, "State");
|
||||
state = value->valueint;
|
||||
cJSON_Delete(scans);
|
||||
return RequestResult::Success();
|
||||
}
|
||||
mErrorMessage = resp.getContent();
|
||||
return RequestResult::Fail(mErrorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
RequestResult ReconClient::QueryScan(const std::string &aScanID, int &state)
|
||||
{
|
||||
cJSON *obj = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(obj, "ScanID", aScanID.data());
|
||||
char *str = cJSON_Print(obj);
|
||||
cJSON_Delete(obj);
|
||||
std::string content;
|
||||
content.append(str);
|
||||
free(str);
|
||||
auto resp = mRequest.post(mHost + QUERY_SCAN_URL, content, mHeaders);
|
||||
if (resp.httpCode() == 200)
|
||||
{
|
||||
cJSON *scans = cJSON_Parse(resp.getContent().data());
|
||||
int size = cJSON_GetArraySize(scans);
|
||||
if (size == 1)
|
||||
{
|
||||
cJSON *scaninf = cJSON_GetArrayItem(scans, 0);
|
||||
if (scaninf != NULL)
|
||||
{
|
||||
cJSON *value = cJSON_GetObjectItem(scaninf, "State");
|
||||
state = value->valueint;
|
||||
cJSON_Delete(scans);
|
||||
return RequestResult::Success();
|
||||
}
|
||||
mErrorMessage ="get null from server";
|
||||
return RequestResult::Fail(mErrorMessage);
|
||||
}
|
||||
else{
|
||||
mErrorMessage = resp.getContent();
|
||||
return RequestResult::Fail(mErrorMessage);
|
||||
}
|
||||
mErrorMessage = "get null from server";
|
||||
return RequestResult::Fail(mErrorMessage);
|
||||
}
|
||||
|
||||
RequestResult ReconClient::QueryReconID(const std::string& aScanID, std::string& aReconID)
|
||||
else
|
||||
{
|
||||
cJSON * obj = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(obj, "ScanID", aScanID.data());
|
||||
char* str = cJSON_Print(obj);
|
||||
cJSON_Delete(obj);
|
||||
std::string content;
|
||||
content.append(str);
|
||||
free(str);
|
||||
auto resp = mRequest.post(mHost+QUERY_RECON_URL,content,mHeaders);
|
||||
if (resp.httpCode() == 204){
|
||||
return RequestResult::Success();
|
||||
}
|
||||
else if (resp.httpCode() == 200){
|
||||
aReconID = resp.getContent();
|
||||
return RequestResult::Success(aReconID);
|
||||
}
|
||||
else{
|
||||
mErrorMessage = resp.getContent();
|
||||
return RequestResult::Fail(mErrorMessage);
|
||||
|
||||
}
|
||||
mErrorMessage = resp.getContent();
|
||||
return RequestResult::Fail(mErrorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
RequestResult ReconClient::QueryReconID(const std::string &aScanID, std::string &aReconID)
|
||||
{
|
||||
cJSON *obj = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(obj, "ScanID", aScanID.data());
|
||||
char *str = cJSON_Print(obj);
|
||||
cJSON_Delete(obj);
|
||||
std::string content;
|
||||
content.append(str);
|
||||
free(str);
|
||||
auto resp = mRequest.post(mHost + QUERY_RECON_URL, content, mHeaders);
|
||||
if (resp.httpCode() == 204)
|
||||
{
|
||||
return RequestResult::Success();
|
||||
}
|
||||
else if (resp.httpCode() == 200)
|
||||
{
|
||||
aReconID = resp.getContent();
|
||||
return RequestResult::Success(aReconID);
|
||||
}
|
||||
else
|
||||
{
|
||||
mErrorMessage = resp.getContent();
|
||||
return RequestResult::Fail(mErrorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
RequestResult ReconClient::QueryReconTask(const std::string &aReconID, TaskQueryResult &aTask)
|
||||
{
|
||||
cJSON *obj = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(obj, "ReconID", aReconID.data());
|
||||
char *str = cJSON_Print(obj);
|
||||
cJSON_Delete(obj);
|
||||
std::string content;
|
||||
content.append(str);
|
||||
free(str);
|
||||
auto resp = mRequest.post(mHost + QUERY_RECON_URL, content, mHeaders);
|
||||
if (resp.httpCode() == 200)
|
||||
{
|
||||
cJSON *task = cJSON_Parse(resp.getContent().data());
|
||||
aTask.ReconID = cJSON_GetObjectItem(task, "ReconID")->valuestring;
|
||||
aTask.ScanID = cJSON_GetObjectItem(task, "ScanID")->valuestring;
|
||||
aTask.Start = cJSON_GetObjectItem(task, "Start")->valueint != 0;
|
||||
aTask.SeqID = cJSON_GetObjectItem(task, "SeqID")->valueint;
|
||||
cJSON_Delete(task);
|
||||
return RequestResult::Success();
|
||||
}
|
||||
else
|
||||
{
|
||||
mErrorMessage = resp.getContent();
|
||||
return RequestResult::Fail(mErrorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
RequestResult ReconClient::QueryVersion()
|
||||
{
|
||||
std::string content;
|
||||
content.append("1");
|
||||
auto resp = mRequest.post(mHost + QUERY_VERSION_URL, content, mHeaders);
|
||||
if (resp.httpCode() == 200)
|
||||
{
|
||||
std::string version = resp.getContent();
|
||||
return RequestResult::Success(version);
|
||||
}
|
||||
else
|
||||
{
|
||||
mErrorMessage = resp.getContent();
|
||||
return RequestResult::Fail(mErrorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
RequestResult ReconClient::SetPACSSetting(const PACSSetting &aSetting)
|
||||
{
|
||||
cJSON *obj = cJSON_CreateObject();
|
||||
if (!aSetting.AETitle.empty())
|
||||
{
|
||||
cJSON_AddStringToObject(obj, "AETitle", aSetting.AETitle.c_str());
|
||||
}
|
||||
if (!aSetting.ServerIP.empty())
|
||||
{
|
||||
cJSON_AddStringToObject(obj, "IP", aSetting.ServerIP.c_str());
|
||||
}
|
||||
if (!aSetting.ServerAETitle.empty())
|
||||
{
|
||||
cJSON_AddStringToObject(obj, "ServerAETitle", aSetting.ServerAETitle.c_str());
|
||||
}
|
||||
if (aSetting.Port > 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(obj, "Port", aSetting.Port);
|
||||
}
|
||||
char *str = cJSON_Print(obj);
|
||||
cJSON_Delete(obj);
|
||||
std::string content;
|
||||
content.append(str);
|
||||
free(str);
|
||||
auto resp = mRequest.post(mHost + SET_PACS_URL, content, mHeaders);
|
||||
if (resp.httpCode() == 200)
|
||||
{
|
||||
return RequestResult::Success();
|
||||
}
|
||||
else
|
||||
{
|
||||
mErrorMessage = resp.getContent();
|
||||
return RequestResult::Fail(mErrorMessage);
|
||||
}
|
||||
|
||||
RequestResult ReconClient::QueryReconTask(const std::string& aReconID, TaskQueryResult& aTask)
|
||||
{
|
||||
cJSON * obj = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(obj, "ReconID", aReconID.data());
|
||||
char* str = cJSON_Print(obj);
|
||||
cJSON_Delete(obj);
|
||||
std::string content;
|
||||
content.append(str);
|
||||
free(str);
|
||||
auto resp = mRequest.post(mHost+QUERY_RECON_URL,content,mHeaders);
|
||||
if (resp.httpCode() == 200){
|
||||
cJSON * task = cJSON_Parse(resp.getContent().data());
|
||||
aTask.ReconID = cJSON_GetObjectItem(task, "ReconID")->valuestring;
|
||||
aTask.ScanID= cJSON_GetObjectItem(task, "ScanID")->valuestring;
|
||||
aTask.Start = cJSON_GetObjectItem(task, "Start")->valueint!=0;
|
||||
aTask.SeqID = cJSON_GetObjectItem(task, "SeqID")->valueint;
|
||||
cJSON_Delete(task);
|
||||
return RequestResult::Success();
|
||||
}
|
||||
else{
|
||||
mErrorMessage = resp.getContent();
|
||||
return RequestResult::Fail(mErrorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
RequestResult ReconClient::QueryVersion()
|
||||
{
|
||||
std::string content;
|
||||
content.append("1");
|
||||
auto resp = mRequest.post(mHost+QUERY_VERSION_URL,content,mHeaders);
|
||||
if (resp.httpCode() == 200){
|
||||
std::string version = resp.getContent();
|
||||
return RequestResult::Success(version);
|
||||
}
|
||||
else{
|
||||
mErrorMessage = resp.getContent();
|
||||
return RequestResult::Fail(mErrorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
RequestResult ReconClient::SetPACSSetting(const PACSSetting& aSetting)
|
||||
{
|
||||
cJSON * obj = cJSON_CreateObject();
|
||||
if (!aSetting.AETitle.empty()){
|
||||
cJSON_AddStringToObject(obj, "AETitle", aSetting.AETitle.c_str());
|
||||
}
|
||||
if (!aSetting.ServerIP.empty()){
|
||||
cJSON_AddStringToObject(obj, "IP", aSetting.ServerIP.c_str());
|
||||
}
|
||||
if (!aSetting.ServerAETitle.empty()){
|
||||
cJSON_AddStringToObject(obj, "ServerAETitle", aSetting.ServerAETitle.c_str());
|
||||
}
|
||||
if (aSetting.Port>0){
|
||||
cJSON_AddNumberToObject(obj, "Port", aSetting.Port);
|
||||
}
|
||||
char* str = cJSON_Print(obj);
|
||||
cJSON_Delete(obj);
|
||||
std::string content;
|
||||
content.append(str);
|
||||
free(str);
|
||||
auto resp = mRequest.post(mHost+SET_PACS_URL,content,mHeaders);
|
||||
if (resp.httpCode() == 200){
|
||||
return RequestResult::Success();
|
||||
}
|
||||
else{
|
||||
mErrorMessage = resp.getContent();
|
||||
return RequestResult::Fail(mErrorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,30 +4,26 @@
|
||||
#include "Request.h"
|
||||
#include "RequestResult.h"
|
||||
#include "ProtocolStructs.h"
|
||||
namespace Recon {
|
||||
class ReconClient
|
||||
{
|
||||
public:
|
||||
ReconClient(std::string aCerPath, std::string aKeyPath);
|
||||
~ReconClient();
|
||||
void SetHost(const std::string& aHost);
|
||||
RequestResult CheckActive();
|
||||
RequestResult Create(const Scan& aScan);
|
||||
RequestResult QueryScan(const std::string& aScanID, int& state);
|
||||
RequestResult QueryReconID(const std::string& aScanID, std::string& aReconID);
|
||||
RequestResult QueryReconTask(const std::string& aReconID, TaskQueryResult& aTask);
|
||||
RequestResult QueryVersion();
|
||||
RequestResult SetPACSSetting(const PACSSetting& aSetting);
|
||||
class ReconClient
|
||||
{
|
||||
public:
|
||||
ReconClient(std::string aCerPath, std::string aKeyPath);
|
||||
~ReconClient();
|
||||
void SetHost(const std::string &aHost);
|
||||
RequestResult CheckActive();
|
||||
RequestResult Create(const Scan &aScan);
|
||||
RequestResult QueryScan(const std::string &aScanID, int &state);
|
||||
RequestResult QueryReconID(const std::string &aScanID, std::string &aReconID);
|
||||
RequestResult QueryReconTask(const std::string &aReconID, TaskQueryResult &aTask);
|
||||
RequestResult QueryVersion();
|
||||
RequestResult SetPACSSetting(const PACSSetting &aSetting);
|
||||
|
||||
private:
|
||||
std::string mVersionContent;
|
||||
std::string mHost;
|
||||
std::string mErrorMessage;
|
||||
Req::Request mRequest;
|
||||
std::unordered_map<std::string, std::string> mHeaders;
|
||||
};
|
||||
private:
|
||||
std::string mVersionContent;
|
||||
std::string mHost;
|
||||
std::string mErrorMessage;
|
||||
Request mRequest;
|
||||
std::unordered_map<std::string, std::string> mHeaders;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif // __RECONCLIENT_H__
|
||||
@@ -24,7 +24,7 @@ ReconManager* ReconManager::getInstance()
|
||||
|
||||
ReconManager::ReconManager(QObject* aParent)
|
||||
: QObject(aParent)
|
||||
, mReconClient(new Recon::ReconClient(CRT_FILE, KEY_FILE))
|
||||
, mReconClient(new ReconClient(CRT_FILE, KEY_FILE))
|
||||
, mIsConnected(false)
|
||||
{
|
||||
init();
|
||||
|
||||
@@ -4,11 +4,7 @@
|
||||
#include <QObject>
|
||||
|
||||
class QTimer;
|
||||
|
||||
namespace Recon
|
||||
{
|
||||
class ReconClient;
|
||||
}
|
||||
class ReconClient;
|
||||
|
||||
class ReconManager : public QObject
|
||||
{
|
||||
@@ -43,7 +39,7 @@ signals:
|
||||
void getReconVersionResponsed(const QString& aResult);
|
||||
|
||||
private:
|
||||
Recon::ReconClient* mReconClient;
|
||||
ReconClient* mReconClient;
|
||||
bool mIsConnected;
|
||||
};
|
||||
|
||||
|
||||
155
src/recon/Request.cpp
Normal file
155
src/recon/Request.cpp
Normal file
@@ -0,0 +1,155 @@
|
||||
#include "Request.h"
|
||||
#include "Response.h"
|
||||
#include "curl/curl.h"
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
#include <exception>
|
||||
#include <new>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
namespace
|
||||
{
|
||||
static size_t
|
||||
WriteMemoryCallback(char *contents, size_t size, size_t nmemb, void *userp)
|
||||
{
|
||||
size_t realsize = size * nmemb;
|
||||
std::string *str = (std::string *)userp;
|
||||
if (realsize)
|
||||
{
|
||||
str->append(contents);
|
||||
}
|
||||
return realsize;
|
||||
}
|
||||
}
|
||||
Request::Request() : mCurl(NULL),
|
||||
mHeaders(NULL),
|
||||
mVerbose(false),
|
||||
mValid(true)
|
||||
{
|
||||
mCurl = curl_easy_init();
|
||||
if (!mCurl)
|
||||
{
|
||||
mValid = false;
|
||||
}
|
||||
}
|
||||
|
||||
Request::~Request()
|
||||
{
|
||||
if (mCurl)
|
||||
{
|
||||
curl_easy_cleanup(mCurl);
|
||||
}
|
||||
if (mHeaders)
|
||||
{
|
||||
curl_slist_free_all((struct curl_slist *)mHeaders);
|
||||
}
|
||||
}
|
||||
|
||||
void Request::setClientCertificate(const std::string &cerPath, const std::string &keyPath)
|
||||
{
|
||||
if (!mValid)
|
||||
{
|
||||
return;
|
||||
}
|
||||
mCert = cerPath;
|
||||
mKey = keyPath;
|
||||
curl_easy_setopt(mCurl, CURLOPT_SSLCERT, cerPath.data());
|
||||
curl_easy_setopt(mCurl, CURLOPT_SSLKEY, keyPath.data());
|
||||
}
|
||||
|
||||
void Request::setVerbose(bool verbose)
|
||||
{
|
||||
mVerbose = verbose;
|
||||
}
|
||||
|
||||
Response Request::post(const std::string &url, const std::string &body,
|
||||
const std::unordered_map<std::string, std::string> &headers)
|
||||
{
|
||||
if (!mValid)
|
||||
{
|
||||
return Response::ErrorResponse("http handle not valid!");
|
||||
}
|
||||
if (url.empty())
|
||||
{
|
||||
return Response::ErrorResponse("post error, url is empty!");
|
||||
}
|
||||
int protocolIdx = url.find_first_of("http");
|
||||
if (protocolIdx < 0)
|
||||
{
|
||||
return Response::ErrorResponse("not http protocal find in the url!");
|
||||
}
|
||||
curl_easy_setopt(mCurl, CURLOPT_URL, url.data());
|
||||
// https设置
|
||||
if (url.length() > protocolIdx + 5 && url[protocolIdx + 4] == 's')
|
||||
{
|
||||
// 避免使用本地ca
|
||||
curl_easy_setopt(mCurl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
// 关于host名验证,便于直接使用ip地址访问!!!
|
||||
curl_easy_setopt(mCurl, CURLOPT_SSL_VERIFYHOST, 0L);
|
||||
}
|
||||
|
||||
curl_easy_setopt(mCurl, CURLOPT_POST, 1L);
|
||||
// CURLOPT_POSTFIELDS 不可以为空不然会有问题
|
||||
if (body.empty())
|
||||
{
|
||||
curl_easy_setopt(mCurl, CURLOPT_POSTFIELDS, "");
|
||||
}
|
||||
else
|
||||
{
|
||||
curl_easy_setopt(mCurl, CURLOPT_POSTFIELDS, body.data());
|
||||
}
|
||||
|
||||
// set headers
|
||||
if (!headers.empty())
|
||||
{
|
||||
for (auto item : headers)
|
||||
{
|
||||
std::string header;
|
||||
header.append(item.first);
|
||||
header.append(": ");
|
||||
header.append(item.second);
|
||||
mHeaders = curl_slist_append((struct curl_slist *)mHeaders, header.data());
|
||||
}
|
||||
curl_easy_setopt(mCurl, CURLOPT_HTTPHEADER, mHeaders);
|
||||
}
|
||||
Response resp;
|
||||
curl_easy_setopt(mCurl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
|
||||
curl_easy_setopt(mCurl, CURLOPT_WRITEDATA, (void *)&(resp.getContent()));
|
||||
|
||||
curl_easy_setopt(mCurl, CURLOPT_TIMEOUT, 3L);
|
||||
curl_easy_setopt(mCurl, CURLOPT_CONNECTTIMEOUT, 3L);
|
||||
if (mVerbose)
|
||||
{
|
||||
curl_easy_setopt(mCurl, CURLOPT_VERBOSE, 1L);
|
||||
}
|
||||
CURLcode res;
|
||||
/* Perform the request, res will get the return code */
|
||||
res = curl_easy_perform(mCurl);
|
||||
|
||||
if (mHeaders)
|
||||
{
|
||||
curl_slist_free_all((struct curl_slist *)mHeaders);
|
||||
mHeaders = NULL;
|
||||
}
|
||||
/* Check for errors */
|
||||
if (res != CURLE_OK)
|
||||
{
|
||||
char buffer[4096] = {0};
|
||||
sprintf(buffer, "post failed because do curl_easy_perform() failed:%s \n", curl_easy_strerror(res));
|
||||
return Response::ErrorResponse(buffer);
|
||||
}
|
||||
long response_code;
|
||||
curl_easy_getinfo(mCurl, CURLINFO_RESPONSE_CODE, &response_code);
|
||||
resp.setHttpCode(response_code);
|
||||
return std::move(resp);
|
||||
}
|
||||
void Request::Init()
|
||||
{
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
}
|
||||
|
||||
void Request::Dispose()
|
||||
{
|
||||
curl_global_cleanup();
|
||||
}
|
||||
33
src/recon/Request.h
Normal file
33
src/recon/Request.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef _GUI_REQUEST_H__
|
||||
#define _GUI_REQUEST_H__
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "Response.h"
|
||||
class Request
|
||||
{
|
||||
public:
|
||||
static void Init();
|
||||
static void Dispose();
|
||||
Request();
|
||||
Request(Request &&) = delete;
|
||||
Request(const Request &) = delete;
|
||||
Request &operator=(Request &&) = delete;
|
||||
Request &operator=(const Request &) = delete;
|
||||
~Request();
|
||||
bool isValid();
|
||||
void setClientCertificate(const std::string &cerPath, const std::string &keyPath);
|
||||
void setVerbose(bool verbose);
|
||||
Response post(const std::string &url, const std::string &body = std::string(),
|
||||
const std::unordered_map<std::string, std::string> &headers =
|
||||
std::unordered_map<std::string, std::string>());
|
||||
|
||||
private:
|
||||
void *mCurl;
|
||||
void *mHeaders;
|
||||
std::string mCert;
|
||||
std::string mKey;
|
||||
bool mVerbose;
|
||||
bool mValid;
|
||||
};
|
||||
#endif // __REQUEST_H__
|
||||
37
src/recon/Response.cpp
Normal file
37
src/recon/Response.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "Response.h"
|
||||
|
||||
Response::Response() : mResponsed(true)
|
||||
{
|
||||
}
|
||||
|
||||
Response Response::ErrorResponse(const std::string &content)
|
||||
{
|
||||
Response resp ;
|
||||
resp.mContent = content;
|
||||
resp.mResponsed = false;
|
||||
return resp;
|
||||
}
|
||||
|
||||
Response::~Response()
|
||||
{
|
||||
}
|
||||
|
||||
void Response::setHttpCode(long code)
|
||||
{
|
||||
mHttpCode = code;
|
||||
}
|
||||
|
||||
long Response::httpCode()
|
||||
{
|
||||
return mHttpCode;
|
||||
}
|
||||
|
||||
std::string &Response::getContent()
|
||||
{
|
||||
return mContent;
|
||||
}
|
||||
|
||||
bool Response::isResponsed()
|
||||
{
|
||||
return mResponsed;
|
||||
}
|
||||
25
src/recon/Response.h
Normal file
25
src/recon/Response.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef _GUI_RESPONSE_H__
|
||||
#define _GUI_RESPONSE_H__
|
||||
#include "string"
|
||||
#include <string>
|
||||
class Response
|
||||
{
|
||||
public:
|
||||
Response();
|
||||
static Response ErrorResponse(const std::string &content);
|
||||
Response(Response &&) = default;
|
||||
Response(const Response &) = default;
|
||||
Response &operator=(Response &&) = delete;
|
||||
Response &operator=(const Response &) = delete;
|
||||
~Response();
|
||||
long httpCode();
|
||||
void setHttpCode(long code);
|
||||
std::string &getContent();
|
||||
bool isResponsed();
|
||||
|
||||
private:
|
||||
long mHttpCode = 404;
|
||||
std::string mContent;
|
||||
bool mResponsed;
|
||||
};
|
||||
#endif // _GUI_RESPONSE_H__
|
||||
Reference in New Issue
Block a user