Comments update and minor fixes

This commit is contained in:
moss9001 2021-10-22 13:34:56 +03:00
parent 7ddad07df2
commit 1120e55f99
6 changed files with 269 additions and 265 deletions

View file

@ -1,11 +1,11 @@
#include "https_call.h" #include "https_call.h"
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/asio/buffer.hpp> #include <boost/asio/buffer.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/case_conv.hpp> #include <boost/algorithm/string/case_conv.hpp>
#include <boost/algorithm/string/trim.hpp>
//#include <iostream> //#include <iostream>
@ -16,239 +16,238 @@ namespace detail {
static const char Cr = 0x0D; static const char Cr = 0x0D;
static const char Lf = 0x0A; static const char Lf = 0x0A;
static const char * CrLf = "\x0D\x0A"; static const char *CrLf = "\x0D\x0A";
static const char * CrLfCrLf = "\x0D\x0A\x0D\x0A"; static const char *CrLfCrLf = "\x0D\x0A\x0D\x0A";
using namespace boost::asio; using namespace boost::asio;
[[noreturn]] static void throwError(const std::string & msg) { [[noreturn]] static void throwError(const std::string &msg) {
throw std::runtime_error(msg); throw std::runtime_error(msg);
} }
class Impl { class Impl {
public: 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) : void exec() {
m_Call(call), resolve();
m_Request(request), connect();
m_Response(response), sendRequest();
m_Service(), processResponse();
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();
}
private: private:
const HttpsCall &m_Call;
const HttpRequest &m_Request;
HttpResponse &m_Response;
const HttpsCall & m_Call; io_service m_Service;
const HttpRequest & m_Request; ssl::context m_Context;
HttpResponse & m_Response; ssl::stream<ip::tcp::socket> m_Socket;
ip::tcp::endpoint m_Endpoint;
streambuf m_ResponseBuf;
uint32_t m_ContentLength;
io_service m_Service; void resolve() {
ssl::context m_Context;
ssl::stream<ip::tcp::socket> m_Socket;
ip::tcp::endpoint m_Endpoint;
streambuf m_ResponseBuf;
uint32_t m_ContentLength;
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); if (m_Call.port() != 0) // if port was specified
auto query = ip::tcp::resolver::query(m_Call.host(), "https"); m_Endpoint.port(m_Call.port()); // force set port
auto iter = resolver.resolve(query); }
m_Endpoint = *iter;
if (m_Call.port() != 0) // if port was specified void connect() {
m_Endpoint.port(m_Call.port()); // force set port
} // 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); streambuf requestBuf;
m_Socket.handshake(ssl::stream_base::client); std::ostream stream(&requestBuf);
}
void sendRequest() { // start string: <method> <path> HTTP/1.0
streambuf requestBuf; stream << m_Request.method << " " << m_Request.path << " HTTP/1.0" << CrLf;
std::ostream stream(&requestBuf);
// 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) { // content
//ASSERT(m_Endpoint.port() == m_Call.port());
stream << ":" << m_Call.port();
}
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()) { const auto &h = m_Request.headers;
stream << "Content-Type: " << m_Request.contentType << CrLf;
stream << "Content-Length: " << m_Request.body.size() << CrLf;
}
// 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()) { stream << "Accept: *\x2F*" << CrLf;
if (h.size() < 2) stream << "Connection: close" << CrLf;
throw 1;
stream << h;
if ((h.substr(h.size() - 2) != CrLf))
stream << CrLf;
}
// other // end
stream << "Accept: *\x2F*" << CrLf; stream << CrLf;
stream << "Connection: close" << CrLf;
// end // content
stream << CrLf; if (!m_Request.body.empty())
stream << m_Request.body;
// content // send
if (!m_Request.body.empty()) write(m_Socket, requestBuf);
stream << m_Request.body; }
// 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; // read/skip headers
stream >> httpVersion;
stream >> m_Response.statusCode;
if (!stream || httpVersion.substr(0, 5) != "HTTP/") { for (;;) {
throw "invalid response"; 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);
}
}
// read/skip headers void processResponse() {
for(;;) { auto &socket = m_Socket;
std::string header; auto &buf = m_ResponseBuf;
if (!std::getline(stream, header, Lf) || (header.size() == 1 && header[0] == Cr)) auto &contentLength = m_ContentLength;
break; auto &body = m_Response.body;
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);
}
} read_until(socket, buf, CrLfCrLf);
void processResponse() { processHeaders();
auto & socket = m_Socket; // check content length
auto & buf = m_ResponseBuf;
auto & contentLength = m_ContentLength;
auto & body = m_Response.body;
read_until(socket, buf, CrLfCrLf); if (contentLength < 2) // minimum content is "{}"
throwError("invalid response body (too short)");
helper1(); if (contentLength > m_Call.responseSizeLimitBytes())
throwError("response body size limit exceeded");
if (contentLength < 2) // minimum content is "{}" // read body
throwError("invalid response body (too short)");
if (contentLength > m_Call.responseSizeLimitBytes()) auto avail = buf.size(); // size of body data already stored in the buffer
throwError("response body size limit exceeded");
auto avail = buf.size(); if (avail > contentLength)
throwError("invalid response body (content length mismatch)");
if (avail > contentLength) body.resize(contentLength);
throwError("invalid response body (content length mismatch)");
body.resize(contentLength); if (avail) {
// copy already existing data
if (avail != buf.sgetn(&body[0], avail)) {
throwError("stream read failed");
}
}
if (avail) { auto rest = contentLength - avail; // size of remaining part of response body
if (avail != buf.sgetn(&body[0], avail)) {
throwError("stream read failed");
}
}
auto rest = contentLength - avail; boost::system::error_code errorCode;
boost::system::error_code errorCode; read(socket, buffer(&body[avail], rest), errorCode); // read remaining part
read(socket, buffer(&body[avail], rest), errorCode);
socket.shutdown(errorCode);
}
socket.shutdown(errorCode);
}
}; };
} // detail } // namespace detail
// HttpsCall // HttpsCall
HttpsCall::HttpsCall(const std::string & host, uint16_t port) : HttpsCall::HttpsCall(const std::string &host, uint16_t port) :
m_Host(host), m_Host(host),
m_Port(port) 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;
} }
} // net bool HttpsCall::exec(const HttpRequest &request, HttpResponse *response) {
} // peerplays
// 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

