peerplays_migrated/libraries/plugins/peerplays_sidechain/common/https_call.cpp

254 lines
5.8 KiB
C++
Raw Normal View History

2021-10-18 11:42:52 +00:00
#include "https_call.h"
#include <boost/asio.hpp>
#include <boost/asio/buffer.hpp>
2021-10-22 10:34:56 +00:00
#include <boost/asio/ssl.hpp>
2021-10-18 11:42:52 +00:00
#include <boost/algorithm/string/case_conv.hpp>
2021-10-22 10:34:56 +00:00
#include <boost/algorithm/string/trim.hpp>
2021-10-18 11:42:52 +00:00
2021-10-20 21:39:43 +00:00
//#include <iostream>
2021-10-18 11:42:52 +00:00
namespace peerplays {
namespace net {
namespace detail {
static const char Cr = 0x0D;
static const char Lf = 0x0A;
2021-10-22 10:34:56 +00:00
static const char *CrLf = "\x0D\x0A";
static const char *CrLfCrLf = "\x0D\x0A\x0D\x0A";
2021-10-18 11:42:52 +00:00
using namespace boost::asio;
2021-10-22 10:34:56 +00:00
[[noreturn]] static void throwError(const std::string &msg) {
throw std::runtime_error(msg);
2021-10-18 11:42:52 +00:00
}
class Impl {
public:
2021-10-22 10:34:56 +00:00
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();
}
2021-10-18 11:42:52 +00:00
private:
2021-10-22 10:34:56 +00:00
const HttpsCall &m_Call;
const HttpRequest &m_Request;
HttpResponse &m_Response;
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
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;
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
void resolve() {
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
// resolve TCP endpoint for host name
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
ip::tcp::resolver resolver(m_Service);
auto query = ip::tcp::resolver::query(m_Call.host(), "https");
auto iter = resolver.resolve(query);
m_Endpoint = *iter;
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
if (m_Call.port() != 0) // if port was specified
m_Endpoint.port(m_Call.port()); // force set port
}
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
void connect() {
2021-10-20 21:39:43 +00:00
2021-10-22 10:34:56 +00:00
// TCP connect
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
m_Socket.lowest_layer().connect(m_Endpoint);
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
// SSL connect
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
m_Socket.set_verify_mode(ssl::verify_peer);
m_Socket.handshake(ssl::stream_base::client);
}
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
void sendRequest() {
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
streambuf requestBuf;
std::ostream stream(&requestBuf);
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
// start string: <method> <path> HTTP/1.0
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
stream << m_Request.method << " " << m_Request.path << " HTTP/1.0" << CrLf;
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
// host
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
stream << "Host: " << m_Call.host();
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
if (m_Call.port() != 0) {
//ASSERT(m_Endpoint.port() == m_Call.port());
stream << ":" << m_Call.port();
}
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
stream << CrLf;
2021-10-21 14:47:46 +00:00
2021-10-22 10:34:56 +00:00
// content
2021-10-21 14:47:46 +00:00
2021-10-22 10:34:56 +00:00
if (!m_Request.body.empty()) {
stream << "Content-Type: " << m_Request.contentType << CrLf;
stream << "Content-Length: " << m_Request.body.size() << CrLf;
}
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
// additional headers
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
const auto &h = m_Request.headers;
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
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;
}
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
// other
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
stream << "Accept: *\x2F*" << CrLf;
stream << "Connection: close" << CrLf;
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
// end
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
stream << CrLf;
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
// content
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
if (!m_Request.body.empty())
stream << m_Request.body;
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
// send
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
write(m_Socket, requestBuf);
}
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
void processHeaders() {
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
std::istream stream(&m_ResponseBuf);
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
std::string httpVersion;
stream >> httpVersion;
stream >> m_Response.statusCode;
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
if (!stream || httpVersion.substr(0, 5) != "HTTP/") {
throwError("invalid response");
}
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
// read/skip headers
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
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);
}
}
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
void processResponse() {
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
auto &socket = m_Socket;
auto &buf = m_ResponseBuf;
auto &contentLength = m_ContentLength;
auto &body = m_Response.body;
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
read_until(socket, buf, CrLfCrLf);
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
processHeaders();
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
// check content length
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
if (contentLength < 2) // minimum content is "{}"
throwError("invalid response body (too short)");
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
if (contentLength > m_Call.responseSizeLimitBytes())
throwError("response body size limit exceeded");
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
// read body
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
auto avail = buf.size(); // size of body data already stored in the buffer
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
if (avail > contentLength)
throwError("invalid response body (content length mismatch)");
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
body.resize(contentLength);
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
if (avail) {
// copy already existing data
if (avail != buf.sgetn(&body[0], avail)) {
throwError("stream read failed");
}
}
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
auto rest = contentLength - avail; // size of remaining part of response body
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
boost::system::error_code errorCode;
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
read(socket, buffer(&body[avail], rest), errorCode); // read remaining part
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
socket.shutdown(errorCode);
}
2021-10-18 11:42:52 +00:00
};
2021-10-22 10:34:56 +00:00
} // namespace detail
2021-10-18 11:42:52 +00:00
// HttpsCall
2021-10-22 10:34:56 +00:00
HttpsCall::HttpsCall(const std::string &host, uint16_t port) :
m_Host(host),
m_Port(port) {
}
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
bool HttpsCall::exec(const HttpRequest &request, HttpResponse *response) {
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
// ASSERT(response);
auto &resp = *response;
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
detail::Impl impl(*this, request, resp);
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
try {
resp.clear();
impl.exec();
} catch (...) {
resp.clear();
return false;
}
2021-10-18 11:42:52 +00:00
2021-10-22 10:34:56 +00:00
return true;
2021-10-18 11:42:52 +00:00
}
2021-10-22 10:34:56 +00:00
}
} // namespace peerplays::net