Malloy
Loading...
Searching...
No Matches
connection_tls.hpp
1#pragma once
2
3#include "connection.hpp"
4#include "../../core/tcp/stream.hpp"
5
6#include <boost/beast/ssl/ssl_stream.hpp>
7
8namespace malloy::client::http
9{
10
14 template<typename... ConnArgs>
16 public connection<connection_tls<ConnArgs...>, ConnArgs...>,
17 public std::enable_shared_from_this<connection_tls<ConnArgs...>>
18 {
19 using parent_t = connection<connection_tls<ConnArgs...>, ConnArgs...>;
20
21 public:
23 std::shared_ptr<spdlog::logger> logger,
24 boost::asio::io_context& io_ctx,
25 boost::asio::ssl::context& tls_ctx,
26 const std::uint64_t body_limit
27 ) :
28 parent_t(std::move(logger), io_ctx, body_limit),
29 m_stream(boost::asio::make_strand(io_ctx), tls_ctx)
30 {
31 }
32
33 // Called by base class
34 [[nodiscard]]
35 boost::beast::ssl_stream<malloy::tcp::stream<>>&
36 stream()
37 {
38 return m_stream;
39 }
40
41 void
42 hook_connected()
43 {
44 // Perform the TLS handshake
45 m_stream.async_handshake(
46 boost::asio::ssl::stream_base::client,
47 boost::beast::bind_front_handler(
48 &connection_tls::on_handshake,
49 this->shared_from_this()
50 )
51 );
52 }
53
54 private:
55 boost::beast::ssl_stream<malloy::tcp::stream<>> m_stream;
56
57 void
58 on_handshake(const boost::beast::error_code ec)
59 {
60 if (ec)
61 return parent_t::m_logger->error("on_handshake(): {}", ec.message());
62
63 // Set a timeout on the operation
64 boost::beast::get_lowest_layer(m_stream).expires_after(std::chrono::seconds(30));
65
66 // Send the HTTP request to the remote host
67 parent_t::send_request();
68 }
69 };
70}
Definition: connection_tls.hpp:18
Definition: connection.hpp:26