3#include "type_traits.hpp"
4#include "http/connection_plain.hpp"
5#include "websocket/connection.hpp"
6#include "../core/controller.hpp"
7#include "../core/error.hpp"
8#include "../core/detail/controller_run_result.hpp"
9#include "../core/http/request.hpp"
10#include "../core/http/response.hpp"
11#include "../core/http/type_traits.hpp"
12#include "../core/http/url.hpp"
13#include "../core/http/utils.hpp"
14#include "../core/tcp/stream.hpp"
16 #include "http/connection_tls.hpp"
18 #include <boost/beast/ssl.hpp>
21#include <boost/asio/strand.hpp>
22#include <spdlog/logger.h>
26namespace boost::asio::ssl
31namespace malloy::client
35 class connection_plain;
50 using header_type = boost::beast::http::response_header<>;
51 using value_type = std::string;
54 std::variant<boost::beast::http::string_body>
55 body_for(
const header_type&)
const
61 setup_body(
const header_type&, std::string&)
const
112#if MALLOY_FEATURE_TLS
118 [[nodiscard(
"init might fail")]]
142 std::future<malloy::error_code>
149 return make_http_connection<false>(std::move(req), std::forward<Callback>(done), std::move(filter));
172 concepts::response_filter Filter = detail::default_resp_filter
174 requires concepts::http_callback<Callback, Filter>
176 std::future<malloy::error_code>
179 const std::string_view url,
184 auto req = malloy::http::build_request<ReqBody>(method_, url);
189 ec.assign(0, boost::beast::generic_category());
190 std::promise<malloy::error_code> p;
191 p.set_value(std::forward<malloy::error_code>(ec));
192 return p.get_future();
196#if MALLOY_FEATURE_TLS
198 return make_http_connection<true>(std::move(*req), std::forward<Callback>(done), std::move(filter));
201 return make_http_connection<false>(std::move(*req), std::forward<Callback>(done), std::move(filter));
204#if MALLOY_FEATURE_TLS
213 concepts::response_filter Filter = detail::default_resp_filter
215 requires concepts::http_callback<Callback, Filter>
217 std::future<malloy::error_code>
224 return make_http_connection<true>(std::move(req), std::forward<Callback>(done), std::move(filter));
235 const std::string& host,
237 const std::string& resource,
238 std::invocable<
malloy::error_code, std::shared_ptr<websocket::connection>>
auto&& handler
244 make_ws_connection<true>(host, port, resource, std::forward<
decltype(handler)>(handler));
265 add_ca(
const std::string& contents);
286 const std::string& host,
288 const std::string& resource,
289 std::invocable<
malloy::error_code, std::shared_ptr<websocket::connection>>
auto&& handler
293 make_ws_connection<false>(host, port, resource, std::forward<
decltype(handler)>(handler));
297 std::shared_ptr<boost::asio::ssl::context> m_tls_ctx{
nullptr};
298 std::unique_ptr<boost::asio::io_context> m_ioc_sm{std::make_unique<boost::asio::io_context>()};
299 boost::asio::io_context* m_ioc{m_ioc_sm.get()};
305 start(controller& ctrl)
307 return session{ctrl.m_cfg, ctrl.m_tls_ctx, std::move(ctrl.m_ioc_sm)};
319 throw std::logic_error(
"TLS context not initialized.");
328 std::future<malloy::error_code>
331 std::promise<malloy::error_code> prom;
332 auto err_channel = prom.get_future();
333 [req,
this](
auto&& cb) {
334#if MALLOY_FEATURE_TLS
335 if constexpr (isHttps) {
337 auto conn = std::make_shared<http::connection_tls<Body, Filter, std::decay_t<Callback>>>(
338 m_cfg.logger->clone(m_cfg.logger->name() +
" | HTTPS connection"),
346 const std::string hostname{ req.base()[malloy::http::field::host] };
347 if (!SSL_set_tlsext_host_name(conn->stream().native_handle(), hostname.c_str())) {
349 m_cfg.logger->error(
"could not set SNI hostname.");
350 boost::system::error_code ec{
static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category()};
351 throw boost::system::system_error{ec};
360 cb(std::make_shared<http::connection_plain<Body, Filter, std::decay_t<Callback>>>(
361 m_cfg.logger->clone(m_cfg.logger->name() +
" | HTTP connection"),
367 ([
this, prom = std::move(prom), req = std::move(req), filter = std::forward<Filter>(filter), cb = std::forward<Callback>(cb)](
auto&& conn)
mutable {
368 if (!malloy::http::has_field(req, malloy::http::field::user_agent))
369 req.set(malloy::http::field::user_agent, m_cfg.user_agent);
375 std::forward<Callback>(cb),
376 std::forward<Filter>(filter)
384 template<
bool isSecure>
387 const std::string& host,
389 const std::string& resource,
390 std::invocable<
malloy::error_code, std::shared_ptr<websocket::connection>>
auto&& handler
394 auto resolver = std::make_shared<boost::asio::ip::tcp::resolver>(boost::asio::make_strand(*m_ioc));
395 resolver->async_resolve(
397 std::to_string(port),
398 [
this, resolver, done = std::forward<
decltype(handler)>(handler), resource](
auto ec,
auto results)
mutable {
400 std::invoke(std::forward<
decltype(done)>(done), ec, std::shared_ptr<websocket::connection>{
nullptr});
403#if MALLOY_FEATURE_TLS
404 if constexpr (isSecure) {
405 return malloy::websocket::stream{boost::beast::ssl_stream<malloy::tcp::stream<>>{
406 malloy::tcp::stream<>{boost::asio::make_strand(*m_ioc)}, *m_tls_ctx}};
411 }(), m_cfg.user_agent);
413 conn->connect(results, resource, [conn, done = std::forward<
decltype(done)>(done)](
auto ec)
mutable {
415 std::invoke(std::forward<
decltype(handler)>(done), ec, std::shared_ptr<websocket::connection>{
nullptr});
417 std::invoke(std::forward<
decltype(handler)>(done), ec, conn);
425 auto start(controller&) -> controller::session;
Definition: controller.hpp:74
std::future< malloy::error_code > http_request(malloy::http::request< ReqBody > req, Callback &&done, Filter filter={})
Definition: controller.hpp:143
bool init_tls()
Definition: controller.cpp:19
void ws_connect(const std::string &host, std::uint16_t port, const std::string &resource, std::invocable< malloy::error_code, std::shared_ptr< websocket::connection > > auto &&handler)
Definition: controller.hpp:285
void wss_connect(const std::string &host, std::uint16_t port, const std::string &resource, std::invocable< malloy::error_code, std::shared_ptr< websocket::connection > > auto &&handler)
Same as ws_connect() but uses TLS.
Definition: controller.hpp:234
std::future< malloy::error_code > https_request(malloy::http::request< ReqBody > req, Callback &&done, Filter filter={})
Definition: controller.hpp:218
void add_ca(const std::string &contents)
Like add_ca_file(std::filesystem::path) but loads from an in-memory string.
Definition: controller.cpp:40
malloy::detail::controller_run_result< std::shared_ptr< boost::asio::ssl::context > > session
Definition: controller.hpp:79
std::future< malloy::error_code > http_request(const malloy::http::method method_, const std::string_view url, Callback &&done, Filter filter={})
Definition: controller.hpp:177
void add_ca_file(const std::filesystem::path &file)
Load a certificate authority for use with TLS validation.
Definition: controller.cpp:30
Definition: controller_run_result.hpp:43
Definition: request.hpp:19
constexpr void use_tls(const bool enabled) noexcept
Definition: request.hpp:117
Definition: response.hpp:22
static std::shared_ptr< connection > make(const std::shared_ptr< spdlog::logger > logger, stream &&ws, const std::string &agent_string)
Construct a new connection object.
Definition: connection.hpp:106
Websocket stream. May use TLS.
Definition: stream.hpp:50
Definition: type_traits.hpp:68
Definition: type_traits.hpp:56
Definition: type_traits.hpp:9
boost::beast::http::verb method
Definition: types.hpp:18
boost::beast::error_code error_code
Error code used to signify errors without throwing. Truthy means it holds an error.
Definition: error.hpp:9
Definition: controller.hpp:86
std::string user_agent
Agent string used for connections.
Definition: controller.hpp:91
std::uint64_t body_limit
Definition: controller.hpp:96
Default filter provided to ease use of interface.
Definition: controller.hpp:48
Definition: controller_run_result.hpp:19