Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions libs/crossplatform/include/CrossPlatformUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ class CrossPlatformUtils
* @return The umask value as a std::filesystem::perms type
*/
static std::filesystem::perms GetCurrentUmask();

/**
* @brief Get CRLF line endings regardless of platform
* @return <CR><LF>
*/
static std::string Crlf();
};

#endif /* CROSSPLATFORM_UTILS_H */
6 changes: 6 additions & 0 deletions libs/crossplatform/src/CrossPlatformUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,10 @@ const std::pair<std::string, int> CrossPlatformUtils::ExecCommand(const std::str
return std::make_pair(result, ret_code);
}

std::string CrossPlatformUtils::Crlf()
{
static const std::string crlf = CRLF;
return crlf;
}

// end of CrossplatformUtils.cpp
1 change: 1 addition & 0 deletions libs/crossplatform/src/Darwin/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ constexpr const char* USER_PROFILE = "HOME";
constexpr const char* PACK_ROOT_DIR = "/arm/packs";
constexpr const char* CACHE_DIR = "/.cache";
constexpr const char* HOST_TYPE = "mac";
constexpr const char* CRLF = "\r\n";

#endif // CROSSPLATFORM_CONSTANTS_H
1 change: 1 addition & 0 deletions libs/crossplatform/src/Emscripten/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ constexpr const char* USER_PROFILE = "";
constexpr const char* PACK_ROOT_DIR = "";
constexpr const char* CACHE_DIR = "";
constexpr const char* HOST_TYPE = "";
constexpr const char* CRLF = "";

#endif // CROSSPLATFORM_CONSTANTS_H
1 change: 1 addition & 0 deletions libs/crossplatform/src/Linux/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ constexpr const char* PACK_ROOT_DIR = "/arm/packs";
constexpr const char* PROC_SELF_EXE = "/proc/self/exe";
constexpr const char* CACHE_DIR = "/.cache";
constexpr const char* HOST_TYPE = "linux";
constexpr const char* CRLF = "\r\n";

#endif // CROSSPLATFORM_CONSTANTS_H
1 change: 1 addition & 0 deletions libs/crossplatform/src/Windows/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ constexpr const char* USER_PROFILE = "USERPROFILE";
constexpr const char* PACK_ROOT_DIR = "\\Arm\\Packs";
constexpr const char* CACHE_DIR = "\\AppData\\Local";
constexpr const char* HOST_TYPE = "win";
constexpr const char* CRLF = "\n";

#endif // CROSSPLATFORM_CONSTANTS_H
14 changes: 12 additions & 2 deletions tools/projmgr/include/ProjMgrRpcServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,23 @@ class ProjMgrRpcServer {
*/
void SetContentLengthHeader(bool value) { m_contextLength = value; }

/**
* @brief get request from stdin with content length header
* @return string request
*/
const std::string GetRequestFromStdinWithLength(void);

/**
* @brief get request from stdin
* @param string request
*/
const std::string GetRequestFromStdin(void);

protected:
ProjMgr& m_manager;
bool m_debug = false;
bool m_shutdown = false;
bool m_contextLength = false;
const std::string GetRequestFromStdinWithLength(void);
const std::string GetRequestFromStdin(void);
};

#endif // PROJMGRRPCSERVER_H
11 changes: 9 additions & 2 deletions tools/projmgr/src/ProjMgrRpcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "ProjMgrLogger.h"
#include "ProjMgr.h"
#include "ProductInfo.h"

#include "CrossPlatformUtils.h"
#include <RteFsUtils.h>

#include <fstream>
Expand All @@ -31,10 +33,13 @@ const string ProjMgrRpcServer::GetRequestFromStdinWithLength(void) {
string line;
int contentLength = 0;
const string& header = CONTENT_LENGTH_HEADER;
while (getline(cin, line) && !line.empty() && !cin.fail()) {
while (getline(cin, line) && !cin.fail()) {
if (line.find(header) == 0) {
contentLength = RteUtils::StringToInt(line.substr(header.length()), 0);
}
if (line.empty() || line.front() == '\r' || line.front() == '\n') {
break;
}
}
string request(contentLength, '\0');
cin.read(&request[0], contentLength);
Expand Down Expand Up @@ -138,7 +143,9 @@ bool ProjMgrRpcServer::Run(void) {

// Send response
if (m_contextLength) {
cout << CONTENT_LENGTH_HEADER << response.size() << std::endl << std::endl << response << std::flush;
// compliant to https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#baseProtocol
cout << CONTENT_LENGTH_HEADER << response.size() <<
CrossPlatformUtils::Crlf() << CrossPlatformUtils::Crlf() << response << std::flush;
} else {
cout << response << std::endl;
}
Expand Down
32 changes: 32 additions & 0 deletions tools/projmgr/test/src/ProjMgrRpcTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "ProjMgrRpcServerData.h"
#include "ProjMgrLogger.h"

#include "CrossPlatformUtils.h"
#include "ProductInfo.h"

using namespace std;
Expand All @@ -20,6 +21,7 @@ class ProjMgrRpcTests : public ProjMgr, public ::testing::Test {
virtual ~ProjMgrRpcTests() {}
string FormatRequest(const int id, const string& method, const json& params);
vector<json> RunRpcMethods(const string& strIn);
string RunRpcMethodsWithContent(const string& strIn);
};

string ProjMgrRpcTests::FormatRequest(const int id, const string& method, const json& params = json()) {
Expand Down Expand Up @@ -47,6 +49,30 @@ vector<json> ProjMgrRpcTests::RunRpcMethods(const string& strIn) {
return responses;
}

string ProjMgrRpcTests::RunRpcMethodsWithContent(const string& strIn) {
StdStreamRedirect streamRedirect;
streamRedirect.SetInString(strIn);
char* argv[] = { (char*)"csolution", (char*)"rpc", (char*)"--content-length" };
EXPECT_EQ(0, RunProjMgr(3, argv, 0));
return streamRedirect.GetOutString();
}

TEST_F(ProjMgrRpcTests, ContentLength) {
StdStreamRedirect streamRedirect;
ProjMgrRpcServer server(*this);
const auto& request = FormatRequest(1, "GetVersion");

auto requestWithHeader = "Content-Length:46\n\n" + request;
streamRedirect.SetInString(requestWithHeader);
auto parsedRequest = server.GetRequestFromStdinWithLength();
EXPECT_EQ(request, parsedRequest);

requestWithHeader = "Content-Length:46\r\n\r\n" + request;
streamRedirect.SetInString(requestWithHeader);
parsedRequest = server.GetRequestFromStdinWithLength();
EXPECT_EQ(request, parsedRequest);
}

TEST_F(ProjMgrRpcTests, RpcGetVersion) {
const auto& requests = FormatRequest(1, "GetVersion");
const auto& responses = RunRpcMethods(requests);
Expand All @@ -55,6 +81,12 @@ TEST_F(ProjMgrRpcTests, RpcGetVersion) {
EXPECT_EQ(string(VERSION_STRING), responses[0]["result"]);
}

TEST_F(ProjMgrRpcTests, RpcGetVersionWithContent) {
const auto& requests = "Content-Length:46\n\n" + FormatRequest(1, "GetVersion");
const auto& responses = RunRpcMethodsWithContent(requests);
EXPECT_TRUE(responses.find(CrossPlatformUtils::Crlf() + CrossPlatformUtils::Crlf() + "{") != string::npos);
}

TEST_F(ProjMgrRpcTests, RpcLoadSolution) {
const string& csolution = testinput_folder + "/TestRpc/minimal.csolution.yml";
const auto& requests =
Expand Down
Loading