Applying comments

This commit is contained in:
moss9001 2021-10-22 18:45:16 +03:00
parent f9cfd54295
commit 32987edb2a
8 changed files with 343 additions and 377 deletions

View file

@ -15,7 +15,6 @@ add_library( peerplays_sidechain
bitcoin/utils.cpp
bitcoin/sign_bitcoin_transaction.cpp
common/rpc_client.cpp
common/https_call.cpp
common/net_utl.cpp
common/utils.cpp
hive/asset.cpp

View file

@ -1,253 +0,0 @@
#include "https_call.h"
#include <boost/asio.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/algorithm/string/trim.hpp>
//#include <iostream>
namespace peerplays {
namespace net {
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";
using namespace boost::asio;
[[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();
}
void exec() {
resolve();
connect();
sendRequest();
processResponse();
}
private:
const HttpsCall &m_Call;
const HttpRequest &m_Request;
HttpResponse &m_Response;
io_service m_Service;
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
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
}
void connect() {
// TCP connect
m_Socket.lowest_layer().connect(m_Endpoint);
// SSL connect
m_Socket.set_verify_mode(ssl::verify_peer);
m_Socket.handshake(ssl::stream_base::client);
}
void sendRequest() {
streambuf requestBuf;
std::ostream stream(&requestBuf);
// start string: <method> <path> HTTP/1.0
stream << m_Request.method << " " << m_Request.path << " HTTP/1.0" << CrLf;
// host
stream << "Host: " << m_Call.host();
if (m_Call.port() != 0) {
//ASSERT(m_Endpoint.port() == m_Call.port());
stream << ":" << m_Call.port();
}
stream << CrLf;
// content
if (!m_Request.body.empty()) {
stream << "Content-Type: " << m_Request.contentType << CrLf;
stream << "Content-Length: " << m_Request.body.size() << CrLf;
}
// additional headers
const auto &h = m_Request.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;
}
// other
stream << "Accept: *\x2F*" << CrLf;
stream << "Connection: close" << CrLf;
// end
stream << CrLf;
// content
if (!m_Request.body.empty())
stream << m_Request.body;
// send
write(m_Socket, requestBuf);
}
void processHeaders() {
std::istream stream(&m_ResponseBuf);
std::string httpVersion;
stream >> httpVersion;
stream >> m_Response.statusCode;
if (!stream || httpVersion.substr(0, 5) != "HTTP/") {
throwError("invalid response");
}
// 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);
}
}
void processResponse() {
auto &socket = m_Socket;
auto &buf = m_ResponseBuf;
auto &contentLength = m_ContentLength;
auto &body = m_Response.body;
read_until(socket, buf, CrLfCrLf);
processHeaders();
// check content length
if (contentLength < 2) // minimum content is "{}"
throwError("invalid response body (too short)");
if (contentLength > m_Call.responseSizeLimitBytes())
throwError("response body size limit exceeded");
// read body
auto avail = buf.size(); // size of body data already stored in the buffer
if (avail > 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");
}
}
auto rest = contentLength - avail; // size of remaining part of response body
boost::system::error_code errorCode;
read(socket, buffer(&body[avail], rest), errorCode); // read remaining part
socket.shutdown(errorCode);
}
};
} // 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;
}
}
} // namespace peerplays::net

View file

@ -1,84 +0,0 @@
#pragma once
#include <string>
namespace peerplays {
namespace net {
struct HttpRequest {
std::string method; // ex: "POST"
std::string path; // ex: "/"
std::string headers;
std::string body;
std::string contentType; // ex: "application/json"
HttpRequest() {}
HttpRequest(const std::string & method_, const std::string & path_, const std::string & headers_, const std::string & body_, const std::string & contentType_) :
method(method_),
path(path_),
headers(headers_),
body(body_),
contentType(contentType_)
{}
HttpRequest(const std::string & method_, const std::string & path_, const std::string & headers_, const std::string & body_ = std::string()) :
method(method_),
path(path_),
headers(headers_),
body(body_),
contentType("application/json")
{}
void clear() {
method.clear();
path.clear();
headers.clear();
body.clear();
contentType.clear();
}
};
struct HttpResponse {
uint16_t statusCode;
std::string body;
void clear() {
statusCode = 0;
body = decltype(body)();
}
};
class HttpsCall {
public:
static constexpr auto ResponseSizeLimitBytes = 1024 * 1024;
HttpsCall(const std::string & host, uint16_t port = 0);
const std::string & host() const {
return m_Host;
}
uint16_t port() const {
return m_Port;
}
uint32_t responseSizeLimitBytes() const {
return ResponseSizeLimitBytes;
}
bool exec(const HttpRequest & request, HttpResponse * response);
private:
std::string m_Host;
uint16_t m_Port;
};
} // net
} // peerplays

