-
-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
189 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
find_package(CURL REQUIRED) | ||
|
||
add_library(jsonrpccpp-connector-curl curlclient.h curlclient.cpp) | ||
target_link_libraries(jsonrpccpp-connector-curl ${CURL_LIBRARIES}) | ||
|
||
set_target_properties(jsonrpccpp-connector-curl PROPERTIES | ||
PUBLIC_HEADER "curlclient.h" | ||
SOVERSION ${SO_VERSION} | ||
VERSION ${VERSION_STRING} | ||
) | ||
|
||
install(TARGETS jsonrpccpp-connector-curl | ||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} | ||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} | ||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} | ||
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/jsonrpccpp/connector/curl | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
|
||
#include "curlclient.h" | ||
#include <curl/curl.h> | ||
#include <sstream> | ||
#include <string> | ||
#include "../../exception.h" | ||
|
||
using namespace jsonrpc; | ||
using namespace std; | ||
|
||
// Rationale, see here: http://curl.haxx.se/libcurl/c/curl_global_init.html | ||
class curl_initializer { | ||
public: | ||
curl_initializer() { curl_global_init(CURL_GLOBAL_ALL); } | ||
~curl_initializer() { curl_global_cleanup(); } | ||
}; | ||
static curl_initializer _curl_init = curl_initializer(); | ||
|
||
CurlClient::CurlClient(const string &url, long timeout, bool verifyTLS) | ||
: url(url), timeout(timeout), verifyTLS(verifyTLS) { | ||
this->AddHeader("Content-Type", "application/json"); | ||
this->AddHeader("charsets", "utf-8"); | ||
} | ||
|
||
size_t WriteCallback(char *contents, size_t size, size_t nmemb, void *userp) { | ||
((std::string *)userp)->append((char *)contents, size * nmemb); | ||
return size * nmemb; | ||
} | ||
|
||
string CurlClient::SendRPCMessage(const string &message) { | ||
CURL *curl = curl_easy_init(); | ||
string response = ""; | ||
|
||
struct curl_slist *headers = NULL; | ||
for (auto header : this->headers) { | ||
headers = curl_slist_append(headers, (header.first + ": " + header.second).c_str()); | ||
} | ||
|
||
curl_easy_setopt(curl, CURLOPT_URL, this->url.c_str()); | ||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); | ||
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout); | ||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, message.c_str()); | ||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); | ||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); | ||
|
||
if (!this->verifyTLS) { | ||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); | ||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); | ||
} | ||
|
||
CURLcode res = curl_easy_perform(curl); | ||
long http_code = 0; | ||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); | ||
|
||
curl_slist_free_all(headers); | ||
curl_easy_cleanup(curl); | ||
|
||
if (res == 7) { | ||
throw JsonRpcException(ExceptionCode::ERROR_CLIENT_CONNECTOR, "Could not connect to " + this->url); | ||
} else if (res == 28) { | ||
throw JsonRpcException(ExceptionCode::ERROR_CLIENT_CONNECTOR, "Operation timed out " + this->url); | ||
} else if (res != CURLE_OK) { | ||
stringstream message; | ||
message << "Unkown libcurl error " << res << ": " << curl_easy_strerror(res); | ||
throw JsonRpcException(ExceptionCode::ERROR_CLIENT_CONNECTOR, message.str()); | ||
} else if (http_code != 200) { | ||
stringstream message; | ||
message << "Received HTTP status code " << http_code << ": " << response; | ||
throw JsonRpcException(ExceptionCode::ERROR_CLIENT_CONNECTOR, message.str()); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
void CurlClient::AddHeader(const std::string &attr, const std::string &val) { this->headers[attr] = val; } | ||
|
||
void CurlClient::RemoveHeader(const std::string &attr) { this->headers.erase(attr); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
|
||
#include <map> | ||
#include "../iclientconnector.h" | ||
|
||
namespace jsonrpc { | ||
class CurlClient : public IClientConnector { | ||
public: | ||
/** | ||
* @param timeout - timeout in milliseconds, if zero, no timeout is set | ||
*/ | ||
CurlClient(const std::string &url, long timeout = 0, bool verifyTLS = true); | ||
virtual std::string SendRPCMessage(const std::string &message); | ||
|
||
void setTLSVerification(bool enabled); | ||
|
||
void AddHeader(const std::string &attr, const std::string &val); | ||
void RemoveHeader(const std::string &attr); | ||
|
||
private: | ||
std::map<std::string, std::string> headers; | ||
std::string url; | ||
long timeout; | ||
bool verifyTLS; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
namespace jsonrpc { | ||
class IClientConnector { | ||
public: | ||
virtual ~IClientConnector() {} | ||
virtual std::string SendRPCMessage(const std::string& message) = 0; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include <exception> | ||
#include <sstream> | ||
#include <string> | ||
|
||
namespace jsonrpc { | ||
enum ExceptionCode { ERROR_CLIENT_CONNECTOR = -32003 }; | ||
|
||
class JsonRpcException : public std::exception { | ||
public: | ||
JsonRpcException(int code, const std::string& message) : code(code) { | ||
std::stringstream str; | ||
str << "JsonRpcException " << code << ": " << message; | ||
this->message = str.str(); | ||
} | ||
|
||
virtual const char* what() const throw() { return message.c_str(); } | ||
|
||
private: | ||
int code; | ||
std::string message; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters