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 <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>
|
||||
|
||||
|
|
@ -27,7 +27,6 @@ using namespace boost::asio;
|
|||
|
||||
class Impl {
|
||||
public:
|
||||
|
||||
Impl(const HttpsCall &call, const HttpRequest &request, HttpResponse &response) :
|
||||
m_Call(call),
|
||||
m_Request(request),
|
||||
|
|
@ -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,7 +48,6 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
|
||||
const HttpsCall &m_Call;
|
||||
const HttpRequest &m_Request;
|
||||
HttpResponse &m_Response;
|
||||
|
|
@ -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;
|
||||
|
||||
|
|
@ -121,8 +117,9 @@ private:
|
|||
|
||||
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,7 +152,7 @@ private:
|
|||
stream >> m_Response.statusCode;
|
||||
|
||||
if (!stream || httpVersion.substr(0, 5) != "HTTP/") {
|
||||
throw "invalid response";
|
||||
throwError("invalid response");
|
||||
}
|
||||
|
||||
// read/skip headers
|
||||
|
|
@ -178,7 +175,6 @@ private:
|
|||
boost::algorithm::trim(value);
|
||||
m_ContentLength = std::stol(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void processResponse() {
|
||||
|
|
@ -190,7 +186,9 @@ private:
|
|||
|
||||
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,31 +206,30 @@ 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) :
|
||||
m_Host(host),
|
||||
m_Port(port)
|
||||
{}
|
||||
m_Port(port) {
|
||||
}
|
||||
|
||||
bool HttpsCall::exec(const HttpRequest &request, HttpResponse *response) {
|
||||
|
||||
|
|
@ -250,5 +249,5 @@ bool HttpsCall::exec(const HttpRequest & request, HttpResponse * response) {
|
|||
return true;
|
||||
}
|
||||
|
||||
} // net
|
||||
} // peerplays
|
||||
}
|
||||
} // namespace peerplays::net
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
#include <boost/asio.hpp>
|
||||
|
||||
|
||||
namespace peerplays {
|
||||
namespace net {
|
||||
|
||||
|
|
@ -10,7 +9,7 @@ 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();
|
||||
|
|
@ -24,5 +23,5 @@ std::string stripProtoName(const std::string & url) {
|
|||
return url.substr(index + 3);
|
||||
}
|
||||
|
||||
} // net
|
||||
} // peerplays
|
||||
}
|
||||
} // namespace peerplays::net
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
|
|
|||
Loading…
Reference in a new issue