View file

@ -1,27 +1,25 @@
#include "net_utl.h"
#include "net_utl.hpp"
#include <boost/asio.hpp>
namespace peerplays {
namespace net {
namespace graphene { namespace peerplays_sidechain {
std::string resolveHostAddr(const std::string &hostName) {
std::string resolve_host_addr(const std::string &host_name) {
using namespace boost::asio;
io_service service;
ip::tcp::resolver resolver(service);
auto query = ip::tcp::resolver::query(hostName, std::string());
auto query = ip::tcp::resolver::query(host_name, 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) {
std::string strip_proto_name(const std::string &url) {
auto index = url.find("://");
if (index == std::string::npos)
return url;
return url.substr(index + 3);
}
}
} // namespace peerplays::net
}} // namespace graphene::peerplays_sidechain

View file

@ -1,18 +0,0 @@
#pragma once
#include <string>
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);
} // net
} // peerplays

View file

@ -0,0 +1,15 @@
#pragma once
#include <string>
namespace graphene { namespace peerplays_sidechain {
// resolve IP address by host name
// ex: api.hive.blog -> 52.79.10.214
std::string resolve_host_addr(const std::string &host_name);
// remove schema part from URL
// ex: http://api.hive.blog -> api.hive.blog
std::string strip_proto_name(const std::string &url);
}} // namespace graphene::peerplays_sidechain

View file

