Comments update and minor fixes
This commit is contained in:
parent
7ddad07df2
commit
1120e55f99
6 changed files with 269 additions and 265 deletions
|
|
@ -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,19 +16,18 @@ 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) :
|
||||||
Impl(const HttpsCall & call, const HttpRequest & request, HttpResponse & response) :
|
|
||||||
m_Call(call),
|
m_Call(call),
|
||||||
m_Request(request),
|
m_Request(request),
|
||||||
m_Response(response),
|
m_Response(response),
|
||||||
|
|
@ -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,10 +48,9 @@ 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;
|
|
||||||
|
|
||||||
io_service m_Service;
|
io_service m_Service;
|
||||||
ssl::context m_Context;
|
ssl::context m_Context;
|
||||||
|
|
@ -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;
|
||||||
|
|
||||||
|
|
@ -117,12 +113,13 @@ private:
|
||||||
|
|
||||||
// additional headers
|
// additional headers
|
||||||
|
|
||||||
const auto & h = m_Request.headers;
|
const auto &h = m_Request.headers;
|
||||||
|
|
||||||
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,12 +152,12 @@ 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
|
||||||
|
|
||||||
for(;;) {
|
for (;;) {
|
||||||
std::string header;
|
std::string header;
|
||||||
if (!std::getline(stream, header, Lf) || (header.size() == 1 && header[0] == Cr))
|
if (!std::getline(stream, header, Lf) || (header.size() == 1 && header[0] == Cr))
|
||||||
break;
|
break;
|
||||||
|
|
@ -178,19 +175,20 @@ private:
|
||||||
boost::algorithm::trim(value);
|
boost::algorithm::trim(value);
|
||||||
m_ContentLength = std::stol(value);
|
m_ContentLength = std::stol(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void processResponse() {
|
void processResponse() {
|
||||||
|
|
||||||
auto & socket = m_Socket;
|
auto &socket = m_Socket;
|
||||||
auto & buf = m_ResponseBuf;
|
auto &buf = m_ResponseBuf;
|
||||||
auto & contentLength = m_ContentLength;
|
auto &contentLength = m_ContentLength;
|
||||||
auto & body = m_Response.body;
|
auto &body = m_Response.body;
|
||||||
|
|
||||||
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,36 +206,35 @@ 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) {
|
||||||
|
|
||||||
// ASSERT(response);
|
// ASSERT(response);
|
||||||
auto & resp = *response;
|
auto &resp = *response;
|
||||||
|
|
||||||
detail::Impl impl(*this, request, resp);
|
detail::Impl impl(*this, request, resp);
|
||||||
|
|
||||||
|
|
@ -250,5 +249,5 @@ bool HttpsCall::exec(const HttpRequest & request, HttpResponse * response) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // net
|
}
|
||||||
} // peerplays
|
} // namespace peerplays::net
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue