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>
@ -27,7 +27,6 @@ using namespace boost::asio;
class Impl { class Impl {
public: public:
Impl(const HttpsCall &call, const HttpRequest &request, HttpResponse &response) : Impl(const HttpsCall &call, const HttpRequest &request, HttpResponse &response) :
m_Call(call), m_Call(call),
m_Request(request), m_Request(request),
@ -37,8 +36,7 @@ public:
m_Socket(m_Service, m_Context), m_Socket(m_Service, m_Context),
m_Endpoint(), m_Endpoint(),
m_ResponseBuf(call.responseSizeLimitBytes()), m_ResponseBuf(call.responseSizeLimitBytes()),
m_ContentLength(0) m_ContentLength(0) {
{
m_Context.set_default_verify_paths(); m_Context.set_default_verify_paths();
} }
@ -50,7 +48,6 @@ public:
} }
private: private:
const HttpsCall &m_Call; const HttpsCall &m_Call;
const HttpRequest &m_Request; const HttpRequest &m_Request;
HttpResponse &m_Response; HttpResponse &m_Response;
@ -73,7 +70,6 @@ private:
if (m_Call.port() != 0) // if port was specified if (m_Call.port() != 0) // if port was specified
m_Endpoint.port(m_Call.port()); // force set port m_Endpoint.port(m_Call.port()); // force set port
} }
void connect() { void connect() {
@ -93,7 +89,7 @@ private:
streambuf requestBuf; streambuf requestBuf;
std::ostream stream(&requestBuf); std::ostream stream(&requestBuf);
// start string: method path HTTP/1.0 // start string: <method> <path> HTTP/1.0
stream << m_Request.method << " " << m_Request.path << " HTTP/1.0" << CrLf; stream << m_Request.method << " " << m_Request.path << " HTTP/1.0" << CrLf;
@ -121,8 +117,9 @@ private:
if (!h.empty()) { if (!h.empty()) {
if (h.size() < 2) if (h.size() < 2)
throw 1; throwError("invalid headers data");
stream << h; stream << h;
// ensure headers finished correctly
if ((h.substr(h.size() - 2) != CrLf)) if ((h.substr(h.size() - 2) != CrLf))
stream << CrLf; stream << CrLf;
} }
@ -146,7 +143,7 @@ private:
write(m_Socket, requestBuf); write(m_Socket, requestBuf);
} }
void helper1() { void processHeaders() {
std::istream stream(&m_ResponseBuf); std::istream stream(&m_ResponseBuf);
@ -155,7 +152,7 @@ private:
stream >> m_Response.statusCode; stream >> m_Response.statusCode;
if (!stream || httpVersion.substr(0, 5) != "HTTP/") { if (!stream || httpVersion.substr(0, 5) != "HTTP/") {
throw "invalid response"; throwError("invalid response");
} }
// read/skip headers // read/skip headers
@ -178,7 +175,6 @@ private:
boost::algorithm::trim(value); boost::algorithm::trim(value);
m_ContentLength = std::stol(value); m_ContentLength = std::stol(value);
} }
} }
void processResponse() { void processResponse() {
@ -190,7 +186,9 @@ private:
read_until(socket, buf, CrLfCrLf); read_until(socket, buf, CrLfCrLf);
helper1(); processHeaders();
// check content length
if (contentLength < 2) // minimum content is "{}" if (contentLength < 2) // minimum content is "{}"
throwError("invalid response body (too short)"); throwError("invalid response body (too short)");
@ -198,7 +196,9 @@ private:
if (contentLength > m_Call.responseSizeLimitBytes()) if (contentLength > m_Call.responseSizeLimitBytes())
throwError("response body size limit exceeded"); throwError("response body size limit exceeded");
auto avail = buf.size(); // read body
auto avail = buf.size(); // size of body data already stored in the buffer
if (avail > contentLength) if (avail > contentLength)
throwError("invalid response body (content length mismatch)"); throwError("invalid response body (content length mismatch)");
@ -206,31 +206,30 @@ private:
body.resize(contentLength); body.resize(contentLength);
if (avail) { if (avail) {
// copy already existing data
if (avail != buf.sgetn(&body[0], avail)) { if (avail != buf.sgetn(&body[0], avail)) {
throwError("stream read failed"); 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); read(socket, buffer(&body[avail], rest), errorCode); // read remaining part
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) { bool HttpsCall::exec(const HttpRequest &request, HttpResponse *response) {
@ -250,5 +249,5 @@ bool HttpsCall::exec(const HttpRequest & request, HttpResponse * response) {
return true; return true;
} }
} // net }
} // peerplays } // 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,7 +2,6 @@
#include <boost/asio.hpp> #include <boost/asio.hpp>
namespace peerplays { namespace peerplays {
namespace net { namespace net {
@ -10,7 +9,7 @@ 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();
@ -24,5 +23,5 @@ std::string stripProtoName(const std::string & 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

@ -137,7 +137,6 @@ fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) {
} }
return reply; return reply;
} }
std::string host; std::string host;
@ -188,7 +187,6 @@ fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) {
} }
return reply; return reply;
} }
}} // namespace graphene::peerplays_sidechain }} // namespace graphene::peerplays_sidechain

View file

@ -153,15 +153,18 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi
auto host = peerplays::net::stripProtoName(node_ip); auto host = peerplays::net::stripProtoName(node_ip);
fc::ip::address addr; fc::ip::address addr;
try { try {
// IP address assumed
addr = fc::ip::address(host); addr = fc::ip::address(host);
} catch (...) { } catch (...) {
try { try {
// host name assumed
addr = fc::ip::address(peerplays::net::resolveHostAddr(host)); addr = fc::ip::address(peerplays::net::resolveHostAddr(host));
} catch (...) { } catch (...) {
elog("Failed to resolve Hive node address ${ip}", ("ip", node_ip)); elog("Failed to resolve Hive node address ${ip}", ("ip", node_ip));
FC_ASSERT(false); 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));