@ -2,6 +2,10 @@
#include <sstream>
#include <boost/asio.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
@ -10,9 +14,316 @@
#include <fc/network/ip.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/algorithm/string/trim.hpp>
#include "https_call.h"
#include "net_utl.h"
#include "net_utl.hpp"
namespace graphene { namespace peerplays_sidechain {
struct http_request {
std::string method; // ex: "POST"
std::string path; // ex: "/"
std::string headers;
std::string body;
std::string content_type; // ex: "application/json"
http_request() {
}
http_request(const std::string &method_, const std::string &path_, const std::string &headers_, const std::string &body_, const std::string &content_type_) :
method(method_),
path(path_),
headers(headers_),
body(body_),
content_type(content_type_) {
}
http_request(const std::string &method_, const std::string &path_, const std::string &headers_, const std::string &body_ = std::string()) :
method(method_),
path(path_),
headers(headers_),
body(body_),
content_type("application/json") {
}
void clear() {
method.clear();
path.clear();
headers.clear();
body.clear();
content_type.clear();
}
};
struct http_response {
uint16_t status_code;
std::string body;
void clear() {
status_code = 0;
body = decltype(body)();
}
};
class https_call {
public:
https_call(const std::string &host, uint16_t port = 0) :
m_host(host),
m_port(port) {
}
const std::string &host() const {
return m_host;
}
uint16_t port() const {
return m_port;
}
uint32_t response_size_limit_bytes() const {
return 1024 * 1024;
}
bool exec(const http_request &request, http_response *response);
private:
std::string m_host;
uint16_t m_port;
};
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";
using namespace boost::asio;
[[noreturn]] static void throw_error(const std::string &msg) {
throw std::runtime_error(msg);
}
class https_call_impl {
public:
https_call_impl(const https_call &call, const http_request &request, http_response &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_response_buf(call.response_size_limit_bytes()),
m_content_length(0) {
m_context.set_default_verify_paths();
}
void exec() {
resolve();
connect();
send_request();
process_response();
}
private:
const https_call &m_call;
const http_request &m_request;
http_response &m_response;
io_service m_service;
ssl::context m_context;
ssl::stream<ip::tcp::socket> m_socket;
ip::tcp::endpoint m_endpoint;
streambuf m_response_buf;
uint32_t m_content_length;
void resolve() {
// 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;
if (m_call.port() != 0) // if port was specified
m_endpoint.port(m_call.port()); // force set port
}
void connect() {
// TCP connect
m_socket.lowest_layer().connect(m_endpoint);
// SSL connect
m_socket.set_verify_mode(ssl::verify_peer);
m_socket.handshake(ssl::stream_base::client);
}
void send_request() {
streambuf request_buf;
std::ostream stream(&request_buf);
// start string: <method> <path> HTTP/1.0
stream << m_request.method << " " << m_request.path << " HTTP/1.0" << crlf;
// host
stream << "Host: " << m_call.host();
if (m_call.port() != 0) {
//ASSERT(m_Endpoint.port() == m_Call.port());
stream << ":" << m_call.port();
}
stream << crlf;
// content
if (!m_request.body.empty()) {
stream << "Content-Type: " << m_request.content_type << crlf;
stream << "Content-Length: " << m_request.body.size() << crlf;
}
// additional headers
const auto &h = m_request.headers;
if (!h.empty()) {
if (h.size() < 2)
throw_error("invalid headers data");
stream << h;
// ensure headers finished correctly
if ((h.substr(h.size() - 2) != crlf))
stream << crlf;
}
// other
stream << "Accept: *\x2F*" << crlf;
stream << "Connection: close" << crlf;
// end
stream << crlf;
// content
if (!m_request.body.empty())
stream << m_request.body;
// send
write(m_socket, request_buf);
}
void process_headers() {
std::istream stream(&m_response_buf);
std::string http_version;
stream >> http_version;
stream >> m_response.status_code;
if (!stream || http_version.substr(0, 5) != "HTTP/") {
throw_error("invalid response");
}
// read/skip headers
for (;;) {
std::string header;
if (!std::getline(stream, header, lf) || (header.size() == 1 && header[0] == cr))
break;
if (m_content_length) // 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_content_length = std::stol(value);
}
}
void process_response() {
auto &socket = m_socket;
auto &buf = m_response_buf;
auto &content_length = m_content_length;
auto &body = m_response.body;
read_until(socket, buf, crlfcrlf);
process_headers();
// check content length
if (content_length < 2) // minimum content is "{}"
throw_error("invalid response body (too short)");
if (content_length > m_call.response_size_limit_bytes())
throw_error("response body size limit exceeded");
// read body
auto avail = buf.size(); // size of body data already stored in the buffer
if (avail > content_length)
throw_error("invalid response body (content length mismatch)");
body.resize(content_length);
if (avail) {
// copy already existing data
if (avail != buf.sgetn(&body[0], avail)) {
throw_error("stream read failed");
}
}
auto rest = content_length - avail; // size of remaining part of response body
boost::system::error_code error_code;
read(socket, buffer(&body[avail], rest), error_code); // read remaining part
socket.shutdown(error_code);
}
};
} // namespace detail
bool https_call::exec(const http_request &request, http_response *response) {
// ASSERT(response);
auto &resp = *response;
detail::https_call_impl impl(*this, request, resp);
try {
resp.clear();
impl.exec();
} catch (...) {
resp.clear();
return false;
}
return true;
}
}} // namespace graphene::peerplays_sidechain
namespace graphene { namespace peerplays_sidechain {
@ -114,14 +425,12 @@ fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) {
auto host = ip.substr(8); // skip "https://"
using namespace peerplays::net;
HttpsCall call(host, port);
HttpRequest request("POST", "/", authorization.key + ":" + authorization.val, body);
HttpResponse response;
https_call call(host, port);
http_request request("POST", "/", authorization.key + ":" + authorization.val, body);
http_response response;
if (call.exec(request, &response)) {
reply.status = response.statusCode;
reply.status = response.status_code;
reply.body.resize(response.body.size());
memcpy(&reply.body[0], &response.body[0], response.body.size());
}
@ -130,7 +439,7 @@ fc::http::reply rpc_client::send_post_request(std::string body, bool 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 code: ${code}", ("code", response.status_code));
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()));
@ -153,7 +462,7 @@ fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) {
addr = fc::ip::address(host);
} catch (...) {
try {
addr = fc::ip::address(peerplays::net::resolveHostAddr(host));
addr = fc::ip::address(resolve_host_addr(host));
} catch (...) {
if (show_log) {
std::string url = ip + ":" + std::to_string(port);

View file

@ -28,7 +28,7 @@
#include <graphene/peerplays_sidechain/hive/transaction.hpp>
#include <graphene/utilities/key_conversion.hpp>
#include "common/net_utl.h"
#include "common/net_utl.hpp"
namespace graphene { namespace peerplays_sidechain {
@ -150,7 +150,7 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi
fc::http::connection conn;
try {
auto host = peerplays::net::stripProtoName(node_ip);
auto host = strip_proto_name(node_ip);
fc::ip::address addr;
try {
// IP address assumed
@ -158,7 +158,7 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi
} catch (...) {
try {
// host name assumed
addr = fc::ip::address(peerplays::net::resolveHostAddr(host));
addr = fc::ip::address(resolve_host_addr(host));
} catch (...) {
elog("Failed to resolve Hive node address ${ip}", ("ip", node_ip));
FC_ASSERT(false);