SMACC2
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
cl_http::ssl_http_session Class Reference

#include <ssl_http_session.hpp>

Inheritance diagram for cl_http::ssl_http_session:
Inheritance graph
Collaboration diagram for cl_http::ssl_http_session:
Collaboration graph

Public Member Functions

 ssl_http_session (boost::asio::any_io_executor ioc, boost::asio::ssl::context &ssl_context, const std::function< void(const TResponse &)> response)
 
virtual ~ssl_http_session ()
 
void run (const std::string &host, const std::string &target, const boost::beast::http::verb http_method, const int &version) override
 
std::string getPort () override
 
- Public Member Functions inherited from cl_http::http_session_base
virtual ~http_session_base ()
 

Private Member Functions

void on_resolve (boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results) override
 
void fail (boost::beast::error_code ec, const char *what) override
 
void on_connect (boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type::endpoint_type) override
 
void on_handshake (boost::beast::error_code ec) override
 
void on_write (boost::beast::error_code ec, std::size_t bytes_transferred) override
 
void on_read (boost::beast::error_code ec, std::size_t bytes_transferred) override
 
void on_shutdown (boost::beast::error_code ec) override
 
void setBody (const std::string &body) override
 
void setHeaders (const std::unordered_map< std::string, std::string > &headers) override
 
void appendToHeader (const std::string &key, const std::string &val)
 

Private Attributes

const std::string kPort = "443"
 
std::function< void(const TResponse &)> onResponse
 
boost::asio::ip::tcp::resolver resolver_
 
boost::beast::ssl_stream< boost::beast::tcp_stream > stream_
 
boost::beast::flat_buffer buffer_
 
boost::beast::http::request< boost::beast::http::string_body > req_
 
TResponse res_
 

Additional Inherited Members

- Public Types inherited from cl_http::http_session_base
using TResponse = boost::beast::http::response< boost::beast::http::string_body >
 

Detailed Description

Definition at line 37 of file ssl_http_session.hpp.

Constructor & Destructor Documentation

◆ ssl_http_session()

cl_http::ssl_http_session::ssl_http_session ( boost::asio::any_io_executor  ioc,
boost::asio::ssl::context &  ssl_context,
const std::function< void(const TResponse &)>  response 
)

Definition at line 27 of file ssl_http_session.cpp.

30: onResponse{response}, resolver_{ioc}, stream_{ioc, ssl_context}
31{
32}
boost::beast::ssl_stream< boost::beast::tcp_stream > stream_
std::function< void(const TResponse &)> onResponse
boost::asio::ip::tcp::resolver resolver_

◆ ~ssl_http_session()

virtual cl_http::ssl_http_session::~ssl_http_session ( )
inlinevirtual

Definition at line 47 of file ssl_http_session.hpp.

47{}

Member Function Documentation

◆ appendToHeader()

void cl_http::ssl_http_session::appendToHeader ( const std::string &  key,
const std::string &  val 
)
private

Definition at line 44 of file ssl_http_session.cpp.

45{
46 req_.set(key, val);
47}
boost::beast::http::request< boost::beast::http::string_body > req_

References req_.

Referenced by setHeaders().

Here is the caller graph for this function:

◆ fail()

void cl_http::ssl_http_session::fail ( boost::beast::error_code  ec,
const char *  what 
)
overrideprivatevirtual

Implements cl_http::http_session_base.

Definition at line 87 of file ssl_http_session.cpp.

88{
89 std::cout << "Failure!..." << std::endl;
90 std::cerr << what << ": " << ec.message() << "\n";
91 res_.result(boost::beast::http::status::bad_request);
92 res_.reason() = ec.message();
94}

References onResponse, and res_.

Referenced by on_connect(), on_handshake(), on_read(), on_resolve(), and on_write().

Here is the caller graph for this function:

◆ getPort()

std::string cl_http::ssl_http_session::getPort ( )
inlineoverridevirtual

Implements cl_http::http_session_base.

Definition at line 54 of file ssl_http_session.hpp.

