mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-26 22:55:53 +00:00
270
unittests/util/TestHttpSyncClient.hpp
Normal file
270
unittests/util/TestHttpSyncClient.hpp
Normal file
@@ -0,0 +1,270 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of clio: https://github.com/XRPLF/clio
|
||||
Copyright (c) 2023, the clio developers.
|
||||
|
||||
Permission to use, copy, modify, and distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <boost/asio/buffer.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/ssl/context.hpp>
|
||||
#include <boost/asio/ssl/error.hpp>
|
||||
#include <boost/asio/ssl/stream_base.hpp>
|
||||
#include <boost/asio/ssl/verify_context.hpp>
|
||||
#include <boost/asio/ssl/verify_mode.hpp>
|
||||
#include <boost/beast/core/buffers_to_string.hpp>
|
||||
#include <boost/beast/core/error.hpp>
|
||||
#include <boost/beast/core/flat_buffer.hpp>
|
||||
#include <boost/beast/core/stream_traits.hpp>
|
||||
#include <boost/beast/core/tcp_stream.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/http/field.hpp>
|
||||
#include <boost/beast/http/message.hpp>
|
||||
#include <boost/beast/http/string_body.hpp>
|
||||
#include <boost/beast/http/verb.hpp>
|
||||
#include <boost/beast/ssl/ssl_stream.hpp>
|
||||
#include <boost/beast/version.hpp>
|
||||
#include <boost/beast/websocket/rfc6455.hpp>
|
||||
#include <boost/beast/websocket/stream.hpp>
|
||||
#include <boost/beast/websocket/stream_base.hpp>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/tls1.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace http = boost::beast::http;
|
||||
namespace net = boost::asio;
|
||||
namespace ssl = boost::asio::ssl;
|
||||
using tcp = boost::asio::ip::tcp;
|
||||
|
||||
struct WebHeader {
|
||||
WebHeader(http::field name, std::string value) : name(name), value(std::move(value))
|
||||
{
|
||||
}
|
||||
http::field name;
|
||||
std::string value;
|
||||
};
|
||||
|
||||
struct HttpSyncClient {
|
||||
static std::string
|
||||
syncPost(
|
||||
std::string const& host,
|
||||
std::string const& port,
|
||||
std::string const& body,
|
||||
std::vector<WebHeader> additionalHeaders = {}
|
||||
)
|
||||
{
|
||||
return syncRequest(host, port, body, std::move(additionalHeaders), http::verb::post);
|
||||
}
|
||||
|
||||
static std::string
|
||||
syncGet(
|
||||
std::string const& host,
|
||||
std::string const& port,
|
||||
std::string const& body,
|
||||
std::string const& target,
|
||||
std::vector<WebHeader> additionalHeaders = {}
|
||||
)
|
||||
{
|
||||
return syncRequest(host, port, body, std::move(additionalHeaders), http::verb::get, target);
|
||||
}
|
||||
|
||||
private:
|
||||
static std::string
|
||||
syncRequest(
|
||||
std::string const& host,
|
||||
std::string const& port,
|
||||
std::string const& body,
|
||||
std::vector<WebHeader> additionalHeaders,
|
||||
http::verb method,
|
||||
std::string target = "/"
|
||||
)
|
||||
{
|
||||
boost::asio::io_context ioc;
|
||||
|
||||
net::ip::tcp::resolver resolver(ioc);
|
||||
boost::beast::tcp_stream stream(ioc);
|
||||
|
||||
auto const results = resolver.resolve(host, port);
|
||||
stream.connect(results);
|
||||
|
||||
http::request<http::string_body> req{method, "/", 10};
|
||||
req.set(http::field::host, host);
|
||||
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
|
||||
|
||||
for (auto& header : additionalHeaders) {
|
||||
req.set(header.name, header.value);
|
||||
}
|
||||
|
||||
req.target(target);
|
||||
req.body() = std::string(body);
|
||||
req.prepare_payload();
|
||||
http::write(stream, req);
|
||||
|
||||
boost::beast::flat_buffer buffer;
|
||||
http::response<http::string_body> res;
|
||||
http::read(stream, buffer, res);
|
||||
|
||||
boost::beast::error_code ec;
|
||||
stream.socket().shutdown(tcp::socket::shutdown_both, ec);
|
||||
|
||||
return res.body();
|
||||
}
|
||||
};
|
||||
|
||||
class WebSocketSyncClient {
|
||||
net::io_context ioc_;
|
||||
tcp::resolver resolver_{ioc_};
|
||||
boost::beast::websocket::stream<tcp::socket> ws_{ioc_};
|
||||
|
||||
public:
|
||||
void
|
||||
connect(std::string const& host, std::string const& port, std::vector<WebHeader> additionalHeaders = {})
|
||||
{
|
||||
auto const results = resolver_.resolve(host, port);
|
||||
auto const ep = net::connect(ws_.next_layer(), results);
|
||||
|
||||
// Update the host_ string. This will provide the value of the
|
||||
// Host HTTP header during the WebSocket handshake.
|
||||
// See https://tools.ietf.org/html/rfc7230#section-5.4
|
||||
auto const hostPort = host + ':' + std::to_string(ep.port());
|
||||
|
||||
ws_.set_option(boost::beast::websocket::stream_base::decorator([additionalHeaders = std::move(additionalHeaders
|
||||
)](boost::beast::websocket::request_type& req) {
|
||||
req.set(http::field::user_agent, std::string(BOOST_BEAST_VERSION_STRING) + " websocket-client-coro");
|
||||
for (auto const& header : additionalHeaders) {
|
||||
req.set(header.name, header.value);
|
||||
}
|
||||
}));
|
||||
|
||||
ws_.handshake(hostPort, "/");
|
||||
}
|
||||
|
||||
void
|
||||
disconnect()
|
||||
{
|
||||
ws_.close(boost::beast::websocket::close_code::normal);
|
||||
}
|
||||
|
||||
std::string
|
||||
syncPost(std::string const& body)
|
||||
{
|
||||
boost::beast::flat_buffer buffer;
|
||||
|
||||
ws_.write(net::buffer(std::string(body)));
|
||||
ws_.read(buffer);
|
||||
|
||||
return boost::beast::buffers_to_string(buffer.data());
|
||||
}
|
||||
};
|
||||
|
||||
struct HttpsSyncClient {
|
||||
static bool
|
||||
verify_certificate(bool /* preverified */, boost::asio::ssl::verify_context& /* ctx */)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
static std::string
|
||||
syncPost(std::string const& host, std::string const& port, std::string const& body)
|
||||
{
|
||||
net::io_context ioc;
|
||||
boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);
|
||||
ctx.set_default_verify_paths();
|
||||
ctx.set_verify_mode(ssl::verify_none);
|
||||
|
||||
tcp::resolver resolver(ioc);
|
||||
boost::beast::ssl_stream<boost::beast::tcp_stream> stream(ioc, ctx);
|
||||
|
||||
// We can't fix this so have to ignore
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wold-style-cast"
|
||||
if (!SSL_set_tlsext_host_name(stream.native_handle(), host.c_str()))
|
||||
#pragma GCC diagnostic pop
|
||||
{
|
||||
boost::beast::error_code const ec{static_cast<int>(::ERR_get_error()), net::error::get_ssl_category()};
|
||||
throw boost::beast::system_error{ec};
|
||||
}
|
||||
|
||||
auto const results = resolver.resolve(host, port);
|
||||
boost::beast::get_lowest_layer(stream).connect(results);
|
||||
stream.handshake(ssl::stream_base::client);
|
||||
|
||||
http::request<http::string_body> req{http::verb::post, "/", 10};
|
||||
req.set(http::field::host, host);
|
||||
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
|
||||
req.body() = std::string(body);
|
||||
req.prepare_payload();
|
||||
http::write(stream, req);
|
||||
|
||||
boost::beast::flat_buffer buffer;
|
||||
http::response<http::string_body> res;
|
||||
http::read(stream, buffer, res);
|
||||
|
||||
boost::beast::error_code ec;
|
||||
stream.shutdown(ec);
|
||||
|
||||
return res.body();
|
||||
}
|
||||
};
|
||||
|
||||
class WebServerSslSyncClient {
|
||||
net::io_context ioc_;
|
||||
std::optional<boost::beast::websocket::stream<boost::beast::ssl_stream<tcp::socket>>> ws_;
|
||||
|
||||
public:
|
||||
void
|
||||
connect(std::string const& host, std::string const& port)
|
||||
{
|
||||
boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);
|
||||
ctx.set_default_verify_paths();
|
||||
ctx.set_verify_mode(ssl::verify_none);
|
||||
|
||||
tcp::resolver resolver{ioc_};
|
||||
ws_.emplace(ioc_, ctx);
|
||||
|
||||
auto const results = resolver.resolve(host, port);
|
||||
net::connect(ws_->next_layer().next_layer(), results.begin(), results.end());
|
||||
ws_->next_layer().handshake(ssl::stream_base::client);
|
||||
|
||||
ws_->set_option(boost::beast::websocket::stream_base::decorator([](boost::beast::websocket::request_type& req) {
|
||||
req.set(http::field::user_agent, std::string(BOOST_BEAST_VERSION_STRING) + " websocket-client-coro");
|
||||
}));
|
||||
|
||||
ws_->handshake(host, "/");
|
||||
}
|
||||
|
||||
void
|
||||
disconnect()
|
||||
{
|
||||
ws_->close(boost::beast::websocket::close_code::normal);
|
||||
}
|
||||
|
||||
std::string
|
||||
syncPost(std::string const& body)
|
||||
{
|
||||
boost::beast::flat_buffer buffer;
|
||||
ws_->write(net::buffer(std::string(body)));
|
||||
ws_->read(buffer);
|
||||
|
||||
return boost::beast::buffers_to_string(buffer.data());
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user