SMACC2
Loading...
Searching...
No Matches
http_session.cpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16*
17
18Author: Jacobus Lock
19
20******************************************************************************************************************/
21
23
24namespace cl_http
25{
26
28 boost::asio::any_io_executor ioc, const std::function<void(const TResponse &)> response)
29: onResponse{response}, resolver_{ioc}, stream_{ioc}
30{
31}
32
33void http_session::setBody(const std::string & body) { req_.body() = body; }
34
36 const std::string & host, const std::string & target, const boost::beast::http::verb http_method,
37 const int & version)
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}
51
53 boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results)
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}
64
65void http_session::fail(boost::beast::error_code ec, char const * what)
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}
73
75 boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type::endpoint_type)
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}
86
87void http_session::on_write(boost::beast::error_code ec, std::size_t bytes_transferred)
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}
98
99void http_session::on_read(boost::beast::error_code ec, std::size_t bytes_transferred)
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}
114} // namespace cl_http
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_
const std::string kPort
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