54{ return kPort; }

References kPort.

◆ on_connect()

void cl_http::ssl_http_session::on_connect ( boost::beast::error_code  ec,
boost::asio::ip::tcp::resolver::results_type::endpoint_type   
)
overrideprivatevirtual

Implements cl_http::http_session_base.

Definition at line 96 of file ssl_http_session.cpp.

98{
99 if (ec) return fail(ec, "connect");
100
101 // Set a timeout on the operation
102 boost::beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(1));
103
104 // Send the HTTP request to the remote host
105 stream_.async_handshake(
106 boost::asio::ssl::stream_base::client,
107 boost::beast::bind_front_handler(&ssl_http_session::on_handshake, shared_from_this()));
108}
void fail(boost::beast::error_code ec, const char *what) override
void on_handshake(boost::beast::error_code ec) override

References fail(), on_handshake(), and stream_.

Referenced by on_resolve().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ on_handshake()

void cl_http::ssl_http_session::on_handshake ( boost::beast::error_code  ec)
overrideprivatevirtual

Reimplemented from cl_http::http_session_base.

Definition at line 110 of file ssl_http_session.cpp.

111{
112 if (ec) return fail(ec, "handshake");
113
114 // Set a timeout on the operation
115 boost::beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(1));
116
117 // Send the HTTP request to the remote host
118 boost::beast::http::async_write(
119 stream_, req_,
120 boost::beast::bind_front_handler(&ssl_http_session::on_write, shared_from_this()));
121}
void on_write(boost::beast::error_code ec, std::size_t bytes_transferred) override

References fail(), on_write(), req_, and stream_.

Referenced by on_connect().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ on_read()

void cl_http::ssl_http_session::on_read ( boost::beast::error_code  ec,
std::size_t  bytes_transferred 
)
overrideprivatevirtual

Implements cl_http::http_session_base.

Definition at line 135 of file ssl_http_session.cpp.

136{
137 boost::ignore_unused(bytes_transferred);
138
139 if (ec) return fail(ec, "read");
140
141 boost::beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(1));
142
143 // Gracefully close the socket
144 // stream_.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
145 stream_.async_shutdown(
146 boost::beast::bind_front_handler(&ssl_http_session::on_shutdown, shared_from_this()));
147}
void on_shutdown(boost::beast::error_code ec) override

References fail(), on_shutdown(), and stream_.

Referenced by on_write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ on_resolve()

void cl_http::ssl_http_session::on_resolve ( boost::beast::error_code  ec,
boost::asio::ip::tcp::resolver::results_type  results 
)
overrideprivatevirtual

Implements cl_http::http_session_base.

Definition at line 74 of file ssl_http_session.cpp.

76{
77 if (ec) return fail(ec, "resolve");
78
79 // Set a timeout on the operation
80 boost::beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(1));
81
82 // Make the connection on the IP address we get from a lookup
83 boost::beast::get_lowest_layer(stream_).async_connect(
84 results, boost::beast::bind_front_handler(&ssl_http_session::on_connect, shared_from_this()));
85}
void on_connect(boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type::endpoint_type) override

References fail(), on_connect(), and stream_.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ on_shutdown()

void cl_http::ssl_http_session::on_shutdown ( boost::beast::error_code  ec)
overrideprivatevirtual

Reimplemented from cl_http::http_session_base.

Definition at line 149 of file ssl_http_session.cpp.

150{
151 // not_connected happens sometimes so don't bother reporting it.
152 if (ec == boost::asio::error::eof)
153 {
154 // Rationale:
155 // http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
156 ec = {};
157 }
158 // if (ec) return fail(ec, "shutdown");
159
160 // If we get here then the connection is closed gracefully
162}

References onResponse, and res_.

Referenced by on_read().

Here is the caller graph for this function:

◆ on_write()

void cl_http::ssl_http_session::on_write ( boost::beast::error_code  ec,
std::size_t  bytes_transferred 
)
overrideprivatevirtual

Implements cl_http::http_session_base.

Definition at line 123 of file ssl_http_session.cpp.

