From f013753b122d43e364f6d6039a38e5c54e65306e Mon Sep 17 00:00:00 2001 From: Daniel Kraft Date: Tue, 5 Mar 2019 08:22:48 +0100 Subject: [PATCH 01/17] Allow setting bind address for HTTP server. This modifies the HTTP server connector so that the bind address can be specified if desired (e.g. to listen only locally). See https://github.com/cinemast/libjson-rpc-cpp/issues/200. --- CHANGELOG.md | 7 +++ .../server/connectors/httpserver.cpp | 37 +++++++++++++- src/jsonrpccpp/server/connectors/httpserver.h | 15 ++++++ src/test/test_connector_http.cpp | 51 +++++++++++++++++-- 4 files changed, 104 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e77a3589..ffd45e0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## Unreleased + +### Added +- The `HttpServer` connector now has a `SetBindAddress` method, which can + be used to bind to a specific address rather than listen on all + interfaces of the machine (#259) + ## [v1.1.1] - 2018-10-31 ### Fixed diff --git a/src/jsonrpccpp/server/connectors/httpserver.cpp b/src/jsonrpccpp/server/connectors/httpserver.cpp index 5a4471e9..cc5cb31d 100644 --- a/src/jsonrpccpp/server/connectors/httpserver.cpp +++ b/src/jsonrpccpp/server/connectors/httpserver.cpp @@ -9,6 +9,7 @@ #include "httpserver.h" #include +#include #include #include #include @@ -31,6 +32,31 @@ HttpServer::HttpServer(int port, const std::string &sslcert, : AbstractServerConnector(), port(port), threads(threads), running(false), path_sslcert(sslcert), path_sslkey(sslkey), daemon(NULL) {} +HttpServer::~HttpServer() { + if (bind_address != nullptr) + freeaddrinfo(bind_address); +} + +bool HttpServer::SetBindAddress(const std::string& addr) { + if (bind_address != nullptr) { + freeaddrinfo(bind_address); + bind_address = nullptr; + } + + std::ostringstream port_str; + port_str << port; + + const char* addr_cstr = nullptr; + if (!addr.empty()) + addr_cstr = addr.c_str(); + + struct addrinfo hints; + std::memset(&hints, 0, sizeof(hints)); + hints.ai_flags = AI_PASSIVE; + + return getaddrinfo(addr_cstr, port_str.str().c_str(), &hints, &bind_address) == 0; +} + IClientConnectionHandler *HttpServer::GetHandler(const std::string &url) { if (AbstractServerConnector::GetHandler() != NULL) return AbstractServerConnector::GetHandler(); @@ -48,6 +74,12 @@ bool HttpServer::StartListening() { const bool has_poll = (MHD_is_feature_supported(MHD_FEATURE_POLL) == MHD_YES); unsigned int mhd_flags; + + // If no bind address has been set explicitly, specify one that listens + // on all interfaces now. + if (bind_address == nullptr && !SetBindAddress("")) + return false; + if (has_epoll) // In MHD version 0.9.44 the flag is renamed to // MHD_USE_EPOLL_INTERNALLY_LINUX_ONLY. In later versions both @@ -69,6 +101,7 @@ bool HttpServer::StartListening() { HttpServer::callback, this, MHD_OPTION_HTTPS_MEM_KEY, this->sslkey.c_str(), MHD_OPTION_HTTPS_MEM_CERT, this->sslcert.c_str(), MHD_OPTION_THREAD_POOL_SIZE, this->threads, + MHD_OPTION_SOCK_ADDR, bind_address->ai_addr, MHD_OPTION_END); } catch (JsonRpcException &ex) { return false; @@ -76,7 +109,9 @@ bool HttpServer::StartListening() { } else { this->daemon = MHD_start_daemon( mhd_flags, this->port, NULL, NULL, HttpServer::callback, this, - MHD_OPTION_THREAD_POOL_SIZE, this->threads, MHD_OPTION_END); + MHD_OPTION_THREAD_POOL_SIZE, this->threads, + MHD_OPTION_SOCK_ADDR, bind_address->ai_addr, + MHD_OPTION_END); } if (this->daemon != NULL) this->running = true; diff --git a/src/jsonrpccpp/server/connectors/httpserver.h b/src/jsonrpccpp/server/connectors/httpserver.h index 895cb957..cb55a3cb 100644 --- a/src/jsonrpccpp/server/connectors/httpserver.h +++ b/src/jsonrpccpp/server/connectors/httpserver.h @@ -15,11 +15,13 @@ #include #if defined(_WIN32) && !defined(__CYGWIN__) #include +#include #if defined(_MSC_FULL_VER) && !defined (_SSIZE_T_DEFINED) #define _SSIZE_T_DEFINED typedef intptr_t ssize_t; #endif // !_SSIZE_T_DEFINED */ #else +#include #include #include #include @@ -49,6 +51,18 @@ namespace jsonrpc */ HttpServer(int port, const std::string& sslcert = "", const std::string& sslkey = "", int threads = 50); + ~HttpServer(); + + /** + * Sets the address at which the server will bind when started. + * By default, the server listens at all interfaces. If this + * method is called with a non-empty string before starting the + * server, then it will only bind to the specified address. + * This method returns true on success and false if the given + * address could not be set as bind address. + */ + bool SetBindAddress(const std::string& addr); + virtual bool StartListening(); virtual bool StopListening(); @@ -62,6 +76,7 @@ namespace jsonrpc int port; int threads; bool running; + struct addrinfo* bind_address = nullptr; std::string path_sslcert; std::string path_sslkey; std::string sslcert; diff --git a/src/test/test_connector_http.cpp b/src/test/test_connector_http.cpp index 54083c55..abc83842 100644 --- a/src/test/test_connector_http.cpp +++ b/src/test/test_connector_http.cpp @@ -21,21 +21,33 @@ using namespace jsonrpc; using namespace std; #define TEST_PORT 8383 -#define CLIENT_URL "http://localhost:8383" +#define CLIENT_URL "http://127.0.0.1:8383" + +/* When the server is listening on all interfaces, then also this alternate + URL should be fine to connect to it. When it is bound specifically to + 127.0.0.1, though, then this should not work. */ +#define ALTERNATE_CLIENT_URL "http://127.0.0.2:8383" #define TEST_MODULE "[connector_http]" namespace testhttpserver { -struct F { +struct ServerFixture { HttpServer server; - HttpClient client; MockClientConnectionHandler handler; - F() : server(TEST_PORT), client(CLIENT_URL) { + ServerFixture() : server(TEST_PORT) { server.SetHandler(&handler); + } + + ~ServerFixture() { server.StopListening(); } +}; + +struct F : public ServerFixture { + HttpClient client; + + F() : client(CLIENT_URL) { server.StartListening(); } - ~F() { server.StopListening(); } }; bool check_exception1(JsonRpcException const &ex) { @@ -64,6 +76,35 @@ TEST_CASE("test_http_client_error", TEST_MODULE) { JsonRpcException, check_exception1); } +TEST_CASE_METHOD(ServerFixture, "test_http_client_bind_globally", TEST_MODULE) { + CHECK(server.StartListening()); + + HttpClient client(ALTERNATE_CLIENT_URL); + handler.response = "exampleresponse"; + std::string result; + client.SendRPCMessage("examplerequest", result); + + CHECK(handler.request == "examplerequest"); + CHECK(result == "exampleresponse"); +} + +TEST_CASE_METHOD(ServerFixture, "test_http_client_bind_address", TEST_MODULE) { + server.SetBindAddress("127.0.0.1"); + CHECK(server.StartListening()); + + HttpClient client1(CLIENT_URL); + handler.response = "exampleresponse"; + std::string result; + client1.SendRPCMessage("examplerequest", result); + + CHECK(handler.request == "examplerequest"); + CHECK(result == "exampleresponse"); + + HttpClient client2(ALTERNATE_CLIENT_URL); + CHECK_EXCEPTION_TYPE(client2.SendRPCMessage("examplerequest", result), + JsonRpcException, check_exception1); +} + #ifndef WIN32 TEST_CASE("test_http_server_multiplestart", TEST_MODULE) { HttpServer server(TEST_PORT); From 5ee24f520fd4a055acae4e9a3d243bbb995759e8 Mon Sep 17 00:00:00 2001 From: Peter Spiess-Knafl Date: Fri, 15 Mar 2019 17:24:20 +0100 Subject: [PATCH 02/17] Fix ArchLinux base image --- docker/ArchLinux.Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/ArchLinux.Dockerfile b/docker/ArchLinux.Dockerfile index d802a5a0..93495528 100644 --- a/docker/ArchLinux.Dockerfile +++ b/docker/ArchLinux.Dockerfile @@ -1,4 +1,4 @@ -FROM base/archlinux:latest +FROM archlinux/base:latest MAINTAINER Peter Spiess-Knafl ENV OS=arch RUN pacman -Sy --noconfirm \ From 0c41c35c880ca8d2ef677bbc068e87fc22384735 Mon Sep 17 00:00:00 2001 From: Peter Spiess-Knafl Date: Fri, 15 Mar 2019 17:36:43 +0100 Subject: [PATCH 03/17] Remove brew ssl --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 5e503f63..767aafc2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ before_install: then brew update brew install jsoncpp argtable curl hiredis redis - brew install libmicrohttpd --with-ssl + brew install libmicrohttpd fi matrix: From 331078b2cd413fddf545b23c364e7a494cb17486 Mon Sep 17 00:00:00 2001 From: Peter Spiess-Knafl Date: Mon, 18 Mar 2019 21:34:32 +0100 Subject: [PATCH 04/17] Fix osx build --- .../server/connectors/httpserver.cpp | 42 ++---- src/jsonrpccpp/server/connectors/httpserver.h | 132 +++++++++--------- src/test/test_connector_http.cpp | 14 +- 3 files changed, 85 insertions(+), 103 deletions(-) diff --git a/src/jsonrpccpp/server/connectors/httpserver.cpp b/src/jsonrpccpp/server/connectors/httpserver.cpp index cc5cb31d..e2622bcb 100644 --- a/src/jsonrpccpp/server/connectors/httpserver.cpp +++ b/src/jsonrpccpp/server/connectors/httpserver.cpp @@ -32,29 +32,18 @@ HttpServer::HttpServer(int port, const std::string &sslcert, : AbstractServerConnector(), port(port), threads(threads), running(false), path_sslcert(sslcert), path_sslkey(sslkey), daemon(NULL) {} -HttpServer::~HttpServer() { - if (bind_address != nullptr) - freeaddrinfo(bind_address); -} - -bool HttpServer::SetBindAddress(const std::string& addr) { - if (bind_address != nullptr) { - freeaddrinfo(bind_address); - bind_address = nullptr; - } +HttpServer::~HttpServer() {} - std::ostringstream port_str; - port_str << port; +bool HttpServer::SetBindAddress(const std::string &addr) { - const char* addr_cstr = nullptr; - if (!addr.empty()) - addr_cstr = addr.c_str(); + bind_address.sin_family = AF_INET; + bind_address.sin_port = htons(this->port); - struct addrinfo hints; - std::memset(&hints, 0, sizeof(hints)); - hints.ai_flags = AI_PASSIVE; - - return getaddrinfo(addr_cstr, port_str.str().c_str(), &hints, &bind_address) == 0; + if (addr == "") + bind_address.sin_addr.s_addr = htonl(INADDR_ANY); + else + inet_aton("127.0.0.1", (struct in_addr *)&bind_address.sin_addr.s_addr); + return true; } IClientConnectionHandler *HttpServer::GetHandler(const std::string &url) { @@ -75,10 +64,7 @@ bool HttpServer::StartListening() { (MHD_is_feature_supported(MHD_FEATURE_POLL) == MHD_YES); unsigned int mhd_flags; - // If no bind address has been set explicitly, specify one that listens - // on all interfaces now. - if (bind_address == nullptr && !SetBindAddress("")) - return false; + SetBindAddress(""); if (has_epoll) // In MHD version 0.9.44 the flag is renamed to @@ -101,17 +87,15 @@ bool HttpServer::StartListening() { HttpServer::callback, this, MHD_OPTION_HTTPS_MEM_KEY, this->sslkey.c_str(), MHD_OPTION_HTTPS_MEM_CERT, this->sslcert.c_str(), MHD_OPTION_THREAD_POOL_SIZE, this->threads, - MHD_OPTION_SOCK_ADDR, bind_address->ai_addr, - MHD_OPTION_END); + MHD_OPTION_SOCK_ADDR, &bind_address, MHD_OPTION_END); } catch (JsonRpcException &ex) { return false; } } else { this->daemon = MHD_start_daemon( mhd_flags, this->port, NULL, NULL, HttpServer::callback, this, - MHD_OPTION_THREAD_POOL_SIZE, this->threads, - MHD_OPTION_SOCK_ADDR, bind_address->ai_addr, - MHD_OPTION_END); + MHD_OPTION_THREAD_POOL_SIZE, this->threads, MHD_OPTION_SOCK_ADDR, + &bind_address, MHD_OPTION_END); } if (this->daemon != NULL) this->running = true; diff --git a/src/jsonrpccpp/server/connectors/httpserver.h b/src/jsonrpccpp/server/connectors/httpserver.h index cb55a3cb..2fb96702 100644 --- a/src/jsonrpccpp/server/connectors/httpserver.h +++ b/src/jsonrpccpp/server/connectors/httpserver.h @@ -14,83 +14,85 @@ #include #include #if defined(_WIN32) && !defined(__CYGWIN__) -#include #include -#if defined(_MSC_FULL_VER) && !defined (_SSIZE_T_DEFINED) +#include +#if defined(_MSC_FULL_VER) && !defined(_SSIZE_T_DEFINED) #define _SSIZE_T_DEFINED typedef intptr_t ssize_t; #endif // !_SSIZE_T_DEFINED */ #else +#include #include -#include -#include #include +#include +#include #endif +#include "../abstractserverconnector.h" #include #include -#include "../abstractserverconnector.h" - -namespace jsonrpc -{ - /** - * This class provides an embedded HTTP Server, based on libmicrohttpd, to handle incoming Requests and send HTTP 1.1 - * valid responses. - * Note that this class will always send HTTP-Status 200, even though an JSON-RPC Error might have occurred. Please - * always check for the JSON-RPC Error Header. - */ - class HttpServer: public AbstractServerConnector - { - public: - - /** - * @brief HttpServer, constructor for the included HttpServer - * @param port on which the server is listening - * @param enableSpecification - defines if the specification is returned in case of a GET request - * @param sslcert - defines the path to a SSL certificate, if this path is != "", then SSL/HTTPS is used with the given certificate. - */ - HttpServer(int port, const std::string& sslcert = "", const std::string& sslkey = "", int threads = 50); - - ~HttpServer(); - - /** - * Sets the address at which the server will bind when started. - * By default, the server listens at all interfaces. If this - * method is called with a non-empty string before starting the - * server, then it will only bind to the specified address. - * This method returns true on success and false if the given - * address could not be set as bind address. - */ - bool SetBindAddress(const std::string& addr); - - virtual bool StartListening(); - virtual bool StopListening(); - - bool virtual SendResponse(const std::string& response, - void* addInfo = NULL); - bool virtual SendOptionsResponse(void* addInfo); - - void SetUrlHandler(const std::string &url, IClientConnectionHandler *handler); - - private: - int port; - int threads; - bool running; - struct addrinfo* bind_address = nullptr; - std::string path_sslcert; - std::string path_sslkey; - std::string sslcert; - std::string sslkey; - - struct MHD_Daemon *daemon; - - std::map urlhandler; - - static int callback(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls); - - IClientConnectionHandler* GetHandler(const std::string &url); - }; +namespace jsonrpc { +/** + * This class provides an embedded HTTP Server, based on libmicrohttpd, to + * handle incoming Requests and send HTTP 1.1 valid responses. Note that this + * class will always send HTTP-Status 200, even though an JSON-RPC Error might + * have occurred. Please always check for the JSON-RPC Error Header. + */ +class HttpServer : public AbstractServerConnector { +public: + /** + * @brief HttpServer, constructor for the included HttpServer + * @param port on which the server is listening + * @param enableSpecification - defines if the specification is returned in + * case of a GET request + * @param sslcert - defines the path to a SSL certificate, if this path is != + * "", then SSL/HTTPS is used with the given certificate. + */ + HttpServer(int port, const std::string &sslcert = "", + const std::string &sslkey = "", int threads = 50); + + ~HttpServer(); + + /** + * Sets the address at which the server will bind when started. + * By default, the server listens at all interfaces. If this + * method is called with a non-empty string before starting the + * server, then it will only bind to the specified address. + * This method returns true on success and false if the given + * address could not be set as bind address. + */ + bool SetBindAddress(const std::string &addr); + + virtual bool StartListening(); + virtual bool StopListening(); + + bool virtual SendResponse(const std::string &response, void *addInfo = NULL); + bool virtual SendOptionsResponse(void *addInfo); + + void SetUrlHandler(const std::string &url, IClientConnectionHandler *handler); + +private: + int port; + int threads; + bool running; + struct sockaddr_in bind_address; + std::string path_sslcert; + std::string path_sslkey; + std::string sslcert; + std::string sslkey; + + struct MHD_Daemon *daemon; + + std::map urlhandler; + + static int callback(void *cls, struct MHD_Connection *connection, + const char *url, const char *method, const char *version, + const char *upload_data, size_t *upload_data_size, + void **con_cls); + + IClientConnectionHandler *GetHandler(const std::string &url); +}; } /* namespace jsonrpc */ #endif /* JSONRPC_CPP_HTTPSERVERCONNECTOR_H_ */ diff --git a/src/test/test_connector_http.cpp b/src/test/test_connector_http.cpp index abc83842..f474c95f 100644 --- a/src/test/test_connector_http.cpp +++ b/src/test/test_connector_http.cpp @@ -35,9 +35,7 @@ struct ServerFixture { HttpServer server; MockClientConnectionHandler handler; - ServerFixture() : server(TEST_PORT) { - server.SetHandler(&handler); - } + ServerFixture() : server(TEST_PORT) { server.SetHandler(&handler); } ~ServerFixture() { server.StopListening(); } }; @@ -45,9 +43,7 @@ struct ServerFixture { struct F : public ServerFixture { HttpClient client; - F() : client(CLIENT_URL) { - server.StartListening(); - } + F() : client(CLIENT_URL) { server.StartListening(); } }; bool check_exception1(JsonRpcException const &ex) { @@ -77,9 +73,9 @@ TEST_CASE("test_http_client_error", TEST_MODULE) { } TEST_CASE_METHOD(ServerFixture, "test_http_client_bind_globally", TEST_MODULE) { - CHECK(server.StartListening()); + REQUIRE(server.StartListening()); - HttpClient client(ALTERNATE_CLIENT_URL); + HttpClient client(CLIENT_URL); handler.response = "exampleresponse"; std::string result; client.SendRPCMessage("examplerequest", result); @@ -90,7 +86,7 @@ TEST_CASE_METHOD(ServerFixture, "test_http_client_bind_globally", TEST_MODULE) { TEST_CASE_METHOD(ServerFixture, "test_http_client_bind_address", TEST_MODULE) { server.SetBindAddress("127.0.0.1"); - CHECK(server.StartListening()); + REQUIRE(server.StartListening()); HttpClient client1(CLIENT_URL); handler.response = "exampleresponse"; From 786d6cd7d6bac62da27298df7450ba3da86e849c Mon Sep 17 00:00:00 2001 From: Peter Spiess-Knafl Date: Mon, 18 Mar 2019 21:35:50 +0100 Subject: [PATCH 05/17] Fix changelog --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffd45e0f..9fbbc172 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -87,7 +87,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Invalid pointer handling in HTTP-Server ### Added -- HttpServer can now be configured to listen localhost only - TCP Server + Client connectors ## Changed From 6a1a52d90342e5c57e2c8fc5c13a0f1042e26c9d Mon Sep 17 00:00:00 2001 From: Peter Spiess-Knafl Date: Mon, 18 Mar 2019 21:44:06 +0100 Subject: [PATCH 06/17] Fix bind addr string --- src/jsonrpccpp/server/connectors/httpserver.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jsonrpccpp/server/connectors/httpserver.cpp b/src/jsonrpccpp/server/connectors/httpserver.cpp index e2622bcb..d56e7445 100644 --- a/src/jsonrpccpp/server/connectors/httpserver.cpp +++ b/src/jsonrpccpp/server/connectors/httpserver.cpp @@ -42,7 +42,7 @@ bool HttpServer::SetBindAddress(const std::string &addr) { if (addr == "") bind_address.sin_addr.s_addr = htonl(INADDR_ANY); else - inet_aton("127.0.0.1", (struct in_addr *)&bind_address.sin_addr.s_addr); + inet_aton(addr.c_str(), (struct in_addr *)&bind_address.sin_addr.s_addr); return true; } From 96f4e82851b9002134705bc9007900ba966260e5 Mon Sep 17 00:00:00 2001 From: Peter Spiess-Knafl Date: Mon, 18 Mar 2019 22:03:41 +0100 Subject: [PATCH 07/17] Revert bindaddress --- .../server/connectors/httpserver.cpp | 19 ++----------------- src/jsonrpccpp/server/connectors/httpserver.h | 1 - src/test/test_connector_http.cpp | 3 ++- src/test/test_connector_tcpsocket.cpp | 2 +- 4 files changed, 5 insertions(+), 20 deletions(-) diff --git a/src/jsonrpccpp/server/connectors/httpserver.cpp b/src/jsonrpccpp/server/connectors/httpserver.cpp index d56e7445..537bdac0 100644 --- a/src/jsonrpccpp/server/connectors/httpserver.cpp +++ b/src/jsonrpccpp/server/connectors/httpserver.cpp @@ -34,18 +34,6 @@ HttpServer::HttpServer(int port, const std::string &sslcert, HttpServer::~HttpServer() {} -bool HttpServer::SetBindAddress(const std::string &addr) { - - bind_address.sin_family = AF_INET; - bind_address.sin_port = htons(this->port); - - if (addr == "") - bind_address.sin_addr.s_addr = htonl(INADDR_ANY); - else - inet_aton(addr.c_str(), (struct in_addr *)&bind_address.sin_addr.s_addr); - return true; -} - IClientConnectionHandler *HttpServer::GetHandler(const std::string &url) { if (AbstractServerConnector::GetHandler() != NULL) return AbstractServerConnector::GetHandler(); @@ -64,8 +52,6 @@ bool HttpServer::StartListening() { (MHD_is_feature_supported(MHD_FEATURE_POLL) == MHD_YES); unsigned int mhd_flags; - SetBindAddress(""); - if (has_epoll) // In MHD version 0.9.44 the flag is renamed to // MHD_USE_EPOLL_INTERNALLY_LINUX_ONLY. In later versions both @@ -87,15 +73,14 @@ bool HttpServer::StartListening() { HttpServer::callback, this, MHD_OPTION_HTTPS_MEM_KEY, this->sslkey.c_str(), MHD_OPTION_HTTPS_MEM_CERT, this->sslcert.c_str(), MHD_OPTION_THREAD_POOL_SIZE, this->threads, - MHD_OPTION_SOCK_ADDR, &bind_address, MHD_OPTION_END); + MHD_OPTION_END); } catch (JsonRpcException &ex) { return false; } } else { this->daemon = MHD_start_daemon( mhd_flags, this->port, NULL, NULL, HttpServer::callback, this, - MHD_OPTION_THREAD_POOL_SIZE, this->threads, MHD_OPTION_SOCK_ADDR, - &bind_address, MHD_OPTION_END); + MHD_OPTION_THREAD_POOL_SIZE, this->threads, MHD_OPTION_END); } if (this->daemon != NULL) this->running = true; diff --git a/src/jsonrpccpp/server/connectors/httpserver.h b/src/jsonrpccpp/server/connectors/httpserver.h index 2fb96702..43e20cf3 100644 --- a/src/jsonrpccpp/server/connectors/httpserver.h +++ b/src/jsonrpccpp/server/connectors/httpserver.h @@ -76,7 +76,6 @@ class HttpServer : public AbstractServerConnector { int port; int threads; bool running; - struct sockaddr_in bind_address; std::string path_sslcert; std::string path_sslkey; std::string sslcert; diff --git a/src/test/test_connector_http.cpp b/src/test/test_connector_http.cpp index f474c95f..b85abdf5 100644 --- a/src/test/test_connector_http.cpp +++ b/src/test/test_connector_http.cpp @@ -84,6 +84,7 @@ TEST_CASE_METHOD(ServerFixture, "test_http_client_bind_globally", TEST_MODULE) { CHECK(result == "exampleresponse"); } +/* TEST_CASE_METHOD(ServerFixture, "test_http_client_bind_address", TEST_MODULE) { server.SetBindAddress("127.0.0.1"); REQUIRE(server.StartListening()); @@ -99,7 +100,7 @@ TEST_CASE_METHOD(ServerFixture, "test_http_client_bind_address", TEST_MODULE) { HttpClient client2(ALTERNATE_CLIENT_URL); CHECK_EXCEPTION_TYPE(client2.SendRPCMessage("examplerequest", result), JsonRpcException, check_exception1); -} +}*/ #ifndef WIN32 TEST_CASE("test_http_server_multiplestart", TEST_MODULE) { diff --git a/src/test/test_connector_tcpsocket.cpp b/src/test/test_connector_tcpsocket.cpp index adf624a1..57bfff44 100644 --- a/src/test/test_connector_tcpsocket.cpp +++ b/src/test/test_connector_tcpsocket.cpp @@ -25,7 +25,7 @@ using namespace std; #define TEST_MODULE "[connector_tcpsocket]" #define IP "127.0.0.1" -#define PORT 50000 +#define PORT 50012 namespace testtcpsocketserver { struct F { From 6cfa2a16bffd5b529dceec7b1cddc790974eecdd Mon Sep 17 00:00:00 2001 From: kaustubh Date: Wed, 27 Mar 2019 13:57:34 +0530 Subject: [PATCH 08/17] Add try-catch for json exception --- src/jsonrpccpp/server/abstractprotocolhandler.cpp | 12 +++++++++--- src/jsonrpccpp/server/rpcprotocolserver12.cpp | 13 ++++++++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/jsonrpccpp/server/abstractprotocolhandler.cpp b/src/jsonrpccpp/server/abstractprotocolhandler.cpp index 580090eb..258689f8 100644 --- a/src/jsonrpccpp/server/abstractprotocolhandler.cpp +++ b/src/jsonrpccpp/server/abstractprotocolhandler.cpp @@ -33,9 +33,15 @@ void AbstractProtocolHandler::HandleRequest(const std::string &request, Json::Value resp; Json::FastWriter w; - if (reader.parse(request, req, false)) { - this->HandleJsonRequest(req, resp); - } else { + try { + if (reader.parse(request, req, false)) { + this->HandleJsonRequest(req, resp); + } else { + this->WrapError( + Json::nullValue, Errors::ERROR_RPC_JSON_PARSE_ERROR, + Errors::GetErrorMessage(Errors::ERROR_RPC_JSON_PARSE_ERROR), resp); + } + } catch (const Json::Exception &e) { this->WrapError(Json::nullValue, Errors::ERROR_RPC_JSON_PARSE_ERROR, Errors::GetErrorMessage(Errors::ERROR_RPC_JSON_PARSE_ERROR), resp); diff --git a/src/jsonrpccpp/server/rpcprotocolserver12.cpp b/src/jsonrpccpp/server/rpcprotocolserver12.cpp index 4bf9e7a5..c8edaeac 100644 --- a/src/jsonrpccpp/server/rpcprotocolserver12.cpp +++ b/src/jsonrpccpp/server/rpcprotocolserver12.cpp @@ -27,13 +27,20 @@ void RpcProtocolServer12::HandleRequest(const std::string &request, Json::Value resp; Json::FastWriter w; - if (reader.parse(request, req, false)) { - this->GetHandler(req).HandleJsonRequest(req, resp); - } else { + try { + if (reader.parse(request, req, false)) { + this->GetHandler(req).HandleJsonRequest(req, resp); + } else { + this->GetHandler(req).WrapError( + Json::nullValue, Errors::ERROR_RPC_JSON_PARSE_ERROR, + Errors::GetErrorMessage(Errors::ERROR_RPC_JSON_PARSE_ERROR), resp); + } + } catch (const Json::Exception &e) { this->GetHandler(req).WrapError( Json::nullValue, Errors::ERROR_RPC_JSON_PARSE_ERROR, Errors::GetErrorMessage(Errors::ERROR_RPC_JSON_PARSE_ERROR), resp); } + if (resp != Json::nullValue) retValue = w.write(resp); } From 7ee6451d928d13af39728192a9598a0f1d9e4c1d Mon Sep 17 00:00:00 2001 From: Peter Spiess-Knafl Date: Wed, 27 Mar 2019 10:44:02 +0100 Subject: [PATCH 09/17] Fix static build of jsonrpcstub (Closes #263) --- src/stubgenerator/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stubgenerator/CMakeLists.txt b/src/stubgenerator/CMakeLists.txt index b4fb566e..155e892d 100644 --- a/src/stubgenerator/CMakeLists.txt +++ b/src/stubgenerator/CMakeLists.txt @@ -33,7 +33,7 @@ set_target_properties(${ALL_LIBS} PROPERTIES VERSION "${VERSION_STRING}" SOVERSI add_executable(jsonrpcstub main.cpp) if (BUILD_STATIC_LIBS AND NOT BUILD_SHARED_LIBS) - target_link_libraries(jsonrpcstub jsonrpccommonStatic libjsonrpcstubStatic ) + target_link_libraries(jsonrpcstub common libjsonrpcstubStatic ) else() target_link_libraries(jsonrpcstub jsonrpccommon libjsonrpcstub ) endif() From f7cd4a700e0759e76a05ec76661248369b405c14 Mon Sep 17 00:00:00 2001 From: Peter Spiess-Knafl Date: Fri, 29 Mar 2019 00:16:49 +0100 Subject: [PATCH 10/17] Catch jsoncpp exception when parsing --- src/jsonrpccpp/client/client.cpp | 10 +++++++--- src/jsonrpccpp/client/rpcprotocolclient.cpp | 11 ++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/jsonrpccpp/client/client.cpp b/src/jsonrpccpp/client/client.cpp index c698c0c9..fccd667a 100644 --- a/src/jsonrpccpp/client/client.cpp +++ b/src/jsonrpccpp/client/client.cpp @@ -34,10 +34,14 @@ void Client::CallProcedures(const BatchCall &calls, BatchResponse &result) { Json::Reader reader; Json::Value tmpresult; - if (!reader.parse(response, tmpresult) || !tmpresult.isArray()) { - throw JsonRpcException(Errors::ERROR_CLIENT_INVALID_RESPONSE, - "Array expected."); + try { + if (!reader.parse(response, tmpresult) || !tmpresult.isArray()) { + throw JsonRpcException(Errors::ERROR_CLIENT_INVALID_RESPONSE, "Array expected."); + } + } catch (const Json::Exception& e) { + throw JsonRpcException(Errors::ERROR_RPC_JSON_PARSE_ERROR, Errors::GetErrorMessage(Errors::ERROR_RPC_JSON_PARSE_ERROR), response); } + for (unsigned int i = 0; i < tmpresult.size(); i++) { if (tmpresult[i].isObject()) { diff --git a/src/jsonrpccpp/client/rpcprotocolclient.cpp b/src/jsonrpccpp/client/rpcprotocolclient.cpp index 6ba0740f..182d8f6a 100644 --- a/src/jsonrpccpp/client/rpcprotocolclient.cpp +++ b/src/jsonrpccpp/client/rpcprotocolclient.cpp @@ -42,9 +42,14 @@ void RpcProtocolClient::HandleResponse(const std::string &response, Json::Value &result) { Json::Reader reader; Json::Value value; - if (reader.parse(response, value)) { - this->HandleResponse(value, result); - } else { + + try { + if (reader.parse(response, value)) { + this->HandleResponse(value, result); + } else { + throw JsonRpcException(Errors::ERROR_RPC_JSON_PARSE_ERROR, " " + response); + } + } catch (Json::Exception& e) { throw JsonRpcException(Errors::ERROR_RPC_JSON_PARSE_ERROR, " " + response); } } From 4f24acaf4c6737cd07d40a02edad0a56147e0713 Mon Sep 17 00:00:00 2001 From: Peter Spiess-Knafl Date: Fri, 29 Mar 2019 00:38:33 +0100 Subject: [PATCH 11/17] Add method for binding to localhost --- .gitignore | 1 + .../server/connectors/httpserver.cpp | 20 +++++++++++++++++-- src/jsonrpccpp/server/connectors/httpserver.h | 14 ++++--------- src/test/test_connector_http.cpp | 18 +++++++++-------- 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index e83bf66e..3ff53bb4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ profile # xcode noise +build2/* build/* *.mode1 *.mode1v3 diff --git a/src/jsonrpccpp/server/connectors/httpserver.cpp b/src/jsonrpccpp/server/connectors/httpserver.cpp index 537bdac0..fa28759a 100644 --- a/src/jsonrpccpp/server/connectors/httpserver.cpp +++ b/src/jsonrpccpp/server/connectors/httpserver.cpp @@ -30,7 +30,7 @@ struct mhd_coninfo { HttpServer::HttpServer(int port, const std::string &sslcert, const std::string &sslkey, int threads) : AbstractServerConnector(), port(port), threads(threads), running(false), - path_sslcert(sslcert), path_sslkey(sslkey), daemon(NULL) {} + path_sslcert(sslcert), path_sslkey(sslkey), daemon(NULL), bindlocalhost(false) {} HttpServer::~HttpServer() {} @@ -44,6 +44,11 @@ IClientConnectionHandler *HttpServer::GetHandler(const std::string &url) { return NULL; } +HttpServer& HttpServer::BindLocalhost() { + this->bindlocalhost = true; + return *this; +} + bool HttpServer::StartListening() { if (!this->running) { const bool has_epoll = @@ -63,7 +68,18 @@ bool HttpServer::StartListening() { #endif else if (has_poll) mhd_flags = MHD_USE_POLL_INTERNALLY; - if (this->path_sslcert != "" && this->path_sslkey != "") { + + if (this->bindlocalhost) { + memset(&this->loopback_addr, 0, sizeof(this->loopback_addr)); + loopback_addr.sin_family = AF_INET; + loopback_addr.sin_port = htons(this->port); + loopback_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + + this->daemon = MHD_start_daemon( + mhd_flags, this->port, NULL, NULL, HttpServer::callback, this, + MHD_OPTION_THREAD_POOL_SIZE, this->threads, MHD_OPTION_SOCK_ADDR, (struct sockaddr *)(&(this->loopback_addr)), MHD_OPTION_END); + + } else if (this->path_sslcert != "" && this->path_sslkey != "") { try { SpecificationParser::GetFileContent(this->path_sslcert, this->sslcert); SpecificationParser::GetFileContent(this->path_sslkey, this->sslkey); diff --git a/src/jsonrpccpp/server/connectors/httpserver.h b/src/jsonrpccpp/server/connectors/httpserver.h index 43e20cf3..7785edbb 100644 --- a/src/jsonrpccpp/server/connectors/httpserver.h +++ b/src/jsonrpccpp/server/connectors/httpserver.h @@ -54,15 +54,8 @@ class HttpServer : public AbstractServerConnector { ~HttpServer(); - /** - * Sets the address at which the server will bind when started. - * By default, the server listens at all interfaces. If this - * method is called with a non-empty string before starting the - * server, then it will only bind to the specified address. - * This method returns true on success and false if the given - * address could not be set as bind address. - */ - bool SetBindAddress(const std::string &addr); + //Bind to localhost only, deactivates TLS settings + HttpServer& BindLocalhost(); virtual bool StartListening(); virtual bool StopListening(); @@ -82,8 +75,9 @@ class HttpServer : public AbstractServerConnector { std::string sslkey; struct MHD_Daemon *daemon; - + bool bindlocalhost; std::map urlhandler; + struct sockaddr_in loopback_addr; static int callback(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, diff --git a/src/test/test_connector_http.cpp b/src/test/test_connector_http.cpp index b85abdf5..340c0db3 100644 --- a/src/test/test_connector_http.cpp +++ b/src/test/test_connector_http.cpp @@ -35,7 +35,9 @@ struct ServerFixture { HttpServer server; MockClientConnectionHandler handler; - ServerFixture() : server(TEST_PORT) { server.SetHandler(&handler); } + ServerFixture() : server(TEST_PORT) { + server.SetHandler(&handler); + } ~ServerFixture() { server.StopListening(); } }; @@ -43,7 +45,9 @@ struct ServerFixture { struct F : public ServerFixture { HttpClient client; - F() : client(CLIENT_URL) { server.StartListening(); } + F() : client(CLIENT_URL) { + server.StartListening(); + } }; bool check_exception1(JsonRpcException const &ex) { @@ -73,9 +77,9 @@ TEST_CASE("test_http_client_error", TEST_MODULE) { } TEST_CASE_METHOD(ServerFixture, "test_http_client_bind_globally", TEST_MODULE) { - REQUIRE(server.StartListening()); + CHECK(server.StartListening()); - HttpClient client(CLIENT_URL); + HttpClient client(ALTERNATE_CLIENT_URL); handler.response = "exampleresponse"; std::string result; client.SendRPCMessage("examplerequest", result); @@ -84,10 +88,8 @@ TEST_CASE_METHOD(ServerFixture, "test_http_client_bind_globally", TEST_MODULE) { CHECK(result == "exampleresponse"); } -/* TEST_CASE_METHOD(ServerFixture, "test_http_client_bind_address", TEST_MODULE) { - server.SetBindAddress("127.0.0.1"); - REQUIRE(server.StartListening()); + CHECK(server.BindLocalhost().StartListening()); HttpClient client1(CLIENT_URL); handler.response = "exampleresponse"; @@ -100,7 +102,7 @@ TEST_CASE_METHOD(ServerFixture, "test_http_client_bind_address", TEST_MODULE) { HttpClient client2(ALTERNATE_CLIENT_URL); CHECK_EXCEPTION_TYPE(client2.SendRPCMessage("examplerequest", result), JsonRpcException, check_exception1); -}*/ +} #ifndef WIN32 TEST_CASE("test_http_server_multiplestart", TEST_MODULE) { From 35a66c0ebed41fd4c251f5279e62392f78b42e8e Mon Sep 17 00:00:00 2001 From: Peter Spiess-Knafl Date: Fri, 29 Mar 2019 00:56:15 +0100 Subject: [PATCH 12/17] Alternate client url not defined in OSX --- src/test/test_connector_http.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/test_connector_http.cpp b/src/test/test_connector_http.cpp index 340c0db3..e091f37c 100644 --- a/src/test/test_connector_http.cpp +++ b/src/test/test_connector_http.cpp @@ -76,6 +76,7 @@ TEST_CASE("test_http_client_error", TEST_MODULE) { JsonRpcException, check_exception1); } +#ifndef __APPLE__ TEST_CASE_METHOD(ServerFixture, "test_http_client_bind_globally", TEST_MODULE) { CHECK(server.StartListening()); @@ -87,6 +88,7 @@ TEST_CASE_METHOD(ServerFixture, "test_http_client_bind_globally", TEST_MODULE) { CHECK(handler.request == "examplerequest"); CHECK(result == "exampleresponse"); } +#endif TEST_CASE_METHOD(ServerFixture, "test_http_client_bind_address", TEST_MODULE) { CHECK(server.BindLocalhost().StartListening()); From d68d0ff1e3ac7331b71408d6cc160e6749c347a4 Mon Sep 17 00:00:00 2001 From: Peter Spiess-Knafl Date: Fri, 29 Mar 2019 01:10:32 +0100 Subject: [PATCH 13/17] Update catch and fix include path (Closes #251) --- src/catch/CMakeLists.txt | 4 ++-- src/test/main.cpp | 2 +- src/test/test_client.cpp | 2 +- src/test/test_common.cpp | 2 +- src/test/test_connector_filedescriptor.cpp | 2 +- src/test/test_connector_http.cpp | 2 +- src/test/test_connector_redis.cpp | 2 +- src/test/test_connector_tcpsocket.cpp | 2 +- src/test/test_connector_unixdomainsocket.cpp | 2 +- src/test/test_integration.cpp | 2 +- src/test/test_server.cpp | 2 +- src/test/test_stubgenerator.cpp | 2 +- 12 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/catch/CMakeLists.txt b/src/catch/CMakeLists.txt index 7cdc226a..5e928714 100644 --- a/src/catch/CMakeLists.txt +++ b/src/catch/CMakeLists.txt @@ -5,8 +5,8 @@ include(ExternalProject) ExternalProject_Add( catch PREFIX ${CMAKE_BINARY_DIR}/catch - URL https://github.com/catchorg/Catch2/archive/v2.0.1.tar.gz - URL_HASH SHA1=5c191a031edebd0525640ed2f38cbf64bacb1803 + URL https://github.com/catchorg/Catch2/archive/v2.7.0.tar.gz + URL_HASH SHA1=6df37d5b64a71b840a6a9d8c79c3705aa8a3f56e CONFIGURE_COMMAND "" BUILD_COMMAND "" INSTALL_COMMAND "" diff --git a/src/test/main.cpp b/src/test/main.cpp index f802d01a..e66d146b 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -8,4 +8,4 @@ ************************************************************************/ #define CATCH_CONFIG_MAIN -#include +#include diff --git a/src/test/test_client.cpp b/src/test/test_client.cpp index 4cee3f3d..6817ad59 100644 --- a/src/test/test_client.cpp +++ b/src/test/test_client.cpp @@ -9,7 +9,7 @@ #include "checkexception.h" #include "mockclientconnector.h" -#include +#include #include #define TEST_MODULE "[client]" diff --git a/src/test/test_common.cpp b/src/test/test_common.cpp index 76de0481..b772f7ca 100644 --- a/src/test/test_common.cpp +++ b/src/test/test_common.cpp @@ -8,7 +8,7 @@ ************************************************************************/ #include "checkexception.h" -#include +#include #include #include #include diff --git a/src/test/test_connector_filedescriptor.cpp b/src/test/test_connector_filedescriptor.cpp index 9c05a6f8..d3d77af5 100644 --- a/src/test/test_connector_filedescriptor.cpp +++ b/src/test/test_connector_filedescriptor.cpp @@ -9,7 +9,7 @@ #ifdef FILEDESCRIPTOR_TESTING #include "mockclientconnectionhandler.h" -#include +#include #include #include #include diff --git a/src/test/test_connector_http.cpp b/src/test/test_connector_http.cpp index e091f37c..9609727c 100644 --- a/src/test/test_connector_http.cpp +++ b/src/test/test_connector_http.cpp @@ -8,7 +8,7 @@ ************************************************************************/ #ifdef HTTP_TESTING -#include +#include #include #include #include diff --git a/src/test/test_connector_redis.cpp b/src/test/test_connector_redis.cpp index 80bd1e03..9a24ef14 100644 --- a/src/test/test_connector_redis.cpp +++ b/src/test/test_connector_redis.cpp @@ -8,7 +8,7 @@ ************************************************************************/ #ifdef REDIS_TESTING -#include +#include #include #include diff --git a/src/test/test_connector_tcpsocket.cpp b/src/test/test_connector_tcpsocket.cpp index 57bfff44..a5260b81 100644 --- a/src/test/test_connector_tcpsocket.cpp +++ b/src/test/test_connector_tcpsocket.cpp @@ -9,7 +9,7 @@ #ifdef TCPSOCKET_TESTING #include "mockclientconnectionhandler.h" -#include +#include #include #include diff --git a/src/test/test_connector_unixdomainsocket.cpp b/src/test/test_connector_unixdomainsocket.cpp index a3d237ab..3621bf73 100644 --- a/src/test/test_connector_unixdomainsocket.cpp +++ b/src/test/test_connector_unixdomainsocket.cpp @@ -9,7 +9,7 @@ #ifdef UNIXDOMAINSOCKET_TESTING #include "mockclientconnectionhandler.h" -#include +#include #include #include diff --git a/src/test/test_integration.cpp b/src/test/test_integration.cpp index d884411d..e86e3da7 100644 --- a/src/test/test_integration.cpp +++ b/src/test/test_integration.cpp @@ -8,7 +8,7 @@ ************************************************************************/ #ifdef STUBGEN_TESTING -#include +#include #ifdef HTTP_TESTING #include diff --git a/src/test/test_server.cpp b/src/test/test_server.cpp index 02086f5f..c50f391c 100644 --- a/src/test/test_server.cpp +++ b/src/test/test_server.cpp @@ -9,7 +9,7 @@ #include "mockserverconnector.h" #include "testserver.h" -#include +#include #define TEST_MODULE "[server]" diff --git a/src/test/test_stubgenerator.cpp b/src/test/test_stubgenerator.cpp index 08bc554b..4a8f9489 100644 --- a/src/test/test_stubgenerator.cpp +++ b/src/test/test_stubgenerator.cpp @@ -8,7 +8,7 @@ ************************************************************************/ #ifdef STUBGEN_TESTING -#include +#include #include #include From 64fef4b4660551410cd7645c91328f15e74e7df0 Mon Sep 17 00:00:00 2001 From: Peter Spiess-Knafl Date: Fri, 29 Mar 2019 02:07:53 +0100 Subject: [PATCH 14/17] Fixed jsoncpp deprecations --- src/jsonrpccpp/client/batchcall.cpp | 9 +++++---- src/jsonrpccpp/client/rpcprotocolclient.cpp | 9 ++++----- src/jsonrpccpp/common/specificationwriter.cpp | 5 +++-- src/jsonrpccpp/server/abstractprotocolhandler.cpp | 5 +++-- src/jsonrpccpp/server/rpcprotocolserver12.cpp | 5 +++-- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/jsonrpccpp/client/batchcall.cpp b/src/jsonrpccpp/client/batchcall.cpp index 11aca3ae..d3e83c14 100644 --- a/src/jsonrpccpp/client/batchcall.cpp +++ b/src/jsonrpccpp/client/batchcall.cpp @@ -37,11 +37,12 @@ int BatchCall::addCall(const string &methodname, const Json::Value ¶ms, string BatchCall::toString(bool fast) const { string result; if (fast) { - Json::FastWriter writer; - result = writer.write(this->result); + Json::StreamWriterBuilder wbuilder; + wbuilder["indentation"] = ""; + result = Json::writeString(wbuilder,this->result); } else { - Json::StyledWriter writer; - result = writer.write(this->result); + Json::StreamWriterBuilder wbuilder; + result = Json::writeString(wbuilder, this->result); } return result; } diff --git a/src/jsonrpccpp/client/rpcprotocolclient.cpp b/src/jsonrpccpp/client/rpcprotocolclient.cpp index 182d8f6a..e224dfa6 100644 --- a/src/jsonrpccpp/client/rpcprotocolclient.cpp +++ b/src/jsonrpccpp/client/rpcprotocolclient.cpp @@ -30,12 +30,11 @@ void RpcProtocolClient::BuildRequest(const std::string &method, const Json::Value ¶meter, std::string &result, bool isNotification) { Json::Value request; - Json::FastWriter writer; + Json::StreamWriterBuilder wbuilder; + wbuilder["indentation"] = ""; this->BuildRequest(1, method, parameter, request, isNotification); - if (omitEndingLineFeed) { - writer.omitEndingLineFeed(); - } - result = writer.write(request); + + result = Json::writeString(wbuilder, request); } void RpcProtocolClient::HandleResponse(const std::string &response, diff --git a/src/jsonrpccpp/common/specificationwriter.cpp b/src/jsonrpccpp/common/specificationwriter.cpp index 04aa4f9f..84319f9b 100644 --- a/src/jsonrpccpp/common/specificationwriter.cpp +++ b/src/jsonrpccpp/common/specificationwriter.cpp @@ -28,8 +28,9 @@ SpecificationWriter::toJsonValue(const vector &procedures) { return result; } std::string SpecificationWriter::toString(const vector &procedures) { - Json::StyledWriter wr; - return wr.write(toJsonValue(procedures)); + Json::StreamWriterBuilder wb; + wb["indentation"] = " "; + return Json::writeString(wb, toJsonValue(procedures)); } bool SpecificationWriter::toFile(const std::string &filename, const vector &procedures) { diff --git a/src/jsonrpccpp/server/abstractprotocolhandler.cpp b/src/jsonrpccpp/server/abstractprotocolhandler.cpp index 258689f8..96b18f52 100644 --- a/src/jsonrpccpp/server/abstractprotocolhandler.cpp +++ b/src/jsonrpccpp/server/abstractprotocolhandler.cpp @@ -31,7 +31,8 @@ void AbstractProtocolHandler::HandleRequest(const std::string &request, Json::Reader reader; Json::Value req; Json::Value resp; - Json::FastWriter w; + Json::StreamWriterBuilder wbuilder; + wbuilder["indentation"] = ""; try { if (reader.parse(request, req, false)) { @@ -48,7 +49,7 @@ void AbstractProtocolHandler::HandleRequest(const std::string &request, } if (resp != Json::nullValue) - retValue = w.write(resp); + retValue = Json::writeString(wbuilder,resp); } void AbstractProtocolHandler::ProcessRequest(const Json::Value &request, diff --git a/src/jsonrpccpp/server/rpcprotocolserver12.cpp b/src/jsonrpccpp/server/rpcprotocolserver12.cpp index c8edaeac..214dcd71 100644 --- a/src/jsonrpccpp/server/rpcprotocolserver12.cpp +++ b/src/jsonrpccpp/server/rpcprotocolserver12.cpp @@ -25,7 +25,8 @@ void RpcProtocolServer12::HandleRequest(const std::string &request, Json::Reader reader; Json::Value req; Json::Value resp; - Json::FastWriter w; + Json::StreamWriterBuilder wbuilder; + wbuilder["indentation"] = ""; try { if (reader.parse(request, req, false)) { @@ -42,7 +43,7 @@ void RpcProtocolServer12::HandleRequest(const std::string &request, } if (resp != Json::nullValue) - retValue = w.write(resp); + retValue = Json::writeString(wbuilder, resp); } AbstractProtocolHandler & From 9ea743b2754973427a2c44ed70976ece1fe276a6 Mon Sep 17 00:00:00 2001 From: Peter Spiess-Knafl Date: Fri, 29 Mar 2019 02:19:18 +0100 Subject: [PATCH 15/17] Fix pkgconfig files for conditional builds (#250) --- cmake/libjsonrpccpp-client.pc.cmake | 2 +- cmake/libjsonrpccpp-server.pc.cmake | 2 +- src/jsonrpccpp/CMakeLists.txt | 10 ++++++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/cmake/libjsonrpccpp-client.pc.cmake b/cmake/libjsonrpccpp-client.pc.cmake index 295e8456..acad8706 100644 --- a/cmake/libjsonrpccpp-client.pc.cmake +++ b/cmake/libjsonrpccpp-client.pc.cmake @@ -1,5 +1,5 @@ Name: libjsonrpccpp-client Description: A C++ client implementation of json-rpc. Version: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION} -Libs: -L${FULL_PATH_LIBDIR} -ljsoncpp -ljsonrpccpp-common -ljsonrpccpp-client -lcurl -lhiredis +Libs: -L${FULL_PATH_LIBDIR} -ljsoncpp -ljsonrpccpp-common -ljsonrpccpp-client ${CLIENT_LIBS} Cflags: -I${FULL_PATH_INCLUDEDIR} diff --git a/cmake/libjsonrpccpp-server.pc.cmake b/cmake/libjsonrpccpp-server.pc.cmake index 10688ad4..1c693eeb 100644 --- a/cmake/libjsonrpccpp-server.pc.cmake +++ b/cmake/libjsonrpccpp-server.pc.cmake @@ -1,5 +1,5 @@ Name: libjsonrpccpp-server Description: A C++ server implementation of json-rpc. Version: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION} -Libs: -L${FULL_PATH_LIBDIR} -ljsoncpp -ljsonrpccpp-common -ljsonrpccpp-server -lmicrohttpd -lhiredis +Libs: -L${FULL_PATH_LIBDIR} -ljsoncpp -ljsonrpccpp-common -ljsonrpccpp-server ${SERVER_LIBS} Cflags: -I${FULL_PATH_INCLUDEDIR} diff --git a/src/jsonrpccpp/CMakeLists.txt b/src/jsonrpccpp/CMakeLists.txt index c8098652..acc76133 100644 --- a/src/jsonrpccpp/CMakeLists.txt +++ b/src/jsonrpccpp/CMakeLists.txt @@ -33,18 +33,22 @@ set(client_connector_libs "") set(server_connector_source "") set(server_connector_header "") set(server_connector_libs "") +set(SERVER_LIBS "") +set(CLIENT_LIBS "") # setup sources for http connectors if (HTTP_CLIENT) list(APPEND client_connector_header "client/connectors/httpclient.h") list(APPEND client_connector_source "client/connectors/httpclient.cpp") - list(APPEND client_connector_libs ${CURL_LIBRARIES}) + list(APPEND client_connector_libs ${CURL_LIBRARIES}) + set(CLIENT_LIBS "${CLIENT_LIBS} -lcurl") endif() if (HTTP_SERVER) list(APPEND server_connector_header "server/connectors/httpserver.h") list(APPEND server_connector_source "server/connectors/httpserver.cpp") - list(APPEND server_connector_libs ${CMAKE_THREAD_LIBS_INIT} ${MHD_LIBRARIES}) + list(APPEND server_connector_libs ${CMAKE_THREAD_LIBS_INIT} ${MHD_LIBRARIES}) + set(SERVER_LIBS "${SERVER_LIBS} -lmicrohttpd") endif() # setup sources for redis connectors @@ -53,12 +57,14 @@ if (REDIS_CLIENT) list(APPEND client_connector_source "client/connectors/redisclient.cpp") list(APPEND client_connector_libs ${HIREDIS_LIBRARIES}) include_directories(${HIREDIS_INCLUDE_DIRS}) + set(CLIENT_LIBS "${CLIENT_LIBS} -lhiredis") endif() if (REDIS_SERVER) list(APPEND server_connector_header "server/connectors/redisserver.h") list(APPEND server_connector_source "server/connectors/redisserver.cpp") list(APPEND server_connector_libs ${CMAKE_THREAD_LIBS_INIT} ${HIREDIS_LIBRARIES}) + set(SERVER_LIBS "${SERVER_LIBS} -lhiredis") endif() # setup sources for unix domain socket connectors From bfee032e59ce111a97278bcf41ea0b17e49b75e2 Mon Sep 17 00:00:00 2001 From: Peter Spiess-Knafl Date: Fri, 29 Mar 2019 02:19:46 +0100 Subject: [PATCH 16/17] Update changelog --- CHANGELOG.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fbbc172..c7c8ad8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased ### Added -- The `HttpServer` connector now has a `SetBindAddress` method, which can - be used to bind to a specific address rather than listen on all - interfaces of the machine (#259) +- The `HttpServer` connector now has a `BindLocalhost` method (#261) + +### Fixed +- Don't precompress and honor GnuInstallDir for manpage of jsonrpcstub (#252) +- Arch Linux CI build (base image changed) +- brew CI build (no longer has `--ssl` flag for libmicrohttpd) +- Catching `Jsoncpp::Exception` for `.parse()` invocations +- Compile issue when building static only (#263) +- Update catch2 to 2.7.0 and fix include path (#251) +- Removed deprecated jsoncpp invocations + ## [v1.1.1] - 2018-10-31 From b938e7acf7db5dbc9f39029e652e6d5330ed02e0 Mon Sep 17 00:00:00 2001 From: Peter Spiess-Knafl Date: Fri, 29 Mar 2019 02:24:09 +0100 Subject: [PATCH 17/17] Version bump to 1.2.0 --- CHANGELOG.md | 4 +--- CMakeLists.txt | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7c8ad8f..b4187948 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). -## Unreleased - +## [v1.2.0] - 2019-03-29 ### Added - The `HttpServer` connector now has a `BindLocalhost` method (#261) @@ -20,7 +19,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [v1.1.1] - 2018-10-31 - ### Fixed - Build issue on RHEL7 (#244, #246) - Build with empty install prefix (#226) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c542659..81167267 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,8 +22,8 @@ if (${CMAKE_MAJOR_VERSION} GREATER 2) endif() set(MAJOR_VERSION 1) -set(MINOR_VERSION 1) -set(PATCH_VERSION 1) +set(MINOR_VERSION 2) +set(PATCH_VERSION 0) set(SO_VERSION 1) if(NOT MSVC)