View file

@ -60,9 +60,7 @@ public:
HttpsCall(const std::string & host, uint16_t port = 0); 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; return m_Host;
} }
@ -74,6 +72,8 @@ public:
return ResponseSizeLimitBytes; return ResponseSizeLimitBytes;
} }
bool exec(const HttpRequest & request, HttpResponse * response);
private: private:
std::string m_Host; std::string m_Host;
uint16_t m_Port; uint16_t m_Port;

View file

@ -2,27 +2,26 @@
#include <boost/asio.hpp> #include <boost/asio.hpp>
namespace peerplays { namespace peerplays {
namespace net { namespace net {
std::string resolveHostAddr(const std::string & hostName) { std::string resolveHostAddr(const std::string &hostName) {
using namespace boost::asio; using namespace boost::asio;
io_service service; io_service service;
ip::tcp::resolver resolver(service); ip::tcp::resolver resolver(service);
auto query = ip::tcp::resolver::query(hostName, ""); auto query = ip::tcp::resolver::query(hostName, std::string());
auto iter = resolver.resolve(query); auto iter = resolver.resolve(query);
auto endpoint = *iter; auto endpoint = *iter;
auto addr = ((ip::tcp::endpoint)endpoint).address(); auto addr = ((ip::tcp::endpoint)endpoint).address();
return addr.to_string(); return addr.to_string();
} }
std::string stripProtoName(const std::string & url) { std::string stripProtoName(const std::string &url) {
auto index = url.find("://"); auto index = url.find("://");
if (index == std::string::npos) if (index == std::string::npos)
return url; return url;
return url.substr(index + 3); return url.substr(index + 3);
} }
} // net }
} // peerplays } // namespace peerplays::net

View file

@ -5,7 +5,12 @@
namespace peerplays { namespace peerplays {
namespace net { namespace net {
// resolve IP address by host name
// ex: api.hive.blog -> 52.79.10.214
std::string resolveHostAddr(const std::string & hostName); 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); std::string stripProtoName(const std::string & url);

View file

@ -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 rpc_client::send_post_request(std::string body, bool show_log) {
fc::http::reply reply; fc::http::reply reply;
auto start = ip.substr(0, 6); auto start = ip.substr(0, 6);
boost::algorithm::to_lower(start); 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); HttpsCall call(host, port);
HttpRequest request("POST", "/", authorization.key + ":" + authorization.val, body); HttpRequest request("POST", "/", authorization.key + ":" + authorization.val, body);
HttpResponse response; HttpResponse response;
if (call.exec(request, &response)) { if (call.exec(request, &response)) {
reply.status = response.statusCode; reply.status = response.statusCode;
reply.body.resize(response.body.size()); reply.body.resize(response.body.size());
memcpy(&reply.body[0], &response.body[0], response.body.size()); memcpy(&reply.body[0], &response.body[0], response.body.size());
} }
if (show_log) { if (show_log) {
std::string url = ip + ":" + std::to_string(port); std::string url = ip + ":" + std::to_string(port);
ilog("### Request URL: ${url}", ("url", url)); ilog("### Request URL: ${url}", ("url", url));
ilog("### Request: ${body}", ("body", body)); ilog("### Request: ${body}", ("body", body));
ilog("### Response code: ${code}", ("code", response.statusCode)); ilog("### Response code: ${code}", ("code", response.statusCode));
ilog("### Response len: ${len}", ("len", response.body.size())); ilog("### Response len: ${len}", ("len", response.body.size()));
std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); std::stringstream ss(std::string(reply.body.begin(), reply.body.end()));
ilog("### Response body: ${ss}", ("ss", ss.str())); 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:/") std::string url = "http://" + host + ":" + std::to_string(port);
host = ip.substr(7); // skip "http://" fc::ip::address addr;
else
host = ip;
std::string url = "http://" + host + ":" + std::to_string(port); try {
fc::ip::address addr; 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 {
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 { fc::http::connection conn;
conn.connect_to(fc::ip::endpoint(addr, port));
fc::http::connection conn; //if (wallet.length() > 0) {
conn.connect_to(fc::ip::endpoint(addr, port)); // url = url + "/wallet/" + wallet;
//}
//if (wallet.length() > 0) { reply = conn.request("POST", url, body, fc::http::headers{authorization});
// url = url + "/wallet/" + wallet;
//}
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));
if (show_log) { std::stringstream ss(std::string(reply.body.begin(), reply.body.end()));
ilog("### Request URL: ${url}", ("url", url)); ilog("### Response: ${ss}", ("ss", ss.str()));
ilog("### Request: ${body}", ("body", body)); }
std::stringstream ss(std::string(reply.body.begin(), reply.body.end()));
ilog("### Response: ${ss}", ("ss", ss.str()));
}
return reply;
return reply;
} }
}} // namespace graphene::peerplays_sidechain }} // namespace graphene::peerplays_sidechain

View file

@ -150,18 +150,21 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi
fc::http::connection conn; fc::http::connection conn;
try { try {
auto host = peerplays::net::stripProtoName(node_ip); auto host = peerplays::net::stripProtoName(node_ip);
fc::ip::address addr; fc::ip::address addr;
try { try {
addr = fc::ip::address(host); // IP address assumed
} catch (...) { addr = fc::ip::address(host);
try { } catch (...) {
addr = fc::ip::address(peerplays::net::resolveHostAddr(host)); try {
} catch (...) { // host name assumed
elog("Failed to resolve Hive node address ${ip}", ("ip", node_ip)); addr = fc::ip::address(peerplays::net::resolveHostAddr(host));
FC_ASSERT(false); } 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)); conn.connect_to(fc::ip::endpoint(addr, node_rpc_port));
} catch (fc::exception &e) { } catch (fc::exception &e) {
elog("No Hive node running at ${ip} or wrong rpc port: ${port}", ("ip", node_ip)("port", node_rpc_port)); elog("No Hive node running at ${ip} or wrong rpc port: ${port}", ("ip", node_ip)("port", node_rpc_port));