124{
125 boost::ignore_unused(bytes_transferred);
126
127 if (ec) return fail(ec, "write");
128
129 // Receive the HTTP response
130 boost::beast::http::async_read(
132 boost::beast::bind_front_handler(&ssl_http_session::on_read, shared_from_this()));
133}
boost::beast::flat_buffer buffer_
void on_read(boost::beast::error_code ec, std::size_t bytes_transferred) override

References buffer_, fail(), on_read(), res_, and stream_.

Referenced by on_handshake().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

void cl_http::ssl_http_session::run ( const std::string &  host,
const std::string &  target,
const boost::beast::http::verb  http_method,
const int &  version 
)
overridevirtual

Implements cl_http::http_session_base.

Definition at line 49 of file ssl_http_session.cpp.

52{
53 // Set up an HTTP request
54 req_.version(version);
55 req_.method(http_method);
56 req_.target(target);
57 req_.set(boost::beast::http::field::host, host);
58 req_.set(boost::beast::http::field::user_agent, BOOST_BEAST_VERSION_STRING);
59
60 // Set an SNI to deal with CDNs
61 if (!SSL_set_tlsext_host_name(stream_.native_handle(), host.c_str()))
62 {
63 boost::system::error_code ec{
64 static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category()};
65 std::cerr << ec.message() << "\n";
66 return;
67 }
68 // Look up the domain name
69 resolver_.async_resolve(
70 host.c_str(), kPort.c_str(),
71 boost::beast::bind_front_handler(&ssl_http_session::on_resolve, shared_from_this()));
72}
void on_resolve(boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results) override

References kPort, on_resolve(), req_, resolver_, and stream_.

Here is the call graph for this function:

◆ setBody()

void cl_http::ssl_http_session::setBody ( const std::string &  body)
overrideprivatevirtual

Implements cl_http::http_session_base.

Definition at line 34 of file ssl_http_session.cpp.

34{ req_.body() = body; }

References req_.

◆ setHeaders()

void cl_http::ssl_http_session::setHeaders ( const std::unordered_map< std::string, std::string > &  headers)
overrideprivatevirtual

Implements cl_http::http_session_base.

Definition at line 36 of file ssl_http_session.cpp.

37{
38 for (const auto & [key, val] : headers)
39 {
40 this->appendToHeader(key, val);
41 }
42}
void appendToHeader(const std::string &key, const std::string &val)

References appendToHeader().

Here is the call graph for this function:

Member Data Documentation

◆ buffer_

boost::beast::flat_buffer cl_http::ssl_http_session::buffer_
private

Definition at line 78 of file ssl_http_session.hpp.

Referenced by on_write().

◆ kPort

const std::string cl_http::ssl_http_session::kPort = "443"
private

Definition at line 57 of file ssl_http_session.hpp.

Referenced by getPort(), and run().

◆ onResponse

std::function<void(const TResponse &)> cl_http::ssl_http_session::onResponse
private

Definition at line 73 of file ssl_http_session.hpp.

Referenced by fail(), and on_shutdown().

◆ req_

boost::beast::http::request<boost::beast::http::string_body> cl_http::ssl_http_session::req_
private

Definition at line 79 of file ssl_http_session.hpp.

Referenced by appendToHeader(), on_handshake(), run(), and setBody().

◆ res_

TResponse cl_http::ssl_http_session::res_
private

Definition at line 80 of file ssl_http_session.hpp.

Referenced by fail(), on_shutdown(), and on_write().

◆ resolver_

boost::asio::ip::tcp::resolver cl_http::ssl_http_session::resolver_
private

Definition at line 75 of file ssl_http_session.hpp.

Referenced by run().

◆ stream_

boost::beast::ssl_stream<boost::beast::tcp_stream> cl_http::ssl_http_session::stream_
private

Definition at line 77 of file ssl_http_session.hpp.

Referenced by on_connect(), on_handshake(), on_read(), on_resolve(), on_write(), and run().


The documentation for this class was generated from the following files: