28 boost::asio::any_io_executor ioc,
const std::function<
void(
const TResponse &)> response)
29: onResponse{response}, resolver_{ioc}, stream_{ioc}
36 const std::string & host,
const std::string & target,
const boost::beast::http::verb http_method,
40 req_.version(version);
41 req_.method(http_method);
43 req_.set(boost::beast::http::field::host, host);
44 req_.set(boost::beast::http::field::user_agent, BOOST_BEAST_VERSION_STRING);
48 host.c_str(),
kPort.c_str(),
53 boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results)
55 if (ec)
return fail(ec,
"resolve");
58 stream_.expires_after(std::chrono::seconds(1));
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();
75 boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type::endpoint_type)
77 if (ec)
return fail(ec,
"connect");
80 stream_.expires_after(std::chrono::seconds(1));
83 boost::beast::http::async_write(
89 boost::ignore_unused(bytes_transferred);
91 if (ec)
return fail(ec,
"write");
94 boost::beast::http::async_read(
101 boost::ignore_unused(bytes_transferred);
103 if (ec)
return fail(ec,
"read");
106 stream_.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
109 if (ec && ec != boost::beast::errc::not_connected)
return fail(ec,
"shutdown");
boost::beast::http::response< boost::beast::http::string_body > TResponse
void on_resolve(boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results) override
void on_read(boost::beast::error_code ec, std::size_t bytes_transferred) override
boost::beast::flat_buffer buffer_
void setBody(const std::string &body) override
boost::beast::http::request< boost::beast::http::string_body > req_
void run(const std::string &host, const std::string &target, const boost::beast::http::verb http_method, const int &version) override
http_session(boost::asio::any_io_executor ioc, const std::function< void(const TResponse &)> response)
boost::asio::ip::tcp::resolver resolver_
void on_connect(boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type::endpoint_type) override
boost::beast::tcp_stream stream_
std::function< void(const TResponse &)> onResponse
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