Skip to content

Commit

Permalink
update methods for HTTP server
Browse files Browse the repository at this point in the history
  • Loading branch information
moniwe authored and azoitl committed Mar 3, 2024
1 parent 2f7d1b4 commit 9f1de47
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
59 changes: 29 additions & 30 deletions src/com/HTTP/httplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,7 @@ EComResponse CHttpComLayer::openConnection(char *paLayerParameter) {
if(checkSDsAndRDsType()) {
switch(mFb->getComServiceType()){
case e_Server:
if(1 == mFb->getNumSD()) {
mPath = std::string(paLayerParameter);
if(getExtEvHandler<CHTTP_Handler>().addServerPath(this, mPath)) {
eRetVal = e_InitOk;
}
} else {
DEVLOG_ERROR("[HTTP Layer] The FB with PARAM %s coudln't be initialized since only one SD is possible in HTTP Server which is for the response\n",
paLayerParameter);
}
eRetVal = startServer(paLayerParameter);
break;
case e_Client:
eRetVal = openClientConnection(paLayerParameter);
Expand All @@ -64,6 +56,21 @@ EComResponse CHttpComLayer::openConnection(char *paLayerParameter) {
return eRetVal;
}

EComResponse CHttpComLayer::startServer (char *paLayerParameter) {
EComResponse eRetVal = e_InitInvalidId;
if (1 == mFb->getNumSD()) {
mPath = std::string(paLayerParameter);
if (getExtEvHandler<CHTTP_Handler>().addServerPath(this, mPath)) {
eRetVal = e_InitOk;
}
} else {
DEVLOG_ERROR(
"[HTTP Layer] The FB with PARAM %s coudln't be initialized since only one SD is possible in HTTP Server which is for the response\n",
paLayerParameter);
}
return eRetVal;
}

EComResponse CHttpComLayer::openClientConnection(char* paLayerParameter) {
EComResponse eRetVal = e_InitInvalidId;
if(2 == mFb->getNumRD() && 3 == mFb->getNumSD()) {
Expand Down Expand Up @@ -161,10 +168,10 @@ EComResponse CHttpComLayer::sendData(void *paData, unsigned int) {
if(mCorrectlyInitialized) {
switch(mFb->getComServiceType()){
case e_Server:
sendDataAsServer(getSDx(paData, 0), getSDx(paData, 1), getSDx(paData, 2));
sendDataAsServer(paData);
break;
case e_Client:
sendDataAsClient(getSDx(paData, 0), getSDx(paData, 1), getSDx(paData, 2));
sendDataAsClient(paData);
break;
default:
// e_Publisher and e_Subscriber
Expand Down Expand Up @@ -195,16 +202,18 @@ void CHttpComLayer::createRequest() {
}
}

void CHttpComLayer::sendDataAsServer(const CIEC_ANY &paSD0, const CIEC_ANY &paSD1, const CIEC_ANY &paSD2) {
serializeData(paSD0, paSD1, paSD2);
void CHttpComLayer::sendDataAsServer(void *paData) {
serializeData(getSDx(paData, 0), mBody);
createRequest();
CHttpParser::createResponse(mRequest, "HTTP/1.1 200 OK"s, "text/plain"s, mBody);
getExtEvHandler<CHTTP_Handler>().sendServerAnswer(this, mRequest);
mInterruptResp = e_ProcessDataOk;
}

void CHttpComLayer::sendDataAsClient(const CIEC_ANY &paSD0, const CIEC_ANY &paSD1, const CIEC_ANY &paSD2) {
serializeData(paSD0, paSD1, paSD2);
void CHttpComLayer::sendDataAsClient(void *paData) {
serializeData(getSDx(paData, 0), mAuth);
serializeData(getSDx(paData, 1), mParams);
serializeData(getSDx(paData, 2), mBody);
createRequest();
if(getExtEvHandler<CHTTP_Handler>().sendClientData(this, mRequest)) {
mInterruptResp = e_ProcessDataOk;
Expand Down Expand Up @@ -314,21 +323,11 @@ void CHttpComLayer::closeConnection() {
getExtEvHandler<CHTTP_Handler>().forceClose(this);
}

void CHttpComLayer::serializeData(const CIEC_ANY& paSD0, const CIEC_ANY& paSD1, const CIEC_ANY& paSD2) {
if(CIEC_ANY::e_WSTRING == paSD0.getDataTypeID()){
mAuth = static_cast<const CIEC_WSTRING&>(paSD0).getValue();
}else if(CIEC_ANY::e_STRING == paSD0.getDataTypeID()){
mAuth = static_cast<const CIEC_STRING&>(paSD0).getStorage();
}
if(CIEC_ANY::e_WSTRING == paSD1.getDataTypeID()){
mParams = static_cast<const CIEC_WSTRING&>(paSD1).getValue();
}else if(CIEC_ANY::e_STRING == paSD1.getDataTypeID()){
mParams = static_cast<const CIEC_STRING&>(paSD1).getStorage();
}
if(CIEC_ANY::e_WSTRING == paSD2.getDataTypeID()){
mBody = static_cast<const CIEC_WSTRING&>(paSD2).getValue();
}else if(CIEC_ANY::e_STRING == paSD2.getDataTypeID()){
mBody = static_cast<const CIEC_STRING&>(paSD2).getStorage();
void CHttpComLayer::serializeData(const CIEC_ANY& paSDx, std::string& paMember) {
if(CIEC_ANY::e_WSTRING == paSDx.getDataTypeID()){
paMember = static_cast<const CIEC_WSTRING&>(paSDx).getValue();
}else if(CIEC_ANY::e_STRING == paSDx.getDataTypeID()){
paMember = static_cast<const CIEC_STRING&>(paSDx).getStorage();
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/com/HTTP/httplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ namespace forte {

EComResponse openConnection(char* paLayerParameter) override;

EComResponse startServer(char* paLayerParameter);

void closeConnection() override;

EComResponse processInterrupt() override;
Expand Down Expand Up @@ -70,11 +72,11 @@ namespace forte {
EComResponse handleHTTPResponse(char *paData);

/** Serializes the data to a char* */
void serializeData(const CIEC_ANY& paSD0, const CIEC_ANY& paSD1, const CIEC_ANY& paSD2);
void serializeData(const CIEC_ANY& paSDx, std::string& paMember);

void sendDataAsServer(const CIEC_ANY &paSD0, const CIEC_ANY &paSD1, const CIEC_ANY &paSD2);
void sendDataAsServer(void *paData);

void sendDataAsClient(const CIEC_ANY &paSD0, const CIEC_ANY &paSD1, const CIEC_ANY &paSD2);
void sendDataAsClient(void *paData);

const CIEC_ANY& getSDx(void *paData, int paSdNum);

Expand All @@ -89,6 +91,7 @@ namespace forte {
bool storeRequestType(const char* paType);

void createRequest();
EComResponse startServer (EComResponse eRetVal, char *paLayerParameter);

EComResponse mInterruptResp;

Expand All @@ -115,8 +118,6 @@ namespace forte {
std::string mContentType;

bool mCorrectlyInitialized;

bool mHasParameterInSD;
};

}
Expand Down
10 changes: 5 additions & 5 deletions src/com/HTTP/httpparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ using namespace forte::com_infra;
using namespace std::string_literals;

void CHttpParser::createGetRequest (std::string &paDest, const std::string &paHost, const TForteUInt16 paPort, const std::string &paPath, const std::string &paAuth, const std::string &paParams) {
CHttpParser::addCommonHeader(paDest, paHost, paPort, paPath, paAuth, paParams, CHttpComLayer::e_GET);
CHttpParser::addHeaderEnding(paDest);
addCommonHeader(paDest, paHost, paPort, paPath, paAuth, paParams, CHttpComLayer::e_GET);
addHeaderEnding(paDest);
}

void CHttpParser::addBodyToRequest (const std::string &paBody, const std::string &paContentType, std::string &paDest) {
Expand All @@ -35,7 +35,7 @@ void CHttpParser::addBodyToRequest (const std::string &paBody, const std::string
paDest += paContentType;
paDest += "\r\nContent-Length: "s;
paDest += std::to_string(paBody.length());
CHttpParser::addHeaderEnding(paDest);
addHeaderEnding(paDest);
}
if (!paBody.empty()) {
paDest += paBody;
Expand All @@ -44,12 +44,12 @@ void CHttpParser::addBodyToRequest (const std::string &paBody, const std::string

void CHttpParser::createPutPostRequest (std::string &paDest, const std::string &paHost, const TForteUInt16 paPort, const std::string &paPath, const std::string &paAuth, const std::string &paParams, const std::string &paBody,
const std::string& paContentType, CHttpComLayer::ERequestType paType) {
CHttpParser::addCommonHeader(paDest, paHost, paPort, paPath, paAuth, paParams, paType);
addCommonHeader(paDest, paHost, paPort, paPath, paAuth, paParams, paType);
addBodyToRequest(paBody, paContentType, paDest);
}

bool CHttpParser::parseResponse(std::string& paBody, std::string& paResponseCode, char* paSrc) {
if(CHttpParser::getHttpResponseCode(paResponseCode, paSrc)) {
if(getHttpResponseCode(paResponseCode, paSrc)) {
char* helperChar = strstr(paSrc, "\r\n\r\n"); // Extract data from HTTP response char
if(nullptr != helperChar) {
helperChar += sizeof("\r\n\r\n") - 1;
Expand Down

0 comments on commit 9f1de47

Please sign in to comment.