diff --git a/src/com/HTTP/httplayer.cpp b/src/com/HTTP/httplayer.cpp index c27cd2622..ebef450f6 100644 --- a/src/com/HTTP/httplayer.cpp +++ b/src/com/HTTP/httplayer.cpp @@ -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().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); @@ -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().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()) { @@ -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 @@ -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().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().sendClientData(this, mRequest)) { mInterruptResp = e_ProcessDataOk; @@ -314,21 +323,11 @@ void CHttpComLayer::closeConnection() { getExtEvHandler().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(paSD0).getValue(); - }else if(CIEC_ANY::e_STRING == paSD0.getDataTypeID()){ - mAuth = static_cast(paSD0).getStorage(); - } - if(CIEC_ANY::e_WSTRING == paSD1.getDataTypeID()){ - mParams = static_cast(paSD1).getValue(); - }else if(CIEC_ANY::e_STRING == paSD1.getDataTypeID()){ - mParams = static_cast(paSD1).getStorage(); - } - if(CIEC_ANY::e_WSTRING == paSD2.getDataTypeID()){ - mBody = static_cast(paSD2).getValue(); - }else if(CIEC_ANY::e_STRING == paSD2.getDataTypeID()){ - mBody = static_cast(paSD2).getStorage(); +void CHttpComLayer::serializeData(const CIEC_ANY& paSDx, std::string& paMember) { + if(CIEC_ANY::e_WSTRING == paSDx.getDataTypeID()){ + paMember = static_cast(paSDx).getValue(); + }else if(CIEC_ANY::e_STRING == paSDx.getDataTypeID()){ + paMember = static_cast(paSDx).getStorage(); } } diff --git a/src/com/HTTP/httplayer.h b/src/com/HTTP/httplayer.h index 50050da4e..ba55a85cd 100644 --- a/src/com/HTTP/httplayer.h +++ b/src/com/HTTP/httplayer.h @@ -40,6 +40,8 @@ namespace forte { EComResponse openConnection(char* paLayerParameter) override; + EComResponse startServer(char* paLayerParameter); + void closeConnection() override; EComResponse processInterrupt() override; @@ -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); @@ -89,6 +91,7 @@ namespace forte { bool storeRequestType(const char* paType); void createRequest(); + EComResponse startServer (EComResponse eRetVal, char *paLayerParameter); EComResponse mInterruptResp; @@ -115,8 +118,6 @@ namespace forte { std::string mContentType; bool mCorrectlyInitialized; - - bool mHasParameterInSD; }; } diff --git a/src/com/HTTP/httpparser.cpp b/src/com/HTTP/httpparser.cpp index d56c086bf..9fe6d3d6e 100644 --- a/src/com/HTTP/httpparser.cpp +++ b/src/com/HTTP/httpparser.cpp @@ -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) { @@ -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; @@ -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;