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 <boost/asio.hpp>
#include <boost/asio/ssl.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/trim.hpp>
//#include <iostream>
@ -16,19 +16,18 @@ 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) {
[[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) :
Impl(const HttpsCall &call, const HttpRequest &request, HttpResponse &response) :
m_Call(call),
m_Request(request),
m_Response(response),
@ -37,8 +36,7 @@ public:
m_Socket(m_Service, m_Context),
m_Endpoint(),
m_ResponseBuf(call.responseSizeLimitBytes()),
m_ContentLength(0)
{
m_ContentLength(0) {
m_Context.set_default_verify_paths();
}
@ -50,10 +48,9 @@ public:
}
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;
@ -73,7 +70,6 @@ private:
if (m_Call.port() != 0) // if port was specified
m_Endpoint.port(m_Call.port()); // force set port
}
void connect() {
@ -93,7 +89,7 @@ private:
streambuf 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;
@ -117,12 +113,13 @@ private:
// additional headers
const auto & h = m_Request.headers;
const auto &h = m_Request.headers;
if (!h.empty()) {
if (h.size() < 2)
throw 1;
throwError("invalid headers data");
stream << h;
// ensure headers finished correctly
if ((h.substr(h.size() - 2) != CrLf))
stream << CrLf;
}
@ -146,7 +143,7 @@ private:
write(m_Socket, requestBuf);
}
void helper1() {
void processHeaders() {
std::istream stream(&m_ResponseBuf);
@ -155,12 +152,12 @@ private:
stream >> m_Response.statusCode;
if (!stream || httpVersion.substr(0, 5) != "HTTP/") {
throw "invalid response";
throwError("invalid response");
}
// read/skip headers
for(;;) {
for (;;) {
std::string header;
if (!std::getline(stream, header, Lf) || (header.size() == 1 && header[0] == Cr))
break;
@ -178,19 +175,20 @@ private:
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;
auto &socket = m_Socket;
auto &buf = m_ResponseBuf;
auto &contentLength = m_ContentLength;
auto &body = m_Response.body;
read_until(socket, buf, CrLfCrLf);
helper1();
processHeaders();
// check content length
if (contentLength < 2) // minimum content is "{}"
throwError("invalid response body (too short)");
@ -198,7 +196,9 @@ private:
if (contentLength > m_Call.responseSizeLimitBytes())
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)
throwError("invalid response body (content length mismatch)");
@ -206,36 +206,35 @@ private:
body.resize(contentLength);
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;
read(socket, buffer(&body[avail], rest), 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) :
HttpsCall::HttpsCall(const std::string &host, uint16_t port) :
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) {
// ASSERT(response);
auto & resp = *response;
// ASSERT(response);
auto &resp = *response;
detail::Impl impl(*this, request, resp);
@ -250,5 +249,5 @@ bool HttpsCall::exec(const HttpRequest & request, HttpResponse * response) {
return true;
}
} // net
} // peerplays
}
} // namespace peerplays::net

View file

@ -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;

View file

@ -2,27 +2,26 @@
#include <boost/asio.hpp>
namespace peerplays {
namespace net {
std::string resolveHostAddr(const std::string & hostName) {
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 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) {
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

View file

@ -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);

View file

@ -137,7 +137,6 @@ fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) {
}
return reply;
}
std::string host;
@ -188,7 +187,6 @@ fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) {
}
return reply;
}
}} // 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);
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));