From 1120e55f993f54af607ac7f580454ad5a1852fcf Mon Sep 17 00:00:00 2001 From: moss9001 Date: Fri, 22 Oct 2021 13:34:56 +0300 Subject: [PATCH] Comments update and minor fixes --- .../peerplays_sidechain/common/https_call.cpp | 335 +++++++++--------- .../peerplays_sidechain/common/https_call.h | 6 +- .../peerplays_sidechain/common/net_utl.cpp | 33 +- .../peerplays_sidechain/common/net_utl.h | 5 + .../peerplays_sidechain/common/rpc_client.cpp | 128 ++++--- .../sidechain_net_handler_hive.cpp | 27 +- 6 files changed, 269 insertions(+), 265 deletions(-) diff --git a/libraries/plugins/peerplays_sidechain/common/https_call.cpp b/libraries/plugins/peerplays_sidechain/common/https_call.cpp index 796296ac..93136b32 100644 --- a/libraries/plugins/peerplays_sidechain/common/https_call.cpp +++ b/libraries/plugins/peerplays_sidechain/common/https_call.cpp @@ -1,11 +1,11 @@ #include "https_call.h" #include -#include #include +#include -#include #include +#include //#include @@ -16,239 +16,238 @@ namespace detail { static const char Cr = 0x0D; static const char Lf = 0x0A; -static const char * CrLf = "\x0D\x0A"; -static const char * CrLfCrLf = "\x0D\x0A\x0D\x0A"; +static const char *CrLf = "\x0D\x0A"; +static const char *CrLfCrLf = "\x0D\x0A\x0D\x0A"; using namespace boost::asio; -[[noreturn]] static void throwError(const std::string & msg) { - throw std::runtime_error(msg); +[[noreturn]] static void throwError(const std::string &msg) { + throw std::runtime_error(msg); } class Impl { public: + Impl(const HttpsCall &call, const HttpRequest &request, HttpResponse &response) : + m_Call(call), + m_Request(request), + m_Response(response), + m_Service(), + m_Context(ssl::context::tlsv12_client), + m_Socket(m_Service, m_Context), + m_Endpoint(), + m_ResponseBuf(call.responseSizeLimitBytes()), + m_ContentLength(0) { + m_Context.set_default_verify_paths(); + } - Impl(const HttpsCall & call, const HttpRequest & request, HttpResponse & response) : - m_Call(call), - m_Request(request), - m_Response(response), - m_Service(), - m_Context(ssl::context::tlsv12_client), - m_Socket(m_Service, m_Context), - m_Endpoint(), - m_ResponseBuf(call.responseSizeLimitBytes()), - m_ContentLength(0) - { - m_Context.set_default_verify_paths(); - } - - void exec() { - resolve(); - connect(); - sendRequest(); - processResponse(); - } + void exec() { + resolve(); + connect(); + sendRequest(); + processResponse(); + } private: + const HttpsCall &m_Call; + const HttpRequest &m_Request; + HttpResponse &m_Response; - const HttpsCall & m_Call; - const HttpRequest & m_Request; - HttpResponse & m_Response; + io_service m_Service; + ssl::context m_Context; + ssl::stream m_Socket; + ip::tcp::endpoint m_Endpoint; + streambuf m_ResponseBuf; + uint32_t m_ContentLength; - io_service m_Service; - ssl::context m_Context; - ssl::stream m_Socket; - ip::tcp::endpoint m_Endpoint; - streambuf m_ResponseBuf; - uint32_t m_ContentLength; + void resolve() { - void resolve() { + // resolve TCP endpoint for host name - // resolve TCP endpoint for host name + ip::tcp::resolver resolver(m_Service); + auto query = ip::tcp::resolver::query(m_Call.host(), "https"); + auto iter = resolver.resolve(query); + m_Endpoint = *iter; - ip::tcp::resolver resolver(m_Service); - auto query = ip::tcp::resolver::query(m_Call.host(), "https"); - auto iter = resolver.resolve(query); - m_Endpoint = *iter; + if (m_Call.port() != 0) // if port was specified + m_Endpoint.port(m_Call.port()); // force set port + } - if (m_Call.port() != 0) // if port was specified - m_Endpoint.port(m_Call.port()); // force set port + void connect() { - } + // TCP connect - void connect() { + m_Socket.lowest_layer().connect(m_Endpoint); - // TCP connect + // SSL connect - m_Socket.lowest_layer().connect(m_Endpoint); + m_Socket.set_verify_mode(ssl::verify_peer); + m_Socket.handshake(ssl::stream_base::client); + } - // SSL connect + void sendRequest() { - m_Socket.set_verify_mode(ssl::verify_peer); - m_Socket.handshake(ssl::stream_base::client); - } + streambuf requestBuf; + std::ostream stream(&requestBuf); - void sendRequest() { + // start string: HTTP/1.0 - streambuf requestBuf; - std::ostream stream(&requestBuf); + stream << m_Request.method << " " << m_Request.path << " HTTP/1.0" << CrLf; - // start string: method path HTTP/1.0 + // host - stream << m_Request.method << " " << m_Request.path << " HTTP/1.0" << CrLf; + stream << "Host: " << m_Call.host(); - // host + if (m_Call.port() != 0) { + //ASSERT(m_Endpoint.port() == m_Call.port()); + stream << ":" << m_Call.port(); + } - stream << "Host: " << m_Call.host(); + stream << CrLf; - if (m_Call.port() != 0) { - //ASSERT(m_Endpoint.port() == m_Call.port()); - stream << ":" << m_Call.port(); - } + // content - stream << CrLf; + if (!m_Request.body.empty()) { + stream << "Content-Type: " << m_Request.contentType << CrLf; + stream << "Content-Length: " << m_Request.body.size() << CrLf; + } - // content + // additional headers - if (!m_Request.body.empty()) { - stream << "Content-Type: " << m_Request.contentType << CrLf; - stream << "Content-Length: " << m_Request.body.size() << CrLf; - } + const auto &h = m_Request.headers; - // additional headers + if (!h.empty()) { + if (h.size() < 2) + throwError("invalid headers data"); + stream << h; + // ensure headers finished correctly + if ((h.substr(h.size() - 2) != CrLf)) + stream << CrLf; + } - const auto & h = m_Request.headers; + // other - if (!h.empty()) { - if (h.size() < 2) - throw 1; - stream << h; - if ((h.substr(h.size() - 2) != CrLf)) - stream << CrLf; - } + stream << "Accept: *\x2F*" << CrLf; + stream << "Connection: close" << CrLf; - // other + // end - stream << "Accept: *\x2F*" << CrLf; - stream << "Connection: close" << CrLf; + stream << CrLf; - // end + // content - stream << CrLf; + if (!m_Request.body.empty()) + stream << m_Request.body; - // content + // send - if (!m_Request.body.empty()) - stream << m_Request.body; + write(m_Socket, requestBuf); + } - // send + void processHeaders() { - write(m_Socket, requestBuf); - } + std::istream stream(&m_ResponseBuf); - void helper1() { + std::string httpVersion; + stream >> httpVersion; + stream >> m_Response.statusCode; - std::istream stream(&m_ResponseBuf); + if (!stream || httpVersion.substr(0, 5) != "HTTP/") { + throwError("invalid response"); + } - std::string httpVersion; - stream >> httpVersion; - stream >> m_Response.statusCode; - - if (!stream || httpVersion.substr(0, 5) != "HTTP/") { - throw "invalid response"; - } + // read/skip headers - // read/skip headers + for (;;) { + std::string header; + if (!std::getline(stream, header, Lf) || (header.size() == 1 && header[0] == Cr)) + break; + if (m_ContentLength) // if content length is already known + continue; // continue skipping headers + auto pos = header.find(':'); + if (pos == std::string::npos) + continue; + auto name = header.substr(0, pos); + boost::algorithm::trim(name); + boost::algorithm::to_lower(name); + if (name != "content-length") + continue; + auto value = header.substr(pos + 1); + boost::algorithm::trim(value); + m_ContentLength = std::stol(value); + } + } - for(;;) { - std::string header; - if (!std::getline(stream, header, Lf) || (header.size() == 1 && header[0] == Cr)) - break; - if (m_ContentLength) // if content length is already known - continue; // continue skipping headers - auto pos = header.find(':'); - if (pos == std::string::npos) - continue; - auto name = header.substr(0, pos); - boost::algorithm::trim(name); - boost::algorithm::to_lower(name); - if (name != "content-length") - continue; - auto value = header.substr(pos + 1); - boost::algorithm::trim(value); - m_ContentLength = std::stol(value); - } + void processResponse() { - } + auto &socket = m_Socket; + auto &buf = m_ResponseBuf; + auto &contentLength = m_ContentLength; + auto &body = m_Response.body; - void processResponse() { + read_until(socket, buf, CrLfCrLf); - auto & socket = m_Socket; - auto & buf = m_ResponseBuf; - auto & contentLength = m_ContentLength; - auto & body = m_Response.body; + processHeaders(); - read_until(socket, buf, CrLfCrLf); + // check content length - helper1(); + if (contentLength < 2) // minimum content is "{}" + throwError("invalid response body (too short)"); - if (contentLength < 2) // minimum content is "{}" - throwError("invalid response body (too short)"); + if (contentLength > m_Call.responseSizeLimitBytes()) + throwError("response body size limit exceeded"); - if (contentLength > m_Call.responseSizeLimitBytes()) - throwError("response body size limit exceeded"); + // read body - auto avail = buf.size(); + auto avail = buf.size(); // size of body data already stored in the buffer - if (avail > contentLength) - throwError("invalid response body (content length mismatch)"); + if (avail > contentLength) + throwError("invalid response body (content length mismatch)"); - body.resize(contentLength); + body.resize(contentLength); - if (avail) { - if (avail != buf.sgetn(&body[0], avail)) { - throwError("stream read failed"); - } - } + if (avail) { + // copy already existing data + if (avail != buf.sgetn(&body[0], avail)) { + throwError("stream read failed"); + } + } - auto rest = contentLength - avail; + auto rest = contentLength - avail; // size of remaining part of response body - boost::system::error_code errorCode; + boost::system::error_code errorCode; - read(socket, buffer(&body[avail], rest), errorCode); - - socket.shutdown(errorCode); - - } + read(socket, buffer(&body[avail], rest), errorCode); // read remaining part + socket.shutdown(errorCode); + } }; -} // detail +} // namespace detail // HttpsCall -HttpsCall::HttpsCall(const std::string & host, uint16_t port) : - m_Host(host), - m_Port(port) -{} - -bool HttpsCall::exec(const HttpRequest & request, HttpResponse * response) { - -// ASSERT(response); - auto & resp = *response; - - detail::Impl impl(*this, request, resp); - - try { - resp.clear(); - impl.exec(); - } catch (...) { - resp.clear(); - return false; - } - - return true; +HttpsCall::HttpsCall(const std::string &host, uint16_t port) : + m_Host(host), + m_Port(port) { } -} // net -} // peerplays +bool HttpsCall::exec(const HttpRequest &request, HttpResponse *response) { + + // ASSERT(response); + auto &resp = *response; + + detail::Impl impl(*this, request, resp); + + try { + resp.clear(); + impl.exec(); + } catch (...) { + resp.clear(); + return false; + } + + return true; +} + +} +} // namespace peerplays::net diff --git a/libraries/plugins/peerplays_sidechain/common/https_call.h b/libraries/plugins/peerplays_sidechain/common/https_call.h index 5a96d481..595b4a58 100644 --- a/libraries/plugins/peerplays_sidechain/common/https_call.h +++ b/libraries/plugins/peerplays_sidechain/common/https_call.h @@ -60,9 +60,7 @@ public: HttpsCall(const std::string & host, uint16_t port = 0); - bool exec(const HttpRequest & request, HttpResponse * response); - - const std::string host() const { + const std::string & host() const { return m_Host; } @@ -74,6 +72,8 @@ public: return ResponseSizeLimitBytes; } + bool exec(const HttpRequest & request, HttpResponse * response); + private: std::string m_Host; uint16_t m_Port; diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.cpp b/libraries/plugins/peerplays_sidechain/common/net_utl.cpp index 6037438a..07271b81 100644 --- a/libraries/plugins/peerplays_sidechain/common/net_utl.cpp +++ b/libraries/plugins/peerplays_sidechain/common/net_utl.cpp @@ -2,27 +2,26 @@ #include - namespace peerplays { namespace net { -std::string resolveHostAddr(const std::string & hostName) { - using namespace boost::asio; - io_service service; - ip::tcp::resolver resolver(service); - auto query = ip::tcp::resolver::query(hostName, ""); - auto iter = resolver.resolve(query); - auto endpoint = *iter; - auto addr = ((ip::tcp::endpoint)endpoint).address(); - return addr.to_string(); +std::string resolveHostAddr(const std::string &hostName) { + using namespace boost::asio; + io_service service; + ip::tcp::resolver resolver(service); + auto query = ip::tcp::resolver::query(hostName, std::string()); + auto iter = resolver.resolve(query); + auto endpoint = *iter; + auto addr = ((ip::tcp::endpoint)endpoint).address(); + return addr.to_string(); } -std::string stripProtoName(const std::string & url) { - auto index = url.find("://"); - if (index == std::string::npos) - return url; - return url.substr(index + 3); +std::string stripProtoName(const std::string &url) { + auto index = url.find("://"); + if (index == std::string::npos) + return url; + return url.substr(index + 3); } -} // net -} // peerplays +} +} // namespace peerplays::net diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.h b/libraries/plugins/peerplays_sidechain/common/net_utl.h index 22533aa0..6cdd2cd5 100644 --- a/libraries/plugins/peerplays_sidechain/common/net_utl.h +++ b/libraries/plugins/peerplays_sidechain/common/net_utl.h @@ -5,7 +5,12 @@ namespace peerplays { namespace net { +// resolve IP address by host name +// ex: api.hive.blog -> 52.79.10.214 std::string resolveHostAddr(const std::string & hostName); + +// remove schema part from URL +// ex: http://api.hive.blog -> api.hive.blog std::string stripProtoName(const std::string & url); diff --git a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp index 2774e062..d3b9d46e 100644 --- a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp +++ b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp @@ -106,89 +106,87 @@ std::string rpc_client::send_post_request(std::string method, std::string params fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { - fc::http::reply reply; - auto start = ip.substr(0, 6); - boost::algorithm::to_lower(start); + fc::http::reply reply; + auto start = ip.substr(0, 6); + boost::algorithm::to_lower(start); - if (start == "https:") { + if (start == "https:") { - auto host = ip.substr(8); // skip "https://" + auto host = ip.substr(8); // skip "https://" - using namespace peerplays::net; + using namespace peerplays::net; - HttpsCall call(host, port); - HttpRequest request("POST", "/", authorization.key + ":" + authorization.val, body); - HttpResponse response; + HttpsCall call(host, port); + HttpRequest request("POST", "/", authorization.key + ":" + authorization.val, body); + HttpResponse response; - if (call.exec(request, &response)) { - reply.status = response.statusCode; - reply.body.resize(response.body.size()); - memcpy(&reply.body[0], &response.body[0], response.body.size()); - } + if (call.exec(request, &response)) { + reply.status = response.statusCode; + reply.body.resize(response.body.size()); + memcpy(&reply.body[0], &response.body[0], response.body.size()); + } - if (show_log) { - std::string url = ip + ":" + std::to_string(port); - ilog("### Request URL: ${url}", ("url", url)); - ilog("### Request: ${body}", ("body", body)); - ilog("### Response code: ${code}", ("code", response.statusCode)); - ilog("### Response len: ${len}", ("len", response.body.size())); - std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); - ilog("### Response body: ${ss}", ("ss", ss.str())); - } + if (show_log) { + std::string url = ip + ":" + std::to_string(port); + ilog("### Request URL: ${url}", ("url", url)); + ilog("### Request: ${body}", ("body", body)); + ilog("### Response code: ${code}", ("code", response.statusCode)); + ilog("### Response len: ${len}", ("len", response.body.size())); + std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); + ilog("### Response body: ${ss}", ("ss", ss.str())); + } - return reply; + return reply; + } - } + std::string host; - std::string host; + if (start == "http:/") + host = ip.substr(7); // skip "http://" + else + host = ip; - if (start == "http:/") - host = ip.substr(7); // skip "http://" - else - host = ip; + std::string url = "http://" + host + ":" + std::to_string(port); + fc::ip::address addr; - std::string url = "http://" + host + ":" + std::to_string(port); - fc::ip::address addr; + try { + addr = fc::ip::address(host); + } catch (...) { + try { + addr = fc::ip::address(peerplays::net::resolveHostAddr(host)); + } catch (...) { + if (show_log) { + std::string url = ip + ":" + std::to_string(port); + ilog("### Request URL: ${url}", ("url", url)); + ilog("### Request: ${body}", ("body", body)); + ilog("### Request: error: host address resolve failed"); + } + return reply; + } + } - try { - addr = fc::ip::address(host); - } catch (...) { - try { - addr = fc::ip::address(peerplays::net::resolveHostAddr(host)); - } catch (...) { - if (show_log) { - std::string url = ip + ":" + std::to_string(port); - ilog("### Request URL: ${url}", ("url", url)); - ilog("### Request: ${body}", ("body", body)); - ilog("### Request: error: host address resolve failed"); - } - return reply; - } - } + try { - try { + fc::http::connection conn; + conn.connect_to(fc::ip::endpoint(addr, port)); - fc::http::connection conn; - conn.connect_to(fc::ip::endpoint(addr, port)); + //if (wallet.length() > 0) { + // url = url + "/wallet/" + wallet; + //} - //if (wallet.length() > 0) { - // url = url + "/wallet/" + wallet; - //} + reply = conn.request("POST", url, body, fc::http::headers{authorization}); - reply = conn.request("POST", url, body, fc::http::headers{authorization}); + } catch (...) { + } - } catch (...) { - } - - if (show_log) { - ilog("### Request URL: ${url}", ("url", url)); - ilog("### Request: ${body}", ("body", body)); - std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); - ilog("### Response: ${ss}", ("ss", ss.str())); - } - - return reply; + if (show_log) { + ilog("### Request URL: ${url}", ("url", url)); + ilog("### Request: ${body}", ("body", body)); + std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); + ilog("### Response: ${ss}", ("ss", ss.str())); + } + return reply; } }} // namespace graphene::peerplays_sidechain diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp index 3565416b..3dfc9aee 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp @@ -150,18 +150,21 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi fc::http::connection conn; try { - auto host = peerplays::net::stripProtoName(node_ip); - fc::ip::address addr; - try { - addr = fc::ip::address(host); - } catch (...) { - try { - addr = fc::ip::address(peerplays::net::resolveHostAddr(host)); - } catch (...) { - elog("Failed to resolve Hive node address ${ip}", ("ip", node_ip)); - FC_ASSERT(false); - } - } + auto host = peerplays::net::stripProtoName(node_ip); + fc::ip::address addr; + try { + // IP address assumed + addr = fc::ip::address(host); + } catch (...) { + try { + // host name assumed + addr = fc::ip::address(peerplays::net::resolveHostAddr(host)); + } catch (...) { + elog("Failed to resolve Hive node address ${ip}", ("ip", node_ip)); + FC_ASSERT(false); + } + } + // try to connect to TCP endpoint conn.connect_to(fc::ip::endpoint(addr, node_rpc_port)); } catch (fc::exception &e) { elog("No Hive node running at ${ip} or wrong rpc port: ${port}", ("ip", node_ip)("port", node_rpc_port));