85 malloy::controller::config
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()};
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.");
325 malloy::http::concepts::body Body,
329 std::future<malloy::error_code>
330 make_http_connection(malloy::http::request<Body>&& req, Callback&& cb, Filter&& filter)
332 std::promise<malloy::error_code> prom;
333 auto err_channel = prom.get_future();
336 if (!malloy::http::has_field(req, malloy::http::field::user_agent)) {
337 req.set(malloy::http::field::user_agent, m_cfg.user_agent);
340#if MALLOY_FEATURE_TLS
341 if constexpr (isHttps) {
343 auto conn = std::make_shared<http::connection_tls<Body, Filter>>(
344 m_cfg.logger->clone(m_cfg.logger->name() +
" | HTTPS connection"),
351 auto ec = conn->set_hostname(req.base()[malloy::http::field::host]);
358 boost::asio::co_spawn(
360 conn->run(std::move(req), std::forward<Filter>(filter)),
361 [conn, prom = std::move(prom), cb = std::move(cb)](std::exception_ptr e, http::request_result<Filter> req_result)
mutable {
368 std::rethrow_exception(e);
372 if (!req_result.error_code)
373 cb(std::move(req_result.response));
376 prom.set_value(req_result.error_code);
382 auto conn = std::make_shared<http::connection_plain<Body, Filter>>(
383 m_cfg.logger->clone(m_cfg.logger->name() +
" | HTTP connection"),
389 boost::asio::co_spawn(
391 conn->run(std::move(req), std::forward<Filter>(filter)),
392 [conn, prom = std::move(prom), cb = std::move(cb)](std::exception_ptr e, http::request_result<Filter> req_result)
mutable {
399 std::rethrow_exception(e);
403 if (!req_result.error_code)
404 cb(std::move(req_result.response));
407 prom.set_value(req_result.error_code);
410#if MALLOY_FEATURE_TLS
417 template<
bool isSecure>
420 const std::string& host,
422 const std::string& resource,
423 std::invocable<
malloy::error_code, std::shared_ptr<websocket::connection>>
auto&& handler
427 auto resolver = std::make_shared<boost::asio::ip::tcp::resolver>(boost::asio::make_strand(*m_ioc));
428 resolver->async_resolve(
430 std::to_string(port),
431 [
this, resolver, done = std::forward<
decltype(handler)>(handler), resource](
auto ec,
auto results)
mutable {
433 std::invoke(std::forward<
decltype(done)>(done), ec, std::shared_ptr<websocket::connection>{
nullptr});
436#if MALLOY_FEATURE_TLS
437 if constexpr (isSecure) {
438 return malloy::websocket::stream{boost::beast::ssl_stream<malloy::tcp::stream<>>{
439 malloy::tcp::stream<>{boost::asio::make_strand(*m_ioc)}, *m_tls_ctx}};
443 return malloy::websocket::stream{malloy::tcp::stream<>{boost::asio::make_strand(*m_ioc)}};
444 }(), m_cfg.user_agent);
446 conn->connect(results, resource, [conn, done = std::forward<
decltype(done)>(done)](
auto ec)
mutable {
448 std::invoke(std::forward<
decltype(handler)>(done), ec, std::shared_ptr<websocket::connection>{
nullptr});
450 std::invoke(std::forward<
decltype(handler)>(done), ec, conn);