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

#include <http_session.hpp>

Inheritance diagram for cl_http::http_session:
Inheritance graph
Collaboration diagram for cl_http::http_session:
Collaboration graph

Public Member Functions

 http_session (boost::asio::any_io_executor ioc, const std::function< void(const TResponse &)> response)
 
virtual ~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_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 setBody (const std::string &body) override
 
void setHeaders (const std::unordered_map< std::string, std::string > &headers) override
 

Private Attributes

const std::string kPort = "80"
 
std::function< void(const TResponse &)> onResponse
 
boost::asio::ip::tcp::resolver resolver_
 
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 >
 
- Protected Member Functions inherited from cl_http::http_session_base
virtual void on_handshake (boost::beast::error_code ec)
 
virtual void on_shutdown (boost::beast::error_code ec)
 

Detailed Description

Definition at line 35 of file http_session.hpp.

Constructor & Destructor Documentation

◆ http_session()

cl_http::http_session::http_session ( boost::asio::any_io_executor  ioc,
const std::function< void(const TResponse &)>  response 
)

Definition at line 27 of file http_session.cpp.

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

◆ ~http_session()

virtual cl_http::http_session::~http_session ( )
inlinevirtual

Definition at line 43 of file http_session.hpp.

43{}

Member Function Documentation

◆ fail()

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

Implements cl_http::http_session_base.

Definition at line 65 of file http_session.cpp.

66{
67 std::cout << "Failure!..." << std::endl;
68 std::cerr << what << ": " << ec.message() << "\n";
69 res_.result(boost::beast::http::status::bad_request);
70 res_.reason() = ec.message();
72}

References onResponse, and res_.

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

Here is the caller graph for this function:

◆ getPort()

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

Implements cl_http::http_session_base.

Definition at line 50 of file http_session.hpp.

50{ return kPort; }
const std::string kPort

References kPort.

◆ on_connect()

void cl_http::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 74 of file http_session.cpp.

76{
77 if (ec) return fail(ec, "connect");
78
79 // Set a timeout on the operation
80 stream_.expires_after(std::chrono::seconds(1));
81
82 // Send the HTTP request to the remote host
83 boost::beast::http::async_write(
84 stream_, req_, boost::beast::bind_front_handler(&http_session::on_write, shared_from_this()));
85}
boost::beast::http::request< boost::beast::http::string_body > req_
void fail(boost::beast::error_code ec, const char *what) override
void on_write(boost::beast::error_code ec, std::size_t bytes_transferred) override

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

Referenced by on_resolve().

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

◆ on_read()

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

Implements cl_http::http_session_base.

Definition at line 99 of file http_session.cpp.

100{
101 boost::ignore_unused(bytes_transferred);
102
103 if (ec) return fail(ec, "read");
104
105 // Gracefully close the socket
106 stream_.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
107
108 // not_connected happens sometimes so don't bother reporting it.
109 if (ec && ec != boost::beast::errc::not_connected) return fail(ec, "shutdown");
110
111 // If we get here then the connection is closed gracefully
113}

References fail(), onResponse, res_, 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::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 52 of file http_session.cpp.

54{
55 if (ec) return fail(ec, "resolve");
56
57 // Set a timeout on the operation
58 stream_.expires_after(std::chrono::seconds(1));
59
60 // Make the connection on the IP address we get from a lookup
61 stream_.async_connect(
62 results, boost::beast::bind_front_handler(&http_session::on_connect, shared_from_this()));
63}
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_write()

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

Implements cl_http::http_session_base.

Definition at line 87 of file http_session.cpp.

88{
89 boost::ignore_unused(bytes_transferred);
90
91 if (ec) return fail(ec, "write");
92
93 // Receive the HTTP response
94 boost::beast::http::async_read(
96 boost::beast::bind_front_handler(&http_session::on_read, shared_from_this()));
97}
void on_read(boost::beast::error_code ec, std::size_t bytes_transferred) override
boost::beast::flat_buffer buffer_

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

Referenced by on_connect().

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

◆ run()

void cl_http::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 35 of file http_session.cpp.

38{
39 // Set up an HTTP request
40 req_.version(version);
41 req_.method(http_method);
42 req_.target(target);
43 req_.set(boost::beast::http::field::host, host);
44 req_.set(boost::beast::http::field::user_agent, BOOST_BEAST_VERSION_STRING);
45
46 // Look up the domain name
47 resolver_.async_resolve(
48 host.c_str(), kPort.c_str(),
49 boost::beast::bind_front_handler(&http_session::on_resolve, shared_from_this()));
50}
void on_resolve(boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results) override

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

Here is the call graph for this function:

◆ setBody()

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

Implements cl_http::http_session_base.

Definition at line 33 of file http_session.cpp.

33{ req_.body() = body; }

References req_.

◆ setHeaders()

void cl_http::http_session::setHeaders ( const std::unordered_map< std::string, std::string > &  headers)
inlineoverrideprivatevirtual

Implements cl_http::http_session_base.

Definition at line 64 of file http_session.hpp.

64{}

Member Data Documentation

◆ buffer_

boost::beast::flat_buffer cl_http::http_session::buffer_
private

Definition at line 70 of file http_session.hpp.

Referenced by on_write().

◆ kPort

const std::string cl_http::http_session::kPort = "80"
private

Definition at line 53 of file http_session.hpp.

Referenced by getPort(), and run().

◆ onResponse

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

Definition at line 66 of file http_session.hpp.

Referenced by fail(), and on_read().

◆ req_

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

Definition at line 71 of file http_session.hpp.

Referenced by on_connect(), run(), and setBody().

◆ res_

TResponse cl_http::http_session::res_
private

Definition at line 72 of file http_session.hpp.

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

◆ resolver_

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

Definition at line 68 of file http_session.hpp.

Referenced by run().

◆ stream_

boost::beast::tcp_stream cl_http::http_session::stream_
private

Definition at line 69 of file http_session.hpp.

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


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